Using Liquibase and DB changes - Heigvd/Wegas GitHub Wiki

Command line usage ##Liquibase setup##

  1. Download liquibase command line tool. Extract it anywhere.
  2. Add postgreSQL jdbc driver to liquibase directory.
  3. (Optional, Wegas setup) Create a property file liquibase.properties in liquibase directory to avoid command line parameters. Adapt it to your needs and path ( [somepath] ).
driver=org.postgresql.Driver
# Path matching wegas root; JDBC driver
classpath=postgresql-9.1-901.jdbc4.jar 
url=jdbc:postgresql://localhost:5432/wegas_dev
username=user
password=123
# default changelog to use, relative to classpath
changeLogFile=db.changelog.xml
### DIFF params ###
referenceUrl=jdbc:postgresql://localhost:5432/wegas_dev
referenceUsername=user
referencePassword=123

##Usage examples## Using command line (assume: current directory is liquibase directory and liquibase.properties is set)

Source: Liquibase manual

  • generateChangeLog Generate a complete changelog for target database and stores it in changeLogFile
liquibase --url="jdbc:postgresql://[targetserver]:5432/[targetdatabase]" \
  --changeLogFile="ignoreClasspathFile.xml" generateChangeLog
  • diffChangeLog Generate a changelog between old database to new database and stores it in changeLogFile
liquibase --url="jdbc:postgresql://[oldserver]:5432/[olddatabase]" \
  diffChangeLog --referenceUrl="jdbc:postgresql://[newserver]:5432/[newdatabase]"
  • update Use changeLogFile's changeSet to patch target database
liquibase --url="jdbc:postgresql://[targetserver]:5432/[targetdatabase]" \
  --changeLogFile="relativeToClasspathFile.xml" update
  • changeLogSync Update target database's version with changeLogFile. Those patches won't applie during further updates
liquibase --url="jdbc:postgresql://[targetserver]:5432/[targetdatabase]" \
  --changeLogFile="relativeToClasspathFile.xml" changeLogSync
  • status Check if changeLogFile's changeSets where applied
liquibase --url="jdbc:postgresql://[targetserver]:5432/[targetdatabase]" \
  --changeLogFile="relativeToClasspathFile.xml" status

Notes

  • Change log generation creates a changeSet for each change. It is possible to combine every changeSet into one changeSet.
  • To export data, add --diffTypes="data" to generateChangeLog command.
  • It is possible to apply two changeSets with the same checkSum, the set (file, author, id) has to be unique.

DB changes use case

  • In PGAdmin, clear your database (drop and recreate your database or delete all table)
  • In NetBeans, open file "persitence.xml" (wegas-core -> Other Source -> src/main/resources -> META-INF) and, in view "design", in section "General", change Table Generation Strategie to "create".
  • Build project and run it (will create tables)
  • Use liquibase command "diffChangeLog" between your new datatable and an older one (on your production server f.i.)
  • Open file META-INF in your liquibase folder and copy the new xml file in the wegas file "dbchangelogs" (wegas-core -> Other Source -> src/main/resources in NetBean or \Wegas\wegas-core\src\main\resources in your files system)
  • Add some commentaries in this file (what did these changes) and change is name by the current timestamp.
  • Use liquibase command (changeLogSync) (to add this change in your database and don't try to make this change a second time).
  • Your changelog is ready to use.