* Added basic import support for PostgreSQL.

* Requiring database adapter is now "require 'ar-extensions/import/<adapter_name>" so it won't conflict with previous versions of ar-extensions or other libraries extracted out of older ar-extensions, which "require 'ar-extensions'"
This commit is contained in:
Zach Dennis 2010-04-08 20:57:10 -04:00
parent 075104a944
commit 3523138a7a
10 changed files with 54 additions and 16 deletions

View file

@ -1,8 +1,16 @@
require 'rake/testtask' 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." desc "Runs generic database tests."
Rake::TestTask.new("test") { |t| Rake::TestTask.new("test") { |t|

View file

@ -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"

View file

@ -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

View file

@ -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"

View file

@ -1,3 +1,6 @@
require "active_record/connection_adapters/mysql_adapter"
require File.join File.dirname(__FILE__), "base"
module ActiveRecord::Extensions::ConnectionAdapters::MysqlAdapter # :nodoc: module ActiveRecord::Extensions::ConnectionAdapters::MysqlAdapter # :nodoc:
include ActiveRecord::Extensions::Import::ImportSupport include ActiveRecord::Extensions::Import::ImportSupport

View file

@ -0,0 +1,4 @@
require "active_record/connection_adapters/postgresql_adapter"
require File.join File.dirname(__FILE__), "base"
ActiveRecord::Extensions.require_adapter "postgresql"

View file

@ -0,0 +1,2 @@
require "active_record/connection_adapters/sqlite3_adapter"
require File.join File.dirname(__FILE__), "base"

View file

@ -9,9 +9,10 @@ mysql:
<<: *common <<: *common
adapter: mysql adapter: mysql
postgres: postgresql:
<<: *common <<: *common
adapter: postgres username: zdennis
adapter: postgresql
min_messages: warning min_messages: warning
oracle: oracle:

View file

@ -77,10 +77,13 @@ describe "#import" do
topic = Generate(:topic, :title => "foobar") topic = Generate(:topic, :title => "foobar")
assert_no_difference "Topic.count" do assert_no_difference "Topic.count" do
begin begin
topic.title = "baz" Topic.transaction do
Topic.import [topic] topic.title = "baz"
Topic.import [topic]
end
rescue Exception rescue Exception
# no-op # PostgreSQL raises PgError due to key constraints
# I don't know why ActiveRecord doesn't catch these. *sigh*
end end
end end
assert_equal "foobar", topic.reload.title assert_equal "foobar", topic.reload.title

View file

@ -12,7 +12,7 @@ require 'active_support/core_ext/object/returning'
require "delorean" require "delorean"
require "ar-extensions" require "active_record"
require "logger" require "logger"
require "ruby-debug" require "ruby-debug"
@ -89,6 +89,9 @@ require "rails"
class MyApplication < Rails::Application ; end class MyApplication < Rails::Application ; end
adapter = ENV["ARE_DB"] || "sqlite3" adapter = ENV["ARE_DB"] || "sqlite3"
# load the library
require "ar-extensions/import/#{adapter}"
ActiveRecord::Base.logger = Logger.new("log/test.log") ActiveRecord::Base.logger = Logger.new("log/test.log")
ActiveRecord::Base.logger.level = Logger::DEBUG ActiveRecord::Base.logger.level = Logger::DEBUG
ActiveRecord::Base.configurations["test"] = YAML.load(this_dir.join("database.yml").open)[adapter] ActiveRecord::Base.configurations["test"] = YAML.load(this_dir.join("database.yml").open)[adapter]