diff --git a/Rakefile b/Rakefile index 4b0e189..bfa55ec 100644 --- a/Rakefile +++ b/Rakefile @@ -1,8 +1,16 @@ require 'rake/testtask' -task :default => [:test] +task :default => ["display:notice"] -ADAPTERS = %w(mysql postgresql sqlite sqlite3 oracle) +ADAPTERS = %w(mysql postgresql sqlite3 oracle) + +namespace :display do + task :notice do + puts + puts "To run tests you must supply the adapter, see rake -T for more information." + puts + end +end desc "Runs generic database tests." Rake::TestTask.new("test") { |t| diff --git a/lib/ar-extensions.rb b/lib/ar-extensions.rb deleted file mode 100644 index 8d00dd1..0000000 --- a/lib/ar-extensions.rb +++ /dev/null @@ -1,8 +0,0 @@ -require "active_record" -require "active_record/version" - -module ActiveRecord::Extensions -end - -require "ar-extensions/import" -require "ar-extensions/active_record/adapters/abstract_adapter" diff --git a/lib/ar-extensions/active_record/adapters/postgresql_adapter.rb b/lib/ar-extensions/active_record/adapters/postgresql_adapter.rb new file mode 100644 index 0000000..bd983eb --- /dev/null +++ b/lib/ar-extensions/active_record/adapters/postgresql_adapter.rb @@ -0,0 +1,9 @@ +module ActiveRecord # :nodoc: + module ConnectionAdapters # :nodoc: + class PostgreSQLAdapter # :nodoc: + def next_value_for_sequence(sequence_name) + %{nextval('#{sequence_name}')} + end + end + end +end diff --git a/lib/ar-extensions/import/base.rb b/lib/ar-extensions/import/base.rb new file mode 100644 index 0000000..0e6d0eb --- /dev/null +++ b/lib/ar-extensions/import/base.rb @@ -0,0 +1,13 @@ +require "active_record" +require "active_record/version" + +module ActiveRecord::Extensions + AdapterPath = File.join File.expand_path(File.dirname(__FILE__)), "/../active_record/adapters" + + def self.require_adapter(adapter) + require File.join(AdapterPath,"/#{adapter}_adapter") + end +end + +require "ar-extensions/import" +require "ar-extensions/active_record/adapters/abstract_adapter" diff --git a/lib/ar-extensions/import/mysql.rb b/lib/ar-extensions/import/mysql.rb index e78c489..0053069 100644 --- a/lib/ar-extensions/import/mysql.rb +++ b/lib/ar-extensions/import/mysql.rb @@ -1,3 +1,6 @@ +require "active_record/connection_adapters/mysql_adapter" +require File.join File.dirname(__FILE__), "base" + module ActiveRecord::Extensions::ConnectionAdapters::MysqlAdapter # :nodoc: include ActiveRecord::Extensions::Import::ImportSupport diff --git a/lib/ar-extensions/import/postgresql.rb b/lib/ar-extensions/import/postgresql.rb new file mode 100644 index 0000000..10fac1b --- /dev/null +++ b/lib/ar-extensions/import/postgresql.rb @@ -0,0 +1,4 @@ +require "active_record/connection_adapters/postgresql_adapter" + +require File.join File.dirname(__FILE__), "base" +ActiveRecord::Extensions.require_adapter "postgresql" diff --git a/lib/ar-extensions/import/sqlite3.rb b/lib/ar-extensions/import/sqlite3.rb new file mode 100644 index 0000000..9c4fc82 --- /dev/null +++ b/lib/ar-extensions/import/sqlite3.rb @@ -0,0 +1,2 @@ +require "active_record/connection_adapters/sqlite3_adapter" +require File.join File.dirname(__FILE__), "base" \ No newline at end of file diff --git a/test/database.yml b/test/database.yml index 00b4f32..b89291d 100644 --- a/test/database.yml +++ b/test/database.yml @@ -9,9 +9,10 @@ mysql: <<: *common adapter: mysql -postgres: +postgresql: <<: *common - adapter: postgres + username: zdennis + adapter: postgresql min_messages: warning oracle: diff --git a/test/import_test.rb b/test/import_test.rb index 80610aa..ce422f8 100644 --- a/test/import_test.rb +++ b/test/import_test.rb @@ -77,10 +77,13 @@ describe "#import" do topic = Generate(:topic, :title => "foobar") assert_no_difference "Topic.count" do begin - topic.title = "baz" - Topic.import [topic] + Topic.transaction do + topic.title = "baz" + Topic.import [topic] + end rescue Exception - # no-op + # PostgreSQL raises PgError due to key constraints + # I don't know why ActiveRecord doesn't catch these. *sigh* end end assert_equal "foobar", topic.reload.title diff --git a/test/test_helper.rb b/test/test_helper.rb index f377bfd..e53b05c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,7 +12,7 @@ require 'active_support/core_ext/object/returning' require "delorean" -require "ar-extensions" +require "active_record" require "logger" require "ruby-debug" @@ -89,6 +89,9 @@ require "rails" class MyApplication < Rails::Application ; end adapter = ENV["ARE_DB"] || "sqlite3" +# load the library +require "ar-extensions/import/#{adapter}" + ActiveRecord::Base.logger = Logger.new("log/test.log") ActiveRecord::Base.logger.level = Logger::DEBUG ActiveRecord::Base.configurations["test"] = YAML.load(this_dir.join("database.yml").open)[adapter]