Manikanta Reddy A
Manikanta Reddy A

Reputation: 1

How to copy the the data from one Table to another Table Using Liquibase scripts

I want to know the solution for the below Test ( to copy the the data from one Table to another Table) using Liquibase a part of Corda.

Case: As part of I want to create a new table into an existing database which has already a table called TableA (which has id, name, value columns) which has some data init, I have created a TableB (which has same id, name, value columns) and I wanted to copy the data from TableA to TableB.

For that I had used the following liquibase script as suggested here in Liquibase and In-order to test I had connected to PostgreSQL DB and connected schema called "corda_schema" which has the tables.

<changeSet author="liquibase.corda" id="update-table">
      <update schemaName="corda_schema" tableName="TableB">
        <column name="id" valueComputed="(SELECT id from TableA)"/>
    <column name="name" valueComputed="(SELECT name from TableA)"/>
    <column name="value" valueComputed="(SELECT value from TableA)"/>
      </update>
</changeSet>

I was getting the following error when I tried with the Liquibase update script

Error: liquibase.corda failed
Error: schema "CORDA_SCHEMA" not found in SQL statement

If I don't given the schema name in update like this

<update tableName="TableB">
    <column name="value" valueComputed="(SELECT value from TableA)"/>
</update>

the Liquibase is searching in the Public schema for TableA and I get this error:

Error: liquibase.corda failed Error: schema "PUBLIC" not found in SQL statement`

And also I tried this Liquibase script changeSet by creating the table itself I tried to update data, this changeSet is running and table is created but data is not copied.

<changeSet author="liquibase.corda" id="update-table">
  <createTable schemaName = "corda_schema" tableName="TableB">
    <column name="id" valueComputed= "(SELECT id FROM TableA)"/>
</createTable>
</changeSet>

Please suggest anything I am missing or any other usages that will make my test success to get the data from one table to another table.

Thanks in advance.

Upvotes: 0

Views: 1559

Answers (2)

MasterMarg
MasterMarg

Reputation: 1

Don't you think that you have to state your schema in tableA name when you put it into valueComputed property of column tag?

It looks like it should be

<column name="id" valueComputed="(SELECT id from corda_schema.TableA)"/>

Upvotes: 0

Daryl Doak
Daryl Doak

Reputation: 244

I would suggest to just use custom sql:

<changeSet author="liquibase.corda" id="insert-table">
      <sql>
        insert into corda_schema.TableB
        select id,name,value from corda_schema.TableA;
      </sql>
</changeSet>

Upvotes: 1

Related Questions