Skip to content

Instantly share code, notes, and snippets.

@alpaca-tc
Created May 23, 2021 13:14
Show Gist options
  • Save alpaca-tc/322d86a2e48e3a9aa33655fd83b2671c to your computer and use it in GitHub Desktop.
Save alpaca-tc/322d86a2e48e3a9aa33655fd83b2671c to your computer and use it in GitHub Desktop.
ridgepoleで全ての主キー・外部キーをunsigned: trueで定義する
# Schemafileで下記のように読み込む
# require_relative './lib/ridgepole_unsigned_primary_id'
# `create_table`, `t.references` で、常に `unsigned: true` を有効にするための拡張
# 個別に指定することもできるが、可読性が悪くなるのでデフォルトとする
module UnsingedReferences
def references(*args)
options = args.extract_options!
options[:unsigned] = true
args.push(options)
super(*args)
end
Ridgepole::DSLParser::TableDefinition.prepend(self)
end
# NOTE: 新しいridgepoleならCLIからデフォルトtable_optionsを渡すこともできるので、この処理は消しても良い
# https://github.com/winebarrel/ridgepole/pull/331
module WithDefaultTableOptions
DEFAULT_TABLE_OPTIONS = {
id: {
type: :bigint,
unsigned: true
},
charset: 'utf8mb4',
collation: 'utf8mb4_bin'
}.freeze
def create_table(table_name, options = {})
super(table_name, DEFAULT_TABLE_OPTIONS.dup.merge(options))
end
Ridgepole::DSLParser::Context.prepend(self)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment