Class: ActiveRecord::ConnectionAdapters::TidbAdapter

Inherits:
Mysql2Adapter
  • Object
show all
Includes:
Sequence::Adapter
Defined in:
lib/active_record/connection_adapters/tidb_adapter.rb

Constant Summary collapse

ADAPTER_NAME =
'Tidb'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sequence::Adapter

#check_sequences, #create_sequence, #drop_sequence, #recreate_sequence

Constructor Details

#initialize(connection, logger, conn_params, config) ⇒ TidbAdapter

Returns a new instance of TidbAdapter.



86
87
88
89
90
91
# File 'lib/active_record/connection_adapters/tidb_adapter.rb', line 86

def initialize(connection, logger, conn_params, config)
  super(connection, logger, conn_params, config)

  tidb_version_string = query_value('select version()')
  @tidb_version = tidb_version_string[/TiDB-v([0-9.]+)/, 1]
end

Class Method Details

.database_exists?(config) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/active_record/connection_adapters/tidb_adapter.rb', line 101

def self.database_exists?(config)
  !ActiveRecord::Base.tidb_connection(config).nil?
rescue ActiveRecord::NoDatabaseError
  false
end

Instance Method Details

#new_column_from_field(_table_name, field) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/active_record/connection_adapters/tidb_adapter.rb', line 107

def new_column_from_field(_table_name, field)
   = (field[:Type], field[:Extra])
  default = field[:Default]
  default_function = nil

  if .type == :datetime && /\ACURRENT_TIMESTAMP(?:\([0-6]?\))?\z/i.match?(default)
    default = "#{default} ON UPDATE #{default}" if /on update CURRENT_TIMESTAMP/i.match?(field[:Extra])
    default_function = default
    default = nil
  elsif .extra == 'DEFAULT_GENERATED'
    default = +"(#{default})" unless default.start_with?('(')
    default_function = default
    default = nil
  elsif default.to_s =~ /nextval/i
    default_function = default
    default = nil
  end

  MySQL::Column.new(
    field[:Field],
    default,
    ,
    field[:Null] == 'YES',
    default_function,
    collation: field[:Collation],
    comment: field[:Comment].presence
  )
end

#supports_advisory_locks?Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/active_record/connection_adapters/tidb_adapter.rb', line 55

def supports_advisory_locks?
  # https://github.com/pingcap/tidb/issues/14994 support is required
  false
end

#supports_bulk_alter?Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/active_record/connection_adapters/tidb_adapter.rb', line 50

def supports_bulk_alter?
  # https://github.com/pingcap/tidb/issues/14766 support is required
  false
end

#supports_common_table_expressions?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/active_record/connection_adapters/tidb_adapter.rb', line 82

def supports_common_table_expressions?
  tidb_version >= '5.1.0'
end

#supports_expression_index?Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
# File 'lib/active_record/connection_adapters/tidb_adapter.rb', line 73

def supports_expression_index?
  sql = <<~SQL
    SELECT VALUE 
    FROM INFORMATION_SCHEMA.CLUSTER_CONFIG 
    WHERE `KEY` = 'experimental.allow-expression-index' AND `TYPE` = 'tidb'
  SQL
  query_value(sql) == 'true'
end

#supports_foreign_keys?Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/active_record/connection_adapters/tidb_adapter.rb', line 45

def supports_foreign_keys?
  # https://github.com/pingcap/tidb/issues/18209 support is required
  false
end

#supports_index_sort_order?Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/active_record/connection_adapters/tidb_adapter.rb', line 68

def supports_index_sort_order?
  # https://github.com/pingcap/tidb/issues/2519 support is required
  false
end

#supports_json?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/active_record/connection_adapters/tidb_adapter.rb', line 64

def supports_json?
  true
end

#supports_optimizer_hints?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/active_record/connection_adapters/tidb_adapter.rb', line 60

def supports_optimizer_hints?
  true
end

#supports_savepoints?Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/active_record/connection_adapters/tidb_adapter.rb', line 40

def supports_savepoints?
  # https://github.com/pingcap/tidb/issues/6840 support is required
  false
end

#tidb_versionObject



97
98
99
# File 'lib/active_record/connection_adapters/tidb_adapter.rb', line 97

def tidb_version
  Version.new(tidb_version_string)
end

#tidb_version_stringObject



93
94
95
# File 'lib/active_record/connection_adapters/tidb_adapter.rb', line 93

def tidb_version_string
  @tidb_version
end