Module: DataMapper::Migrations::DataObjectsAdapter
- Includes:
- SQL
- Included in:
- MysqlAdapter, OracleAdapter, PostgresAdapter, SqliteAdapter, SqlserverAdapter
- Defined in:
- lib/dm-migrations/adapters/dm-do-adapter.rb
Defined Under Namespace
Modules: ClassMethods, SQL
Instance Method Summary collapse
- #create_model_storage(model) ⇒ Object
- #destroy_model_storage(model) ⇒ Object
-
#field_exists?(storage_name, column_name) ⇒ Boolean
Returns whether the field exists.
-
#storage_exists?(storage_name) ⇒ Boolean
Returns whether the storage_name exists.
- #upgrade_model_storage(model) ⇒ Object
Methods included from SQL
#add_column_statement, #alter_table_add_column_statement, #create_index_statement, #create_index_statements, #create_table_statement, #create_unique_index_statements, #drop_table_statement, #indexes, #property_schema_hash, #property_schema_statement, #schema_name, #supports_drop_table_if_exists?, #supports_serial?, #unique_indexes
Instance Method Details
#create_model_storage(model) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# 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) ⇒ Object
108 109 110 111 112 |
# 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) ⇒ Boolean
Returns whether the field exists.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 40 def field_exists?(storage_name, column_name) statement = DataMapper::Ext::String.compress_lines(<<-SQL) SELECT COUNT(*) FROM "information_schema"."columns" WHERE "table_schema" = ? AND "table_name" = ? AND "column_name" = ? SQL select(statement, schema_name, storage_name, column_name).first > 0 end |
#storage_exists?(storage_name) ⇒ Boolean
Returns whether the storage_name exists.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 17 def storage_exists?(storage_name) statement = DataMapper::Ext::String.compress_lines(<<-SQL) SELECT COUNT(*) FROM "information_schema"."tables" WHERE "table_type" = 'BASE TABLE' AND "table_schema" = ? AND "table_name" = ? SQL select(statement, schema_name, storage_name).first > 0 end |
#upgrade_model_storage(model) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# 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.[: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 |