1. Qiita
  2. Items
  3. dropwizard

【メモ】Dropwizardのmigrationでmigration.xmlファイルを指定する

  • 3
    Like
  • 0
    Comment
More than 1 year has passed since last update.

概要

migrationファイル(changelogファイル)を指定して使いたい
(デフォルトだとsrc/main/resources/migrations.xmlを使う)

コード

jp.hoge.HogeApiConfiguration.java

package jp.hoge;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotEmpty;

import com.fasterxml.jackson.annotation.JsonProperty;

import io.dropwizard.Configuration;
import io.dropwizard.db.DataSourceFactory;

public class HogeConfiguration extends Configuration {

  @Valid
  @NotNull
  @JsonProperty
  private DataSourceFactory databaseHoge = new DataSourceFactory();

  public DataSourceFactory getHogeDataSourceFactory() {
    return databaseHoge;
  } 
}

package jp.hoge;

import org.skife.jdbi.v2.DBI;

import io.dropwizard.Application;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.jdbi.DBIFactory;
import io.dropwizard.migrations.MigrationsBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

public class HogeApplication extends Application<HogeConfiguration> {

  public static void main(String[] args) throws Exception {
    new HogeApplication().run(args);
  }

  @Override
  public String getName() {
    return "hoge";
  }

  @Override
  public void initialize(Bootstrap<HogeConfiguration> bootstrap) {
    bootstrap.addBundle(new MigrationsBundle<HogeConfiguration>() {
      @Override
      public DataSourceFactory getDataSourceFactory(HogeConfiguration configuration) {
        return configuration.getHogeDataSourceFactory();
      }
    });
  }

  @Override
  public void run(HogeConfiguration configuration,Environment environment) throws Exception {

  }
}

/home/hoge/hoge_config.yml
databaseHoge:
  driverClass: com.mysql.jdbc.Driver
  user: hoge_user
  password: hogepassword
  url: jdbc:mysql://127.0.0.1:3306/hoge
  properties:
    charSet: UTF-8
/home/hoge/hoge_migration.yml
<?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.0.xsd">

    <changeSet id="1" author="y.hogetaro">
        <createTable tableName="messages">
            <column name="id" type="int(11)" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(64)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>

実行コマンド

# dry-run
java -jar hoge-1.0.jar db migrate /home/hoge/hoge_config.yml --migrations /home/hoge/hoge_migration.yml --dry-run

注意点

includeの使い方

--migrationsで指定しないでデフォルトのままsrc/main/resources/migrations.xmlを使っていれば相対パスでもincludeを認識してくれたけど、--migrationsでchangelogファイルを指定すると、includeのファイルもフルパスで指定してあげないとダメっぽい。