Class: DbSchema::DSL::TableYielder

Inherits:
Object
  • Object
show all
Defined in:
lib/db_schema/dsl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, block) ⇒ TableYielder

Returns a new instance of TableYielder.



55
56
57
58
# File 'lib/db_schema/dsl.rb', line 55

def initialize(table_name, block)
  @table_name = table_name
  block.call(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, name, *args, &block) ⇒ Object



79
80
81
# File 'lib/db_schema/dsl.rb', line 79

def method_missing(method_name, name, *args, &block)
  field(name, method_name, args.first || {})
end

Instance Attribute Details

#table_nameObject (readonly)

Returns the value of attribute table_name.



53
54
55
# File 'lib/db_schema/dsl.rb', line 53

def table_name
  @table_name
end

Class Method Details

.build_foreign_key(fkey_fields, table_name:, references:, name: nil, on_update: :no_action, on_delete: :no_action, deferrable: false) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/db_schema/dsl.rb', line 195

def build_foreign_key(fkey_fields, table_name:, references:, name: nil, on_update: :no_action, on_delete: :no_action, deferrable: false)
  fkey_name = name || :"#{table_name}_#{fkey_fields.first}_fkey"

  if references.is_a?(Array)
    # [:table, :field]
    referenced_table, *referenced_keys = references

    Definitions::ForeignKey.new(
      name:       fkey_name,
      fields:     fkey_fields,
      table:      referenced_table,
      keys:       referenced_keys,
      on_delete:  on_delete,
      on_update:  on_update,
      deferrable: deferrable
    )
  else
    # :table
    Definitions::ForeignKey.new(
      name:       fkey_name,
      fields:     fkey_fields,
      table:      references,
      on_delete:  on_delete,
      on_update:  on_update,
      deferrable: deferrable
    )
  end
end

.build_index(columns, table_name:, name: nil, primary: false, unique: false, using: :btree, where: nil, **ordered_fields) ⇒ Object



146
147
148
149
150
151
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/db_schema/dsl.rb', line 146

def build_index(columns, table_name:, name: nil, primary: false, unique: false, using: :btree, where: nil, **ordered_fields)
  if columns.last.is_a?(Hash)
    *ascending_columns, ordered_expressions = columns
  else
    ascending_columns = columns
    ordered_expressions = {}
  end

  columns_data = ascending_columns.each_with_object({}) do |column_name, columns|
    columns[column_name] = :asc
  end.merge(ordered_fields).merge(ordered_expressions)

  index_columns = columns_data.map do |column_name, column_order_options|
    options = case column_order_options
    when :asc
      {}
    when :desc
      { order: :desc }
    when :asc_nulls_first
      { nulls: :first }
    when :desc_nulls_last
      { order: :desc, nulls: :last }
    else
      raise ArgumentError, 'Only :asc, :desc, :asc_nulls_first and :desc_nulls_last options are supported.'
    end

    if column_name.is_a?(String)
      Definitions::Index::Expression.new(column_name, **options)
    else
      Definitions::Index::TableField.new(column_name, **options)
    end
  end

  index_name = name || if primary
    "#{table_name}_pkey"
  else
    "#{table_name}_#{index_columns.map(&:index_name_segment).join('_')}_index"
  end

  Definitions::Index.new(
    name:      index_name,
    columns:   index_columns,
    primary:   primary,
    unique:    unique,
    type:      using,
    condition: where
  )
end

Instance Method Details

#array(name, of:, **options) ⇒ Object



68
69
70
# File 'lib/db_schema/dsl.rb', line 68

def array(name, of:, **options)
  field(name, :array, element_type: of, **options)
end

#check(name, condition) ⇒ Object



95
96
97
# File 'lib/db_schema/dsl.rb', line 95

def check(name, condition)
  checks << Definitions::CheckConstraint.new(name: name, condition: condition)
end

#checksObject



137
138
139
# File 'lib/db_schema/dsl.rb', line 137

def checks
  @checks ||= []
end

#field(name, type, primary_key: false, unique: false, index: false, references: nil, check: nil, **options) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/db_schema/dsl.rb', line 107

def field(name, type, primary_key: false, unique: false, index: false, references: nil, check: nil, **options)
  fields << Definitions::Field.build(name, type, options)

  if primary_key
    primary_key(name)
  end

  if unique
    index(name, unique: true)
  elsif index
    index(name)
  end

  if references
    foreign_key(name, references: references)
  end

  if check
    check("#{table_name}_#{name}_check", check)
  end
end

#fieldsObject



129
130
131
# File 'lib/db_schema/dsl.rb', line 129

def fields
  @fields ||= []
end

#foreign_key(*fkey_fields, **fkey_options) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/db_schema/dsl.rb', line 99

def foreign_key(*fkey_fields, **fkey_options)
  foreign_keys << TableYielder.build_foreign_key(
    fkey_fields,
    table_name: table_name,
    **fkey_options
  )
end

#foreign_keysObject



141
142
143
# File 'lib/db_schema/dsl.rb', line 141

def foreign_keys
  @foreign_keys ||= []
end

#index(*columns, **index_options) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/db_schema/dsl.rb', line 87

def index(*columns, **index_options)
  indexes << TableYielder.build_index(
    columns,
    table_name: table_name,
    **index_options
  )
end

#indexesObject



133
134
135
# File 'lib/db_schema/dsl.rb', line 133

def indexes
  @indexes ||= []
end

#primary_key(*columns, name: nil) ⇒ Object



83
84
85
# File 'lib/db_schema/dsl.rb', line 83

def primary_key(*columns, name: nil)
  index(*columns, name: name, primary: true, unique: true)
end