LoginSignup
1
0

More than 3 years have passed since last update.

ridgepoleでRANGEパーティショニング

Last updated at Posted at 2020-12-05

以前、ridgepoleでRANGEパーティショニング(したかったけどワーニングが気持ち悪いから諦めてSQL書いた件) という記事を書いた。
これは、ridgepoleでの管理を諦めて生SQLを叩いていた。

しかしその後、複数人で開発しているとちらほら問題が…。
そこでもう少し試行錯誤してみて、ちょっと無理矢理だけどridgepoleにスキーマ管理させる方法を見つけたので書いておく。

やり方

options に無理矢理クエリを組み込む!w

comment = "コメント"
partitions = []
partitions << "PARTITION p201610 VALUES LESS THAN ('2016-10-01') ENGINE = InnoDB"
partitions << "PARTITION p201611 VALUES LESS THAN ('2016-11-01') ENGINE = InnoDB"
# partitionは必要に応じて、列挙する
partitioning_sql = "/*!50500 PARTITION BY RANGE COLUMNS(created_at)\n(#{partitions.join(",\n ")}) */"

create_table "histories", id: false, primary_key: %i[id created_at], force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='#{comment}'\n#{partitioning_sql} */" do |t|
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end
  • この手法を使う場合コメントもoptionsにいれないとエラーが出る

注意点

MySQLのバージョンによってはパーティショニングしているテーブルに対してのカラム追加はオンラインDDLにならない事がある。
カジュアルに実行してしまうとサービス停止に追い込む事になるので気をつけましょう!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0