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
- #dialect ⇒ 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
#alter_table_add_column_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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 82 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
104 105 106 107 108 |
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 104 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 |
#dialect ⇒ Object
8 9 10 |
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 8 def dialect self.class.to_s.sub(/.*::/, '').sub(/Adapter$/,'') end |
#field_exists?(storage_name, column_name) ⇒ Boolean
Returns whether the field exists.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 44 def field_exists?(storage_name, column_name) statement = <<-SQL.compress_lines 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.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 21 def storage_exists?(storage_name) statement = <<-SQL.compress_lines 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
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/dm-migrations/adapters/dm-do-adapter.rb', line 57 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 property end.compact end end |