Create A Changelog: Changeset
Create A Changelog: Changeset
Create a ChangeLog
The database change log is an XML, JSON, YAML or SQL file which
describes all changes that need to be performed to update the
database.
In most cases, you want to create 1 file for each release. Each file
consists of one or more change sets.
Create a ChangeSet
A changeSet describes a set of changes that Liquibase executes
within one transaction. You can add as many changes to a set as you
like. But to avoid any problems during a rollback, you shouldn’t define
more than one logical change per set.
Each changeSet gets identified by the name of the author and an id.
Liquibase stores this information together with the name of the
change log file in the databasechangelog table to keep track on the
executed change sets.
www.thoughts-on-java.org
Getting Started With Liquibase
Create a Database Table
The following code snippet shows you a changeSet that creates the
table author with the columns id, firstname, lastname and version.
You just need a createTable element which has to define the name of
the table you want to create. On top of that, you can specify other
attributes, like the name of the database schema or the tablespace.
You also need to provide at least one column tag as a nested
property. In the example, I use 4 of these tags, to define the 4
database columns of the author table.
You can also use a constraints tag to define a primary key, not null,
unique, foreign key or cascade constraint.
www.thoughts-on-java.org
Getting Started With Liquibase
Define a Primary Key
If you didn’t define the primary key when you created the table, you
can add the constraint with an addPrimaryKey tag.
www.thoughts-on-java.org
Getting Started With Liquibase
Generate a ChangeLog
You don’t need to write the changeLog file yourself if you already
have an existing database. You can then use the command line client
to generate the file.
liquibase --driver=org.postgresql.Driver \
--classpath=myFiles\postgresql-9.4.1212.jre7.jar \
--changeLogFile=myFiles/db.changelog-1.0.xml \
--url="jdbc:postgresql://localhost:5432/recipes" \
--username=postgres \
--password=postgres \
generateChangeLog
liquibase --driver=org.postgresql.Driver \
--classpath=myFiles\postgresql-9.4.1212.jre7.jar \
--changeLogFile=myFiles/db.changelog-1.0.xml \
--url="jdbc:postgresql://localhost:5432/test_liquibase" \
--username=postgres \
--password=postgres \
updateSQL
www.thoughts-on-java.org
Getting Started With Liquibase
Execute a ChangeLog
After you’ve created and checked the changeLog yourself or used the
command line client to create it, you can choose between multiple
options to execute it. I use the command line client in the following
example but you can also use a maven plugin to create the database
as part of your build or deployment process or you can use a Servlet,
Spring or CDI Listener to automatically create or update the
database at application startup.
liquibase --driver=org.postgresql.Driver \
--classpath=myFiles\postgresql-9.4.1212.jre7.jar \
--changeLogFile=myFiles/db.changelog-1.0.xml \
--url="jdbc:postgresql://localhost:5432/test_liquibase" \
--username=postgres \
--password=postgres \
update
www.thoughts-on-java.org