Skip to content

Instantly share code, notes, and snippets.

@dbussink
Created August 7, 2008 16:09
Show Gist options
  • Save dbussink/4437 to your computer and use it in GitHub Desktop.
Save dbussink/4437 to your computer and use it in GitHub Desktop.
index 914a147..f8f5d0c 100644
--- a/lib/dm-core/auto_migrations.rb
+++ b/lib/dm-core/auto_migrations.rb
@@ -8,9 +8,14 @@ module DataMapper
#
# @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 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
@@ -34,11 +39,35 @@ module DataMapper
#
# @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
diff --git a/spec/unit/auto_migrations_spec.rb b/spec/unit/auto_migrations_spec.rb
index 4b086a7..38af09b 100644
--- a/spec/unit/auto_migrations_spec.rb
+++ b/spec/unit/auto_migrations_spec.rb
@@ -85,7 +85,8 @@ describe DataMapper::AutoMigrations do
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment