Compare commits

...

60 Commits

Author SHA1 Message Date
Denis Knauf 038a29682e import_many, import_*: returns last_inserted_id 2013-01-03 17:07:10 +01:00
Zach Dennis 1c675d7c31 Merge branch 'michael-groble_master' 2012-12-14 10:29:46 -05:00
Zach Dennis 6697e2b4db Ensuring that the geo spatial adapters are successfully wired up and working with base import functionality.
[#47]
2012-12-14 10:29:04 -05:00
michael groble eec20355e5 support rgeo spatial database adapter names 2012-12-14 10:08:46 -05:00
Zach Dennis 5f0e220038 Merge branch 'dougo_clear-new-record-on-synchronize' 2012-12-14 10:03:51 -05:00
Doug Orleans d0c1055b65 Fix example in doc comment. 2012-12-14 09:59:59 -05:00
Doug Orleans 9fd19a36bd Also clear the destroyed flag after synchronizing. 2012-12-14 09:59:58 -05:00
Doug Orleans 413e35fedd Add failing test for destroyed records.
Also, generalize the previous test to use persisted? rather than !new_record?.
2012-12-14 09:59:58 -05:00
Doug Orleans 31ff20a332 Fix failing test - clear @new_record on synchronize. 2012-12-14 09:59:58 -05:00
Doug Orleans 58cac8bfb0 Test that no records are new after synchronize, not all.
Also fix the spelling of :synchronize_keys.
2012-12-14 09:59:58 -05:00
Zach Dennis 082a95df0e Merge branch 'sishen_master' 2012-12-14 09:51:29 -05:00
Dingding Ye 1ae5122642 Performance improvements.
* Serialize the column using specified coder before inserting
* Cache the ActiveRecord connection once per call to generate values SQL
* Call sequence_name last since it's more expensive
2012-12-14 09:50:36 -05:00
Zach Dennis ec2059e802 assert_include => assert_includes 2012-12-14 09:49:54 -05:00
Zach Dennis a4f17e6d45 Merge remote-tracking branch 'chewi/scope-awareness' 2012-12-14 09:40:03 -05:00
Zach Dennis 394e0b51c3 Version bump to 0.2.11 2012-10-05 13:10:38 -04:00
Zach Dennis ee5604d4d5 Merge pull request #67 from mildmojo/fix_sqlite3_adapter_spelling
Fix case-sensitive SQLite3 adapter name typos.
2012-10-01 06:44:46 -07:00
mildmojo 43bd628e05 Fixes case-sensitive SQLite3 adapter name typos.
The SQLite3 adapter name was referenced as
`ActiveRecord::ConnectionAdapters::Sqlite3Adapter`, but the
ActiveRecord class is spelled `SQLite3Adapter` (upper-case "SQL"). As
a result, `active_record/adapters/sqlite3_adapter.rb` wasn't actually
including the methods from `adapters/sqlite3_adapter.rb` into the AR
adapter class.

This adds a test for proper inheritance and fixes the spelling of
`SQLite3Adapter`, unifying all references to the ActiveRecord version.
2012-10-01 03:08:07 -04:00
James Le Cuirot 22473145d1 Add scope-awareness feature 2012-09-20 15:15:21 +01:00
Zach Dennis c08d31b528 Merge pull request #65 from jabley/missing-column
Provide more detailed error message
2012-09-14 08:00:18 -07:00
James Abley b03af0cd3e Provide more detailed error message
If we don't have a mapping for a column name, provide the column name
as well as the index in the array.
2012-09-14 14:01:00 +01:00
Zach Dennis 6182b944ef Merge pull request #56 from grzuy/support_all_or_none_records
Adds support for :all_or_none option on #import
2012-08-29 20:48:18 -07:00
Zach Dennis 0b03543612 Merge pull request #58 from alexagranov/e6cafdb23a64e68a246a838565afaecf6faf952a
The jdbcmysql_adapter was left out of the InstanceMethods refactoring.
2012-08-29 20:47:07 -07:00
Zach Dennis b4bc1466f1 Adding gemspec. To re-generate use: rake gemspec:generate 2012-08-29 23:45:16 -04:00
Zach Dennis 75a63d77d1 Merge pull request #62 from spectator/seamless_database_pool
Seamless database pool adapter
2012-08-29 20:41:52 -07:00
Zach Dennis 0e14c5f643 Version bump to 0.2.10 2012-08-29 23:38:02 -04:00
Yury Velikanau 190fcd32ff Remove remain of ruby-debug-19 from Gemfile.lock 2012-08-22 19:51:47 -07:00
Yury Velikanau 1d9b30cf83 Put together tests for seamless_database_pool adapter 2012-08-22 19:50:37 -07:00
Yury Velikanau 23a07371dd Add seamless_database_pool gem 2012-08-22 19:49:48 -07:00
Yury Velikanau 151c2f271f Gemfile.lock changes for debugger gem 2012-08-22 19:49:27 -07:00
Yury Velikanau 7628e0a9d8 debugger gem supports ruby 1.9.3 just fine 2012-08-22 18:57:41 -07:00
Yury Velikanau de048bb61e Remove whitespace. 2012-08-22 18:57:15 -07:00
Evan Petrie 9ee02d87ff fix file name 2012-08-22 18:44:48 -07:00
Evan Petrie cc0185abb3 make work with seamless database pool 2012-08-22 18:44:37 -07:00
Zach Dennis 2bb1150e00 Merge pull request #61 from benpickles/missing-column-error
Raise a useful message when passing validate: false and a value doesn't have a corresponding column
2012-08-20 05:53:45 -07:00
Ben Pickles f12d72b0b1 Raise a useful message when a value doesn't have a corresponding column. 2012-08-19 19:41:02 +01:00
Zach Dennis a1a183e43d Updating Gemfile to latest versions of libraries:
* updating deprecated require 'rake/rdoctask' with 'rdoc/task'
* preparing to remove deprecated ActiveRecord set_table_name with self.table_name=
2012-08-06 09:24:34 -04:00
Zach Dennis b500ebe5a1 Updating tests around working with non-standard ActiveRecord models with non-sequence based primary keys.
* move into test/import_test so it runs across all supported adapters

[#60]
2012-08-06 09:23:01 -04:00
Zach Dennis 71a393d000 Merge pull request #60 from jluxenberg/master
Model#import fails to find model's primary key sequence
2012-08-06 06:10:43 -07:00
Jared Luxenberg e12f9c4951 Model#import fails to find model's primary key sequence
Fixes a bug where Model#import does not work when a model's primary key
has no associated 'sequence' in the database. If a value for the primary
key is specified, then the insert should not require a database
'sequence' to generate a value for that primary key field.
2012-08-04 17:35:50 -07:00
Gonzalo Rodriguez eac7d5f949 Adds support for :all_or_none option on #import 2012-05-19 17:40:04 -03:00
Zach Dennis f74bd2fbbd Merge pull request #51 from seamusabshere/patch-1
memoize MySQL's max_allowed_packet
2012-05-03 17:09:20 -07:00
Seamus Abshere b36259af29 Solving #50 - memoize MySQL's max_allowed_packet instead of querying it every time you run .import 2012-03-20 16:40:10 -05:00
Alex Agranov e6cafdb23a somehow the jdbcmysql_adapter was left out in the refactoring that removed usage of the InstanceMethods module 2012-02-28 19:31:55 -05:00
Zach Dennis 3dc99c357d Merge pull request #46 from ebarendt/patch-1
Update README.markdown with correct link
2012-02-17 10:54:22 -08:00
Eric Barendt 8544641260 Update README.markdown 2012-02-15 15:38:37 -08:00
Zach Dennis 41952485a4 Version bump to 0.2.9 2012-01-20 17:50:58 -05:00
Zach Dennis b5c550860c Bumping sqlite3 dependency for development 2012-01-20 17:50:27 -05:00
Zach Dennis a5a9928b26 Making activerecord dependency be require a version of activerecord 3.x. https://github.com/zdennis/activerecord-import/pull/43 2012-01-20 17:48:04 -05:00
Zach Dennis 7fa043cf31 Merge pull request #43 from duckpond/c9a4313f8ba5805bfcca314a5b93d4720130de10
Active record dependency does not work with rails 3.2
2012-01-20 14:38:59 -08:00
Samantha John c9a4313f8b remove dependency on activerecord 3.0 2012-01-20 16:07:53 -05:00
Zach Dennis b2c71336ca Merge pull request #20 from Empact/drop_sum_sizes
Rely on ActiveSupport #sum rather than #sum_sizes
2012-01-06 15:47:53 -08:00
Zach Dennis 2539a71574 Merge pull request #38 from wycats/module_cleanup
Clean up module usage.
2012-01-03 06:20:48 -08:00
Yehuda Katz b5261d0225 Clean up module usage. There's no need to use InstanceMethods -- normal Ruby modules and includes will work fine here. 2012-01-03 00:41:04 -08:00
Zach Dennis a41050c2b5 Merge pull request #33 from nbudin/master
Use connection pool instead of actual connection on load (helps with Rails 3.1 on Heroku)
2011-12-14 15:23:58 -08:00
Nat Budin 08c36d33e4 Use connection pool instead of actual connection to avoid having to connect to the database until necessary 2011-10-10 13:07:10 -04:00
Ben Woosley 8d20594f12 Replace #sum_sizes with calls to ActiveSupport #sum, as splatting large numbers of values is resource intensive andcan cause stack overflows of the sort 'SystemStackError: stack level too deep' 2011-08-16 17:01:11 -07:00
Zach Dennis 3c08fad1a2 Bumping to 0.2.8 2011-08-14 01:39:44 -04:00
Zach Dennis 65dbc9e051 Bumping VERSION to 0.2.8.rc3 2011-07-12 23:43:18 -04:00
Zach Dennis 7cc7c33186 Merge pull request #27 from msgehard/fix_gemfile
Relax pg version dependency a bit.
2011-07-12 20:39:00 -07:00
Mike Gehard 4653feab96 Relax pg version dependency a bit. The Gemfile specifies pg ~>0.9.0 and Rails 3.1 specifies pg 0.11 so there is no way for bundler to install pg for a new download. 2011-07-12 19:04:53 -06:00
41 changed files with 459 additions and 197 deletions

12
Gemfile
View File

@ -1,6 +1,6 @@
source :gemcutter
gem "activerecord", "~> 3.0pre"
gem "activerecord", "~> 3.0"
group :development do
gem "rake"
@ -11,9 +11,10 @@ group :test do
# Database Adapters
platforms :ruby do
gem "mysql", "~> 2.8.1"
gem "mysql2", "~> 0.2.4"
gem "pg", "~> 0.9.0"
gem "mysql2", "~> 0.3.0"
gem "pg", "~> 0.9"
gem "sqlite3-ruby", "~> 1.3.1"
gem "seamless_database_pool", "~> 1.0.11"
end
platforms :jruby do
@ -24,7 +25,7 @@ group :test do
# Support libs
gem "factory_girl", "~> 1.3.3"
gem "delorean", "~> 0.2.0"
# Debugging
platforms :mri_18 do
gem "ruby-debug", "= 0.10.4"
@ -36,7 +37,6 @@ group :test do
end
platforms :mri_19 do
# TODO: Remove the conditional when ruby-debug19 supports Ruby >= 1.9.3
gem "ruby-debug19" if RUBY_VERSION < "1.9.3"
gem "debugger"
end
end

View File

@ -1,84 +1,85 @@
GEM
remote: http://rubygems.org/
specs:
activemodel (3.1.0.rc4)
activesupport (= 3.1.0.rc4)
bcrypt-ruby (~> 2.1.4)
activemodel (3.2.7)
activesupport (= 3.2.7)
builder (~> 3.0.0)
i18n (~> 0.6)
activerecord (3.1.0.rc4)
activemodel (= 3.1.0.rc4)
activesupport (= 3.1.0.rc4)
arel (~> 2.1.1)
tzinfo (~> 0.3.27)
activerecord-jdbc-adapter (1.1.1)
activerecord-jdbcmysql-adapter (1.1.1)
activerecord-jdbc-adapter (= 1.1.1)
activerecord (3.2.7)
activemodel (= 3.2.7)
activesupport (= 3.2.7)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activerecord-jdbc-adapter (1.2.2)
activerecord-jdbcmysql-adapter (1.2.2)
activerecord-jdbc-adapter (~> 1.2.2)
jdbc-mysql (~> 5.1.0)
activesupport (3.1.0.rc4)
activesupport (3.2.7)
i18n (~> 0.6)
multi_json (~> 1.0)
archive-tar-minitar (0.5.2)
arel (2.1.1)
bcrypt-ruby (2.1.4)
bcrypt-ruby (2.1.4-java)
arel (3.0.2)
builder (3.0.0)
chronic (0.3.0)
columnize (0.3.2)
chronic (0.7.0)
columnize (0.3.6)
debugger (1.2.0)
columnize (>= 0.3.1)
debugger-linecache (~> 1.1.1)
debugger-ruby_core_source (~> 1.1.3)
debugger-linecache (1.1.2)
debugger-ruby_core_source (>= 1.1.1)
debugger-ruby_core_source (1.1.3)
delorean (0.2.1)
chronic
factory_girl (1.3.3)
git (1.2.5)
i18n (0.6.0)
jdbc-mysql (5.1.13)
jeweler (1.5.2)
bundler (~> 1.0.0)
jeweler (1.8.4)
bundler (~> 1.0)
git (>= 1.2.5)
rake
linecache (0.43)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
multi_json (1.0.3)
rdoc
json (1.7.4)
json (1.7.4-java)
linecache (0.46)
rbx-require-relative (> 0.0.4)
multi_json (1.3.6)
mysql (2.8.1)
mysql2 (0.2.7)
pg (0.9.0)
rake (0.8.7)
mysql2 (0.3.11)
pg (0.14.0)
rake (0.9.2.2)
rbx-require-relative (0.0.9)
rdoc (3.12)
json (~> 1.4)
ruby-debug (0.10.4)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.4.0)
ruby-debug-base (0.10.4)
linecache (>= 0.3)
ruby-debug-base (0.10.4-java)
ruby-debug-base19 (0.11.25)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby_core_source (>= 0.1.4)
ruby-debug19 (0.11.6)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
sqlite3 (1.3.3)
seamless_database_pool (1.0.11)
activerecord (>= 2.2.2)
sqlite3 (1.3.6)
sqlite3-ruby (1.3.3)
sqlite3 (>= 1.3.3)
tzinfo (0.3.28)
tzinfo (0.3.33)
PLATFORMS
java
ruby
DEPENDENCIES
activerecord (~> 3.0pre)
activerecord (~> 3.0)
activerecord-jdbcmysql-adapter
debugger
delorean (~> 0.2.0)
factory_girl (~> 1.3.3)
jdbc-mysql
jeweler (>= 1.4.0)
mysql (~> 2.8.1)
mysql2 (~> 0.2.4)
pg (~> 0.9.0)
mysql2 (~> 0.3.0)
pg (~> 0.9)
rake
ruby-debug (= 0.10.4)
ruby-debug-base (= 0.10.4)
ruby-debug19
seamless_database_pool (~> 1.0.11)
sqlite3-ruby (~> 1.3.1)

View File

@ -2,7 +2,7 @@
activerecord-import is a library for bulk inserting data using ActiveRecord.
For more information on activerecord-import please see its wiki: http://wiki.github.com/zdennis/activerecord-import/
For more information on activerecord-import please see its wiki: https://github.com/zdennis/activerecord-import/wiki
# License

View File

@ -36,7 +36,7 @@ namespace :display do
end
task :default => ["display:notice"]
ADAPTERS = %w(mysql mysql2 jdbcmysql postgresql sqlite3)
ADAPTERS = %w(mysql mysql2 jdbcmysql postgresql sqlite3 seamless_database_pool mysqlspatial mysql2spatial spatialite postgis)
ADAPTERS.each do |adapter|
namespace :test do
desc "Runs #{adapter} database tests."
@ -63,7 +63,7 @@ rescue LoadError
end
end
require 'rake/rdoctask'
require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""

View File

@ -1 +1 @@
0.2.8.rc2
0.2.11

View File

@ -0,0 +1,68 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = "activerecord-import"
s.version = "0.2.10"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Zach Dennis"]
s.date = "2012-08-30"
s.description = "Extraction of the ActiveRecord::Base#import functionality from ar-extensions for Rails 3 and beyond"
s.email = "zach.dennis@gmail.com"
s.extra_rdoc_files = [
"README.markdown"
]
s.files = [
"README.markdown",
"Rakefile",
"VERSION",
"lib/activerecord-import.rb",
"lib/activerecord-import/active_record/adapters/abstract_adapter.rb",
"lib/activerecord-import/active_record/adapters/jdbcmysql_adapter.rb",
"lib/activerecord-import/active_record/adapters/mysql2_adapter.rb",
"lib/activerecord-import/active_record/adapters/mysql_adapter.rb",
"lib/activerecord-import/active_record/adapters/postgresql_adapter.rb",
"lib/activerecord-import/active_record/adapters/seamless_database_pool_adapter.rb",
"lib/activerecord-import/active_record/adapters/sqlite3_adapter.rb",
"lib/activerecord-import/adapters/abstract_adapter.rb",
"lib/activerecord-import/adapters/mysql_adapter.rb",
"lib/activerecord-import/adapters/postgresql_adapter.rb",
"lib/activerecord-import/adapters/sqlite3_adapter.rb",
"lib/activerecord-import/base.rb",
"lib/activerecord-import/import.rb",
"lib/activerecord-import/mysql.rb",
"lib/activerecord-import/mysql2.rb",
"lib/activerecord-import/postgresql.rb",
"lib/activerecord-import/sqlite3.rb",
"lib/activerecord-import/synchronize.rb"
]
s.homepage = "http://github.com/zdennis/activerecord-import"
s.require_paths = ["lib"]
s.rubygems_version = "1.8.24"
s.summary = "Bulk-loading extension for ActiveRecord"
if s.respond_to? :specification_version then
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<activerecord>, ["~> 3.0"])
s.add_development_dependency(%q<rake>, [">= 0"])
s.add_development_dependency(%q<jeweler>, [">= 1.4.0"])
s.add_runtime_dependency(%q<activerecord>, ["~> 3.0"])
else
s.add_dependency(%q<activerecord>, ["~> 3.0"])
s.add_dependency(%q<rake>, [">= 0"])
s.add_dependency(%q<jeweler>, [">= 1.4.0"])
s.add_dependency(%q<activerecord>, ["~> 3.0"])
end
else
s.add_dependency(%q<activerecord>, ["~> 3.0"])
s.add_dependency(%q<rake>, [">= 0"])
s.add_dependency(%q<jeweler>, [">= 1.4.0"])
s.add_dependency(%q<activerecord>, ["~> 3.0"])
end
end

