Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
76 slides
-
The Ultimate Showdown of Database Migration Tools Pasha Finkelshteyn VS
Anton Arhipov @asm0di0 @antonarhipov -
Why do we need migrations? Manage schema versions in the
db-first approach Maintain schema compatibility in app-first approach On-premise software, where we don’t control the update cycle -
Why do we need migrations? If you don’t use one
of the popular tools, you’ll have to write your own -
You have a column full_name and you need to split
it by space into first_name and last_name -
You have a column full_name and you need to split
it by space into first_name and last_name -
You have a column full_name and you need to split
it by space into first_name and last_name -
SQL src/main/resources/db/migration/V1__Create_person_table.sql ALTER TABLE users ADD COLUMN first_name VARCHAR(255); ALTER
TABLE users ADD COLUMN last_name VARCHAR(255); UPDATE users SET first_name = split_part(full_name, ' ', 1), last_name = split_part(full_name, ' ', 2); -
Java src/main/java/db/migration/V3__Anonymize.java public class V1__SplitFullName extends BaseJavaMigration { @Override public
void migrate(Context context) throws Exception { try (Statement stmt = context.getConnection().createStatement()) { stmt.execute("ALTER TABLE users ADD COLUMN first_name VARCHAR(255)"); stmt.execute("ALTER TABLE users ADD COLUMN last_name VARCHAR(255)"); stmt.execute(""" UPDATE users SET first_name = split_part(full_name, ' ', 1), last_name = split_part(full_name, ' ', 2) """); -
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.6.xsd"> <changeSet id="split-full-name" author="pasha"> <addColumn tableName="users">
<column name="first_name" type="VARCHAR(255)"/> <column name="last_name" type="VARCHAR(255)"/> </addColumn> <update tableName="users"> <column name="first_name" valueComputed="split_part(full_name, ' ', 1)"/> <column name="last_name" valueComputed="split_part(full_name, ' ', 2)"/> </update> </changeSet> </databaseChangeLog> XML -
YAML databaseChangeLog: - changeSet: id: split-full-name author: pasha changes: -
addColumn: tableName: users columns: - name: first_name type: VARCHAR(255) - name: last_name type: VARCHAR(255) - update: tableName: users columns: - name: first_name valueComputed: split_part(full_name, ' ', 1) - name: last_name valueComputed: split_part(full_name, ' ', 2) -
JSON { "databaseChangeLog": [ { "changeSet": { "id": "split-full-name", "author":
"pasha", "changes": [ { "addColumn": { "tableName": "users", "columns": [ { "name": "first_name", "type": "VARCHAR(255)" }, { "name": "last_name", Too long, doesn’t fit the screen -
SQL --liquibase formatted sql --changeset pasha:split-full-name ALTER TABLE users ADD
COLUMN first_name VARCHAR(255); ALTER TABLE users ADD COLUMN last_name VARCHAR(255); UPDATE users SET first_name = split_part(full_name, ' ', 1), last_name = split_part(full_name, ' ', 2); -
Groovy liquibaseRuntime('org.liquibase:liquibase-groovy-dsl:4.0.0') { exclude group: "org.codehaus.groovy", module: "groovy" exclude group:
"org.codehaus.groovy", module: "groovy-sql" } liquibaseRuntime "org.apache.groovy:groovy:4.0.5" liquibaseRuntime "org.apache.groovy:groovy-sql:4.0.5" sql { comment('we should not have added this...') 'delete from my_table' } sql { """ insert into some_table(data_column, date_inserted) values('some_data', '${new Date().toString()}') """ } -
But… Western Naming Order: First Name + Family Name) In
East Asia, the family name usually comes first. Single names (Mononyms) Arabic naming conventions Middle names and multiple given names -
<changeSet id="split-full-name" author="pasha"> <addColumn tableName="users"> <column name="first_name" type="VARCHAR(255)"/> <column name="last_name"
type="VARCHAR(255)"/> </addColumn> <update tableName="users"> <column name="first_name" valueComputed="split_part(full_name, ' ', 1)"/> <column name="last_name" valueComputed="split_part(full_name, ' ', 2)"/> </update> </changeSet> XML -
<changeSet id="split-full-name" author="pasha"> <addColumn tableName="users"> <column name="first_name" type="VARCHAR(255)"/> <column name="last_name"
type="VARCHAR(255)"/> </addColumn> <update tableName="users"> <column name="first_name" valueComputed="split_part(full_name, ' ', 1)"/> <column name="last_name" valueComputed="split_part(full_name, ' ', 2)"/> </update> <rollback> <dropColumn tableName="users" columnName="first_name"/> <dropColumn tableName="users" columnName="last_name"/> </rollback> </changeSet> XML -
Rollback support Can and will do roll back for you!
Rollbacks are a lie! You should write them manually, as a usual migration -
Fixed scheme of versioning: V for migration R for repeated
Versioning Scheme -
R - Repeated migrations CREATE TABLE IF NOT EXISTS app_metadata
( key TEXT PRIMARY KEY, value TEXT NOT NULL ); INSERT INTO app_metadata (key, value) VALUES ('app_version', '1.0.0') ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value; R__Insert_app_version.sql -
R - Repeated migrations -- ${flyway:timestamp} CREATE TABLE IF NOT
EXISTS app_metadata ( key TEXT PRIMARY KEY, value TEXT NOT NULL ); INSERT INTO app_metadata (key, value) VALUES ('app_version', '1.0.0') ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value; R__Insert_app_version.sql Updates the checksum of the file -
R - Repeated migrations CREATE TABLE IF NOT EXISTS app_metadata
( key TEXT PRIMARY KEY, value TEXT NOT NULL ); INSERT INTO app_metadata (key, value) VALUES ('git_commit', '${git.commit}') ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value; R__Insert_app_version.sql Inserts the git hash -
Fixed scheme of versioning: V for migration R for repeated
Versioning Scheme B for baseline -
B - Baseline migrations db/migrations ├── V1__create_table_users.sql ├── V2__create_table_orders.sql ├──
V3__create_table_products.sql ├── V4__insert_data.sql └── B1.0__initial_baseline.sql -
Fixed scheme of versioning: V for migration R for repeated
B for baseline Versioning Scheme U for undo -
Fixed scheme of versioning: V for migration R for repeated
B for baseline U for undo Versioning Scheme Anything you want! -
migrations/ ├── changelog-001-create-users.xml ├── changelog-002-create-orders.xml ├── changelog-003-insert-default-users.xml ├── changelog-004-add-index-orders-date.xml ├──
changelog-005-alter-users-add-column-status.xml ├── changelog-006-update-user-status-default.xml ├── changelog-007-drop-legacy-table.xml └── db-changelog.xml -
migrations/ ├── schema/ │ ├── 001-create-users.sql │ ├── 002-create-orders.json │
├── 005-alter-users-add-column-status.sql │ └── 007-drop-legacy-table.json ├── data/ │ ├── 003-insert-default-users.sql │ └── 006-update-user-status-default.json ├── index/ │ └── 004-add-index-orders-date.sql └── db-changelog.xml -
Uses checksums but allows them to be disabled or updated
Flyway strictly enforces checksums for migration integrity Versioning: Checksum support -
• Learn supported languages • Make your choice • Invent
best practices and guidelines Learning Curve -
• Learn supported languages • Make your choice • Invent
best practices and guidelines Just write your migrations • SQL where it’s enough • Java in case if SQL is not enough Learning Curve -
Preconditions -- Flyway precondition to check if the app_version table
exists -- precondition: assert that the table 'app_version' exists -- precondition-on-fail: WARN (logs a warning and continues) -- precondition-sql: SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'app_version' -- Migration to add a new column to app_version ALTER TABLE app_version ADD COLUMN description VARCHAR(255); -
Preconditions -- Flyway precondition to check if the app_version table
exists -- precondition: assert that the table 'app_version' exists -- precondition-on-fail: WARN (logs a warning and continues) -- precondition-sql: SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'app_version' -- Migration to add a new column to app_version ALTER TABLE app_version ADD COLUMN description VARCHAR(255); -
Preconditions -- Flyway precondition to check if the app_version table
exists -- precondition: assert that the table 'app_version' exists -- precondition-on-fail: WARN (logs a warning and continues) -- precondition-sql: SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'app_version' -- Migration to add a new column to app_version ALTER TABLE app_version ADD COLUMN description VARCHAR(255); -
Preconditions -- Flyway precondition to check if the app_version table
exists -- precondition: assert that the table 'app_version' exists -- precondition-on-fail: WARN (logs a warning and continues) -- precondition-sql: SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'app_version' -- Migration to add a new column to app_version ALTER TABLE app_version ADD COLUMN description VARCHAR(255); -
Preconditions -- Flyway precondition to check if the app_version table
exists -- precondition: assert that the table 'app_version' exists -- precondition-on-fail: WARN (logs a warning and continues) -- precondition-sql: SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'app_version' -- Migration to add a new column to app_version ALTER TABLE app_version ADD COLUMN description VARCHAR(255); -
Preconditions -- Flyway precondition to check if the app_version table
exists -- precondition: assert that the table 'app_version' exists -- precondition-on-fail: WARN (logs a warning and continues) -- precondition-sql: SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'app_version' -- Migration to add a new column to app_version ALTER TABLE app_version ADD COLUMN description VARCHAR(255); -
<databaseChangeLog> <changeSet id="add_description_column" author="dev"> <preconditions onFail="WARN"> <or> <!-- PostgreSQL: Check
if app_version table exists in information_schema --> <sqlCheck expectedResult="1" dbms="postgresql"> SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = current_schema() AND table_name = 'app_version' </sqlCheck> <!-- MySQL: Check if app_version table exists in information_schema --> <sqlCheck expectedResult="1" dbms="mysql"> SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'app_version' </sqlCheck> <!-- Oracle: Check if app_version table exists in user_tables --> <sqlCheck expectedResult="1" dbms="oracle"> SELECT COUNT(*) FROM user_tables WHERE table_name = 'APP_VERSION' </sqlCheck> -
<databaseChangeLog> <changeSet id="add_description_column" author="dev"> <preconditions onFail="WARN"> <or> <!-- PostgreSQL: Check
if app_version table exists in information_schema --> <sqlCheck expectedResult="1" dbms="postgresql"> SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = current_schema() AND table_name = 'app_version' </sqlCheck> <!-- MySQL: Check if app_version table exists in information_schema --> <sqlCheck expectedResult="1" dbms="mysql"> SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'app_version' </sqlCheck> <!-- Oracle: Check if app_version table exists in user_tables --> <sqlCheck expectedResult="1" dbms="oracle"> SELECT COUNT(*) FROM user_tables WHERE table_name = 'APP_VERSION' </sqlCheck> -
<sqlCheck expectedResult="1" dbms="postgresql"> SELECT COUNT(*) FROM information_schema.tables WHERE table_schema =
current_schema() AND table_name = 'app_version' </sqlCheck> <!-- MySQL: Check if app_version table exists in information_schema --> <sqlCheck expectedResult="1" dbms="mysql"> SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'app_version' </sqlCheck> <!-- Oracle: Check if app_version table exists in user_tables --> <sqlCheck expectedResult="1" dbms="oracle"> SELECT COUNT(*) FROM user_tables WHERE table_name = 'APP_VERSION' </sqlCheck> </or> </preconditions> <addColumn tableName="app_version"> <column name="description" type="varchar(255)"/> -
<!-- MySQL: Check if app_version table exists in information_schema -->
<sqlCheck expectedResult="1" dbms="mysql"> SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'app_version' </sqlCheck> <!-- Oracle: Check if app_version table exists in user_tables --> <sqlCheck expectedResult="1" dbms="oracle"> SELECT COUNT(*) FROM user_tables WHERE table_name = 'APP_VERSION' </sqlCheck> </or> </preconditions> <addColumn tableName="app_version"> <column name="description" type="varchar(255)"/> </addColumn> </changeSet> </databaseChangeLog> -
SELECT COUNT(*) FROM user_tables WHERE table_name = 'APP_VERSION' </sqlCheck> </or>
</preconditions> <addColumn tableName="app_version"> <column name="description" type="varchar(255)"/> </addColumn> </changeSet> </databaseChangeLog> -
Preconditions Robust precondition checking before applying changes Simple SQL preconditions
For complex situations use Java For multiple databases - script all the things! -
Diff support Community edition - No :( Enterprise - Yes
(but we have never tried it) Can generate schema differences between environments -
Configuration Complexity A lot of configuration options, anything is configurable,
have to figure it out yourself Configuration? -
<changeSet id="create-and-populate-table" author="example" runInTransaction="false"> <createTable tableName="example_table"> <column name="id" type="INT"> <constraints
primaryKey="true" nullable="false"/> </column> <column name="name" type="VARCHAR(255)"/> </createTable> <insert tableName="example_table"> <column name="id" valueNumeric="1"/> <column name="name" value="First Entry"/> </insert> <!-- Intentional error: 'nonexistent_column' does not exist --> <insert tableName="example_table"> <column name="id" valueNumeric="2"/> <column name="nonexistent_column" value="This will cause an error"/> </insert> </changeSet> -
<changeSet id="create-and-populate-table" author="example" runInTransaction="false"> <createTable tableName="example_table"> <column name="id" type="INT"> <constraints
primaryKey="true" nullable="false"/> </column> <column name="name" type="VARCHAR(255)"/> </createTable> <insert tableName="example_table"> <column name="id" valueNumeric="1"/> <column name="name" value="First Entry"/> </insert> <!-- Intentional error: 'nonexistent_column' does not exist --> <insert tableName="example_table"> <column name="id" valueNumeric="2"/> <column name="nonexistent_column" value="This will cause an error"/> </insert> </changeSet> -
<changeSet id="create-and-populate-table" author="example" runInTransaction="false"> <createTable tableName="example_table"> <column name="id" type="INT"> <constraints
primaryKey="true" nullable="false"/> </column> <column name="name" type="VARCHAR(255)"/> </createTable> <insert tableName="example_table"> <column name="id" valueNumeric="1"/> <column name="name" value="First Entry"/> </insert> <!-- Intentional error: 'nonexistent_column' does not exist --> <insert tableName="example_table"> <column name="id" valueNumeric="2"/> <column name="nonexistent_column" value="This will cause an error"/> </insert> </changeSet> -
flyway.conf flyway.mixed=true V1__create_users_table.sql -- flyway:transactional -- This migration runs entirely
within a transaction CREATE TABLE users ( id SERIAL PRIMARY KEY, full_name VARCHAR(255) NOT NULL ); -
flyway.conf flyway.mixed=true V1__create_users_table.sql -- flyway:transactional -- This migration runs entirely
within a transaction CREATE TABLE users ( id SERIAL PRIMARY KEY, full_name VARCHAR(255) NOT NULL ); -
Change Approach Change-based: Just execute your SQL! Analyze database state
Write a change describing what schema should be Write pre-checks and conditions