Top Banner
Database versioning with FlywayDB Manuel de la Peña Software Engineer @mdelapenya
60

Flywaydb

Jul 12, 2015

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Flywaydb

Database versioning with FlywayDBManuel de la Peña Software Engineer @mdelapenya

Page 2: Flywaydb

Why DB migration tools?

Page 3: Flywaydb

Why DB migration tools?

Lots of developers!!!

Page 4: Flywaydb

Why DB migration tools?

https://cdn1.iconfinder.com/data/icons/database/PNG/256/Database_3.png

CI

Test

Development

Pre

Pro

Lots of developers!!!

Page 5: Flywaydb

…in the code side…

Version Control

Release tool/plugins on CI

Page 6: Flywaydb

…but in the Database…

Page 7: Flywaydb
Page 8: Flywaydb
Page 9: Flywaydb
Page 10: Flywaydb
Page 11: Flywaydb

Which version is your DB?

Page 12: Flywaydb
Page 13: Flywaydb

What we’d love is…

Page 14: Flywaydb

What we’d love is…

Recreate DB from scratch

Make it clear the state of the DB

Migrate in a deterministic way!!

Page 15: Flywaydb

Database migrationsMade easy

Page 16: Flywaydb

How FlywayDB works

Page 17: Flywaydb

How FlywayDB worksMetadata table (SCHEMA_VERSION)

Page 18: Flywaydb

How FlywayDB worksMetadata table (SCHEMA_VERSION)Classpath search for migrations

Page 19: Flywaydb

How FlywayDB worksMetadata table (SCHEMA_VERSION)Classpath search for migrations

SQL or Java

Page 20: Flywaydb

How FlywayDB worksMetadata table (SCHEMA_VERSION)Classpath search for migrations

SQL or JavaSorted by version, and applied in order

Page 21: Flywaydb

How FlywayDB worksMetadata table (SCHEMA_VERSION)Classpath search for migrations

SQL or Java

Compared against current versionSorted by version, and applied in order

Page 22: Flywaydb

How FlywayDB worksMetadata table (SCHEMA_VERSION)Classpath search for migrations

SQL or Java

Compared against current versionSorted by version, and applied in order

Checksum change-detection validation

Page 23: Flywaydb

Supported DB’sOracle

SQL ServerSQL Azure

DB2MySQLMariaDB

Google Cloud SQL

PostgreSQL

H2

HSQL

Derby Android SQLite

Page 24: Flywaydb

Executing FlywayDBCommand Line

Page 25: Flywaydb

Executing FlywayDBCommand Line flyway.properties

jars dirmigrations dirflyway migrate

Page 26: Flywaydb

Executing FlywayDBCommand Line

Maven Plugin

Page 27: Flywaydb

Executing FlywayDBCommand Line

Maven Plugin

Ant

Page 28: Flywaydb

Executing FlywayDBCommand Line

Maven Plugin

Ant

Gradle

Page 29: Flywaydb

Executing FlywayDBCommand Line

Maven Plugin

Ant

Gradle

SBT (Scala Build Tool)

Page 30: Flywaydb

Executing FlywayDBCommand Line

Maven Plugin

Ant

Gradle

SBT (Scala Build Tool)

API Java classesSpring configuration

Page 31: Flywaydb

Migrations

Page 32: Flywaydb

VersionsOne or more numeric parts

Separated by a dots or an underscore

Underscores are replaced by dots at runtime

Leading zeros are ignored on each part

Page 33: Flywaydb

Version examples

1

001

5.2

5_2 (5.2 at runtime)

205.68

1.2.3.4.5.6.7.8.9

20130115113556

2013.1.15.11.35.56

2013.01.15.11.35.56

Page 34: Flywaydb

SQL Migrations

Page 35: Flywaydb

Discovery

Page 36: Flywaydb

Discovery

Filesystem locationClasspath location

Automatically at runtime

Page 37: Flywaydb

Namingprefix: Configurable, default: Vversion: Dots or underscores separate the parts, as many parts as you likeseparator: Configurable, default: __ (2 underscores)description: Underscores or spaces to separate the wordssuffix: Configurable, default: .sql

Page 38: Flywaydb

Sample SQL versions

V_1__Add_phone_number_to_user.sqlV_2__Remove_picture_to_user.sqlV_3__Add_folders_to_article.sql

Page 39: Flywaydb

Syntax

Page 40: Flywaydb

SyntaxSingle/multiline statements

Single/multiline comments

Placeholder replacements

DB specific extensions

Page 41: Flywaydb

Java Migrations

Page 42: Flywaydb

¿Java migrations?

Page 43: Flywaydb

¿Java migrations?

C/LOB changes

Advanced Bulk data operations

Page 44: Flywaydb

Discovery

flyway.setLocations(…)

Page 45: Flywaydb

Namingprefix: Always Vversion: Underscores separate the parts, as many parts as you likeseparator: __ (2 underscores)description: Underscores to separate the words

Page 46: Flywaydb

Sample Java versions

V_1__Add_phone_number_to_user.javaV_2__Remove_picture_to_user.javaV_3__Add_folders_to_article.java

Page 47: Flywaydb

Usage

Page 48: Flywaydb

UsageImplements JdbcMigration

Wraps the migration in a transaction

Extracts the version from class name

Page 49: Flywaydb

Advanced usage

Page 50: Flywaydb

Advanced usageMigrationProviderInfo:

redefine prefix, version, separator...

Page 51: Flywaydb

Advanced usageMigrationProviderInfo:

redefine prefix, version, separator...

MigrationChecksumProvider: getChecksum()

Page 52: Flywaydb

Advanced usageMigrationProviderInfo:

redefine prefix, version, separator...

MigrationChecksumProvider: getChecksum()

Spring support: implements SpringJdbcMigration

(JdbcTemplate)

Page 53: Flywaydb

Migrating existing databases

Page 54: Flywaydb

Steps1. Take a DDL from production 2. Add inserts for all reference data you need 3. Give relevant version number

(V1__Base-.version.sql) 4. Clean all databases 5. Remove their contents with flyway:clean 6. Align databases with production 7. Exactly the same DDL 8. The objects they migrate must be identical to

what is present in production. 9. Give databases initial version

Page 55: Flywaydb

More advanced usage

Page 56: Flywaydb

Advanced usageOSGi support

Cluster migrations in parallel

Multiple schema support

Placeholder replacements ${placeholder}

Before-Hibernate-Export integration

Page 57: Flywaydb

Drawbacks

Page 58: Flywaydb

Drawbacks

Rollback migrations with destructive changes

Initial schema not retrieved from existing database

DB-specific code must be separated by locations

Page 59: Flywaydb

FlywayDBDemo

Page 60: Flywaydb

Thank you!Questions?