From 3523138a7a98dfd3c31916711f87d19199b4a7e2 Mon Sep 17 00:00:00 2001 From: Zach Dennis Date: Thu, 8 Apr 2010 20:57:10 -0400 Subject: [PATCH] * Added basic import support for PostgreSQL. * Requiring database adapter is now "require 'ar-extensions/import/" so it won't conflict with previous versions of ar-extensions or other libraries extracted out of older ar-extensions, which "require 'ar-extensions'" --- Rakefile | 12 ++++++++++-- lib/ar-extensions.rb | 8 -------- .../active_record/adapters/postgresql_adapter.rb | 9 +++++++++ lib/ar-extensions/import/base.rb | 13 +++++++++++++ lib/ar-extensions/import/mysql.rb | 3 +++ lib/ar-extensions/import/postgresql.rb | 4 ++++ lib/ar-extensions/import/sqlite3.rb | 2 ++ test/database.yml | 5 +++-- test/import_test.rb | 9 ++++++--- test/test_helper.rb | 5 ++++- 10 files changed, 54 insertions(+), 16 deletions(-) delete mode 100644 lib/ar-extensions.rb create mode 100644 lib/ar-extensions/active_record/adapters/postgresql_adapter.rb create mode 100644 lib/ar-extensions/import/base.rb create mode 100644 lib/ar-extensions/import/postgresql.rb create mode 100644 lib/ar-extensions/import/sqlite3.rb 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]