active_record_upsert 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +6 -0
- data/active_record_upsert.gemspec +35 -0
- data/bin/console +16 -0
- data/bin/setup +8 -0
- data/lib/active_record_upsert.rb +16 -0
- data/lib/active_record_upsert/active_record.rb +5 -0
- data/lib/active_record_upsert/active_record/connection_adapters/abstract/database_statements.rb +25 -0
- data/lib/active_record_upsert/active_record/connection_adapters/postgresql/database_statements.rb +20 -0
- data/lib/active_record_upsert/active_record/persistence.rb +39 -0
- data/lib/active_record_upsert/active_record/relation.rb +41 -0
- data/lib/active_record_upsert/active_record/timestamp.rb +22 -0
- data/lib/active_record_upsert/arel.rb +6 -0
- data/lib/active_record_upsert/arel/crud.rb +18 -0
- data/lib/active_record_upsert/arel/insert_manager.rb +18 -0
- data/lib/active_record_upsert/arel/nodes.rb +1 -0
- data/lib/active_record_upsert/arel/nodes/do_nothing.rb +5 -0
- data/lib/active_record_upsert/arel/nodes/do_update_set.rb +33 -0
- data/lib/active_record_upsert/arel/nodes/excluded_column.rb +10 -0
- data/lib/active_record_upsert/arel/nodes/insert_statement.rb +59 -0
- data/lib/active_record_upsert/arel/nodes/on_conflict.rb +23 -0
- data/lib/active_record_upsert/arel/nodes/on_conflict_action.rb +6 -0
- data/lib/active_record_upsert/arel/on_conflict_do_update_manager.rb +47 -0
- data/lib/active_record_upsert/arel/visitors/to_sql.rb +54 -0
- data/lib/active_record_upsert/version.rb +3 -0
- metadata +156 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 8e82a70f09e1da45939fb0dba461762e5b7b2986
|
|
4
|
+
data.tar.gz: 736288186198273924c7f0caee5bb94e0a05ce52
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: edfd93a34947411bb9a390203bced749a2a95ca131e05ab1e0c31242f98197a4dfdcb92e9b9746538367377faf977dee1f452c6f7236ad88706aefc368e683ae
|
|
7
|
+
data.tar.gz: 452a0b7898226ba626657d11d426b6e1cec5a805e5978b67b5785cb17868f5509dc61c443699f7d25fd8b594804d896f08e577e88a13a76b880541f63bdb5753
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# ActiveRecordUpsert
|
|
2
|
+
|
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/active_record_upsert`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
4
|
+
|
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'active_record_upsert'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install active_record_upsert
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_record_upsert.
|
|
36
|
+
|
data/Rakefile
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'active_record_upsert/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "active_record_upsert"
|
|
8
|
+
spec.version = ActiveRecordUpsert::VERSION
|
|
9
|
+
spec.authors = ["Jesper Josefsson"]
|
|
10
|
+
spec.email = ["jesper.josefsson@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{PG Upsert for AR}
|
|
13
|
+
|
|
14
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
15
|
+
spec.bindir = "exe"
|
|
16
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
17
|
+
spec.require_paths = ["lib"]
|
|
18
|
+
|
|
19
|
+
spec.add_runtime_dependency "activerecord", "~> 5.0.0.beta2"
|
|
20
|
+
spec.add_runtime_dependency "arel", "~>7.0"
|
|
21
|
+
|
|
22
|
+
if defined?(JRUBY_VERSION)
|
|
23
|
+
spec.platform = 'java'
|
|
24
|
+
spec.add_runtime_dependency 'activerecord-jdbcpostgresql-adapter', '> 0'
|
|
25
|
+
else
|
|
26
|
+
spec.platform = Gem::Platform::RUBY
|
|
27
|
+
spec.add_runtime_dependency 'pg', '> 0'
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
35
|
+
end
|
data/bin/console
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require 'active_record'
|
|
5
|
+
require 'active_record/connection_adapters/postgresql_adapter'
|
|
6
|
+
require "active_record_upsert"
|
|
7
|
+
|
|
8
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
9
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
10
|
+
|
|
11
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
12
|
+
# require "pry"
|
|
13
|
+
# Pry.start
|
|
14
|
+
|
|
15
|
+
require "irb"
|
|
16
|
+
IRB.start
|
data/bin/setup
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require 'active_record_upsert/version'
|
|
2
|
+
|
|
3
|
+
unless defined?(Arel)
|
|
4
|
+
raise 'ActiveRecordUpsert has to be required after ActiveRecord/Arel'
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
unless defined?(ActiveRecord)
|
|
8
|
+
raise 'ActiveRecordUpsert has to be required after ActiveRecord'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
require 'active_record_upsert/arel'
|
|
12
|
+
require 'active_record_upsert/active_record'
|
|
13
|
+
|
|
14
|
+
module ActiveRecordUpsert
|
|
15
|
+
# Your code goes here...
|
|
16
|
+
end
|
data/lib/active_record_upsert/active_record/connection_adapters/abstract/database_statements.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module ActiveRecordUpsert
|
|
2
|
+
module ActiveRecord
|
|
3
|
+
module ConnectionAdapters
|
|
4
|
+
module Abstract
|
|
5
|
+
module DatabaseStatementsExtensions
|
|
6
|
+
def exec_upsert(_sql, _name, _binds, _pk)
|
|
7
|
+
raise NotImplementedError
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def upsert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
|
|
11
|
+
sql, binds, pk, _sequence_name = sql_for_upsert(to_sql(arel, binds), pk, id_value, sequence_name, binds)
|
|
12
|
+
exec_upsert(sql, name, binds, pk)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def sql_for_upsert(sql, pk, id_value, sequence_name, binds)
|
|
16
|
+
[sql, binds, pk, sequence_name]
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# ::ActiveRecord::ConnectionAdapters::DatabaseStatements.include(DatabaseStatementsExtensions)
|
|
21
|
+
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(DatabaseStatementsExtensions)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
data/lib/active_record_upsert/active_record/connection_adapters/postgresql/database_statements.rb
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module ActiveRecordUpsert
|
|
2
|
+
module ActiveRecord
|
|
3
|
+
module ConnectionAdapters
|
|
4
|
+
module Postgresql
|
|
5
|
+
module DatabaseStatementsExtensions
|
|
6
|
+
def sql_for_upsert(sql, pk, id_value, sequence_name, binds)
|
|
7
|
+
sql = "#{sql} RETURNING *"
|
|
8
|
+
super
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def exec_upsert(sql, name, binds, pk)
|
|
12
|
+
exec_query(sql, name, binds)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(DatabaseStatementsExtensions)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module ActiveRecordUpsert
|
|
2
|
+
module ActiveRecord
|
|
3
|
+
module PersistenceExtensions
|
|
4
|
+
def upsert(*args)
|
|
5
|
+
raise ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
|
|
6
|
+
values = run_callbacks(:save) {
|
|
7
|
+
run_callbacks(:create) {
|
|
8
|
+
_upsert_record(*args)
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
assign_attributes(values.first.to_h)
|
|
12
|
+
self
|
|
13
|
+
rescue ::ActiveRecord::RecordInvalid
|
|
14
|
+
false
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def _upsert_record(attribute_names = self.attribute_names)
|
|
18
|
+
attributes_values = arel_attributes_with_values_for_create(attribute_names)
|
|
19
|
+
values = self.class.unscoped.upsert attributes_values
|
|
20
|
+
@new_record = false
|
|
21
|
+
values
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
module ClassMethods
|
|
25
|
+
def upsert(attributes, &block)
|
|
26
|
+
if attributes.is_a?(Array)
|
|
27
|
+
attributes.collect { |attr| upsert(attr, &block) }
|
|
28
|
+
else
|
|
29
|
+
new(attributes, &block).upsert
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
puts 'JOJOJOJOJOJO'
|
|
36
|
+
::ActiveRecord::Base.prepend(PersistenceExtensions)
|
|
37
|
+
::ActiveRecord::Base.extend(PersistenceExtensions::ClassMethods)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module ActiveRecordUpsert
|
|
2
|
+
module ActiveRecord
|
|
3
|
+
module RelationExtensions
|
|
4
|
+
def upsert(values) # :nodoc:
|
|
5
|
+
primary_key_value = nil
|
|
6
|
+
|
|
7
|
+
if primary_key && Hash === values
|
|
8
|
+
primary_key_value = values[values.keys.find { |k|
|
|
9
|
+
k.name == primary_key
|
|
10
|
+
}]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
im = arel_table.create_insert
|
|
14
|
+
im.into arel_table
|
|
15
|
+
|
|
16
|
+
substitutes, binds = substitute_values values
|
|
17
|
+
|
|
18
|
+
cm = arel_table.create_on_conflict_do_update
|
|
19
|
+
cm.target = arel_table[primary_key]
|
|
20
|
+
|
|
21
|
+
filter = ->(o) { [primary_key, 'created_at'].include?(o.name) }
|
|
22
|
+
cm.set(substitutes.reject { |s| filter.call(s.first) })
|
|
23
|
+
on_conflict_binds = binds.reject(&filter)
|
|
24
|
+
|
|
25
|
+
im.on_conflict = cm.to_node
|
|
26
|
+
|
|
27
|
+
im.insert substitutes
|
|
28
|
+
|
|
29
|
+
@klass.connection.upsert(
|
|
30
|
+
im,
|
|
31
|
+
'SQL',
|
|
32
|
+
primary_key,
|
|
33
|
+
primary_key_value,
|
|
34
|
+
nil,
|
|
35
|
+
binds + on_conflict_binds)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
::ActiveRecord::Relation.include(RelationExtensions)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module ActiveRecordUpsert
|
|
2
|
+
module ActiveRecord
|
|
3
|
+
module TimestampExtensions
|
|
4
|
+
def _upsert_record
|
|
5
|
+
if self.record_timestamps
|
|
6
|
+
current_time = current_time_from_proper_timezone
|
|
7
|
+
|
|
8
|
+
all_timestamp_attributes.each do |column|
|
|
9
|
+
column = column.to_s
|
|
10
|
+
if has_attribute?(column) && !attribute_present?(column)
|
|
11
|
+
write_attribute(column, current_time)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
super
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
::ActiveRecord::Base.prepend(TimestampExtensions)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# module ActiveRecordUpsert
|
|
2
|
+
# module Arel
|
|
3
|
+
# module CrudExtensions
|
|
4
|
+
# def create_on_conflict_do_update
|
|
5
|
+
# OnConflictDoUpdateManager.new
|
|
6
|
+
# end
|
|
7
|
+
# end
|
|
8
|
+
#
|
|
9
|
+
# ::Arel::Crud.prepend(CrudExtensions)
|
|
10
|
+
# end
|
|
11
|
+
# end
|
|
12
|
+
module Arel
|
|
13
|
+
class Table
|
|
14
|
+
def create_on_conflict_do_update
|
|
15
|
+
OnConflictDoUpdateManager.new
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module ActiveRecordUpsert
|
|
2
|
+
module Arel
|
|
3
|
+
module InsertManagerExtensions
|
|
4
|
+
def on_conflict= node
|
|
5
|
+
@ast.on_conflict = node
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def do_nothing_on_conflict(target)
|
|
9
|
+
@ast.on_conflict = Nodes::OnConflict.new.tap do |on_conflict|
|
|
10
|
+
on_conflict.target = target
|
|
11
|
+
on_conflict.action = Nodes::DoNothing.new
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
::Arel::InsertManager.include(InsertManagerExtensions)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module Arel
|
|
2
|
+
module Nodes
|
|
3
|
+
class DoUpdateSet < OnConflictAction
|
|
4
|
+
attr_accessor :wheres, :values
|
|
5
|
+
attr_accessor :key
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
@wheres = []
|
|
9
|
+
@values = []
|
|
10
|
+
@key = nil
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def initialize_copy other
|
|
14
|
+
super
|
|
15
|
+
@wheres = @wheres.clone
|
|
16
|
+
@values = @values.clone
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def hash
|
|
20
|
+
[@relation, @wheres, @values, @key].hash
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def eql? other
|
|
24
|
+
self.class == other.class &&
|
|
25
|
+
self.relation == other.relation &&
|
|
26
|
+
self.wheres == other.wheres &&
|
|
27
|
+
self.values == other.values &&
|
|
28
|
+
self.key == other.key
|
|
29
|
+
end
|
|
30
|
+
alias :== :eql?
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# module ActiveRecordUpsert
|
|
2
|
+
# module Arel
|
|
3
|
+
# module Nodes
|
|
4
|
+
# module InsertStatementExtensions
|
|
5
|
+
# attr_accessor :on_conflict
|
|
6
|
+
#
|
|
7
|
+
# def initialize
|
|
8
|
+
# @on_conflict = nil
|
|
9
|
+
# super()
|
|
10
|
+
# end
|
|
11
|
+
#
|
|
12
|
+
# def hash
|
|
13
|
+
# [@relation, @columns, @values, @select, @on_conflict].hash
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
16
|
+
# def eql? other
|
|
17
|
+
# self.class == other.class &&
|
|
18
|
+
# self.relation == other.relation &&
|
|
19
|
+
# self.columns == other.columns &&
|
|
20
|
+
# self.select == other.select &&
|
|
21
|
+
# self.values == other.values &&
|
|
22
|
+
# self.on_conflict == other.on_conflict
|
|
23
|
+
# end
|
|
24
|
+
# end
|
|
25
|
+
#
|
|
26
|
+
# ::Arel::Nodes::InsertStatement.prepend(InsertStatementExtensions)
|
|
27
|
+
# end
|
|
28
|
+
# end
|
|
29
|
+
# end
|
|
30
|
+
|
|
31
|
+
module Arel
|
|
32
|
+
module Nodes
|
|
33
|
+
class InsertStatement
|
|
34
|
+
attr_accessor :on_conflict
|
|
35
|
+
|
|
36
|
+
def initialize
|
|
37
|
+
super()
|
|
38
|
+
@relation = nil
|
|
39
|
+
@columns = []
|
|
40
|
+
@values = nil
|
|
41
|
+
@select = nil
|
|
42
|
+
@on_conflict = nil
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def hash
|
|
46
|
+
[@relation, @columns, @values, @select, @on_conflict].hash
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def eql? other
|
|
50
|
+
self.class == other.class &&
|
|
51
|
+
self.relation == other.relation &&
|
|
52
|
+
self.columns == other.columns &&
|
|
53
|
+
self.select == other.select &&
|
|
54
|
+
self.values == other.values &&
|
|
55
|
+
self.on_conflict == other.on_conflict
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module Arel
|
|
2
|
+
module Nodes
|
|
3
|
+
class OnConflict < Node
|
|
4
|
+
attr_accessor :target, :action
|
|
5
|
+
|
|
6
|
+
def initialize
|
|
7
|
+
super
|
|
8
|
+
@target = nil
|
|
9
|
+
@action = nil
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def hash
|
|
13
|
+
[@target, @action].hash
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def eql? other
|
|
17
|
+
self.class == other.class &&
|
|
18
|
+
self.target == other.target &&
|
|
19
|
+
self.update_statement == other.update_statement
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module Arel
|
|
2
|
+
class OnConflictDoUpdateManager < Arel::TreeManager
|
|
3
|
+
def initialize
|
|
4
|
+
super
|
|
5
|
+
@ast = Nodes::OnConflict.new
|
|
6
|
+
@action = Nodes::DoUpdateSet.new
|
|
7
|
+
@ast.action = @action
|
|
8
|
+
@ctx = @ast
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def target= column
|
|
12
|
+
@ast.target = column
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def target(column)
|
|
16
|
+
@ast.target = column
|
|
17
|
+
self
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def wheres= exprs
|
|
21
|
+
@action.wheres = exprs
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def where expr
|
|
25
|
+
@action.wheres << expr
|
|
26
|
+
self
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def to_node
|
|
30
|
+
@ast
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def set values
|
|
34
|
+
if String === values
|
|
35
|
+
@action.values = [values]
|
|
36
|
+
else
|
|
37
|
+
@action.values = values.map { |column,value|
|
|
38
|
+
Nodes::Assignment.new(
|
|
39
|
+
Nodes::UnqualifiedColumn.new(column),
|
|
40
|
+
value
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
end
|
|
44
|
+
self
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
module ActiveRecordUpsert
|
|
2
|
+
module Arel
|
|
3
|
+
module Visitors
|
|
4
|
+
module ToSqlExtensions
|
|
5
|
+
def visit_Arel_Nodes_InsertStatement(o, collector)
|
|
6
|
+
collector = super
|
|
7
|
+
if o.on_conflict
|
|
8
|
+
maybe_visit o.on_conflict, collector
|
|
9
|
+
else
|
|
10
|
+
collector
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def visit_Arel_Nodes_OnConflict o, collector
|
|
15
|
+
collector << "ON CONFLICT "
|
|
16
|
+
collector << " (#{quote_column_name o.target.name}) "
|
|
17
|
+
maybe_visit o.action, collector
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def visit_Arel_Nodes_DoNothing _o, collector
|
|
21
|
+
collector << "DO NOTHING"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def visit_Arel_Nodes_DoUpdateSet o, collector
|
|
25
|
+
wheres = o.wheres
|
|
26
|
+
|
|
27
|
+
collector << "DO UPDATE "
|
|
28
|
+
unless o.values.empty?
|
|
29
|
+
collector << " SET "
|
|
30
|
+
collector = inject_join o.values, collector, ", "
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
unless wheres.empty?
|
|
34
|
+
collector << " WHERE "
|
|
35
|
+
collector = inject_join wheres, collector, " AND "
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
collector
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def visit_Arel_Nodes_ExcludedColumn o, collector
|
|
42
|
+
collector << "EXCLUDED.#{quote_column_name o.column}"
|
|
43
|
+
collector
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def table_exists?(name)
|
|
47
|
+
schema_cache.data_source_exists?(name)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
::Arel::Visitors::ToSql.prepend(ToSqlExtensions)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: active_record_upsert
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Jesper Josefsson
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-02-18 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activerecord
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 5.0.0.beta2
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 5.0.0.beta2
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: arel
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '7.0'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '7.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: pg
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :runtime
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: bundler
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '1.11'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '1.11'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rake
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '10.0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '10.0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: rspec
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '3.0'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '3.0'
|
|
97
|
+
description:
|
|
98
|
+
email:
|
|
99
|
+
- jesper.josefsson@gmail.com
|
|
100
|
+
executables: []
|
|
101
|
+
extensions: []
|
|
102
|
+
extra_rdoc_files: []
|
|
103
|
+
files:
|
|
104
|
+
- ".gitignore"
|
|
105
|
+
- ".rspec"
|
|
106
|
+
- ".travis.yml"
|
|
107
|
+
- Gemfile
|
|
108
|
+
- README.md
|
|
109
|
+
- Rakefile
|
|
110
|
+
- active_record_upsert.gemspec
|
|
111
|
+
- bin/console
|
|
112
|
+
- bin/setup
|
|
113
|
+
- lib/active_record_upsert.rb
|
|
114
|
+
- lib/active_record_upsert/active_record.rb
|
|
115
|
+
- lib/active_record_upsert/active_record/connection_adapters/abstract/database_statements.rb
|
|
116
|
+
- lib/active_record_upsert/active_record/connection_adapters/postgresql/database_statements.rb
|
|
117
|
+
- lib/active_record_upsert/active_record/persistence.rb
|
|
118
|
+
- lib/active_record_upsert/active_record/relation.rb
|
|
119
|
+
- lib/active_record_upsert/active_record/timestamp.rb
|
|
120
|
+
- lib/active_record_upsert/arel.rb
|
|
121
|
+
- lib/active_record_upsert/arel/crud.rb
|
|
122
|
+
- lib/active_record_upsert/arel/insert_manager.rb
|
|
123
|
+
- lib/active_record_upsert/arel/nodes.rb
|
|
124
|
+
- lib/active_record_upsert/arel/nodes/do_nothing.rb
|
|
125
|
+
- lib/active_record_upsert/arel/nodes/do_update_set.rb
|
|
126
|
+
- lib/active_record_upsert/arel/nodes/excluded_column.rb
|
|
127
|
+
- lib/active_record_upsert/arel/nodes/insert_statement.rb
|
|
128
|
+
- lib/active_record_upsert/arel/nodes/on_conflict.rb
|
|
129
|
+
- lib/active_record_upsert/arel/nodes/on_conflict_action.rb
|
|
130
|
+
- lib/active_record_upsert/arel/on_conflict_do_update_manager.rb
|
|
131
|
+
- lib/active_record_upsert/arel/visitors/to_sql.rb
|
|
132
|
+
- lib/active_record_upsert/version.rb
|
|
133
|
+
homepage:
|
|
134
|
+
licenses: []
|
|
135
|
+
metadata: {}
|
|
136
|
+
post_install_message:
|
|
137
|
+
rdoc_options: []
|
|
138
|
+
require_paths:
|
|
139
|
+
- lib
|
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
141
|
+
requirements:
|
|
142
|
+
- - ">="
|
|
143
|
+
- !ruby/object:Gem::Version
|
|
144
|
+
version: '0'
|
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
146
|
+
requirements:
|
|
147
|
+
- - ">="
|
|
148
|
+
- !ruby/object:Gem::Version
|
|
149
|
+
version: '0'
|
|
150
|
+
requirements: []
|
|
151
|
+
rubyforge_project:
|
|
152
|
+
rubygems_version: 2.5.1
|
|
153
|
+
signing_key:
|
|
154
|
+
specification_version: 4
|
|
155
|
+
summary: PG Upsert for AR
|
|
156
|
+
test_files: []
|