I have two changeLog which has changesets to create two tables .
The changelog having changeset to create person
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<changeSet id="1" author="nvoxland">
<createTable tableName="person">
<column name="id" type="int" autoIncrement="true">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="firstname" type="varchar(50)" />
<column name="lastname" type="varchar(50)">
<constraints nullable="false" />
</column>
<column name="state" type="char(2)" />
<column name="district" type="char(2)" />
<column name="city" type="char(2)" />
</createTable>
</changeSet>
</databaseChangeLog>
and the changelog to create passport
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<changeSet id="2" author="nvoxland">
<createTable tableName="passport">
<column name="passportNo" type="int">
<constraints primaryKey="true" nullable="false" />
</column>
</createTable>
</changeSet>
</databaseChangeLog>
I want to create a new changeset in a separate changelog which can use the above two changelog and add a foreignkey constraint between person and passport.
Something like this
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<changeSet id="3" author="nvoxland">
import changeset 1 & 2
add foreign-key between person & passport
</changeSet>
</databaseChangeLog>
Is this possible using Liquibase? Please suggest the possible ways.
If I get your question right, you can use master-changelog.xml file and specify all your changelog files there.
Your master-changelog may look like this:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<include file="changelog/changelog_file_1.xml" relativeToChangelogFile="true"/>
<include file="changelog/changelog_file_2.xml" relativeToChangelogFile="true"/>
<include file="changelog/changelog_file_3.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
changelog files will be executed in the provided order, so the third changelog will already have all the needed data available.
So in your changelog_file_3.xml you'll have a changeSet with <addForeignKeyConstraint>.
By the way, you don't need to create separate changelog for each changeset.
Related
I try to use liquibase with mysql in my quarkus application, this is the error I'm getting:
incrementBy is not allowed on mysql, db/changeLog.xml::added-autoincrement-to-id::VO
<createTable tableName="my_users">
<column name="id" type="bigint" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
</createTable>
<changeSet id="added-autoincrement-to-id" author="VO">
<addAutoIncrement
columnDataType="bigint"
columnName="id"
incrementBy="1"
startWith="1"
tableName="my_users"/>
</changeSet>
pom.xml
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-liquibase</artifactId>
</dependency>
using the latest mysql version, served by docker.
We are using LB 3.5. Below is the master changelog
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<!-- v13.0.00 -->
<include context="v13_0_00_03" file="changelog-v13.0.00.03.xml" relativeToChangelogFile="true"/>
<include context="v13_0_00_04" file="changelog-v13.0.00.04.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
Now in changelog-v13.0.00.03.xml we have these changeSets:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<changeSet id="CT-39_1" author="auth1" failOnError="false" runOnChange="false" labels="DDL">
<sqlFile encoding="utf8" endDelimiter="\nGO" path="../scripts/ddl/ddl.sql" relativeToChangelogFile="true" />
</changeSet>
<changeSet id="CT-2" author="auth2" runOnChange="false" labels="TRIGPROC">
<preConditions onFail="CONTINUE">
<changeSetExecuted id="CT-39_1" author="auth1" />
</preConditions>
<sqlFile encoding="utf8" endDelimiter="\nGO" path="../scripts/trigprocs/sp_ins1.sql" relativeToChangelogFile="true"/>
</changeSet>
<changeSet id="CT-18" author="auth3" runOnChange="false" labels="TRIGPROC">
<sqlFile encoding="utf8" endDelimiter="\nGO" path="../scripts/trigprocs/sp_ins2.sql"
relativeToChangelogFile="true"/>
</changeSet>
<changeSet id="CT-2228" author="auth4" runOnChange="false" labels="CONFIG">
<sqlFile encoding="utf8" endDelimiter="\nGO" path="../scripts/config/insert_rec.sql" relativeToChangelogFile="true"/>
</changeSet>
The following command works fine as I expect to see SQL file of v13.0.00.03 and labeled as CONFIG:
liquibase --contexts=v13_0_00_03 --labels=CONFIG updateSQL
But this command won't work
liquibase --contexts=v13_0_00_03 --labels="CONFIG, DDL" updateSQL
It only generates SQL for CONFIG and not DDL. I tried "CONFIG or DDL", no luck.
Another issue is that, if I change the 'labels' attribute in changeSets to 'context', LB doesn't generate anything using --contexts="v13_0_00_03, CONFIG".
Are these bugs or have I missed anything here?
The only possibility that I can think of is that the DDL changes have already been applied. You have the changeset that is labeled DDL as runOnChange=false, so even if the file has changed it will not re-run.
How can I generate id for primary key autoincrement column when populating table with Liquibase? With my current configuration Liquibase is putting NULL into ID column.
My changelog file:
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.2.xsd">
<property name="now" value="now()" dbms="mysql,h2"/>
<property name="now" value="current_timestamp" dbms="postgresql"/>
<!--
Added the entity Skill.
-->
<changeSet id="20141029084149" author="jhipster">
<createTable tableName="T_SKILL">
<column name="id" type="bigint">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="value" type="varchar(255)"/>
<column name="description" type="varchar(255)"/>
</createTable>
<loadData encoding="UTF-8"
file="config/liquibase/skills.csv"
separator="|"
tableName="T_SKILL"/>
</changeSet>
</databaseChangeLog>
and skills.csv file:
value|description
java|Java
java-ee|Java Enterprise Edition
junit|JUnit
You need to include autoIncrement="true" in the createTable column tag
<changeSet id="20141029084149" author="jhipster">
<createTable tableName="T_SKILL">
<column name="id" type="bigint" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="value" type="varchar(255)"/>
<column name="description" type="varchar(255)"/>
</createTable>
</changeSet>
I am using the following the quickstart on liquibase
My xml Code:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">
<changeSet id="1" author="bob">
<createTable tableName="department">
<column name="id" type="int">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(50)">
<constraints nullable="false"/>
</column>
<column name="active" type="boolean" defaultValueBoolean="true"/>
</createTable>
</changeSet>
</databaseChangeLog>
When I run Liquibase via command line using "update" command .Liquibase tells me this
INFO 8/31/12 9:17 AM:liquibase: Successfully released change log lock
Liquibase Update Failed: Content is not allowed in prolog.
SEVERE 8/31/12 9:17 AM:liquibase: Content is not allowed in prolog.
liquibase.exception.ChangeLogParseException: Error parsing line 1 column 1 of dat
abase.xml: Content is not allowed in prolog.
at liquibase.parser.core.xml.XMLChangeLogSAXParser.parse(XMLChangeLogSAXP
arser.java:106)
at liquibase.Liquibase.update(Liquibase.java:107)
My update command is
liquibase --driver=com.mysql.jdbc.Driver --classpath=mysql-connector-java-5.1.6.jar --changeLogFile=database.xml --url="jdbc:mysql://localhost:3306/sample" --username=root --password=password update
How do I do this?
It's probably because there are several hidden characters (such as BOM, google XML BOM for more information) at the beginning of your XML file.
This link shows how to remove BOM.
If you are using Windows, XVI32 (a free hex editor) would work for you.
Refer to Liquibase JIRA issue
This problem is related to schema definitions...try with schema definition from attachment example of the given link
I could not find a way to check in a precondition element if a custom property is set.
What I found out about this issue so far is here.
As the ticket comment indicates, extending CustomPrecondition will not help without modifying the API. Is there another way?
The documentation describes a changeLogPropertyDefined precondition.
The following example worked fine for me:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">
<changeSet author="mark (generated)" id="mark-1">
<preConditions onFail="HALT">
<changeLogPropertyDefined property="testing" value="1"/>
</preConditions>
<createTable tableName="TEST001">
<column name="ID" type="VARCHAR(10)">
<constraints nullable="false"/>
</column>
<column name="X" type="VARCHAR(9)">
<constraints nullable="false"/>
</column>
<column name="Y" type="DECIMAL(7,2)"/>
<column name="Z" type="DECIMAL(7,2)"/>
</createTable>
</changeSet>
</databaseChangeLog>
I run liquibase from Maven. The testing property can be set from the command line as follows:
mvn -Dtesting=1 compile