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

Commit

Permalink
Updated integration specs to be runnable using the ADAPTER env variable
Browse files Browse the repository at this point in the history
* By default all non-adapter specific specs run with SQLite3
* Run the tasks with "ADAPTER=postgres rake spec" to run all the
  non-adapter specific specs with PostgreSQL.  Use "mysql" to test
  with the MySQL adapter.  Adding more adapters will be easy.
* Any adapter specific specs, like the MySQL, PostgreSQL and SQLite3
  specs run with their own drivers
* Fixed specs and code that failed when run with different adapters.
  There were ~10 bugs uncovered and fixed, both assumptions made in
  the specs that were untrue, or logic errors in dm-core itself
* Updated PostgreSQL driver to run with notices disabled for implicit
  actions, like automatic index creation for primary keys, not deleting
  unknown tables when DROP TABLE IF EXISTS is used, for example.
  • Loading branch information
Dan Kubb committed May 22, 2008
1 parent 842ebb5 commit 799a506
Show file tree
Hide file tree
Showing 23 changed files with 297 additions and 336 deletions.
19 changes: 7 additions & 12 deletions lib/data_mapper/adapters/data_objects_adapter.rb
Expand Up @@ -234,16 +234,12 @@ def upgrade_model_storage(repository, model)

properties = []

with_connection do |connection|
model.properties(name).each do |property|
schema_hash = property_schema_hash(property, model)
unless field_exists?(table_name, schema_hash[:name])
statement = alter_table_add_column_statement(table_name, schema_hash)
command = connection.create_command(statement)
result = command.execute_non_query
properties << property if result.to_i == 1
end
end
model.properties(name).each do |property|
schema_hash = property_schema_hash(property, model)
next if field_exists?(table_name, schema_hash[:name])
statement = alter_table_add_column_statement(table_name, schema_hash)
result = execute(statement)
properties << property if result.to_i == 1
end

properties
Expand All @@ -257,7 +253,6 @@ def create_model_storage(repository, model)

# TODO: move to dm-more/dm-migrations
def destroy_model_storage(repository, model)
return false unless storage_exists?(model.storage_name(name))
execute(drop_table_statement(model)).to_i == 1
end

Expand Down Expand Up @@ -640,7 +635,7 @@ def create_table_statement(model)

# TODO: move to dm-more/dm-migrations
def drop_table_statement(model)
"DROP TABLE #{quote_table_name(model.storage_name(name))}"
"DROP TABLE IF EXISTS #{quote_table_name(model.storage_name(name))}"
end

# TODO: move to dm-more/dm-migrations
Expand Down
20 changes: 15 additions & 5 deletions lib/data_mapper/adapters/postgres_adapter.rb
Expand Up @@ -52,12 +52,12 @@ def upgrade_model_storage(repository, model)
# TODO: move to dm-more/dm-migrations
def create_model_storage(repository, model)
add_sequences(model)
super
without_notices { super }
end

# TODO: move to dm-more/dm-migrations
def destroy_model_storage(repository, model)
success = super
success = without_notices { super }
model.properties(name).each do |property|
drop_sequence(model, property) if property.serial?
end
Expand All @@ -74,8 +74,7 @@ def create_sequence(model, property)

# TODO: move to dm-more/dm-migrations
def drop_sequence(model, property)
return unless sequence_exists?(model, property)
execute(drop_sequence_statement(model, property))
without_notices { execute(drop_sequence_statement(model, property)) }
end

module SQL
Expand All @@ -85,6 +84,17 @@ def supports_returning?
true
end

# TODO: move to dm-more/dm-migrations
def without_notices(&block)
# execute the block with NOTICE messages disabled
begin
execute('SET client_min_messages = warning')
yield
ensure
execute('RESET client_min_messages')
end
end

# TODO: move to dm-more/dm-migrations
def add_sequences(model)
model.properties(name).each do |property|
Expand Down Expand Up @@ -115,7 +125,7 @@ def create_sequence_statement(model, property)