View File

@ -2,15 +2,15 @@ class ActiveRecord::Base
class << self
def establish_connection_with_activerecord_import(*args)
establish_connection_without_activerecord_import(*args)
ActiveSupport.run_load_hooks(:active_record_connection_established, connection)
ActiveSupport.run_load_hooks(:active_record_connection_established, connection_pool)
end
alias_method_chain :establish_connection, :activerecord_import
end
end
ActiveSupport.on_load(:active_record_connection_established) do |connection|
if !ActiveRecord.const_defined?(:Import) || !ActiveRecord::Import.respond_to?(:load_from_connection)
ActiveSupport.on_load(:active_record_connection_established) do |connection_pool|
if !ActiveRecord.const_defined?(:Import) || !ActiveRecord::Import.respond_to?(:load_from_connection_pool)
require File.join File.dirname(__FILE__), "activerecord-import/base"
end
ActiveRecord::Import.load_from_connection connection
ActiveRecord::Import.load_from_connection_pool connection_pool
end

View File

@ -2,5 +2,5 @@ require "active_record/connection_adapters/mysql_adapter"
require "activerecord-import/adapters/mysql_adapter"
class ActiveRecord::ConnectionAdapters::MysqlAdapter
include ActiveRecord::Import::MysqlAdapter::InstanceMethods
include ActiveRecord::Import::MysqlAdapter
end

