activerecord-import 0.2.11 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -36,7 +36,7 @@ namespace :display do
36
36
  end
37
37
  task :default => ["display:notice"]
38
38
 
39
- ADAPTERS = %w(mysql mysql2 jdbcmysql postgresql sqlite3 seamless_database_pool)
39
+ ADAPTERS = %w(mysql mysql2 jdbcmysql postgresql sqlite3 seamless_database_pool mysqlspatial mysql2spatial spatialite postgis)
40
40
  ADAPTERS.each do |adapter|
41
41
  namespace :test do
42
42
  desc "Runs #{adapter} database tests."
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.11
1
+ 0.3.0
@@ -1,6 +1,6 @@
1
1
  require "active_record/connection_adapters/mysql2_adapter"
2
- require "activerecord-import/adapters/mysql_adapter"
2
+ require "activerecord-import/adapters/mysql2_adapter"
3
3
 
4
4
  class ActiveRecord::ConnectionAdapters::Mysql2Adapter
5
- include ActiveRecord::Import::MysqlAdapter
5
+ include ActiveRecord::Import::Mysql2Adapter
6
6
  end
@@ -1,7 +1,7 @@
1
1
  require "active_record/connection_adapters/sqlite3_adapter"
2
2
  require "activerecord-import/adapters/sqlite3_adapter"
3
3
 
4
- class ActiveRecord::ConnectionAdapters::Sqlite3Adapter
5
- include ActiveRecord::Import::Sqlite3Adapter
4
+ class ActiveRecord::ConnectionAdapters::SQLite3Adapter
5
+ include ActiveRecord::Import::SQLite3Adapter
6
6
  end
7
7
 
@@ -0,0 +1,5 @@
1
+ require File.dirname(__FILE__) + "/mysql_adapter"
2
+
3
+ module ActiveRecord::Import::Mysql2Adapter
4
+ include ActiveRecord::Import::MysqlAdapter
5
+ end
@@ -1,4 +1,4 @@
1
- module ActiveRecord::Import::Sqlite3Adapter
1
+ module ActiveRecord::Import::SQLite3Adapter
2
2
  def next_value_for_sequence(sequence_name)
3
3
  %{nextval('#{sequence_name}')}
4
4
  end
@@ -5,10 +5,20 @@ require "active_record/version"
5
5
  module ActiveRecord::Import
6
6
  AdapterPath = File.join File.expand_path(File.dirname(__FILE__)), "/active_record/adapters"
7
7
 
8
+ def self.base_adapter(adapter)
9
+ case adapter
10
+ when 'mysqlspatial' then 'mysql'
11
+ when 'mysql2spatial' then 'mysql2'
12
+ when 'spatialite' then 'sqlite3'
13
+ when 'postgis' then 'postgresql'
14
+ else adapter
15
+ end
16
+ end
17
+
8
18
  # Loads the import functionality for a specific database adapter
9
19
  def self.require_adapter(adapter)
10
20
  require File.join(AdapterPath,"/abstract_adapter")
11
- require File.join(AdapterPath,"/#{adapter}_adapter")
21
+ require File.join(AdapterPath,"/#{base_adapter(adapter)}_adapter")
12
22
  end
13
23
 
14
24
  # Loads the import functionality for the passed in ActiveRecord connection
@@ -137,8 +137,8 @@ class ActiveRecord::Base
137
137
  #
138
138
  # # Example synchronizing unsaved/new instances in memory by using a uniqued imported field
139
139
  # posts = [BlogPost.new(:title => "Foo"), BlogPost.new(:title => "Bar")]
140
- # BlogPost.import posts, :synchronize => posts
141
- # puts posts.first.new_record? # => false
140
+ # BlogPost.import posts, :synchronize => posts, :synchronize_keys => [:title]
141
+ # puts posts.first.persisted? # => true
142
142
  #
143
143
  # == On Duplicate Key Update (MySQL only)
144
144
  #
@@ -276,6 +276,13 @@ class ActiveRecord::Base
276
276
  # information on +column_names+, +array_of_attributes_ and
277
277
  # +options+.
278
278
  def import_without_validations_or_callbacks( column_names, array_of_attributes, options={} )
279
+ scope_columns, scope_values = scope_attributes.to_a.transpose
280
+
281
+ unless scope_columns.blank?
282
+ column_names.concat scope_columns
283
+ array_of_attributes.each { |a| a.concat scope_values }
284
+ end
285
+
279
286
  columns = column_names.each_with_index.map do |name, i|
280
287
  column = columns_hash[name.to_s]
281
288
 
@@ -310,14 +317,22 @@ class ActiveRecord::Base
310
317
  # Returns SQL the VALUES for an INSERT statement given the passed in +columns+
311
318
  # and +array_of_attributes+.
312
319
  def values_sql_for_columns_and_attributes(columns, array_of_attributes) # :nodoc:
320
+ # connection gets called a *lot* in this high intensity loop.
321
+ # Reuse the same one w/in the loop, otherwise it would keep being re-retreived (= lots of time for large imports)
322
+ connection_memo = connection
313
323
  array_of_attributes.map do |arr|
314
324
  my_values = arr.each_with_index.map do |val,j|
315
325
  column = columns[j]
316
326
 
317
- if val.nil? && !sequence_name.blank? && column.name == primary_key
318
- connection.next_value_for_sequence(sequence_name)
327
+ # be sure to query sequence_name *last*, only if cheaper tests fail, because it's costly
328
+ if val.nil? && column.name == primary_key && !sequence_name.blank?
329
+ connection_memo.next_value_for_sequence(sequence_name)
319
330
  else
320
- connection.quote(column.type_cast(val), column)
331
+ if serialized_attributes.include?(column.name)
332
+ connection_memo.quote(serialized_attributes[column.name].dump(val), column)
333
+ else
334
+ connection_memo.quote(val, column)
335
+ end
321
336
  end
322
337
  end
323
338
  "(#{my_values.join(',')})"
@@ -43,6 +43,10 @@ module ActiveRecord # :nodoc:
43
43
  instance.clear_aggregation_cache
44
44
  instance.clear_association_cache
45
45
  instance.instance_variable_set '@attributes', matched_instance.attributes
46
+ # Since the instance now accurately reflects the record in
47
+ # the database, ensure that instance.persisted? is true.
48
+ instance.instance_variable_set '@new_record', false
49
+ instance.instance_variable_set '@destroyed', false
46
50
  end
47
51
  end
48
52
  end
@@ -52,4 +56,4 @@ module ActiveRecord # :nodoc:
52
56
  self.class.synchronize(instances, key)
53
57
  end
54
58
  end
55
- end
59
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-import
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.11
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-14 00:00:00.000000000 Z
12
+ date: 2013-01-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -95,6 +95,7 @@ files:
95
95
  - lib/activerecord-import/active_record/adapters/seamless_database_pool_adapter.rb
96
96
  - lib/activerecord-import/active_record/adapters/sqlite3_adapter.rb
97
97
  - lib/activerecord-import/adapters/abstract_adapter.rb
98
+ - lib/activerecord-import/adapters/mysql2_adapter.rb
98
99
  - lib/activerecord-import/adapters/mysql_adapter.rb
99
100
  - lib/activerecord-import/adapters/postgresql_adapter.rb
100
101
  - lib/activerecord-import/adapters/sqlite3_adapter.rb
@@ -119,7 +120,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
120
  version: '0'
120
121
  segments:
121
122
  - 0
122
- hash: 4132545664813996444
123
+ hash: -2739522167439917244
123
124
  required_rubygems_version: !ruby/object:Gem::Requirement
124
125
  none: false
125
126
  requirements: