
この投稿では、GradleスクリプトからDBDeployを開始する方法を示します。

それは何ですか?
バージョン管理されたデータベース構造の移行について聞いたことがあるかもしれません。 彼らはこれについてハブに書いた DBDeployは、任意のインスタンスおよび開発マシンのデータベースへのすべての最新の変更を簡単にインストールできる、最もシンプルで最も有名なツールの1つです。 また、 Gradleは、プロジェクトを構築するためのファッショナブルなツールになりました(AntやMavenなど、より優れている)。 彼らはまた彼について書いた。
そして、質問は何ですか?
問題は、GradleスクリプトからDBDeployを実行する方法ですか? DBDeployにはAntのタスクとMavenのプラグインがありますが、DBdeployのプラグインはまだありません(より正確には、まだ初期段階です)。 少し突いて、Gradleスクリプトから同じDBDeploy Antタスクを使用するのが最も簡単な方法であるという結論に達しました(gradleスクリプトからAntタスクを使用する方法について説明しています)。 例を考えてみましょう。
タスクは明確です、どこから始めますか?
私たちのプロジェクトが次の構造を持っているとしましょう。 dbフォルダーにはSQLスクリプトが含まれています。
- db
- create_changelog_table.sql
- 001_create_customer_table.sql
- 002_create_address_table.sql
- 003_etc ...
- build.gradle
次に、3つのタスクを持つGradleスクリプトを作成できます。
build.gradle
project.ext { dbDriver = 'com.mysql.jdbc.Driver' dbUrl = 'jdbc:mysql:///habrahabr' dbUsername = 'habra' dbPassword = 'habr' }
タスクupdateDatabase
task updateDatabase << { ant.taskdef(name: 'dbdeploy', classname: 'com.dbdeploy.AntTarget', classpath: configurations.compile.asPath) ant.dbdeploy(driver: dbDriver, url: dbUrl, userid: dbUsername, password: dbPassword, dir: 'db', dbms: 'mysql', undooutputfile: 'db/undo_last_change.sql') }
タスクcreateChangelogTable
<b>task createChangelogTable</b> << { ant.sql(driver: dbDriver, url: dbUrl, userid: dbUsername, password: dbPassword, encoding: 'UTF-8', classpath: configurations.compile.asPath) { fileset(file: 'db/create_changelog_table.sql') } }
タスクundoLastChange
<b>task undoLastChange</b> << { ant.sql(driver: dbDriver, url: dbUrl, userid: dbUsername, password: dbPassword, encoding: 'UTF-8', classpath: configurations.compile.asPath) { fileset(file: 'db/undo_last_change.sql') } }
実行方法
したがって、3つのタスクがあります。
> gradle createChangelogTable
:createChangelogTable 成功する
> gradle updateDatabase
[ant:dbdeploy] dbdeploy 3.0M3 [ant:dbdeploy]ディレクトリ/ tmp / habr / gradle-dbdeploy / dbから変更スクリプトを読み取り中... [ant:dbdeploy]現在データベースに適用されている変更: [ant:dbdeploy] 1..61 [ant:dbdeploy]利用可能なスクリプト: [ant:dbdeploy] 62..62 [ant:dbdeploy]適用されるもの: [ant:dbdeploy] 62..62 [ant:dbdeploy]適用#62:062_migrate_currency_to_eur.sql ... [ant:dbdeploy]->ステートメント1/5 ... [ant:dbdeploy]->ステートメント2/5 ... [ant:dbdeploy]->ステートメント3/5 ... [ant:dbdeploy]->ステートメント4/5 ... [ant:dbdeploy]->ステートメント5/5 ... [ant:dbdeploy]取り消しスクリプトの生成... 成功する
> gradle undoLastChange
:undoLastChange 成功する
これで、「gradle createChangelogTable」を1回実行し、少なくとも各スクリプト変更後に毎回「gradle updateDatabase」を実行し、「gradle undoLastChange」を使用して最後の変更を取り消すことができます。
まとめると
Gradleはビルドスクリプト用の非常に簡潔で読みやすい構文を提供し、DBDeployは変更をデータベースにプッシュするためのシンプルで信頼できる方法を提供します。 そして、彼らは完全に協力しました。
頑張って!