module DataMapper::Migrations::DataObjectsAdapter
Public Instance Methods
create_model_storage(model)
click to toggle source
@api semipublic
# File lib/dm-migrations/adapters/dm-do-adapter.rb, line 86 def create_model_storage(model) name = self.name properties = model.properties_with_subclasses(name) return false if storage_exists?(model.storage_name(name)) return false if properties.empty? with_connection do |connection| statements = [ create_table_statement(connection, model, properties) ] statements.concat(create_index_statements(model)) statements.concat(create_unique_index_statements(model)) statements.each do |statement| command = connection.create_command(statement) command.execute_non_query end end true end
destroy_model_storage(model)
click to toggle source
@api semipublic
# File lib/dm-migrations/adapters/dm-do-adapter.rb, line 108 def destroy_model_storage(model) return true unless supports_drop_table_if_exists? || storage_exists?(model.storage_name(name)) execute(drop_table_statement(model)) true end
field_exists?(storage_name, column_name)
click to toggle source
Returns whether the field exists.
@param [String] storage_name
a String defining the name of a storage, for example a table name.
@param [String] field
a String defining the name of a field, for example a column name.
@return [Boolean]
true if the field exists.
@api semipublic
# File lib/dm-migrations/adapters/dm-do-adapter.rb, line 40 def field_exists?(storage_name, column_name) statement = DataMapper::Ext::String.compress_lines(" SELECT COUNT(*) FROM "information_schema"."columns" WHERE "table_schema" = ? AND "table_name" = ? AND "column_name" = ? ") select(statement, schema_name, storage_name, column_name).first > 0 end
storage_exists?(storage_name)
click to toggle source
Returns whether the storage_name exists.
@param [String] storage_name
a String defining the name of a storage, for example a table name.
@return [Boolean]
true if the storage exists
@api semipublic
# File lib/dm-migrations/adapters/dm-do-adapter.rb, line 17 def storage_exists?(storage_name) statement = DataMapper::Ext::String.compress_lines(" SELECT COUNT(*) FROM "information_schema"."tables" WHERE "table_type" = 'BASE TABLE' AND "table_schema" = ? AND "table_name" = ? ") select(statement, schema_name, storage_name).first > 0 end
upgrade_model_storage(model)
click to toggle source
@api semipublic
# File lib/dm-migrations/adapters/dm-do-adapter.rb, line 53 def upgrade_model_storage(model) name = self.name properties = model.properties_with_subclasses(name) if success = create_model_storage(model) return properties end table_name = model.storage_name(name) with_connection do |connection| properties.map do |property| schema_hash = property_schema_hash(property) next if field_exists?(table_name, schema_hash[:name]) statement = alter_table_add_column_statement(connection, table_name, schema_hash) command = connection.create_command(statement) command.execute_non_query # For simple :index => true columns, add an appropriate index. # Upgrading doesn't know how to deal with complex indexes yet. if property.options[:index] === true statement = create_index_statement(model, property.name, [property.field]) command = connection.create_command(statement) command.execute_non_query end property end.compact end end