Module: DataMapper::Migrations::DataObjectsAdapter::SQL

Included in:
DataMapper::Migrations::DataObjectsAdapter
Defined in:
lib/dm-migrations/adapters/dm-do-adapter.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_column_statementObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



257
258
259
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 257

def add_column_statement
  'ADD COLUMN'
end

#alter_table_add_column_statement(connection, table_name, schema_hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



133
134
135
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 133

def alter_table_add_column_statement(connection, table_name, schema_hash)
  "ALTER TABLE #{quote_name(table_name)} #{add_column_statement} #{property_schema_statement(connection, schema_hash)}"
end

#create_index_statement(model, index_name, fields) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



167
168
169
170
171
172
173
174
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 167

def create_index_statement(model, index_name, fields)
  table_name = model.storage_name(name)

  DataMapper::Ext::String.compress_lines(<<-SQL)
    CREATE INDEX #{quote_name("index_#{table_name}_#{index_name}")} ON
    #{quote_name(table_name)} (#{fields.map { |field| quote_name(field) }.join(', ')})
  SQL
end

#create_index_statements(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



157
158
159
160
161
162
163
164
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 157

def create_index_statements(model)
  name = self.name
  model.storage_name(name)

  indexes(model).map do |index_name, fields|
    create_index_statement(model, index_name, fields)
  end
end

#create_table_statement(connection, model, properties) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



138
139
140
141
142
143
144
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 138

def create_table_statement(connection, model, properties)
  DataMapper::Ext::String.compress_lines(<<-SQL)
    CREATE TABLE #{quote_name(model.storage_name(name))}
    (#{properties.map { |property| property_schema_statement(connection, property_schema_hash(property)) }.join(', ')},
    PRIMARY KEY(#{properties.key.map { |property| quote_name(property.field) }.join(', ')}))
  SQL
end

#create_unique_index_statements(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 177

def create_unique_index_statements(model)
  name           = self.name
  table_name     = model.storage_name(name)
  key            = model.key(name).map(&:field)
  unique_indexes = unique_indexes(model).reject { |_index_name, fields| fields == key }

  unique_indexes.map do |index_name, fields|
    DataMapper::Ext::String.compress_lines(<<-SQL)
      CREATE UNIQUE INDEX #{quote_name("unique_#{table_name}_#{index_name}")} ON
      #{quote_name(table_name)} (#{fields.map { |field| quote_name(field) }.join(', ')})
    SQL
  end
end

#drop_table_statement(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



147
148
149
150
151
152
153
154
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 147

def drop_table_statement(model)
  table_name = quote_name(model.storage_name(name))
  if supports_drop_table_if_exists?
    "DROP TABLE IF EXISTS #{table_name}"
  else
    "DROP TABLE #{table_name}"
  end
end

#indexes(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



247
248
249
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 247

def indexes(model)
  model.properties(name).indexes
end

#property_schema_hash(property) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



192
193
194
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
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 192

def property_schema_hash(property)
  dump_class = property.dump_class
  type_by_property_class = self.class.type_by_property_class(property.class)

  schema = (type_by_property_class || self.class.type_map[dump_class]).merge(name: property.field)

  schema_primitive = schema[:primitive]

  if dump_class.equal?(String) && schema_primitive != 'TEXT' && schema_primitive != 'CLOB' && schema_primitive != 'NVARCHAR' &&
     schema_primitive != 'BYTEA' && schema_primitive != 'VARBINARY'
    schema[:length] = property.length
  elsif dump_class.equal?(BigDecimal) || dump_class.equal?(Float)
    schema[:precision] = property.precision
    schema[:scale]     = property.scale
  end

  schema[:allow_nil] = property.allow_nil?
  schema[:serial]    = property.serial?

  default = property.default

  if default.nil? || default.respond_to?(:call)
    # remove the default if the property does not allow nil
    schema.delete(:default) unless schema[:allow_nil]
  else
    schema[:default] = property.dump(default)
  end

  schema
end

#property_schema_statement(connection, schema) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 224

def property_schema_statement(connection, schema)
  statement = quote_name(schema[:name])
  statement << " #{schema[:primitive]}"

  length = schema[:length]

  if schema[:precision] && schema[:scale]
    statement << "(#{%i(precision scale).map { |key| connection.quote_value(schema[key]) }.join(', ')})"
  elsif length == 'max'
    statement << '(max)'
  elsif length
    statement << "(#{connection.quote_value(length)})"
  end

  default = schema[:default]
  statement << " DEFAULT #{connection.quote_value(default)}" if default

  statement << ' NOT NULL' unless schema[:allow_nil]

  statement
end

#schema_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (NotImplementedError)


128
129
130
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 128

def schema_name
  raise NotImplementedError, "#{self.class}#schema_name not implemented"
end

#supports_drop_table_if_exists?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


123
124
125
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 123

def supports_drop_table_if_exists?
  false
end

#supports_serial?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adapters that support AUTO INCREMENT fields for CREATE TABLE statements should overwrite this to return true

Returns:

  • (Boolean)


118
119
120
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 118

def supports_serial?
  false
end

#unique_indexes(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



252
253
254
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 252

def unique_indexes(model)
  model.properties(name).unique_indexes
end