Module: Ridgepole::ExtTidb::SchemaCreation

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

Instance Method Summary collapse

Instance Method Details

#tidb?Boolean

この SchemaCreation インスタンスから接続の tidb? を参照できるように

Returns:



7
8
9
10
11
12
13
# File 'lib/ridgepole/ext_tidb/schema_creation.rb', line 7

def tidb?
  if instance_variable_defined?(:@conn)
    conn = instance_variable_get(:@conn)
    return conn.respond_to?(:tidb?) && conn.tidb?
  end
  false
end

#visit_ColumnDefinition(o) ⇒ Object

APPLY: カラムに AUTO_RANDOM(n) を追加(PRIMARY KEY の有無に関わらず列側に付与)



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
59
60
61
62
# File 'lib/ridgepole/ext_tidb/schema_creation.rb', line 31

def visit_ColumnDefinition(o)
  return super unless tidb?

  # AUTO_RANDOM を取り出し(未知キーassertは Hash 拡張で回避済み)
  auto_random_value = o.options.delete(:auto_random)

  sql = super

  # テーブルレベル指定のフォールバック(id の自動生成経路で失われた場合に備える)
  if !auto_random_value && o.respond_to?(:primary_key?) && o.primary_key?
    if instance_variable_defined?(:@conn)
      conn = instance_variable_get(:@conn)
      if conn.instance_variable_defined?(:@tidb_pending_auto_random_pk_bits)
        auto_random_value = conn.instance_variable_get(:@tidb_pending_auto_random_pk_bits)
        begin
          conn.remove_instance_variable(:@tidb_pending_auto_random_pk_bits)
        rescue StandardError
        end
      end
    end
  end

  if auto_random_value
    # 念のため AUTO_INCREMENT を除去
    sql.sub!(/\sAUTO_INCREMENT\b/i, "")

    bits = (auto_random_value == true ? nil : Integer(auto_random_value) rescue nil)
    sql << (bits ? " AUTO_RANDOM(#{bits})" : " AUTO_RANDOM")
  end

  sql
end

#visit_PrimaryKeyDefinition(o) ⇒ Object

デフォルト主キーの生成経路でも AUTO_RANDOM を反映



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ridgepole/ext_tidb/schema_creation.rb', line 15

def visit_PrimaryKeyDefinition(o)
  return super unless tidb?

  auto_random_value = o.options.delete(:auto_random)
  sql = super

  if auto_random_value
    sql.sub!(/\sAUTO_INCREMENT\b/i, "")
    bits = (auto_random_value == true ? nil : Integer(auto_random_value) rescue nil)
    sql << (bits ? " AUTO_RANDOM(#{bits})" : " AUTO_RANDOM")
  end

  sql
end