Class: Ridgepole::DSLParser::TableDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/ridgepole/dsl_parser/table_definition.rb

Constant Summary collapse

DEFAULT_PRIMARY_KEY_TYPE =
:bigint
TYPES =
{
  # https://github.com/rails/rails/blob/main/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L300-L301
  bigint: {},
  binary: {},
  boolean: {},
  date: {},
  datetime: {},
  decimal: {},
  float: {},
  integer: {},
  json: {},
  string: {},
  text: {},
  time: {},
  timestamp: {},
  virtual: {},

  # https://github.com/rails/rails/blob/v6.0.6/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L101
  serial: { null: false },
  bigserial: { null: false },
  # string: {},
  # text: {},
  # integer: {},
  # float: {},
  # decimal: {},
  # datetime: {},
  # time: {},
  # date: {},
  daterange: {},
  numrange: {},
  tsrange: {},
  tstzrange: {},
  int4range: {},
  int8range: {},
  # binary: {},
  # boolean: {},
  xml: {},
  tsvector: {},
  hstore: {},
  inet: {},
  cidr: {},
  macaddr: {},
  uuid: {},
  # json: {},
  jsonb: {},
  ltree: {},
  citext: {},
  point: {},
  line: {},
  lseg: {},
  box: {},
  path: {},
  polygon: {},
  circle: {},
  bit: {},
  bit_varying: {},
  money: {},
  interval: {},
  oid: {},

  # https://github.com/ridgepole/ridgepole/issues/394
  timestamptz: {},

  enum: {},
}.freeze
ALIAS_TYPES =
{
  # https://github.com/rails/rails/blob/v5.0.0.rc1/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb
  tinyblob: [:blob, { limit: 255 }],
  mediumblob: [:binary, { limit: 16_777_215 }],
  longblob: [:binary, { limit: 4_294_967_295 }],
  tinytext: [:text, { limit: 255 }],
  mediumtext: [:text, { limit: 16_777_215 }],
  longtext: [:text, { limit: 4_294_967_295 }],
  unsigned_integer: [:integer, { unsigned: true }],
  unsigned_bigint: [:bigint, { unsigned: true }],
  unsigned_float: [:float, { limit: 24, unsigned: true }],
  unsigned_decimal: [:decimal, { precision: 10, unsigned: true }],
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, base) ⇒ TableDefinition

Returns a new instance of TableDefinition.



8
9
10
11
12
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 8

def initialize(table_name, base)
  @__definition = {}
  @table_name = table_name
  @base = base
end

Instance Attribute Details

#__definitionObject (readonly)

Returns the value of attribute __definition.



6
7
8
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 6

def __definition
  @__definition
end

Instance Method Details

#blob(*args) ⇒ Object

XXX:



118
119
120
121
122
123
124
125
126
127
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 118

def blob(*args)
  options = args.extract_options!
  options = { limit: 65_535 }.merge(options)
  column_names = args

  column_names.each do |name|
    column_type = (0..0xff).cover?(options[:limit]) ? :blob : :binary
    column(name, column_type, options)
  end
end

#check_constraint(expression, options = {}) ⇒ Object



179
180
181
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 179

def check_constraint(expression, options = {})
  @base.add_check_constraint(@table_name, expression, options)
end

#column(name, type, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 14

def column(name, type, options = {})
  name = name.to_s
  index_options = options.key?(:index) ? options.delete(:index) : false

  @__definition[name] = {
    type: type,
    options: options,
  }

  index(name, index_options.is_a?(Hash) ? index_options : {}) if index_options
end

#exclusion_constraint(expression, options = {}) ⇒ Object



183
184
185
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 183

def exclusion_constraint(expression, options = {})
  @base.add_exclusion_constraint(@table_name, expression, options)
end

#foreign_key(name, options = {}) ⇒ Object



142
143
144
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 142

def foreign_key(name, options = {})
  @base.add_foreign_key(@table_name, name, options)
end

#index(name, options = {}) ⇒ Object



138
139
140
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 138

def index(name, options = {})
  @base.add_index(@table_name, name, options)
end

#references(*args) ⇒ Object Also known as: belongs_to



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 152

def references(*args)
  options = args.extract_options!
  polymorphic = options.delete(:polymorphic)
  polymorphic_options = polymorphic.is_a?(Hash) ? polymorphic : {}
  # https://github.com/rails/rails/blob/5-2-1/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L167
  polymorphic_options.merge!(options.slice(:null, :first, :after))
  index_options = options.key?(:index) ? options.delete(:index) : true
  type = options.delete(:type) || DEFAULT_PRIMARY_KEY_TYPE
  foreign_key_options = options.delete(:foreign_key)

  args.each do |col|
    column("#{col}_id", type, options)
    column("#{col}_type", :string, polymorphic_options) if polymorphic
    if index_options
      columns = polymorphic ? ["#{col}_type", "#{col}_id"] : ["#{col}_id"]
      index(columns, index_options.is_a?(Hash) ? index_options : {})
    end
    if foreign_key_options # rubocop:disable Style/Next
      fk_opts = foreign_key_options.is_a?(Hash) ? foreign_key_options.dup : {}
      fk_opts.update(column: "#{col}_id") if col.to_s.singularize != col.to_s
      to_table = fk_opts.delete(:to_table) || col.to_s.pluralize
      @base.add_foreign_key(@table_name, to_table, fk_opts)
    end
  end
end

#timestamps(*args) ⇒ Object



146
147
148
149
150
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 146

def timestamps(*args)
  options = { null: false }.merge(args.extract_options!)
  column(:created_at, :datetime, options.dup)
  column(:updated_at, :datetime, options.dup)
end

#unique_constraint(column_name, options = {}) ⇒ Object



187
188
189
# File 'lib/ridgepole/dsl_parser/table_definition.rb', line 187

def unique_constraint(column_name, options = {})
  @base.add_unique_constraint(@table_name, column_name, options)
end