Created
December 14, 2010 06:29
-
-
Save probablykabari/740076 to your computer and use it in GitHub Desktop.
Allow DataMapper::Model descendants to have different storage names and work with auto_upgrade/auto_migrate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From dac3c04bba2898757e0ea7004b49ec7f63894d50 Mon Sep 17 00:00:00 2001 | |
From: Kabari Hendrick <[email protected]> | |
Date: Tue, 14 Dec 2010 00:26:54 -0600 | |
Subject: [PATCH] allow subclassesd models to have different storage | |
Signed-off-by: Kabari Hendrick <[email protected]> | |
--- | |
lib/dm-migrations/auto_migration.rb | 6 +- | |
spec/integration/model_migration_spec.rb | 64 ++++++++++++++++++++++++++++++ | |
2 files changed, 67 insertions(+), 3 deletions(-) | |
create mode 100644 spec/integration/model_migration_spec.rb | |
diff --git a/lib/dm-migrations/auto_migration.rb b/lib/dm-migrations/auto_migration.rb | |
index cc0f53b..36c0b40 100644 | |
--- a/lib/dm-migrations/auto_migration.rb | |
+++ b/lib/dm-migrations/auto_migration.rb | |
@@ -139,7 +139,7 @@ module DataMapper | |
def auto_upgrade!(repository_name = self.repository_name) | |
assert_valid(true) | |
base_model = self.base_model | |
- if base_model == self | |
+ if base_model == self || (self.storage_names[repository_name] != base_model.storage_names[repository_name]) | |
repository(repository_name).upgrade_model_storage(self) | |
else | |
base_model.auto_upgrade!(repository_name) | |
@@ -156,7 +156,7 @@ module DataMapper | |
def auto_migrate_down!(repository_name = self.repository_name) | |
assert_valid(true) | |
base_model = self.base_model | |
- if base_model == self | |
+ if base_model == self || (self.storage_names[repository_name] != base_model.storage_names[repository_name]) | |
repository(repository_name).destroy_model_storage(self) | |
else | |
base_model.auto_migrate_down!(repository_name) | |
@@ -171,7 +171,7 @@ module DataMapper | |
def auto_migrate_up!(repository_name = self.repository_name) | |
assert_valid(true) | |
base_model = self.base_model | |
- if base_model == self | |
+ if base_model == self || (self.storage_names[repository_name] != base_model.storage_names[repository_name]) | |
repository(repository_name).create_model_storage(self) | |
else | |
base_model.auto_migrate_up!(repository_name) | |
diff --git a/spec/integration/model_migration_spec.rb b/spec/integration/model_migration_spec.rb | |
new file mode 100644 | |
index 0000000..d7c8f21 | |
--- /dev/null | |
+++ b/spec/integration/model_migration_spec.rb | |
@@ -0,0 +1,64 @@ | |
+describe DataMapper::Migrations do | |
+ describe "inheritance" do | |
+ supported_by(:postgres, :mysql, :sqlite, :oracle, :sqlserver) do | |
+ describe "child tables" do | |
+ before(:all) do | |
+ module ::Hospital | |
+ class Person | |
+ include DataMapper::Resource | |
+ property :id, Serial | |
+ property :name, String | |
+ end | |
+ | |
+ class Doctor < Person | |
+ self.storage_names[:default] = 'doctors' | |
+ | |
+ property :hospital, String | |
+ end | |
+ end | |
+ | |
+ @person_model = Hospital::Person | |
+ @doctor_model = Hospital::Doctor | |
+ end | |
+ | |
+ after(:each) do | |
+ repository.destroy_model_storage(@person_model) | |
+ repository.destroy_model_storage(@doctor_model) | |
+ end | |
+ | |
+ describe "::auto_migrate!" do | |
+ it 'should migrate if #storage_name is different' do | |
+ @person_model.auto_migrate! | |
+ repository.storage_exists?(@person_model.storage_name).should be(true) | |
+ @doctor_model.auto_migrate! | |
+ repository.storage_exists?(@doctor_model.storage_name).should be(true) | |
+ end | |
+ end | |
+ | |
+ describe "::auto_upgrade!" do | |
+ before(:all) do | |
+ module Hospital | |
+ class Doctor < Person | |
+ property :resident, Boolean | |
+ end | |
+ end | |
+ end | |
+ | |
+ before(:each) do | |
+ @person_model.auto_migrate! | |
+ @person_model.auto_migrate! | |
+ end | |
+ | |
+ # sanity check | |
+ it { @doctor_model.properties[:resident].should_not be(nil) } | |
+ | |
+ # There is probably a better way to test this... | |
+ it 'should upgrade if #storage_name is different' do | |
+ @doctor_model.auto_upgrade! | |
+ lambda { @doctor_model.first(:resident) }.should_not raise_error(ArgumentError) | |
+ end | |
+ end | |
+ end | |
+ end # supported_by | |
+ end | |
+end | |
-- | |
1.7.3.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment