Skip to content

Instantly share code, notes, and snippets.

@asufana
Last active December 14, 2015 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asufana/9105803 to your computer and use it in GitHub Desktop.
Save asufana/9105803 to your computer and use it in GitHub Desktop.
PlayFramework1 DBマイグレーション手順

PlayFramework1 DBマイグレーション手順

ファーストリリース時

aplication.conf 設定

ddl 更新を無効化、および自動エボリューションを無効化

jpa.ddl=none
evolutions.enabled=false 

dependencies.yml 設定

DBモジュールを追加(スキーマダンプ用)

- play -> db 1.1.1

スキーマダンプ

dbモジュールを利用して、 テーブルスキーマをエボリューションフォルダにダンプ (ドキュメント

$ cd ProjectName
$ play db:export .  --output=db/evolutions/1.sql

ダンプファイルを修正

スキーマダンプファイルにパート指示を追加 (参考

  • 変更の要件を定義する Ups パート指示を追加
  • 変更の要件を定義する Downs パート指示を追加
  • drop記述を削除

ローカルDB作り変え

  • 削除してDBのみ作成

スキーマ適用

Projectフォルダに移動してから実行する(そうしないとエラーになる)

$ cd ProjectName
$ play evolutions:apply .
~        _            _ 
~  _ __ | | __ _ _  _| |
~ | '_ \| |/ _' | || |_|
~ |  __/|_|\____|\__ (_)
~ |_|            |__/   
~
~ play! 1.2.5, http://www.playframework.org
~
~ Connected to jdbc:postgresql://127.0.0.1:5432/ProjectName
~ Application revision is 1 [da39a3e] and Database revision is 0 [da39a3e]
~
~ Applying evolutions:

# ------------------------------------------------------------------------------
# --- Rev:1,Ups - 7a7a843

    create table Contract (
        id int8 not null,
        createDate timestamp not null,
        disable int4 not null,
        .............
        .............

# ------------------------------------------------------------------------------
~
~ Evolutions script successfully applied!
~

本番環境であれば play evolutions:apply . --%prod とする

再度スキーマ適用

もう一度行うとリビジョンチェックを行い最新かどうかを確認する

#エボリューション管理用のテーブルが生成されている(play_evlutions)

$ play evolutions:apply .
~        _            _ 
~  _ __ | | __ _ _  _| |
~ | '_ \| |/ _' | || |_|
~ |  __/|_|\____|\__ (_)
~ |_|            |__/   
~
~ play! 1.2.5, http://www.playframework.org
~
~ Connected to jdbc:postgresql://127.0.0.1:5432/ProjectName
~ Application revision is 1 [da39a3e] and Database revision is 1 [da39a3e]
~
~ Your database is up to date
~

Playマイグレーション手順

ファーストリリース以後

jpa.ddl=none によりモデル変更は自動的に反映されないため、 スキーマインパクトのあるモデル変更時には、2.sql にて alter 記述をする

alter文の作成

適用と除外の両方を記述

各DBツールで適用可否を確認

ALTER TABLE Users ADD COLUMN hoge varchar(255);
ALTER TABLE Users DROP COLUMN hoge;

エボリューションファイルを作成

2.sql, 3.sql とインクリメント番号を付加して作成していく

ファイル名からはどのリリースか判断できないので、ファイル内に記述するのがよいでしょう

# release: tag 20140101-01
# comment: add column hoge at User

# --- !Ups
ALTER TABLE Users ADD COLUMN hoge varchar(255);
 
# --- !Downs
ALTER TABLE Users DROP COLUMN hoge;

スキーマ更新

$ cd ProjectName
$ play evolutions:apply .

アプリケーション起動し、Hibernateエラーが出力されないことを確認

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment