Permalink
Browse files

* added a 'dialect' method to the adapter so the adapter itself decid…

…es which sql dialect it needs (for migrations)

* use the dialect method to determine the right sql-extension for the adapter

* adjusted specs and added one spec to reflect above changes
  • Loading branch information...
1 parent c72ba40 commit fd551c0eab221501d633a2305b3649dde0c240ce @mkristian committed Jun 19, 2010
Showing with 22 additions and 7 deletions.
  1. +4 −0 lib/dm-migrations/adapters/dm-do-adapter.rb
  2. +6 −4 lib/dm-migrations/migration.rb
  3. +12 −3 spec/unit/migration_spec.rb
@@ -5,6 +5,10 @@ module Migrations
module DataObjectsAdapter
+ def dialect
+ self.class.to_s.sub(/.*::/, '').sub(/Adapter$/,'')
+ end
+
# Returns whether the storage_name exists.
#
# @param [String] storage_name
@@ -21,10 +21,12 @@ def initialize( position, name, opts = {}, &block )
@database = DataMapper.repository(@options[:database] || :default)
@adapter = @database.adapter
- case @adapter.class.to_s
- when /Sqlite/ then @adapter.extend(SQL::Sqlite)
- when /Mysql/ then @adapter.extend(SQL::Mysql)
- when /Postgres/ then @adapter.extend(SQL::Postgres)
+ if @adapter.respond_to?(:dialect)
+ begin
+ @adapter.extend(SQL.const_get("#{@adapter.dialect}"))
+ rescue NameError
+ raise "Unsupported Migration Adapter #{@adapter.class} with SQL dialect #{@adapter.dialect}"
+ end
else
raise "Unsupported Migration Adapter #{@adapter.class}"
end
@@ -3,7 +3,7 @@
describe 'Migration' do
supported_by :postgres, :mysql, :sqlite do
before do
- @adapter = mock('adapter', :class => DataMapper::Spec.adapter.class)
+ @adapter = mock('adapter', :dialect => DataMapper::Spec.adapter.dialect)
@repo = mock('DataMapper.repository', :adapter => @adapter)
DataMapper.stub!(:repository).and_return(@repo)
@m = DataMapper::Migration.new(1, :do_nothing, {}) {}
@@ -40,7 +40,7 @@
end
it 'should determine the class of the adapter to be extended' do
- @adapter.should_receive(:class).and_return(DataMapper::Spec.adapter.class)
+ @adapter.should_receive(:dialect).and_return(DataMapper::Spec.adapter.dialect)
DataMapper::Migration.new(1, :do_nothing, {}) {}
end
@@ -49,8 +49,17 @@
DataMapper::Migration.new(1, :do_nothing, {}) {}
end
+ it 'should raise "Unsupported adapter" on an unknown dialect' do
+ @adapter.should_receive(:dialect).any_number_of_times.and_return("Invalid")
+ @adapter.should_receive(:class).and_return("InvalidAdapter")
+ lambda {
+ DataMapper::Migration.new(1, :do_nothing, {}) {}
+ }.should raise_error
+ end
+
it 'should raise "Unsupported adapter" on an unknown adapter' do
- @adapter.should_receive(:class).any_number_of_times.and_return("InvalidAdapter")
+ @adapter.should_receive(:respond_to?).and_return(false)
+ @adapter.should_receive(:class).and_return("InvalidAdapter")
lambda {
DataMapper::Migration.new(1, :do_nothing, {}) {}
}.should raise_error

0 comments on commit fd551c0

Please sign in to comment.