2013年5月18日土曜日

【追記あり】MyBatis Migrationsを触ってはまった。(複数のSQLを実行しようとするとSyntaxErrorException)

※2013/05/18 20:30頃追記1を追加。

DB Migration 入門 vol.1に参加して、MyBatis Migrationsを触ってみた。

はまった点


MySQLを対象にup/downにて、複数文を記述すると

ERROR: Error executing command.  Cause: org.apache.ibatis.jdbc.RuntimeSqlException: Error executing: --  add_data
-- Migration SQL that makes the change goes here.

insert into second_t values (1, 1000);

insert into second_t values (2, 2000);


.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert into second_t values (2, 2000)' at line 7
のようなエラーが。

原因


{repository_root}/environments/development.properties
を見ると、

# This ignores the line delimiters and
# simply sends the entire script at once.
# Use with JDBC drivers that can accept large
# blocks of delimited text at once.
send_full_script=true
との記述。
send_full_scriptがtrueの場合、スクリプトをまるっと投げちゃうと。
JDBCドライバが対応していれば、それをデリミタ毎に分割して実行してくれるようですが、
今回使用したMySQLのドライバはそういうのに対応していないみたいで、
デリミタで区切られた複数の文も1つの文として 実行しようとするみたい。

解決策


上記のsend_full_scriptをfalseにしてやればok。エラーなく複数の文を実行できるようになりました。

勉強会に参加してみて


DB Migrationツールってたくさんあるんですね!
(Playだとevolutionsもありますが。 )
生SQLを都度実行するのは間違いが起こったり、どこまで適用したかわからなかったりするので、
今後とも積極的に使いたいです。

チームでの開発効率向上と、本番環境でのマイグレーションのオペミス削減に役立ちそう。


講師のきょんさん、参加者の皆さんありがとうございました!


追記1


MySQLの複数文実行について


MySQLはもともと複数文の実行に非対応だと教えて頂きました。


@lilyfanjpさんありがとうございます!


きょんさんの発表資料について


こちらにアップされてます。ツールの位置づけの図はあとでアップされるようです
http://kyon-mm.bitbucket.org/_static/slides/cdstudy-mybatis-migration/index.html

使っている言語やスタイルに応じてお好きなものをお使いください。