View File

@ -1,6 +1,6 @@
require "active_record/connection_adapters/mysql2_adapter"
require "activerecord-import/adapters/mysql_adapter"
require "activerecord-import/adapters/mysql2_adapter"
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
include ActiveRecord::Import::MysqlAdapter::InstanceMethods
end
include ActiveRecord::Import::Mysql2Adapter
end

View File

@ -2,5 +2,5 @@ require "active_record/connection_adapters/mysql_adapter"
require "activerecord-import/adapters/mysql_adapter"
class ActiveRecord::ConnectionAdapters::MysqlAdapter
include ActiveRecord::Import::MysqlAdapter::InstanceMethods
end
include ActiveRecord::Import::MysqlAdapter
end

View File

@ -2,6 +2,6 @@ require "active_record/connection_adapters/postgresql_adapter"
require "activerecord-import/adapters/postgresql_adapter"
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
include ActiveRecord::Import::PostgreSQLAdapter::InstanceMethods
include ActiveRecord::Import::PostgreSQLAdapter
end

View File

@ -0,0 +1,7 @@
require "seamless_database_pool"
require "active_record/connection_adapters/seamless_database_pool_adapter"
require "activerecord-import/adapters/mysql_adapter"
class ActiveRecord::ConnectionAdapters::SeamlessDatabasePoolAdapter
include ActiveRecord::Import::MysqlAdapter
end

