$30 off During Our Annual Pro Sale. View Details »

Flyway - Database Migrations Made Easy

Flyway - Database Migrations Made Easy

We all take version control for granted. And it is, when it comes to code. On the database side, however schema evolution is often performed ad-hoc: a quick statement here, a little script there. The result: confusion and uncertainty. Has this update already been applied on this instance? Should we apply this other script first? Is our application going to run against this database?

No more! This is where database migration tools come to the rescue! We'll look at a popular open-source choice called Flyway. By the end of the session, you'll have a solid understanding of what it does, how it works and how to effectively integrate it into your project. We'll look at concepts and solutions. We'll tackle the hard problems and show you how this fits in a Continuous Delivery and Zero Downtime workflow. This will transform how you deal with your database, and you'll never look back.

Axel Fontaine

June 18, 2015
Tweet

More Decks by Axel Fontaine

Other Decks in Technology

Transcript

  1. AXEL FONTAINE
    @axelfontaine
    [email protected]
    Database Migrations made Easy

    View Slide

  2. About Axel Fontaine
    • Founder and CEO of Boxfuse
    • Over 15 years industry experience
    • Continuous Delivery expert & trainer
    • Regular speaker at tech conferences
    • JavaOne RockStar in 2014
    @axelfontaine

    View Slide

  3. flywaydb.org

    View Slide

  4. boxfuse.com

    View Slide

  5. about
    questions

    View Slide

  6. Who is using a relational database ?

    View Slide

  7. View Slide

  8. Why should you use a relational database ?
    • ACID
    • Battle-tested reliability
    • Performance
    • Scaling up
    • Slice & dice data
    • Defer decisions

    View Slide

  9. relational databases
    should be the default choice
    for new projects

    View Slide

  10. What about scaling ???

    View Slide

  11. Algorithms are for
    people who don’t know
    how to buy RAM.
    Clay Shirky

    View Slide

  12. RAM is the new disk

    View Slide

  13. The secrets of scaling
    • Buy RAM (it’s cheap!)
    • Keep LOBs out of the DB
    (only store a reference)
    • Only then look at techniques like sharding

    View Slide

  14. Here is a small story

    View Slide

  15. View Slide

  16. What about NoSQL ???

    View Slide

  17. NoSQL databases are like divas
    They can be great,
    but you’d better know how to please them

    View Slide

  18. Now let’s talk about database migrations ...

    View Slide

  19. View Slide

  20. POLL:
    how are you doing database migrations?
    • Ad-hoc individual statements
    • Scripts (ad-hoc)
    • In-house tool (automated)
    • DB Migration tool
    • DB Migration API on App startup

    View Slide

  21. APP

    View Slide

  22. View Slide

  23. APP

    View Slide

  24. APP
    Axel
    APP
    Prod
    APP
    Test
    APP
    CI
    APP
    Christian

    View Slide

  25. SQL

    View Slide

  26. SQL
    SQL
    SQL
    ?
    ?
    ?

    View Slide

  27. flywaydb.org

    View Slide

  28. 5
    years old

    View Slide

  29. 250 000+
    downloads in 2014

    View Slide

  30. View Slide

  31. View Slide

  32. With techniques such as continuous delivery becoming
    more mainstream, automated database migrations are a
    baseline capability for many software teams. While there
    are many tools in this space, we continue to
    recommend Flyway for its low-friction approach. Flyway
    has a vibrant open-source community behind it, and
    support for both traditional and cloud-based databases
    such as Amazon Redshift and Google Cloud SQL.
    -- ThoughtWorks

    View Slide

  33. How does Flyway work ???

    View Slide

  34. SQL
    SQL
    SQL
    ?
    ?
    ?

    View Slide

  35. How does Flyway work ???
    Empty DB

    View Slide

  36. How does Flyway work ???
    Empty DB Version 2
    Version 1

    View Slide

  37. What were Flyway’s design goals ???

    View Slide

  38. It just works

    View Slide

  39. Keeping it simple
    (saying no)

    View Slide

  40. Convention
    over
    Configuration

    View Slide

  41. No
    Lock-in

    View Slide

  42. 0
    required dependencies

    View Slide

  43. 6
    commands
    info clean
    migrate baseline
    validate repair

    View Slide

  44. Demo

    View Slide

  45. Supported databases

    View Slide

  46. Supported execution platforms

    View Slide

  47. Third party plugins

    View Slide

  48. Basic architecture
    Migration
    Resolver
    API
    Database-
    specific
    support
    Metadata
    Table
    Migration
    Executor

    View Slide

  49. Flyway flyway = new Flyway();
    flyway.setDataSource(url, user, password);
    flyway.migrate();

    View Slide

  50. mvn flyway:migrate





    View Slide


  51. View Slide

  52. flyway migrate -url=… -user=… -password=…

    View Slide

  53. gradle flywayMigrate
    flyway {
    url = ‘…’
    user = ‘…’
    password = ‘…’
    }

    View Slide

  54. Demo

    View Slide

  55. When should you migrate the database ???

    View Slide

  56. Code and database share the same lifecycle
    So why risk having them not fit together?

    View Slide

  57. Database migration should be
    performed on application startup

    View Slide

  58. Flyway flyway = new Flyway();
    flyway.setDataSource(url, user, password);
    flyway.migrate();

    View Slide

  59. The Fear
    of
    DDL

    View Slide

  60. VS
    DML DDL

    View Slide

  61. Name Balance
    Homer 163
    Marge 214
    Bart 9
    Lisa 25

    View Slide

  62. VS
    DML DDL
    Name Balance
    Homer 0
    Marge 0
    Bart 0
    Lisa 0
    Name
    Homer
    Marge
    Bart
    Lisa
    From a business perspective
    the risk of DML and DDL is the same

    View Slide

  63. Demo

    View Slide

  64. A word about in-memory databases

    View Slide

  65. Do you really want to test your code
    with a different query engine
    from the one you have in production???

    View Slide

  66. Getting started

    View Slide

  67. http://flywaydb.org/getstarted

    View Slide

  68. Summary
    • Use a database migration tool

    View Slide

  69. Summary
    • Use a database migration tool
    • Run it on application startup
    • Get started tomorrow 

    View Slide

  70. flywaydb.org

    View Slide

  71. Thanks !
    AXEL FONTAINE
    @axelfontaine
    flywaydb.org boxfuse.com

    View Slide