Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define Changeset for insert query in liquibase

I have two table as following :

CREATE TABLE StudentMaster (
  sId      SERIAL,
  StudentName VARCHAR(50)
);

CREATE TABLE StudentClassMap (
  studnetId BIGINT UNSIGNED NOT NULL,
  studentClass VARCHAR(10),
  FOREIGN KEY (studnetId) REFERENCES StudentMaster (sId)
);

This is my insert query.

INSERT INTO StudentMaster (studentName) values ('Jay Parikh');

INSERT INTO StudentClassMap (studnetId, studentClass)
values ((SELECT sId from StudentMaster where studentName='Jay Parikh'),
        'M.Sc. 1st Year');

I want to define ChangeSet for thes queries in liquibase.

For First query ChangeSet will be :

<changeSet author="unknown" id="insert-example">
    <insert tableName="StudentMaster ">
        <column name="studentName" value="Jay Parikh"/>
    </insert>
</changeSet>

But I don't know how to define ChangeSet for another query.
Any help ? Thanks in advance.

like image 334
unknown Avatar asked Mar 12 '14 15:03

unknown


People also ask

How do I create a changeset in Liquibase?

To generate a newer version of the changelog file with stored logic objects based on the current database state, you need to delete, rename, or move the Objects directory that was created by running the generate-changelog command previously. Then, you can run the generate-changelog command again.

What is a changeset in Liquibase?

The changeset tag is a unit of change that Liquibase executes on a database and which is used to group database Liquibase Change Types together. A list of changes created by multiple changesets are tracked in a changelog.

What is SQL changeset?

Liquibase Concepts Simply put – a changelog contains an ordered list of changesets, and a changeset contains a change. You and your team can specify database changes in one of four different changelog formats: SQL, XML, JSON, or YAML. And, you can even mix and match different types of changelogs, if desired.

How do I tag a database using Liquibase?

Step 1: Add the tagDatabase Change Type to your changeset with the tag attribute as it is shown in the examples. Step 2: Deploy your changeset by running the update command. Now, you should see that the tag has been applied to the DATABASECHANGELOG table as a new changeset row.


1 Answers

Use the valueComputed attribute:

<changeSet author="unknown" id="insert-example-2">
    <insert tableName="StudentClassMap">
        <column name="studentId" valueComputed="(SELECT sId from StudentMaster where studentName='Jay Parikh')"/>
        <column name="studentClass" value="McSc. 1st Year"/>
    </insert>
</changeSet>
like image 130
Nathan Voxland Avatar answered Sep 28 '22 02:09

Nathan Voxland