Howdy, I’m Blaine Carternyoug.org › wp-content ›...
Transcript of Howdy, I’m Blaine Carternyoug.org › wp-content ›...
Howdy, I’m Blaine CarterHowdy, I’m Blaine Carter
Oracle Corporation Developer Advocate for Open Source
Email: [email protected] Blog: learncodeshare.net
Twi�er: @OraBlaineOS YouTube: www.youtube.com/blainecarter
Team: community.oracle.com/docs/DOC-
917690
MasterCreate directory:
RunAllVer5.sql
CreateLB_Groups.sql
CreateLB_People.sql
LoadLB_GroupsData.sql
Update4to5 directory:
UpdateAllVer4to5.sql
AddColGroupsRules.sql
AddColPeopleFavorite_color.sql
Database Change ManagementDatabase Change Management
The Hard WayThe Hard Way
ProblemsProblems
Which script has been run?
Update or Rollback to a specific version?
Test data? → LoadTestData.sql
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
Version Control / Change ManagementVersion Control / Change Management
The BasicsThe Basics
Track changes
Rollback / Switch to a specific revision
Branch / Merge
Diffs
What is Liquibase?What is Liquibase?
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
How Does It Work?How Does It Work?
Changelog Master
Change Sets
XML
JSON
SQL*
Groovy
Others..
Changelog MasterChangelog Master
{"databaseChangeLog": [
{"include": {"file": "runOnce/DD_DEQUEUE_ERRORS_T.createTable.json"}},
{"include": {"file": "runOnce/DD_DINOSAURS_T.createTable.json"}},
{"include": {"file": "runOnce/DD_LOCATIONS_T.createTable.json"}},
{"include": {"file": "runOnce/DD_MEMBERS_T.createTable.json"}},
{"include": {"file": "runOnce/DD_MESSAGES_T.createTable.json"}},
{"include": {"file": "runOnce/DD_SEED_DATA_T.createTable.json"}},
{"include": {"file": "runOnce/DD_SETTINGS_T.createTable.json"}},
{"include": {"file": "runOnce/DD_TEMPLATES_T.createTable.json"}},
{"include": {"file": "runOnChange/views/viewsMaster.json"}},
{"include": {"file": "runOnce/DD_USER_PAYMENT_TYPE.createSynonym.json"}},
{"include": {"file": "runOnChange/triggers/triggersMaster.json"}},
{"include": {"file": "runOnChange/functions/functionsMaster.json"}},
{"include": {"file": "runOnChange/procedures/proceduresMaster.json"}},
{"include": {"file": "runOnChange/packages/packagesMaster.json"}},
{"include": {"file": "runOnce/DD_Load_Data.json"}},
{"include": {"file": "runOnce/DD_Setup_Text_Search.json"}}
]}
ChangesetChangeset
{
"databaseChangeLog": [{
"preConditions": [{
"runningAs": {
"username": "dd"
}
}]
}, {
"changeSet": {
"id": "1507062717550-4",
"author": "bcarter (generated)",
"changes": [{
"createTable": {
"tableName": "DD_MEMBERS_T",
"columns": [{
"column": {
"name": "MEMBER_ID",
"type": "NUMBER(, 0)",
"autoIncrement": true,
"constraints": {
Java Command LineJava Command Line
java $JAVA_OPTS -jar /opt/liquibase/liquibase.jar \
--driver=oracle.jdbc.OracleDriver \
--classpath="/usr/lib/oracle/12.1/client64/lib/ojdbc7.jar" \
--url=jdbc:oracle:thin:lb_demo/dd@dbaccess \
--changeLogFile=changelog/db.changelog-master.json updateSQL >> output.sql
options
--logLevel=DEBUG
--logFile=liquibase.logFile
$JAVA_OPTS is used for my Oracle Exadata Express cloud connection.
Properties FileProperties File
(liquibase.properties)(liquibase.properties)
driver: oracle.jdbc.OracleDriver
classpath: /usr/lib/oracle/12.1/client64/lib/ojdbc7.jar
url: jdbc: oracle:thin:lb_demo/dd@dbaccess
changeLogFile: changelog/db.changelog-master.json
Liquibase Liquibase BestBest Recommended Practices Recommended Practices
Ignore the directory structure I’m using in the demo’s.
h�p://www.liquibase.org/bestpractices.html
So�ware We’ll Be UsingSo�ware We’ll Be Using
DinoDate
SQL Developer
Oracle Exadata Express Cloud Database or Virtual Box
Atom Text Editor
Liquibase -
h�ps://github.com/oracle/dino-date
h�p://www.liquibase.org
InstallInstall
Download -
It's just Java, extract it and go.
h�p://www.liquibase.org/download/index.html
Enough blah blah, let's see it.Enough blah blah, let's see it.
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
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 Exadata
Express Cloud connection.
h�p://www.liquibase.org/quickstart.html
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
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
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
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
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')"
}
}
...
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
Data Changes Are NOT Automatically RolledData Changes Are NOT Automatically Rolled
Back.Back.
Define your own Rollback
{ "rollback": { "delete": { "tableName": "lb_people" } } }
DiffDiff
Command Line:
Liquibase.properties:
h�p://www.liquibase.org/documentation/diff.html
liquibase.sh --url=jdbc:oracle:thin:lb_demo/dd@unknownDB \
diff \
--referenceUrl=jdbc:oracle:thin:lb_demo/dd@goodDB
referenceUrl: jdbc:oracle:thin:lb_diff_demo/dd@dbaccess
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
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
DBDocDBDoc
liquibase DBDoc docs
h�p://www.liquibase.org/documentation/dbdoc.html
Extensions / PluginsExtensions / Plugins
h�ps://liquibase.jira.com/wiki/display/CONTRIB/LiquiBase+Extensions+Portal
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
Howdy, I’m Blaine CarterHowdy, I’m Blaine Carter
Oracle Corporation Developer Advocate for Open Source
Email: [email protected] Blog: learncodeshare.net
Twi�er: @OraBlaineOS YouTube: www.youtube.com/blainecarter
Team: community.oracle.com/docs/DOC-
917690
#11
liquibase updateSQL
liquibase update
#12
liquibase rollbackCount 1
liquibase update
#13
liquibase update
liquibase tag myCoolTag
#14
liquibase rollback ver-1
liquibase update
#15
liquibase update
#17
liquibase --contexts=test update
#18
liquibase rollbackCount 2
liquibase rollbackCount 1
liquibase --contexts=test rollbackCount 2
liquibase --contexts=test update
#19
liquibase diff
liquibase --diffTypes=tables,columns diff
liquibase diffChangeLog
#20
liquibase --changeLogFile=generated.json generateChangeLog
#22
liquibase dropAll
liquibase DBDoc docs
liquibase update
liquibase DBDoc docs
Commands Used For The Demo (by slide number)Commands Used For The Demo (by slide number)