View File

@ -1,7 +1,7 @@
require "active_record/connection_adapters/sqlite3_adapter"
require "activerecord-import/adapters/sqlite3_adapter"
class ActiveRecord::ConnectionAdapters::Sqlite3Adapter
include ActiveRecord::Import::Sqlite3Adapter::InstanceMethods
class ActiveRecord::ConnectionAdapters::SQLite3Adapter
include ActiveRecord::Import::SQLite3Adapter
end

View File

@ -3,12 +3,6 @@ module ActiveRecord::Import::AbstractAdapter
QUERY_OVERHEAD = 8 #This was shown to be true for MySQL, but it's not clear where the overhead is from.
module ClassMethods
# Returns the sum of the sizes of the passed in objects. This should
# probably be moved outside this class, but to where?
def sum_sizes( *objects ) # :nodoc:
objects.inject( 0 ){ |sum,o| sum += o.bytesize }
end
def get_insert_value_sets( values, sql_size, max_bytes ) # :nodoc:
value_sets = []
arr, current_arr_values_size, current_size = [], 0, 0
@ -40,7 +34,7 @@ module ActiveRecord::Import::AbstractAdapter
# elements that are in position >= 1 will be appended to the final SQL.
def insert_many( sql, values, *args ) # :nodoc:
# the number of inserts default
number_of_inserts = 0
number_of_inserts, last_inserted_id = 0, nil
base_sql,post_sql = if sql.is_a?( String )
[ sql, '' ]
@ -51,7 +45,7 @@ module ActiveRecord::Import::AbstractAdapter
sql_size = QUERY_OVERHEAD + base_sql.size + post_sql.size
# the number of bytes the requested insert statement values will take up
values_in_bytes = self.class.sum_sizes( *values )
values_in_bytes = values.sum {|value| value.bytesize }
# the number of bytes (commas) it will take to comma separate our values
comma_separated_bytes = values.size-1
@ -65,17 +59,17 @@ module ActiveRecord::Import::AbstractAdapter
if NO_MAX_PACKET == max or total_bytes < max
number_of_inserts += 1
sql2insert = base_sql + values.join( ',' ) + post_sql
insert( sql2insert, *args )
last_inserted_id = insert( sql2insert, *args )
else
value_sets = self.class.get_insert_value_sets( values, sql_size, max )
value_sets.each do |values|
number_of_inserts += 1
sql2insert = base_sql + values.join( ',' ) + post_sql
insert( sql2insert, *args )
last_inserted_id = insert( sql2insert, *args )
end
end
number_of_inserts
[number_of_inserts, last_inserted_id]
end
def pre_sql_statements(options)

