Skip to content

Commit

Permalink
Refactor auto migration into two steps
Browse files Browse the repository at this point in the history
  • Loading branch information
dbussink committed Aug 7, 2008
1 parent 48371cd commit 9639709
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/dm-core/associations.rb
Expand Up @@ -40,6 +40,7 @@ class UnsavedParentError < RuntimeError
#
# @api private
def many_to_one_relationships
relationships unless @relationships # needs to be initialized!
@relationships.values.collect do |rels| rels.values end.flatten.select do |relationship| relationship.child_model == self end
end

Expand Down
35 changes: 32 additions & 3 deletions lib/dm-core/auto_migrations.rb
Expand Up @@ -8,9 +8,14 @@ class AutoMigrator
#
# @param Symbol repository_name the repository to be migrated
# @calls DataMapper::Resource#auto_migrate!
def self.auto_migrate(repository_name = nil)
DataMapper::Resource.descendants.each do |model|
model.auto_migrate!(repository_name)
def self.auto_migrate(repository_name = nil, *descendants)
descendants = DataMapper::Resource.descendants.to_a if descendants.empty?
descendants.reverse.each do |model|
model.auto_migrate_down!(repository_name)
end

descendants.each do |model|
model.auto_migrate_up!(repository_name)
end
end

Expand All @@ -34,11 +39,35 @@ module AutoMigrations
#
# @param Symbol repository_name the repository to be migrated
def auto_migrate!(repository_name = nil)
auto_migrate_down!(repository_name)
auto_migrate_up!(repository_name)
end

##
# Destructively migrates the data-store down, which basically
# deletes all the models.
# REPEAT: THIS IS DESTRUCTIVE
#
# @param Symbol repository_name the repository to be migrated
def auto_migrate_down!(repository_name = nil)
if self.superclass != Object
self.superclass.auto_migrate!(repository_name)
else
repository(repository_name) do |r|
r.adapter.destroy_model_storage(r, self)
end
end
end

##
# Auto migrates the data-store to match the model
#
# @param Symbol repository_name the repository to be migrated
def auto_migrate_up!(repository_name = nil)
if self.superclass != Object
self.superclass.auto_migrate!(repository_name)
else
repository(repository_name) do |r|
r.adapter.create_model_storage(r, self)
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/integration/association_spec.rb
Expand Up @@ -370,6 +370,13 @@ class Ostrich
Machine.first(:name => 'machine10').should_not be_nil
end

it 'should set and retrieve associations on not yet saved objects' do
e = Machine.create(:name => 'machine10')
y = e.areas.build(:name => 'area10')

y.machine.name.should == 'machine10'
end

it 'should convert NULL parent ids into nils' do
Area.first(:name => 'area2').machine.should be_nil
end
Expand Down
3 changes: 2 additions & 1 deletion spec/unit/auto_migrations_spec.rb
Expand Up @@ -85,7 +85,8 @@

models.each do |model|
DataMapper::Resource.descendants << model
model.should_receive(:auto_migrate!).with(@repository_name)
model.should_receive(:auto_migrate_down!).with(@repository_name)
model.should_receive(:auto_migrate_up!).with(@repository_name)
end

DataMapper::AutoMigrator.auto_migrate(@repository_name)
Expand Down

0 comments on commit 9639709

Please sign in to comment.