Module: DataMapper::Migrations::DataObjectsAdapter

Includes:
SQL
Defined in:
lib/dm-core/migrations.rb

Defined Under Namespace

Modules: ClassMethods, SQL

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SQL

#alter_table_add_column_statement, #create_index_statements, #create_table_statement, #create_unique_index_statements, #drop_table_statement, #property_schema_hash, #property_schema_statement, #schema_name, #supports_drop_table_if_exists?, #supports_serial?

Class Method Details

.included(base) ⇒ 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.

TODO: document



57
58
59
60
61
62
63
64
65
# File 'lib/dm-core/migrations.rb', line 57

def self.included(base)
  base.extend ClassMethods

  DataMapper.extend(Migrations::SingletonMethods)

  [ :Repository, :Model ].each do |name|
    DataMapper.const_get(name).send(:include, Migrations.const_get(name))
  end
end

Instance Method Details

#create_model_storage(model) ⇒ Object

TODO: document



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/dm-core/migrations.rb', line 138

def create_model_storage(model)
  properties = model.properties_with_subclasses(name)

  return false if storage_exists?(model.storage_name(name))
  return false if properties.empty?

  with_connection do |connection|
    statement = create_table_statement(connection, model, properties)
    command   = connection.create_command(statement)
    command.execute_non_query

    (create_index_statements(model) + create_unique_index_statements(model)).each do |statement|
      command   = connection.create_command(statement)
      command.execute_non_query
    end
  end

  true
end

#destroy_model_storage(model) ⇒ Object

TODO: document



160
161
162
163
164
# File 'lib/dm-core/migrations.rb', line 160

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.

Parameters:

  • storage_name (String)

    a String defining the name of a storage, for example a table name.

  • field (String)

    a String defining the name of a field, for example a column name.

Returns:

  • (Boolean)

    true if the field exists.



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dm-core/migrations.rb', line 99

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

  query(statement, schema_name, storage_name, column_name).first > 0
end

#storage_exists?(storage_name) ⇒ Boolean

Returns whether the storage_name exists.

Parameters:

  • storage_name (String)

    a String defining the name of a storage, for example a table name.

Returns:

  • (Boolean)

    true if the storage exists



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dm-core/migrations.rb', line 76

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

  query(statement, schema_name, storage_name).first > 0
end

#upgrade_model_storage(model) ⇒ Object

TODO: document



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dm-core/migrations.rb', line 113

def upgrade_model_storage(model)
  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