View File

@ -0,0 +1,5 @@
require File.dirname(__FILE__) + "/mysql_adapter"
module ActiveRecord::Import::Mysql2Adapter
include ActiveRecord::Import::MysqlAdapter
end

View File

@ -1,59 +1,55 @@
module ActiveRecord::Import::MysqlAdapter
module InstanceMethods
def self.included(klass)
klass.instance_eval do
include ActiveRecord::Import::ImportSupport
include ActiveRecord::Import::OnDuplicateKeyUpdateSupport
end
end
# Returns the maximum number of bytes that the server will allow
# in a single packet
def max_allowed_packet # :nodoc:
include ActiveRecord::Import::ImportSupport
include ActiveRecord::Import::OnDuplicateKeyUpdateSupport
# Returns the maximum number of bytes that the server will allow
# in a single packet
def max_allowed_packet # :nodoc:
@max_allowed_packet ||= begin
result = execute( "SHOW VARIABLES like 'max_allowed_packet';" )
# original Mysql gem responds to #fetch_row while Mysql2 responds to #first
val = result.respond_to?(:fetch_row) ? result.fetch_row[1] : result.first[1]
val.to_i
end
# Returns a generated ON DUPLICATE KEY UPDATE statement given the passed
# in +args+.
def sql_for_on_duplicate_key_update( table_name, *args ) # :nodoc:
sql = ' ON DUPLICATE KEY UPDATE '
arg = args.first
if arg.is_a?( Array )
sql << sql_for_on_duplicate_key_update_as_array( table_name, arg )
elsif arg.is_a?( Hash )
sql << sql_for_on_duplicate_key_update_as_hash( table_name, arg )
elsif arg.is_a?( String )
sql << arg
else
raise ArgumentError.new( "Expected Array or Hash" )
end
sql
end
def sql_for_on_duplicate_key_update_as_array( table_name, arr ) # :nodoc:
results = arr.map do |column|
qc = quote_column_name( column )
"#{table_name}.#{qc}=VALUES(#{qc})"
end
results.join( ',' )
end
def sql_for_on_duplicate_key_update_as_hash( table_name, hsh ) # :nodoc:
sql = ' ON DUPLICATE KEY UPDATE '
results = hsh.map do |column1, column2|
qc1 = quote_column_name( column1 )
qc2 = quote_column_name( column2 )
"#{table_name}.#{qc1}=VALUES( #{qc2} )"
end
results.join( ',')
end
#return true if the statement is a duplicate key record error
def duplicate_key_update_error?(exception)# :nodoc:
exception.is_a?(ActiveRecord::StatementInvalid) && exception.to_s.include?('Duplicate entry')
end
end
end
# Returns a generated ON DUPLICATE KEY UPDATE statement given the passed
# in +args+.
def sql_for_on_duplicate_key_update( table_name, *args ) # :nodoc:
sql = ' ON DUPLICATE KEY UPDATE '
arg = args.first
if arg.is_a?( Array )
sql << sql_for_on_duplicate_key_update_as_array( table_name, arg )
elsif arg.is_a?( Hash )
sql << sql_for_on_duplicate_key_update_as_hash( table_name, arg )
elsif arg.is_a?( String )
sql << arg
else
raise ArgumentError.new( "Expected Array or Hash" )
end
sql
end
def sql_for_on_duplicate_key_update_as_array( table_name, arr ) # :nodoc:
results = arr.map do |column|
qc = quote_column_name( column )
"#{table_name}.#{qc}=VALUES(#{qc})"
end
results.join( ',' )
end
def sql_for_on_duplicate_key_update_as_hash( table_name, hsh ) # :nodoc:
sql = ' ON DUPLICATE KEY UPDATE '
results = hsh.map do |column1, column2|
qc1 = quote_column_name( column1 )
qc2 = quote_column_name( column2 )
"#{table_name}.#{qc1}=VALUES( #{qc2} )"
end
results.join( ',')
end
#return true if the statement is a duplicate key record error
def duplicate_key_update_error?(exception)# :nodoc:
exception.is_a?(ActiveRecord::StatementInvalid) && exception.to_s.include?('Duplicate entry')
end
end

View File

@ -1,13 +1,7 @@
module ActiveRecord::Import::PostgreSQLAdapter
module InstanceMethods
def self.included(klass)
klass.instance_eval do
include ActiveRecord::Import::ImportSupport
end
end
include ActiveRecord::Import::ImportSupport
def next_value_for_sequence(sequence_name)
%{nextval('#{sequence_name}')}
end
def next_value_for_sequence(sequence_name)
%{nextval('#{sequence_name}')}
end
end

View File

@ -1,7 +1,5 @@
module ActiveRecord::Import::Sqlite3Adapter
module InstanceMethods
def next_value_for_sequence(sequence_name)
%{nextval('#{sequence_name}')}
end
module ActiveRecord::Import::SQLite3Adapter
def next_value_for_sequence(sequence_name)
%{nextval('#{sequence_name}')}
end
end

