Git Product home page Git Product logo

play-liquibase's Introduction

Build Status Codacy Badge Codacy Badge Maven Central Dependencies

Play 2.4+ Liquibase Migration Module

Runs Liquibase migrations on Play application startup.

Table Of Contents

Adding Liquibase Module to your Play Scala project

Add dependency to your build.sbt:

libraryDependencies += "com.ticketfly" %% "play-liquibase" % "1.4"

Current version is built against Scala 2.10, 2.11 and 2.12 and works with Play 2.4 and higher.

No additional code changes are necessary. It uses Play Dependency Injection to eagerly run migrations on startup.

Configuration

Add to application.conf:

liquibase {
    url      = "jdbc:mysql://localhost/myschema?logger=com.mysql.jdbc.log.Slf4JLogger"
    driver   = "com.mysql.jdbc.Driver"
    user     = "ticketfly"
    password = "bar123"
}

If you are using Slick with Play, you can reference Slick config:

liquibase = ${slick.dbs.default.db}

Using Liquibase

Liquibase Module uses Liquibase 3.5.3

Example changelog.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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-3.5.xsd">

    <changeSet id="1" author="dragisak">
        <comment>Create a table</comment>
        <createTable tableName="my_table">
            <column name="id" type="bigint" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="event_id" type="bigint">
                <constraints nullable="false"/>
            </column>
            <column name="ticket_type" type="varchar(30)">
                <constraints nullable="false"/>
            </column>
            <column name="onsale_date" type="datetime">
                <constraints nullable="false"/>
            </column>
            <column name="offsale_date" type="datetime">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>

</databaseChangeLog>

Place your changelog.xml file in the conf/liquibase directory. That will make it a part of Play distribution.

You can override name and path to changelog file by setting liquibase.changelog configuration parameter. Default is conf/liquibase/changelog.xml

For details on using Liquibase, go to: www.liquibase.org

Using include and includeAll tags

Example changelog.xml (if you place your schema changelogs in conf/liquibase/schema directory and trigger in conf/liquibase/triggers directory):

<?xml version="1.0" encoding="UTF-8"?>
<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-3.5.xsd">

    <includeAll path="./schema" relativeToChangelogFile="true"/>
    <include path="./triggers/trigger-1.xml" relativeToChangelogFile="true"/>

</databaseChangeLog>

Using contexts

Liquibase contexts can be used to maintain a different set of change sets for different environments or uses. For example, you may have one set to maintain the production schema and one set to maintain the test schema, along with a small set of test data

Context is an attribute of the change set

<changeSet id="2" author="bob" context="test">
        ...
</changeSet>

To run the "test" context only, add to your liquibase configuration in application.conf

liquibase.contexts = ["test"]

Loading changes from the classpath

If your database access code is in a sub-module or library, you may want to keep your change files in, for example, src/main/resources/liquibase of that library. In this case you can choose to load your files from the classpath by specifying the changelog attribute and prepending 'classpath:' to the path. In the scenario where your master.xml file is located in src/main/resources/liquibase, add to your liquibase configuration

liquibase.changelog = "classpath:liquibase/master.xml"

Disabling Liquibase migrations

To disable running Liquibase on startup, you can set

liquibase.enable = false

You can disable Liquibase from command line with -Dliquibase.enable=false.

For details on configuring Play app, see Play Production Configuration

Testing With In-memory Database

There is a special options in H2 url to tell H2 to keep schema after Liquibase has finished: DB_CLOSE_DELAY=-1

Also, you have to make sure that it does not force table name to uppercase with DATABASE_TO_UPPER=false

Example:

  val appWithMemoryDatabase = FakeApplication(
    additionalConfiguration = {
      val driver = "org.h2.Driver"
      val url = s"jdbc:h2:mem:test${Random.nextInt()}:test;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1"
      Map(
        "slick.dbs.default.driver" -> "slick.driver.H2Driver$",
        "slick.dbs.default.db.driver" -> driver,
        "slick.dbs.default.db.url" -> url,
        "slick.dbs.default.db.user" -> "sa",
        "slick.dbs.default.db.password" -> "",
        "liquibase.driver" -> driver,
        "liquibase.url" -> url,
        "liquibase.user" -> "sa",
        "liquibase.password" -> ""
      )
    }
  )

  "save and query" in new WithApplication(appWithMemoryDatabase) {
        ...
        val postRequest = FakeRequest(POST, "/test").withJsonBody(Json.toJson(payload))
        val Some(saveResult) = route(postRequest)
        status(saveResult) must equalTo(OK)
        ...
  }

Logging

To show logs generated by Liquibase, add this to your app's logback.xml:

<logger name="liquibase" level="INFO" />

Publishing Jars to Maven Central

Project uses sbt-sonatype plugin.

After setting your credentials, you can do:

sbt +publishSigned
sbt sonatypeReleaseAll

Copyright and License

All code is available to you under the MIT license, available at http://opensource.org/licenses/MIT and also in the LICENSE file.

Copyright Ticketfly, Inc., 2016.

play-liquibase's People

Contributors

dragisak avatar jtescher avatar vadim-shb avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.