Module: Ridgepole::ExtTidb::TableOptions

Defined in:
lib/ridgepole/ext_tidb/table_options.rb

Overview

TiDB 向け create_table 拡張(CREATE 時に完結)

  • テーブルレベル auto_random を id 定義へ伝播
  • AUTO_RANDOM_BASE をテーブル options に付与

Instance Method Summary collapse

Instance Method Details

#create_table(table_name, **options, &block) ⇒ Object

CREATE 時に auto_random / auto_random_base を正しく反映



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ridgepole/ext_tidb/table_options.rb', line 10

def create_table(table_name, **options, &block)
  return super unless tidb?

  # 独自キーを取り出す(AR の未知キー検証を回避)
  auto_random_value = options.delete(:auto_random)
  auto_random_base_value = options.delete(:auto_random_base)

  # id: false でなければ、テーブルレベル auto_random を id に伝播
  if auto_random_value && options[:id] != false
    id_opt = options[:id]
    id_opts = case id_opt
              when Hash
                id_opt.dup
              when Symbol
                { type: id_opt }
              when true, nil
                {}
              else
                { type: id_opt }
              end
    id_opts[:auto_random] = auto_random_value
    id_opts[:auto_increment] = false
    options[:id] = id_opts
  end

  # AUTO_RANDOM_BASE を options に付与
  if auto_random_base_value
    existing_options = options[:options] || ""
    if existing_options.present?
      options[:options] = "#{existing_options} AUTO_RANDOM_BASE=#{auto_random_base_value}"
    else
      options[:options] = "AUTO_RANDOM_BASE=#{auto_random_base_value}"
    end
  end

  # 主キー経路でビット数が落ちる環境向けのフォールバック用に一時保存
  if auto_random_value && options[:id] != false
    begin
      @tidb_pending_auto_random_pk_bits = auto_random_value
    rescue StandardError
    end
  end

  begin
    super
  ensure
    remove_instance_variable(:@tidb_pending_auto_random_pk_bits) if instance_variable_defined?(:@tidb_pending_auto_random_pk_bits)
  end
end