1. Generate Change Logs from an existing database
Generate change logs from an existing database. For instance, copy postgresSQL driver to %LINQUIBASE_HOME%/postgresql-8.3-603.jdbc3.jar. Run a following line command in console (single line).
java -jar liquibase.jar --classpath=postgresql-8.3-603.jdbc3.jar --driver=org.postgresql.Driver --changeLogFile=./structure.xml --url="jdbc:postgresql://abc.com:5432/store_db" --username=postgres --password=1234 generateChangeLog
If successful, liquibase will show a message “Liquibase ‘generateChangeLog’ Successful”
2. Check a table already presents
Using preCondition tag which follows some form of boolean evaluation to check a table exists. For example, not(tableExists) = not(true) = false, therefore the createTable would not execute.
<changeSet id="1" author="Khiem.Truong"> <preConditions onFail="MARK_RAN"> <not> <tableExists tableName="trading"></tableExists> </not> </preConditions> <createTable tableName="trading"> <column name="code" autoIncrement="true" type="BIGINT"> <constraints nullable="false" primaryKey="true"/> </column> ... </createTable> </changeSet>
3. Insert a record to database.
Similarly, if the select query below >= 1, sqlCheck will return false and raise onFail
<changeSet id="9" author="Khiem.Truong"> <preConditions onFail="MARK_RAN"> <sqlCheck expectedResult="0">select count(*) from id_code_data where symbol='gbst' and assigned_prefix='NAS'</sqlCheck> </preConditions> <insert tableName="id_code_data"> <column name="symbol" value="gbst"/> <column name="name" value="Global banking settlement transaction"/> <column name="assigned_prefix" value="NAS"/> </insert> </changeSet>
4. Change datatype.
A column updates to a new datatype if it already exists. Otherwise, the changeSet is ignore.
<changeSet id="change_eicc_sub_section_label" author="Khiem.Truong"> <preConditions onFail="MARK_RAN"> <columnExists tableName="id_code_data" columnName="name"/> </preConditions> <modifyDataType tableName="id_code_data" columnName="name" newDataType="text" /> </changeSet>
References:
- Liquibase | Database Refactoring | Preconditions. Accessed at http://www.liquibase.org/documentation/preconditions.html on Oct 13, 2016
- Generating change logs. Accessed at http://www.liquibase.org/documentation/generating_changelogs.html on Oct 13, 2016