omoblog

餅は餅屋

SQLBoilerで複数DBに繋げる

業務で1アプリケーション→多データベースの構造になることがあった。

sqlboilerでDAOを生成しようとした時に少しハマったので書いておく。

 やること

  • config fileをDBの数だけ用意

  • Makefile等で-c or --config オプションと一緒にDBの個数分command実行

Makefile

sqlboiler:
    third_party/bin/sqlboiler --version
    third_party/bin/sqlboiler psql --config sqlboiler-one.toml
    third_party/bin/sqlboiler psql --config sqlboiler-two.toml

ハマりポイント

configのoutputが同じpackageに向いていると同じ名前のhelperが複数でき重複宣言でコンパイルエラーになる。 それを避けるためにoutput = ...をファイルごとに分ける

sqlboiler-one.toml

add-global-variants = true
add-panic-variants = true
no-tests = true
output = "db/one"
pkgname = "model"
wire = true

...

sqlboiler-two.toml

add-global-variants = true
add-panic-variants = true
no-tests = true
output = "db/two"
pkgname = "model"
wire = true

...

サンプルコードはこちら

github.com

参考issue

github.com