upsert 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,10 @@
1
+ 0.3.1 / 2012-06-21
2
+
3
+ * Bug fixes
4
+
5
+ * On PostgreSQL, stop nullifying columns that weren't even involved in the upsert
6
+ * On SQLite, properly join WHERE conditions with ' AND ' instead of ','
7
+
1
8
  0.3.0 / 2012-06-21
2
9
 
3
10
  * Enhancements
data/README.md CHANGED
@@ -33,7 +33,13 @@ For bulk upserts, you probably still want to use `Upsert.batch`.
33
33
  Pet.upsert({:name => 'Jerry'}, :breed => 'beagle')
34
34
  Pet.upsert({:name => 'Pierre'}, :breed => 'tabby')
35
35
 
36
- ### The "fixed column set" gotcha
36
+ ### Gotchas
37
+
38
+ #### Undefined behavior if you use this without properly defining UNIQUE indexes
39
+
40
+ Make sure you're upserting against either primary key columns or columns with UNIQUE indexes or both.
41
+
42
+ #### Columns are set based on the first row you pass
37
43
 
38
44
  Currently, the first row you pass in determines the columns that will be used. That's useful for mass importing of many rows with the same columns, but is surprising if you're trying to use a single `Upsert` object to add arbitrary data. For example, this won't work:
39
45
 
@@ -52,6 +58,7 @@ You would need to use a new `Upsert` object. On the other hand, this is totally
52
58
  Pull requests for any of these would be greatly appreciated:
53
59
 
54
60
  1. Fix SQLite tests.
61
+ 2. Provide `require 'upsert/debug'` that will make sure you are selecting on columns that have unique indexes
55
62
  2. If you think there's a fix for the "fixed column set" gotcha...
56
63
  3. Naming suggestions: should "document" be called "setters" or "attributes"?
57
64
 
@@ -4,11 +4,15 @@ class Upsert
4
4
  # @private
5
5
  module PG_Connection
6
6
 
7
+ attr_reader :columns
7
8
  attr_reader :merge_function
8
9
 
9
10
  def chunk
10
11
  return if buffer.empty?
11
12
  row = buffer.shift
13
+ unless @columns.is_a?(Array)
14
+ @columns = row.columns
15
+ end
12
16
  unless merge_function
13
17
  create_merge_function row
14
18
  end
@@ -48,7 +52,7 @@ class Upsert
48
52
  end
49
53
 
50
54
  def column_definitions
51
- @column_definitions ||= ColumnDefinition.all(connection, table_name)
55
+ @column_definitions ||= ColumnDefinition.all(connection, table_name).select { |cd| columns.include?(cd.name) }
52
56
  end
53
57
 
54
58
  private
@@ -36,7 +36,7 @@ class Upsert
36
36
  end
37
37
 
38
38
  def where_sql
39
- selector.map { |k, v| [k, v].join('=') }.join(',')
39
+ selector.map { |k, v| [k, v].join('=') }.join(' AND ')
40
40
  end
41
41
 
42
42
  def set_sql
@@ -1,3 +1,3 @@
1
1
  class Upsert
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -1,4 +1,6 @@
1
1
  shared_examples_for 'is just as correct as other ways' do
2
+ unless ENV['CORR'] == 'false'
3
+
2
4
  describe 'compared to native ActiveRecord' do
3
5
  it "is as correct as than new/set/save" do
4
6
  assert_same_result lotsa_records do |records|
@@ -66,4 +68,5 @@ shared_examples_for 'is just as correct as other ways' do
66
68
  end
67
69
  end
68
70
  end
69
- end
71
+ end
72
+ end
@@ -13,6 +13,14 @@ shared_examples_for 'is a database with an upsert trick' do
13
13
  upsert.row({:name => 'Jerry', :gender => 'male'}, {:tag_number => 4})
14
14
  end
15
15
  end
16
+ it "doesn't nullify columns that are not included in the selector or document" do
17
+ assert_creates(Pet, [{:name => 'Jerry', :gender => 'male', :tag_number => 4}]) do
18
+ one = Upsert.new connection, :pets
19
+ one.row({:name => 'Jerry'}, {:gender => 'male'})
20
+ two = Upsert.new connection, :pets
21
+ two.row({:name => 'Jerry'}, {:tag_number => 4})
22
+ end
23
+ end
16
24
  it "works for a single row (not changing anything)" do
17
25
  upsert = Upsert.new connection, :pets
18
26
  assert_creates(Pet, [{:name => 'Jerry', :gender => 'male'}]) do
@@ -1,4 +1,5 @@
1
1
  shared_examples_for "doesn't blow up on reserved words" do
2
+ unless ENV['RES'] == 'false'
2
3
  # collect and uniq reserved words
3
4
  reserved_words = ['mysql_reserved.txt', 'pg_reserved.txt'].map do |basename|
4
5
  File.expand_path("../../misc/#{basename}", __FILE__)
@@ -40,4 +41,5 @@ shared_examples_for "doesn't blow up on reserved words" do
40
41
  end
41
42
  end
42
43
  end
44
+ end
43
45
  end
@@ -1,4 +1,5 @@
1
1
  shared_examples_for 'can be speeded up with upserting' do
2
+ unless ENV['SPEED'] == 'false'
2
3
  describe 'compared to native ActiveRecord' do
3
4
  it "is faster than new/set/save" do
4
5
  assert_faster_than 'find + new/set/save', lotsa_records do |records|
@@ -67,4 +68,5 @@ shared_examples_for 'can be speeded up with upserting' do
67
68
  end
68
69
  end
69
70
  end
70
- end
71
+ end
72
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: upsert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
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-06-21 00:00:00.000000000 Z
12
+ date: 2012-06-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sqlite3