The Update Process: Changelog
The Update Process: Changelog
www.thoughts-on-java.org
Update A Database With Liquibase
Liquibase can’t restore any data that got deleted during the database
migration. You should, therefore, create a backup before you perform
any update.
<databaseChangeLog>
<include file""myFiles/db.changelog-1.0.xml"/>
<include file""myFiles/db.changelog-1.1.xml"/>
</databaseChangeLog>
You can then provide the master changelog to the Liquibase client. It
will iterate through the included files and check which changeSets need
to be executed to update the database to the latest version.
Add a Table
You can use a createTable tag to tell Liquibase to create a new
database table. The follwing XML snippet creates the author table
with the columns id, firstname, lastname and version.
This changeSet also contains a rollback tag. That’s because Liquibase
doesn’t generate a rollback operation when you create a new
database table. If you want to remove the table when you perform a
rollback, you need to use the rollback tag to provide your own
rollback operation. You can use it with all other Liquibase tags, or
you can provide SQL statements that shall be executed.
www.thoughts-on-java.org
Update A Database With Liquibase
<changeSet author="Thorben" id="1">
<createTable tableName="publisher">
<column name="id" type="BIGINT">
<constraints nullable="false"/>
</column>
<column name="name" type="VARCHAR(255)"/>
<column name="version" type="INT">
<constraints nullable="false"/>
</column>
</createTable>
<rollback>
<dropTable tableName="publisher" />
</rollback>
</changeSet>
Rename a Table
You can rename a database table with a renameTable tag. It requires
2 attributes: the oldTableName and the newTableName. You can also
define the catalogName and schemaName.
You don’t need to provide the rollback operation when you rename a
table. Liquibase can generate the required statement. But you can
use the rollback tag to override the generated statement.
www.thoughts-on-java.org
Update A Database With Liquibase
Drop a Table
You can drop a database table with the dropTable tag. As you can see
in the following code snippet, you just need to provide the tableName
as an attribute.
Be careful and create a database backup before you drop a table.
Otherwise, you will not be able to restore any data that’s stored in
the table.
Add a Column
You can add a database column with an addColumn tag with a
tableName attribute and one or more column tags.
Liquibase can generate the rollback operation, so you only need to
specify it if you want to override the generated statement.
www.thoughts-on-java.org
Update A Database With Liquibase
Rename a Column
You can rename a database column with the renameColumn tag. It
requires the attributes tableName, oldColumnName and
newColumnName.
You don’t need to provide a rollback tag for this operation. Liquibase
generates the required statements.
Drop a Column
You can drop a database column with the dropColumn tag and the
attributes tableName and columnName.
Before you drop a database column, you should create a backup of your
database. Liquibase can’t generate the rollback operation and it’s most
often impossible to recreate the deleted data without a backup.
www.thoughts-on-java.org
Update A Database With Liquibase
Merge 2 Columns
This operation creates a new table column, sets the concatenated
value of the 2 old columns as the value of the new one and drops the
2 old table columns.
You can define this operation with a mergeColumn tag and attributes
to define the tableName, the finalColumnName and finalColumnType,
the names of the 2 old columns as column1Name and column2Name
and an optional joinString.
www.thoughts-on-java.org
Update A Database With Liquibase
liquibase --driver=org.postgresql.Driver \
--classpath=myFiles\postgresql-9.4.1212.jre7.jar \
--changeLogFile=myFiles/db.changelog.xml \
--url="jdbc:postgresql://localhost:5432/test_liquibase" \
--username=postgres \
--password=postgres \
updateSQL
After you reviewed the generated statements, you can call the update
command with the same parameters. Liquibase will then find and
execute the required changeSets to migrate the database to the
latest version.
liquibase --driver=org.postgresql.Driver \
--classpath=myFiles\postgresql-9.4.1212.jre7.jar \
--changeLogFile=myFiles/db.changelog.xml \
--url="jdbc:postgresql://localhost:5432/test_liquibase" \
--username=postgres \
--password=postgres \
update
www.thoughts-on-java.org