Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Web開発を支えるマイグレーションツールについて / sqldef introduction for psql users
yudppp
December 07, 2019
Programming
2
1.7k
Web開発を支えるマイグレーションツールについて / sqldef introduction for psql users
第11回 PostgreSQLアンカンファレンス@東京で発表する資料
https://pgunconf.connpass.com/event/155951/
yudppp
December 07, 2019
Tweet
Share
More Decks by yudppp
See All by yudppp
2019年 HRBrainの技術的挑戦 / hrbain technology challenge 2019
yudppp
3
840
ISUCON向けのツールを作った話 / isutools
yudppp
1
130
Row Level Securityはマルチテナントの銀の弾丸になりうるのか / Row Level Security is silver bullet for multitenancy?
yudppp
18
9.8k
Webサービス開発に必要な統計学入門 / study of statistics for web developers
yudppp
1
140
メジャーな Live Reloaderの違いをちゃんと調べて見た / Compare major live reloader of Go
yudppp
1
1.6k
今年お世話になったnpm module
yudppp
1
580
Goで社内向け管理画面を楽に作る方法
yudppp
5
8.8k
カバレッジ95%以上を実現した テストコードの書き方
yudppp
0
2.2k
Other Decks in Programming
See All in Programming
microCMS × imgixを活用して品質とレスポンスを両立したポートフォリオサイトを作成した話
takehitogoto
0
380
WindowsコンテナDojo:準備編
oniak3ibm
PRO
0
450
Get Ready for Jakarta EE 10
ivargrimstad
0
1.6k
Is Rust a great language for building Kubernetes ecosystem
deepu105
0
140
TechFeed Conference 2022 - Kotlin Experimental
jmatsu
0
340
New Relicを使った Observabilityの実現方法と活用例 / gocon 2022 spring after talk
budougumi0617
0
780
microCMS × Shopifyで、ECサイトがリニューアル後急成長した話
microcms
0
440
SRE NEXT 2022: Sensible Incident Management for Software Startups
takanabe
1
200
Kueue入門/Kueue Introduction
bells17
0
490
Named Document って何?
harunakano
0
150
Improve Build Times in Less Time
zacsweers
6
2.7k
設計とテストの必要性について考える
akeno
0
140
Featured
See All Featured
The Invisible Side of Design
smashingmag
289
48k
Robots, Beer and Maslow
schacon
152
7.1k
Debugging Ruby Performance
tmm1
65
10k
GitHub's CSS Performance
jonrohan
1020
410k
Why Our Code Smells
bkeepers
PRO
324
54k
Keith and Marios Guide to Fast Websites
keithpitt
404
21k
Unsuck your backbone
ammeep
659
55k
A Modern Web Designer's Workflow
chriscoyier
689
180k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
15
900
Faster Mobile Websites
deanohume
294
28k
Producing Creativity
orderedlist
PRO
333
37k
Bash Introduction
62gerente
596
210k
Transcript
Web開発を⽀える マイグレーションツールについて 第11回 PostgreSQLアンカンファレンス@東京
⾃⼰紹介 @yudppp 株式会社HRBrain CTO 好きな⾔葉: 冪等性 嫌いなモノ: 浮動⼩数点の誤差 基本的にWebエンジニア
⽬標評価管理クラウド
HRBrainとは • ⽬標と評価を管理するクラウドSaaSを作って いる会社です。 • リリース開始からもうすぐで3年
先⽇Buildersconにて Row Level Securityは マルチテナントの 銀の弾丸になりうるのか という話をしました https://speakerdeck.com/yudppp/row-level-security-is-silver-bullet-for-multitenancy
概要 • SaaS開発していく上でリリース頻度を⾼く開 発していくには真のマルチテナント(全ての会 社の情報が⼀つのDB)にしていきたいが、セ キュリティ的な不安要素をPostgreSQLの RowLevelSecurityを使って解決した話
PostgreSQL Conference Japanで この内容とマイグレーションについて話そうとしたら CfP落ちました。。
現在のリリース頻度 • 週に2回リリース • 毎回ではないがSQLのマイグレーションが頻 繁に必要
みなさんマイグレーション の管理どうされていますか
元々のマイグレーション管理 gooseというGo製のToolを利⽤
起きた問題① Aさん「81_modify_user_birthday_col.sql」 Bさん「81_add_era_table.sql」 Aさん「Merge!」 Bさん「Merge!」 CI 「Test / Deploy 失敗!」
CIでTest実⾏した瞬間は ⼤丈夫なので Mergeされる前に 検知するのが難しい
起きた問題② 前提: みんなでdev環境⼀つを共有していてfeature ブランチをSlackで告知すれば好きなタイミ ングであげれる運⽤ (本当はfeatureブランチごとdeployしたい)
起きた問題② Bさん「令和追加した確認するのでdev使い ます。」 CI 「Deployed!」 Migrationがなぜか呼ばれない。。 誰かがあげたMigrationがdownされてない
この運⽤をしていると Migrationを戻すタイミング が難しい
解決策を探す
k0kubun/sqldef The easiest idempotent MySQL/PostgreSQL schema management by SQL. This
is inspired by Ridgepole but using SQL, so there's no need to remember Ruby DSL. 定義したschema.sqlとDB接続した情報を⽐較し ていい感じにALTER⽂を流してくれる Go製なのでbinaryだけで動く
https://github.com/k0kubun/sqldef
どうなるか 常にschemaファイルとDBの状態が等しくな るので、上記のような問題は起きない。 もし起きたとしても、schemaファイルが Conflictするので気づける。
sqldefの内部実装 Vitess(Vitess is a database clustering system for horizontal scaling
of MySQL)のsqlparser のsourceを拡張して作られている。 information_schema.table_constraintsあた りをSELECTした結果を元にCREATE⽂を Buildしている。
既知の課題 基本的な構⽂は対応しているが、Vitessから 派⽣したこともありPostgreSQLのあまり使わ れない構⽂が未対応だったりする。(そう例え ばRowLevelSecurityとか) sqldefのPostgreSQLのユーザーがそこまで多 くなさそう。
まとめ 冪等性が担保された安全な⽣活がしたい。 sqldefをPostgreSQLで使うユーザーを増やし て、より洗礼されたツールになってほしい 今⽇の発表で1⼈でもユーザーが増えたら嬉 しい
参考資料 •SQLで羃等にDBスキーマ管理ができるツー ル「sqldef」を作った (https:// k0kubun.hatenablog.com/entry/2018/08/ 25/114455) •http://vitess.io/