Permalink
Browse files

Make auto_upgrade handle unique indexes when materializing missing pr…

…operties.

Previously only handled `property.options[:index] === true` case. If a property
specified `:unique_index => true`, or defaulted to it via `:unique => true`, the
unique index was completely ignored.
  • Loading branch information...
1 parent b59f2fd commit 349073395edd68b13d5a2b981b1f7bd9ca58d35e @jpr5 jpr5 committed Jul 20, 2011
Showing with 23 additions and 16 deletions.
  1. +23 −16 lib/dm-migrations/adapters/dm-do-adapter.rb
@@ -65,15 +65,16 @@ def upgrade_model_storage(model)
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
+ connection.create_command(statement).execute_non_query
+
+ # For now, only support unnamed/ungrouped indices. Give precedence
+ # to :unique_index over :index, since it's possible to specify both.
+ if property.unique_index === true
+ statement = create_unique_index_statement(model, property.name, [property.field])
+ connection.create_command(statement).execute_non_query
+ elsif property.index === true
statement = create_index_statement(model, property.name, [property.field])
- command = connection.create_command(statement)
- command.execute_non_query
+ connection.create_command(statement).execute_non_query
end
property
@@ -94,8 +95,7 @@ def create_model_storage(model)
statements.concat(create_unique_index_statements(model))
statements.each do |statement|
- command = connection.create_command(statement)
- command.execute_non_query
+ connection.create_command(statement).execute_non_query
end
end
@@ -157,6 +157,16 @@ def drop_table_statement(model)
end
# @api private
+ 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
+
+ # @api private
def create_index_statements(model)
table_name = model.storage_name(name)
@@ -166,11 +176,11 @@ def create_index_statements(model)
end
# @api private
- def create_index_statement(model, index_name, fields)
+ def create_unique_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
+ CREATE UNIQUE INDEX #{quote_name("unique_#{table_name}_#{index_name}")} ON
#{quote_name(table_name)} (#{fields.map { |field| quote_name(field) }.join(', ')})
SQL
end
@@ -182,10 +192,7 @@ def create_unique_index_statements(model)
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
+ create_unique_index_statement(model, index_name, fields)
end
end

0 comments on commit 3490733

Please sign in to comment.