It is generally accepted that you should always use proper version control when developing software. Why wouldn’t you do the same with your database changes? It's even more important as part of a DevOps Continuous Integration strategy.
Liquibase is a database independent, open source (Apache 2.0 License) tool for managing database changes. I will discuss some of the features of Liquibase and demonstrate some of the major functionality. I will show how to extract your current schema into Liquibase make changes and roll them back.
I'll then move on to a more general examination of working with repositories on GitHub, and circle back to how all of us can contribute to making Liquibase better. Everything helps, from discussing and reporting new features and bugs, to forking the repository and making pull request both for code and documentation.
You will come away from this session with a better understanding of Liquibase specifically and the usefulness of schema migration tools in your DevOps strategy.
5. Leveraging Open Source for Oracle Database DevelopmentLeveraging Open Source for Oracle Database Development
Cross platform database change management.
h p://www.liquibase.org/
h ps://github.com/liquibase/liquibase
6. Version Control / Change ManagementVersion Control / Change Management
The BasicsThe Basics
Track changes
Rollback / Switch to a specific revision
Branch / Merge
Diffs
8. Liquibase is change management for your database.
Database changes are made using changeSets.
When you run an update, the changeSets are run in order.
Liquibase tracks what has been run, when and who ran it.
You can roll back to a specific version.
Populate default and/or test data.
Contexts -
Diffs -
Documentation -
SQL Output -
Offline -
h p://www.liquibase.org/documentation/contexts.html
h p://www.liquibase.org/documentation/diff.html
h p://www.liquibase.org/documentation/dbdoc.html
h p://www.liquibase.org/documentation/sql_output.html
h p://www.liquibase.org/documentation/offline.html
9. How Does It Work?How Does It Work?
Changelog Master
Change Sets
XML
JSON
SQL*
Groovy
Others..
14. LiquibaseLiquibase BestBest Recommended PracticesRecommended Practices
Ignore the directory structure I’m using in the demo’s.
h p://www.liquibase.org/bestpractices.html
15. So ware We’ll Be UsingSo ware We’ll Be Using
DinoDate
SQL Developer
Oracle Autonomous Transaction Processing Cloud Database or Virtual Box
Atom Text Editor
Liquibase -
h ps://github.com/oracle/dino-date
h p://www.liquibase.org
18. Create Changelog and ChangeSet FilesCreate Changelog and ChangeSet Files
Changelog Example:
- step 1
Changeset Example:
- step 2
Recommended to only perform one change per Change set, but you can
do more if you need.
h p://www.liquibase.org/quickstart.html
h p://www.liquibase.org/quickstart.html
19. Run ItRun It
- step 3
Jar File: java $JAVA_OPTS -jar /opt/liquibase/liquibase.jar updateSQL
Shell Script: liquibase updateSQL
Batch File: liquibase.bat updateSQL
Put the Liquibase directory in your path and run it: liquibase update
The shell script / batch files use the same JAVA_OPTS environment variable for my
Autonomous Transaction Processing Cloud connection.
h p://www.liquibase.org/quickstart.html
20. Rollback X Number of Change SetsRollback X Number of Change Sets
Warning: Rolling back anything in the database can be tricky.
Be very careful if you do this in Production.
liquibase rollbackCount 1
21. Tag a ChangesetTag a Changeset
You can tag a changeset in the file:
You can also use the command line:
"changeSet": {
"id": "1",
"author": "BlaineCarter",
"tagDatabase": {"tag":"ver-1"},
"changes": [
liquibase tag ver-1
22. Rollback to TagRollback to Tag
Other Options:
h p://www.liquibase.org/documentation/rollback.html
liquibase rollback ver-1
h p://www.liquibase.org/documentation/command_line.html
23. Load Data From a FileLoad Data From a File
groups.csv
"changes": [
{
"loadData": {
"file": "changelog/groups.csv",
"schemaName": "lb_demo",
"tableName": "lb_groups"
}
}]
name,description
Trucks,People who like trucks
Rockets,People who like rockets
Horses,People who like horses
Snakes,People who like snakes
24. Insert DataInsert Data
"changes": [
{
"insert": {
"schemaName": "lb_demo",
"tableName": "lb_people",
"columns": [
{
"column": {
"name": "firstname",
"value": "Bob"
}
}, {
"column": {
"name": "group_id",
"valueComputed": "(SELECT id FROM lb_groups where name = 'Trucks')"
}
}
...
25. Context: TestContext: Test
In the change set:
Liquibase.properties
Command Line overrides the properties file:
Context vs Labels
"changeSet": {
"id": "loadData-example",
"author": "liquibase-docs",
"context": "test",
contexts: !test
liquibase --contexts=test update
h p://www.liquibase.org/2014/11/contexts-vs-labels.html
26. Data Changes Are NOT Automatically RolledData Changes Are NOT Automatically Rolled
Back.Back.
Define your own Rollback
{
"rollback": {
"delete": {
"tableName": "lb_people"
}
}
}
28. Generate Changelog (Reverse Engineer)Generate Changelog (Reverse Engineer)
Note that this command currently has some limitations.
It does not export the following types of objects:
Stored procedures, functions, packages & Triggers
h p://www.liquibase.org/documentation/generating_changelogs.html
liquibase --changeLogFile=generated.json generateChangeLog
29. Review The OutputReview The Output
The output won’t be perfect.
Generated by update:
Generated by generateChangeLog:
id INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL
ID NUMBER(*, 0) DEFAULT "LB_DEMO"."ISEQ$$_69966".nextval NOT NULL
32. We Can Make It Be erWe Can Make It Be er
h p://www.liquibase.org/
GitHub Repo:
Fork
Change
Pull Request
h ps://github.com/liquibase/liquibase
There are many ways to contribute.
Enhance code
Add tests
Improve documentation
Submit / review issues
33. Howdy, I’m Blaine CarterHowdy, I’m Blaine Carter
Oracle Corporation
Developer Advocate for Open Source
Email: blaine.carter@oracle.com
Blog: learncodeshare.net
Twi er: @OraBlaineOS
YouTube: www.youtube.com/blainecarter
Team: community.oracle.com/docs/DOC-
917690