Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Ensure Text can be auto-migrated when :index and :unique_index are true
Browse files Browse the repository at this point in the history
[#1103 state:resolved]
  • Loading branch information
dkubb committed Feb 4, 2010
1 parent 121d337 commit 0fd658a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
35 changes: 32 additions & 3 deletions lib/dm-core/migrations.rb
Expand Up @@ -208,7 +208,7 @@ def drop_table_statement(model)
def create_index_statements(model)
name = self.name
table_name = model.storage_name(name)
model.properties(name).indexes.map do |index_name, fields|
indexes(model).map do |index_name, fields|
<<-SQL.compress_lines
CREATE INDEX #{quote_name("index_#{table_name}_#{index_name}")} ON
#{quote_name(table_name)} (#{fields.map { |field| quote_name(field) }.join(', ')})
Expand All @@ -220,7 +220,7 @@ def create_index_statements(model)
def create_unique_index_statements(model)
name = self.name
table_name = model.storage_name(name)
model.properties(name).unique_indexes.map do |index_name, fields|
unique_indexes(model).map do |index_name, fields|
<<-SQL.compress_lines
CREATE UNIQUE INDEX #{quote_name("unique_#{table_name}_#{index_name}")} ON
#{quote_name(table_name)} (#{fields.map { |field| quote_name(field) }.join(', ')})
Expand Down Expand Up @@ -283,6 +283,16 @@ def property_schema_statement(connection, schema)
statement << ' NOT NULL' unless schema[:allow_nil]
statement
end

# @api private
def indexes(model)
model.properties(name).indexes
end

# @api private
def unique_indexes(model)
model.properties(name).unique_indexes
end
end # module SQL

include SQL
Expand Down Expand Up @@ -547,6 +557,26 @@ def integer_display_size(range)
def integer_statement_sign(range)
' UNSIGNED' unless range.first < 0
end

# @api private
def indexes(model)
filter_indexes(model, super)
end

# @api private
def unique_indexes(model)
filter_indexes(model, super)
end

# Filter out any indexes with an unindexable column in MySQL
#
# @api private
def filter_indexes(model, indexes)
field_map = model.properties(name).field_map
indexes.select do |index_name, fields|
fields.all? { |field| field_map[field].type != Types::Text }
end
end
end # module SQL

include SQL
Expand Down Expand Up @@ -1275,7 +1305,6 @@ def type_map

end # module SqlserverAdapter


module Repository
# Determine whether a particular named storage exists in this repository
#
Expand Down
5 changes: 5 additions & 0 deletions lib/dm-core/property_set.rb
Expand Up @@ -147,6 +147,11 @@ def in_context(properties)
properties_in_context.flatten.uniq
end

# @api private
def field_map
map { |property| [ property.field, property ] }.to_hash
end

private

# @api semipublic
Expand Down
22 changes: 22 additions & 0 deletions spec/public/property/text_spec.rb
@@ -0,0 +1,22 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))

describe DataMapper::Property, 'Text type' do
describe 'migration with an index' do
supported_by :all do
before do
@model = DataMapper::Model.new do
storage_names[:default] = 'anonymous'

property :id, DataMapper::Types::Serial
property :body, DataMapper::Types::Text, :index => true
end
end

it 'should allow a migration' do
lambda {
@model.auto_migrate!
}.should_not raise_error(DataObjects::SyntaxError)
end
end
end if defined?(DataObjects::SyntaxError)
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Expand Up @@ -133,7 +133,7 @@ def remove_ivars(object, instance_variables = object.instance_variables)
constant_name = parts.pop.to_sym
base = parts.empty? ? Object : Object.full_const_get(parts.join('::'))

if base.const_defined?(constant_name)
if constant_name.to_s[0] != ?# && base.const_defined?(constant_name)
base.send(:remove_const, constant_name)
end

Expand Down

0 comments on commit 0fd658a

Please sign in to comment.