Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Database versioning
with FlywayDB
Manuel de la Peña
Software Engineer
@mdelapenya
Why DB migration tools?
Why DB migration tools?
Lots of developers!!!
Why DB migration tools?
https://cdn1.iconfinder.com/data/icons/database/PNG/256/Database_3.png
CI
Test
Development
Pre
Pro
...
…in the code side…
Version Control
Release tool/plugins on CI
…but in the Database…
Which version is your DB?
What we’d love is…
What we’d love is…
Recreate DB from scratch
Make it clear the state of the DB
Migrate in a deterministic way!!
Database migrations
Made easy
How FlywayDB works
How FlywayDB works
Metadata table (SCHEMA_VERSION)
How FlywayDB works
Metadata table (SCHEMA_VERSION)
Classpath search for migrations
How FlywayDB works
Metadata table (SCHEMA_VERSION)
Classpath search for migrations
SQL or Java
How FlywayDB works
Metadata table (SCHEMA_VERSION)
Classpath search for migrations
SQL or Java
Sorted by version, and appl...
How FlywayDB works
Metadata table (SCHEMA_VERSION)
Classpath search for migrations
SQL or Java
Compared against current ve...
How FlywayDB works
Metadata table (SCHEMA_VERSION)
Classpath search for migrations
SQL or Java
Compared against current ve...
Supported DB’s
Oracle
SQL Server
SQL Azure
DB2MySQL
MariaDB
Google Cloud SQL
PostgreSQL
H2
HSQL
Derby Android SQLite
Executing FlywayDB
Command Line
Executing FlywayDB
Command Line
flyway.properties
jars dir
migrations dir
flyway migrate
Executing FlywayDB
Command Line
Maven Plugin
Executing FlywayDB
Command Line
Maven Plugin
Ant
Executing FlywayDB
Command Line
Maven Plugin
Ant
Gradle
Executing FlywayDB
Command Line
Maven Plugin
Ant
Gradle
SBT (Scala Build Tool)
Executing FlywayDB
Command Line
Maven Plugin
Ant
Gradle
SBT (Scala Build Tool)
API
Java classes
Spring configuration
Migrations
Versions
One or more numeric parts
Separated by a dots or an underscore
Underscores are replaced by dots at runtime
Leadin...
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.3...
SQL Migrations
Discovery
Discovery
Filesystem locationClasspath location
Automatically at runtime
Naming
prefix: Configurable, default: V
version: Dots or underscores separate the parts,
as many parts as you like
separator...
Sample SQL versions
V_1__Add_phone_number_to_user.sql
V_2__Remove_picture_to_user.sql
V_3__Add_folders_to_article.sql
Syntax
Syntax
Single/multiline statements
Single/multiline comments
Placeholder replacements
DB specific extensions
Java Migrations
¿Java migrations?
¿Java migrations?
C/LOB changes
Advanced Bulk data operations
Discovery
flyway.setLocations(…)
Naming
prefix: Always V
version: Underscores separate the parts, as many
parts as you like
separator: __ (2 underscores)
de...
Sample Java versions
V_1__Add_phone_number_to_user.java
V_2__Remove_picture_to_user.java
V_3__Add_folders_to_article.java
Usage
Usage
Implements JdbcMigration
Wraps the migration in a transaction
Extracts the version from class name
Advanced usage
Advanced usage
MigrationProviderInfo:
redefine prefix, version, separator...
Advanced usage
MigrationProviderInfo:
redefine prefix, version, separator...
MigrationChecksumProvider:
getChecksum()
Advanced usage
MigrationProviderInfo:
redefine prefix, version, separator...
MigrationChecksumProvider:
getChecksum()
Spring...
Migrating existing databases
Steps
1. Take a DDL from production
2. Add inserts for all reference data you need
3. Give relevant version number
(V1__Ba...
More advanced usage
Advanced usage
OSGi support
Cluster migrations in parallel
Multiple schema support
Placeholder replacements ${placeholder}...
Drawbacks
Drawbacks
Rollback migrations with destructive changes
Initial schema not retrieved from existing
database
DB-specific code...
FlywayDB
Demo
Thank you!
Questions?
Flywaydb
Flywaydb
Flywaydb
Flywaydb
Flywaydb
Upcoming SlideShare
Loading in …5
×

Flywaydb

1,206 views

Published on

Slides from my tech talk at Liferay Madrid Office

Published in: Technology
  • Be the first to comment

Flywaydb

  1. 1. Database versioning with FlywayDB Manuel de la Peña Software Engineer @mdelapenya
  2. 2. Why DB migration tools?
  3. 3. Why DB migration tools? Lots of developers!!!
  4. 4. Why DB migration tools? https://cdn1.iconfinder.com/data/icons/database/PNG/256/Database_3.png CI Test Development Pre Pro Lots of developers!!!
  5. 5. …in the code side… Version Control Release tool/plugins on CI
  6. 6. …but in the Database…
  7. 7. Which version is your DB?
  8. 8. What we’d love is…
  9. 9. What we’d love is… Recreate DB from scratch Make it clear the state of the DB Migrate in a deterministic way!!
  10. 10. Database migrations Made easy
  11. 11. How FlywayDB works
  12. 12. How FlywayDB works Metadata table (SCHEMA_VERSION)
  13. 13. How FlywayDB works Metadata table (SCHEMA_VERSION) Classpath search for migrations
  14. 14. How FlywayDB works Metadata table (SCHEMA_VERSION) Classpath search for migrations SQL or Java
  15. 15. How FlywayDB works Metadata table (SCHEMA_VERSION) Classpath search for migrations SQL or Java Sorted by version, and applied in order
  16. 16. How FlywayDB works Metadata table (SCHEMA_VERSION) Classpath search for migrations SQL or Java Compared against current version Sorted by version, and applied in order
  17. 17. How FlywayDB works Metadata table (SCHEMA_VERSION) Classpath search for migrations SQL or Java Compared against current version Sorted by version, and applied in order Checksum change-detection validation
  18. 18. Supported DB’s Oracle SQL Server SQL Azure DB2MySQL MariaDB Google Cloud SQL PostgreSQL H2 HSQL Derby Android SQLite
  19. 19. Executing FlywayDB Command Line
  20. 20. Executing FlywayDB Command Line flyway.properties jars dir migrations dir flyway migrate
  21. 21. Executing FlywayDB Command Line Maven Plugin
  22. 22. Executing FlywayDB Command Line Maven Plugin Ant
  23. 23. Executing FlywayDB Command Line Maven Plugin Ant Gradle
  24. 24. Executing FlywayDB Command Line Maven Plugin Ant Gradle SBT (Scala Build Tool)
  25. 25. Executing FlywayDB Command Line Maven Plugin Ant Gradle SBT (Scala Build Tool) API Java classes Spring configuration
  26. 26. Migrations
  27. 27. Versions One 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
  28. 28. 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
  29. 29. SQL Migrations
  30. 30. Discovery
  31. 31. Discovery Filesystem locationClasspath location Automatically at runtime
  32. 32. Naming prefix: Configurable, default: V version: Dots or underscores separate the parts, as many parts as you like separator: Configurable, default: __ (2 underscores) description: Underscores or spaces to separate the words suffix: Configurable, default: .sql
  33. 33. Sample SQL versions V_1__Add_phone_number_to_user.sql V_2__Remove_picture_to_user.sql V_3__Add_folders_to_article.sql
  34. 34. Syntax
  35. 35. Syntax Single/multiline statements Single/multiline comments Placeholder replacements DB specific extensions
  36. 36. Java Migrations
  37. 37. ¿Java migrations?
  38. 38. ¿Java migrations? C/LOB changes Advanced Bulk data operations
  39. 39. Discovery flyway.setLocations(…)
  40. 40. Naming prefix: Always V version: Underscores separate the parts, as many parts as you like separator: __ (2 underscores) description: Underscores to separate the words
  41. 41. Sample Java versions V_1__Add_phone_number_to_user.java V_2__Remove_picture_to_user.java V_3__Add_folders_to_article.java
  42. 42. Usage
  43. 43. Usage Implements JdbcMigration Wraps the migration in a transaction Extracts the version from class name
  44. 44. Advanced usage
  45. 45. Advanced usage MigrationProviderInfo: redefine prefix, version, separator...
  46. 46. Advanced usage MigrationProviderInfo: redefine prefix, version, separator... MigrationChecksumProvider: getChecksum()
  47. 47. Advanced usage MigrationProviderInfo: redefine prefix, version, separator... MigrationChecksumProvider: getChecksum() Spring support: implements SpringJdbcMigration (JdbcTemplate)
  48. 48. Migrating existing databases
  49. 49. Steps 1. 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
  50. 50. More advanced usage
  51. 51. Advanced usage OSGi support Cluster migrations in parallel Multiple schema support Placeholder replacements ${placeholder} Before-Hibernate-Export integration
  52. 52. Drawbacks
  53. 53. Drawbacks Rollback migrations with destructive changes Initial schema not retrieved from existing database DB-specific code must be separated by locations
  54. 54. FlywayDB Demo
  55. 55. Thank you! Questions?

×