# TODO: move to dm-more/dm-migrations
def drop_sequence_statement(model, property)
"DROP SEQUENCE #{quote_column_name(sequence_name(model, property))}"
"DROP SEQUENCE IF EXISTS #{quote_column_name(sequence_name(model, property))}"
end

# TODO: move to dm-more/dm-migrations
Expand Down
16 changes: 8 additions & 8 deletions lib/data_mapper/associations/many_to_one.rb
Expand Up @@ -12,11 +12,11 @@ def many_to_one(name, options = {})
parent_model_name = options[:class_name] || DataMapper::Inflection.classify(name)

relationship = relationships(repository.name)[name] = Relationship.new(
name,
repository.name,
self.name,
parent_model_name,
options
name,
repository.name,
self.name,
parent_model_name,
options
)

class_eval <<-EOS, __FILE__, __LINE__
Expand All @@ -32,7 +32,7 @@ def #{name}=(parent_resource)
def #{name}_association
@#{name}_association ||= begin
relationship = self.class.relationships(#{repository.name.inspect})[:#{name}]
relationship = self.class.relationships(#{repository.name.inspect})[:#{name}]
association = Proxy.new(relationship, self)
child_associations << association
association
Expand All @@ -59,12 +59,12 @@ def save
end
end
end

def reload!
@parent_resource = nil
self
end

private

def initialize(relationship, child_resource)
Expand Down
10 changes: 5 additions & 5 deletions lib/data_mapper/associations/relationship.rb
Expand Up @@ -23,12 +23,10 @@ def child_key

def parent_key
@parent_key ||= begin
model_properties = parent_model.properties(repository_name)

parent_key = if @parent_properties
model_properties.slice(*@parent_properties)
parent_model.properties(repository_name).slice(*@parent_properties)
else
model_properties.key
parent_model.key(repository_name)
end

PropertySet.new(parent_key)
Expand All @@ -44,7 +42,9 @@ def get_children(parent, options = {}, finder = :all)
end

def get_parent(child)
query = parent_key.to_query(child_key.get(child))
bind_values = child_key.get(child)
return nil if bind_values.any? { |bind_value| bind_value.nil? }
query = parent_key.to_query(bind_values)

DataMapper.repository(repository_name) do
parent_model.first(query.merge(@query))
Expand Down
4 changes: 2 additions & 2 deletions lib/data_mapper/property_set.rb
Expand Up @@ -92,8 +92,8 @@ def lazy_load_context(names)
result
end

def to_query(values)
Hash[ *zip(values).flatten ]
def to_query(bind_values)
Hash[ *zip(bind_values).flatten ]
end

def inspect
Expand Down
2 changes: 1 addition & 1 deletion lib/data_mapper/repository.rb
Expand Up @@ -88,7 +88,7 @@ def save(resource)
# set defaults for new resource
if resource.new_record?
model.properties(name).each do |property|
next if property.default.nil? || resource.attribute_loaded?(property.name)
next if resource.attribute_loaded?(property.name)
property.set(resource, property.default_for(resource))
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/data_mapper/resource.rb
Expand Up @@ -71,7 +71,10 @@ def attribute_set(name, value)
old_value = instance_variable_get(ivar_name)
new_value = property.custom? ? property.type.dump(value, property) : property.typecast(value)

return if old_value == new_value
# skip setting the attribute if the new value equals the old
# value, or if the new value is nil, and the property does not
# allow nil values
return if new_value == old_value || (new_value.nil? && !property.nullable?)

if property.lock?
instance_variable_set("@shadow_#{name}", old_value)
Expand Down
1 change: 1 addition & 0 deletions script/all
@@ -1,4 +1,5 @@
#!/usr/bin/env sh
rake spec:unit
ADAPTER=sqlite3 rake spec:integration
ADAPTER=mysql rake spec:integration
ADAPTER=postgres rake spec:integration

0 comments on commit 799a506

Please sign in to comment.