View File

@ -5,19 +5,25 @@ require "active_record/version"
module ActiveRecord::Import
AdapterPath = File.join File.expand_path(File.dirname(__FILE__)), "/active_record/adapters"
def self.base_adapter(adapter)
case adapter
when 'mysqlspatial' then 'mysql'
when 'mysql2spatial' then 'mysql2'
when 'spatialite' then 'sqlite3'
when 'postgis' then 'postgresql'
else adapter
end
end
# Loads the import functionality for a specific database adapter
def self.require_adapter(adapter)
require File.join(AdapterPath,"/abstract_adapter")
require File.join(AdapterPath,"/#{adapter}_adapter")
require File.join(AdapterPath,"/#{base_adapter(adapter)}_adapter")
end
# Loads the import functionality for the passed in ActiveRecord connection
def self.load_from_connection(connection)
import_adapter = "ActiveRecord::Import::#{connection.class.name.demodulize}::InstanceMethods"
unless connection.class.ancestors.map(&:name).include?(import_adapter)
config = connection.instance_variable_get :@config
require_adapter config[:adapter]
end
def self.load_from_connection_pool(connection_pool)
require_adapter connection_pool.spec.config[:adapter]
end
end

View File

@ -3,7 +3,7 @@ require "ostruct"
module ActiveRecord::Import::ConnectionAdapters ; end
module ActiveRecord::Import #:nodoc:
class Result < Struct.new(:failed_instances, :num_inserts)
class Result < Struct.new(:failed_instances, :num_inserts, :last_inserted_id)
end
module ImportSupport #:nodoc:
@ -17,6 +17,12 @@ module ActiveRecord::Import #:nodoc:
true
end
end
class MissingColumnError < StandardError
def initialize(name, index)
super "Missing column for value <#{name}> at index #{index}"
end
end
end
class ActiveRecord::Base
@ -131,8 +137,8 @@ class ActiveRecord::Base
#
# # Example synchronizing unsaved/new instances in memory by using a uniqued imported field
# posts = [BlogPost.new(:title => "Foo"), BlogPost.new(:title => "Bar")]
# BlogPost.import posts, :synchronize => posts
# puts posts.first.new_record? # => false
# BlogPost.import posts, :synchronize => posts, :synchronize_keys => [:title]
# puts posts.first.persisted? # => true
#
# == On Duplicate Key Update (MySQL only)
#
@ -156,9 +162,10 @@ class ActiveRecord::Base
# BlogPost.import columns, attributes, :on_duplicate_key_update=>{ :title => :title }
#
# = Returns
# This returns an object which responds to +failed_instances+ and +num_inserts+.
# This returns an object which responds to +failed_instances+, +num_inserts+, +last_inserted_id+.
# * failed_instances - an array of objects that fails validation and were not committed to the database. An empty array if no validation is performed.
# * num_inserts - the number of insert statements it took to import the data
# * num_inserts - the number of insert statements it took to import the data.
# * last_inserted_id - the last inserted id. Should be the id of the latest inserted row.
def import( *args )
options = { :validate=>true, :timestamps=>true }
options.merge!( args.pop ) if args.last.is_a? Hash
@ -185,7 +192,7 @@ class ActiveRecord::Base
end
# supports empty array
elsif args.last.is_a?( Array ) and args.last.empty?
return ActiveRecord::Import::Result.new([], 0) if args.last.empty?
return ActiveRecord::Import::Result.new([], 0, nil) if args.last.empty?
# supports 2-element array and array
elsif args.size == 2 and args.first.is_a?( Array ) and args.last.is_a?( Array )
column_names, array_of_attributes = args
@ -212,8 +219,8 @@ class ActiveRecord::Base
return_obj = if is_validating
import_with_validations( column_names, array_of_attributes, options )
else
num_inserts = import_without_validations_or_callbacks( column_names, array_of_attributes, options )
ActiveRecord::Import::Result.new([], num_inserts)
[num_inserts, last_inserted_id] = import_without_validations_or_callbacks( column_names, array_of_attributes, options )
ActiveRecord::Import::Result.new([], num_inserts, last_inserted_id)
end
if options[:synchronize]
@ -254,9 +261,13 @@ class ActiveRecord::Base
end
end
array_of_attributes.compact!
num_inserts = array_of_attributes.empty? ? 0 : import_without_validations_or_callbacks( column_names, array_of_attributes, options )
ActiveRecord::Import::Result.new(failed_instances, num_inserts)
num_inserts, last_inserted_id = if array_of_attributes.empty? || options[:all_or_none] && failed_instances.any?
[0, nil]
else
import_without_validations_or_callbacks( column_names, array_of_attributes, options )
end
ActiveRecord::Import::Result.new(failed_instances, num_inserts, last_inserted_id)
end
# Imports the passed in +column_names+ and +array_of_attributes+
@ -266,7 +277,21 @@ class ActiveRecord::Base
# information on +column_names+, +array_of_attributes_ and
# +options+.
def import_without_validations_or_callbacks( column_names, array_of_attributes, options={} )
columns = column_names.map { |name| columns_hash[name.to_s] }
number_inserted, last_inserted_id = 0, nil
scope_columns, scope_values = scope_attributes.to_a.transpose
unless scope_columns.blank?
column_names.concat scope_columns
array_of_attributes.each { |a| a.concat scope_values }
end
columns = column_names.each_with_index.map do |name, i|
column = columns_hash[name.to_s]
raise ActiveRecord::Import::MissingColumnError.new(name.to_s, i) if column.nil?
column
end
columns_sql = "(#{column_names.map{|name| connection.quote_column_name(name) }.join(',')})"
insert_sql = "INSERT #{options[:ignore] ? 'IGNORE ':''}INTO #{quoted_table_name} #{columns_sql} VALUES "
@ -282,11 +307,11 @@ class ActiveRecord::Base
post_sql_statements = connection.post_sql_statements( quoted_table_name, options )
# perform the inserts
number_inserted = connection.insert_many( [ insert_sql, post_sql_statements ].flatten,
number_inserted, last_inserted_id = connection.insert_many( [ insert_sql, post_sql_statements ].flatten,
values_sql,
"#{self.class.name} Create Many Without Validations Or Callbacks" )
end
number_inserted
[number_inserted, last_inserted_id]
end
private
@ -294,13 +319,22 @@ class ActiveRecord::Base
# Returns SQL the VALUES for an INSERT statement given the passed in +columns+
# and +array_of_attributes+.
def values_sql_for_columns_and_attributes(columns, array_of_attributes) # :nodoc:
# connection gets called a *lot* in this high intensity loop.
# Reuse the same one w/in the loop, otherwise it would keep being re-retreived (= lots of time for large imports)
connection_memo = connection
array_of_attributes.map do |arr|
my_values = arr.each_with_index.map do |val,j|
column = columns[j]
if !sequence_name.blank? && column.name == primary_key && val.nil?
connection.next_value_for_sequence(sequence_name)
# be sure to query sequence_name *last*, only if cheaper tests fail, because it's costly
if val.nil? && column.name == primary_key && !sequence_name.blank?
connection_memo.next_value_for_sequence(sequence_name)
else
connection.quote(column.type_cast(val), column)
if serialized_attributes.include?(column.name)
connection_memo.quote(serialized_attributes[column.name].dump(val), column)
else
connection_memo.quote(val, column)
end
end
end
"(#{my_values.join(',')})"

View File

@ -43,6 +43,10 @@ module ActiveRecord # :nodoc:
instance.clear_aggregation_cache
instance.clear_association_cache
instance.instance_variable_set '@attributes', matched_instance.attributes
# Since the instance now accurately reflects the record in
# the database, ensure that instance.persisted? is true.
instance.instance_variable_set '@new_record', false
instance.instance_variable_set '@destroyed', false
end
end
end
@ -52,4 +56,4 @@ module ActiveRecord # :nodoc:
self.class.synchronize(instances, key)
end
end
end
end

View File

@ -50,3 +50,13 @@ describe "ActiveRecord::ConnectionAdapter::AbstractAdapter" do
end
end
describe "ActiveRecord::Import DB-specific adapter class" do
context "when ActiveRecord::Import is in use" do
it "should appear in the AR connection adapter class's ancestors" do
connection = ActiveRecord::Base.connection
import_class_name = 'ActiveRecord::Import::' + connection.class.name.demodulize
assert_includes connection.class.ancestors, import_class_name.constantize
end
end
end

View File

@ -1 +1 @@
ENV["ARE_DB"] = "mysql2"
ENV["ARE_DB"] = "mysql2"

View File

@ -0,0 +1 @@
ENV["ARE_DB"] = "mysql2spatial"

View File

@ -0,0 +1 @@
ENV["ARE_DB"] = "mysqlspatial"

1
test/adapters/postgis.rb Normal file
View File

@ -0,0 +1 @@
ENV["ARE_DB"] = "postgis"

View File

@ -0,0 +1 @@
ENV["ARE_DB"] = "seamless_database_pool"

View File

@ -0,0 +1 @@
ENV["ARE_DB"] = "spatialite"

View File

@ -13,12 +13,28 @@ mysql2:
<<: *common
adapter: mysql2
mysqlspatial:
<<: *mysql
mysqlspatial2:
<<: *mysql2
seamless_database_pool:
<<: *common
adapter: seamless_database_pool
pool_adapter: mysql2
master:
host: localhost
postgresql:
<<: *common
username: postgres
adapter: postgresql
min_messages: warning
postgis:
<<: *postgresql
oracle:
<<: *common
adapter: oracle
@ -31,3 +47,6 @@ sqlite:
sqlite3:
adapter: sqlite3
database: test.db
spatialite:
<<: *sqlite3

View File

@ -19,6 +19,16 @@ describe "#import" do
end
end
describe "with non-default ActiveRecord models" do
context "that have a non-standard primary key (that is no sequence)" do
it "should import models successfully" do
assert_difference "Widget.count", +3 do
Widget.import Build(3, :widgets)
end
end
end
end
context "with :validation option" do
let(:columns) { %w(title author_name) }
let(:valid_values) { [[ "LDAP", "Jerry Carter"], ["Rails Recipes", "Chad Fowler"]] }
@ -36,6 +46,12 @@ describe "#import" do
result = Topic.import columns, invalid_values, :validate => false
end
end
it 'should raise a specific error if a column does not exist' do
assert_raises ActiveRecord::Import::MissingColumnError do
Topic.import ['foo'], [['bar']], :validate => false
end
end
end
context "with validation checks turned on" do
@ -65,7 +81,45 @@ describe "#import" do
end
end
end
context "with :all_or_none option" do
let(:columns) { %w(title author_name) }
let(:valid_values) { [[ "LDAP", "Jerry Carter"], ["Rails Recipes", "Chad Fowler"]] }
let(:invalid_values) { [[ "The RSpec Book", ""], ["Agile+UX", ""]] }
let(:mixed_values) { valid_values + invalid_values }
context "with validation checks turned on" do
it "should import valid data" do
assert_difference "Topic.count", +2 do
result = Topic.import columns, valid_values, :all_or_none => true
end
end
it "should not import invalid data" do
assert_no_difference "Topic.count" do
result = Topic.import columns, invalid_values, :all_or_none => true
end
end
it "should not import valid data when mixed with invalid data" do
assert_no_difference "Topic.count" do
result = Topic.import columns, mixed_values, :all_or_none => true
end
end
it "should report the failed instances" do
results = Topic.import columns, mixed_values, :all_or_none => true
assert_equal invalid_values.size, results.failed_instances.size
results.failed_instances.each { |e| assert_kind_of Topic, e }
end
it "should report the zero inserts" do
results = Topic.import columns, mixed_values, :all_or_none => true
assert_equal 0, results.num_inserts
end
end
end
context "with :synchronize option" do
context "synchronizing on new records" do
let(:new_topics) { Build(3, :topics) }
@ -80,8 +134,18 @@ describe "#import" do
let(:new_topics) { Build(3, :topics) }
it "reloads data for existing in-memory instances" do
Topic.import(new_topics, :synchronize => new_topics, :synchronize_key => [:title] )
assert new_topics.all?(&:new_record?), "Records should have been reloaded"
Topic.import(new_topics, :synchronize => new_topics, :synchronize_keys => [:title] )
assert new_topics.all?(&:persisted?), "Records should have been reloaded"
end
end
context "synchronizing on destroyed records with explicit conditions" do
let(:new_topics) { Generate(3, :topics) }
it "reloads data for existing in-memory instances" do
new_topics.each &:destroy
Topic.import(new_topics, :synchronize => new_topics, :synchronize_keys => [:title] )
assert new_topics.all?(&:persisted?), "Records should have been reloaded"
end
end
end
@ -239,4 +303,18 @@ describe "#import" do
assert_equal "2010/05/14".to_date, Topic.last.last_read.to_date
end
end
end
context "importing through an association scope" do
[ true, false ].each do |b|
context "when validation is " + (b ? "enabled" : "disabled") do
it "should automatically set the foreign key column" do
books = [[ "David Chelimsky", "The RSpec Book" ], [ "Chad Fowler", "Rails Recipes" ]]
topic = Factory.create :topic
topic.books.import [ :author_name, :title ], books, :validate => b
assert_equal 2, topic.books.count
assert topic.books.all? { |b| b.topic_id == topic.id }
end
end
end
end
end

3
test/models/widget.rb Normal file
View File

@ -0,0 +1,3 @@
class Widget < ActiveRecord::Base
self.primary_key = :w_id
end

View File

@ -0,0 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
require File.expand_path(File.dirname(__FILE__) + '/../support/mysql/assertions')
require File.expand_path(File.dirname(__FILE__) + '/../support/mysql/import_examples')
should_support_mysql_import_functionality

View File

@ -0,0 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
require File.expand_path(File.dirname(__FILE__) + '/../support/mysql/assertions')
require File.expand_path(File.dirname(__FILE__) + '/../support/mysql/import_examples')
should_support_mysql_import_functionality

View File

@ -0,0 +1,4 @@
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
require File.expand_path(File.dirname(__FILE__) + '/../support/postgresql/import_examples')
should_support_postgresql_import_functionality

View File

@ -1,20 +1,4 @@
require File.expand_path('../../test_helper', __FILE__)
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
require File.expand_path(File.dirname(__FILE__) + '/../support/postgresql/import_examples')
describe "#supports_imports?" do
it "should support import" do
assert ActiveRecord::Base.supports_import?
end
end
describe "#import" do
it "should import with a single insert" do
# see ActiveRecord::ConnectionAdapters::AbstractAdapter test for more specifics
assert_difference "Topic.count", +10 do
result = Topic.import Build(3, :topics)
assert_equal 1, result.num_inserts
result = Topic.import Build(7, :topics)
assert_equal 1, result.num_inserts
end
end
end
should_support_postgresql_import_functionality

View File

@ -95,4 +95,8 @@ ActiveRecord::Schema.define do
end
add_index :animals, [:name], :unique => true, :name => 'uk_animals'
create_table :widgets, :id => false, :force => true do |t|
t.integer :w_id
end
end

View File

@ -1,4 +1,10 @@
class SchemaInfo < ActiveRecord::Base
set_table_name 'schema_info'
if respond_to?(:table_name=)
self.table_name = 'schema_info'
else
# this is becoming deprecated in ActiveRecord but not all adapters supported it
# at this time
set_table_name 'schema_info'
end
VERSION = 12
end

View File

@ -10,4 +10,8 @@ end
Factory.define :topic do |m|
m.sequence(:title){ |n| "Title #{n}"}
m.sequence(:author_name){ |n| "Author #{n}"}
end
end
Factory.define :widget do |m|
m.sequence(:w_id){ |n| n}
end

View File

@ -9,7 +9,7 @@ def should_support_mysql_import_functionality
"('7','8','9')" ]
adapter = ActiveRecord::Base.connection.class
values_size_in_bytes = adapter.sum_sizes( *values )
values_size_in_bytes = values.sum {|value| value.bytesize }
base_sql_size_in_bytes = 15
max_bytes = 30
@ -38,7 +38,7 @@ def should_support_mysql_import_functionality
base_sql_size_in_bytes = 15
max_bytes = 26
values_size_in_bytes = adapter.sum_sizes( *values )
values_size_in_bytes = values.sum {|value| value.bytesize }
value_sets = adapter.get_insert_value_sets( values, base_sql_size_in_bytes, max_bytes )
assert_equal 2, value_sets.size, 'Two value sets were expected!'

View File

@ -0,0 +1,21 @@
# encoding: UTF-8
def should_support_postgresql_import_functionality
describe "#supports_imports?" do
it "should support import" do
assert ActiveRecord::Base.supports_import?
end
end
describe "#import" do
it "should import with a single insert" do
# see ActiveRecord::ConnectionAdapters::AbstractAdapter test for more specifics
assert_difference "Topic.count", +10 do
result = Topic.import Build(3, :topics)
assert_equal 1, result.num_inserts
result = Topic.import Build(7, :topics)
assert_equal 1, result.num_inserts
end
end
end
end

View File

@ -44,3 +44,8 @@ adapter_schema = test_dir.join("schema/#{adapter}_schema.rb")
require adapter_schema if File.exists?(adapter_schema)
Dir[File.dirname(__FILE__) + "/models/*.rb"].each{ |file| require file }
# Prevent this deprecation warning from breaking the tests.
module Rake::DeprecatedObjectDSL
remove_method :import
end