Acids and Bases. Properties of Acids/Bases Acids are substances which… Bases are substances which…
Liquibase migration for data bases
-
Upload
roman-uholnikov -
Category
Engineering
-
view
511 -
download
11
Transcript of Liquibase migration for data bases
![Page 1: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/1.jpg)
source control for your DB
![Page 2: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/2.jpg)
whyData Base migration: Data base structure: - Tables, constrains, indexes; Data base data: - Initial data like list of post codes, statuses for order, etc. Data base logic: - stored procedures, triggers, functions
![Page 3: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/3.jpg)
whyData Base migration: Data base structure: - Tables, constrains, indexes; Data base data: - Initial data like list of post codes, statuses for order, etc. Data base logic: - stored procedures, triggers, functions
v1.0.0
![Page 4: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/4.jpg)
whyData Base migration: Data base structure: - Tables, constrains, indexes; Data base data: - Initial data like list of post codes, statuses for order, etc. Data base logic: - stored procedures, triggers, functions
v1.2.1
v1.0.0
![Page 5: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/5.jpg)
whyData Base migration: Data base structure: - Tables, constrains, indexes; Data base data: - Initial data like list of post codes, statuses for order, etc. Data base logic: - stored procedures, triggers, functions
v1.2.1
v1.6.0
v1.0.0
![Page 6: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/6.jpg)
whyData Base migration: Data base structure: - Tables, constrains, indexes; Data base data: - Initial data like list of post codes, statuses for order, etc. Data base logic: - stored procedures, triggers, functions
v1.2.1
v1.6.0
v2.0.1
v1.0.0
![Page 7: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/7.jpg)
whyData Base migration: Data base structure: - Tables, constrains, indexes; Data base data: - Initial data like list of post codes, statuses for order, etc. Data base logic: - stored procedures, triggers, functions
v1.2.1
v1.6.0
v2.0.1
v1.0.0
![Page 8: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/8.jpg)
What Liquibase :
•Apache license •Started in 2006 (active)
![Page 9: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/9.jpg)
What Liquibase :•Database migration for Java;•Can be used as
•Ant, Maven or Gradle plugin, •as CLI tool;• as part of the system :
•Servlet Listener•Spring Listener• JEE CDI Listener
![Page 10: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/10.jpg)
how it works•Supports multiple database types:
MySQL, PostgreSQL, Oracle, MsSql, Sybase_Enterprise, Sybase_Anywhere , DB2, Apache_Derby, HSQL, H2, Informix, Firebird, SQLite
![Page 11: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/11.jpg)
how it worksChanges are grouped into changesets: • Change(s) that should be applied atomically
Changesets are grouped into changelogs:•Files managed in version control
![Page 12: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/12.jpg)
how it worksSupports XML, YAML, JSON (DSL for database changes) and SQL formats:•Create Table, Add PK, Add FK, Add Column, Add Index, …•Drop Table, Drop PK, Drop FK, Drop Column, Drop Index, …•Insert, Update, Delete, …
![Page 13: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/13.jpg)
how it works•Changeslog (XML):<databaseChangeLog xmlns=…>
<changeSet author="liquibase-docs“ id="addColumn-example">
<addColumn catalogName="cat“ schemaName="public" tableName="person">
<column name="address" type="varchar(255)"/> </addColumn> </changeSet>
<changeSet> ….</changeset> <changeSet> ….</changeset>
</databaseChangeLog>
![Page 14: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/14.jpg)
how it works•Changeslog (YAML):
databaseChangeLog: changeSet: id: addColumn-example author: liquibase-docs changes: - addColumn: catalogName: cat columns: - column: name: address type: varchar(255) schemaName: public tableName: person
![Page 15: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/15.jpg)
how it works•Changeslog (JSON)
{ databaseChangeLog: [ "changeSet": { "id": "addColumn-example", "author": "liquibase-docs", "changes": [ { "addColumn": { "catalogName": "cat", "columns": [ { "column": { "name": "address", "type": "varchar(255)“ } }]
, "schemaName": "public", "tableName": "person" } }] }
}
![Page 16: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/16.jpg)
how it works
- Changesets uniquely identified by [Author, ID, File path]
- Liquibase tracks changeset execution in a special table
- Lock table to prevent concurrent Liquibase invocations
- Modified changesets are detected via checksums
![Page 17: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/17.jpg)
how it worksDatabaseChangelogLock:
DatabaseChangelog:
![Page 18: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/18.jpg)
how it worksCLI commands:
update [Count, Tag]
rollback [Count, Data, Tag]
generateChangeLog --diffTypes=tables, views,
columns, indexes, foreignkeys, primarykeys,
uniqueconstraints, data
diff --referenceUrl=<value>
status --verbose
updateSQL ...
![Page 19: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/19.jpg)
let us try it
DDLv1
![Page 20: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/20.jpg)
DDL
v2
v1
Init data, new column(source)
let us try it
![Page 21: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/21.jpg)
how it works
DDL
v2
v3
v1
Init data, new column(source)
Move values in separate table
let us try it
![Page 22: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/22.jpg)
let us try it
DDL
v2
v3
v1
Init data, new column(source)
Move values in separate table
![Page 23: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/23.jpg)
how it works
DEMO
![Page 24: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/24.jpg)
how it works
Spring config (embedded in your
application):<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>${liquibase.version}</version>
</dependency>
<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="myDataSource" /> <property name="changeLog" value="classpath:db-changelog.xml" /> <!-- contexts specifies the runtime contexts to use. --> <property name="contexts" value="test, production" />
</bean>
![Page 25: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/25.jpg)
how it works
Spring Boot config (embedded in your
application ):<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>${liquibase.version}</version>
</dependency>
nonehttps://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-liquibase
![Page 26: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/26.jpg)
how it works•Also:
<changeSet id="4" dbms="oracle"><sqlFile path="5.sql"/></changeSet>
<changeSet id="5" context="test"><sqlFile path="5.sql"/></changeSet>
<changeSet id="6" failOnError="false"><sqlFile path="6.sql"/></changeSet>
![Page 27: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/27.jpg)
how it works•Also:
<preConditions> <dbms type="oracle" /> <runningAs username="SYSTEM" />
</preConditions>
<changeSet id="1" author="bob"> <preConditions onFail="WARN">
<sqlCheck expectedResult="0">select count(*) from oldtable
</sqlCheck> </preConditions>
<comment>Comments should go after preCondition. If they are before then liquibase usually gives error.</comment>
<dropTable tableName="oldtable"/> </changeSet>
![Page 28: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/28.jpg)
recommendations • it is better to have each change in separate changes ;
• Document changesets with <comment> tag;
• Folders structure should reflect migration structure
com example db changelog db.changelog-master.xml db.changelog-1.0.xml db.changelog-1.1.xml db.changelog-2.0.xml
![Page 29: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/29.jpg)
peculiarities
•if you do roll back, Liquibase deletes the log line about the update from databasechangelog table, and after that Liguibase does not know any more that this change was applied and reverted;
•all changes are applied sequentially, so we can not apply/rollback separate mediate changes;
•no straightforward way of customizing warn/error messages;
![Page 30: Liquibase migration for data bases](https://reader038.fdocuments.us/reader038/viewer/2022102609/58719ad41a28ab044e8b59ef/html5/thumbnails/30.jpg)
peculiarities
•Each change set has an “id” and “author” attribute which, along with the directory and file name of the the change log file, uniquely identifies it. Dark side of it is that running the same change set using relative and absolute path will be considered as different changesets!
For example we have file /home/user/changelog-0.1.0.xmlFirst we execute:
$ liquibase --changeLogFile=/home/user/changelog-0.1.0.xml
and then if we try to execute$ liquibase --changeLogFile=changelog-0.1.0.xml