ductr-postgres 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/COPYING.LESSER ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ductr-postgres.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,138 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ductr-postgres (0.1.0)
5
+ ductr (~> 0.1)
6
+ pg (~> 1.4)
7
+ sequel (~> 5.66)
8
+ sequel_pg (~> 1.8)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ activejob (7.0.4.3)
14
+ activesupport (= 7.0.4.3)
15
+ globalid (>= 0.3.6)
16
+ activesupport (7.0.4.3)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 1.6, < 2)
19
+ minitest (>= 5.1)
20
+ tzinfo (~> 2.0)
21
+ annotable (0.1.2)
22
+ ast (2.4.2)
23
+ colorize (0.8.1)
24
+ commander (4.6.0)
25
+ highline (~> 2.0.0)
26
+ concurrent-ruby (1.2.2)
27
+ diff-lcs (1.5.0)
28
+ docile (1.4.0)
29
+ ductr (0.1.2)
30
+ activejob (~> 7.0)
31
+ annotable (~> 0.1)
32
+ colorize (~> 0.8)
33
+ kiba (~> 4.0)
34
+ rufus-scheduler (~> 3.8)
35
+ thor (~> 1.2)
36
+ zeitwerk (~> 2.6)
37
+ et-orbi (1.2.7)
38
+ tzinfo
39
+ fugit (1.8.1)
40
+ et-orbi (~> 1, >= 1.2.7)
41
+ raabro (~> 1.4)
42
+ globalid (1.1.0)
43
+ activesupport (>= 5.0)
44
+ highline (2.0.3)
45
+ i18n (1.12.0)
46
+ concurrent-ruby (~> 1.0)
47
+ json (2.6.3)
48
+ kiba (4.0.0)
49
+ minitest (5.18.0)
50
+ parallel (1.22.1)
51
+ parlour (5.0.0)
52
+ commander (~> 4.5)
53
+ parser
54
+ rainbow (~> 3.0)
55
+ sorbet-runtime (>= 0.5)
56
+ parser (3.2.1.1)
57
+ ast (~> 2.4.1)
58
+ pg (1.4.6)
59
+ raabro (1.4.0)
60
+ rainbow (3.1.1)
61
+ rake (13.0.6)
62
+ redcarpet (3.6.0)
63
+ regexp_parser (2.7.0)
64
+ rexml (3.2.5)
65
+ rspec (3.12.0)
66
+ rspec-core (~> 3.12.0)
67
+ rspec-expectations (~> 3.12.0)
68
+ rspec-mocks (~> 3.12.0)
69
+ rspec-core (3.12.1)
70
+ rspec-support (~> 3.12.0)
71
+ rspec-expectations (3.12.2)
72
+ diff-lcs (>= 1.2.0, < 2.0)
73
+ rspec-support (~> 3.12.0)
74
+ rspec-mocks (3.12.4)
75
+ diff-lcs (>= 1.2.0, < 2.0)
76
+ rspec-support (~> 3.12.0)
77
+ rspec-support (3.12.0)
78
+ rubocop (1.48.1)
79
+ json (~> 2.3)
80
+ parallel (~> 1.10)
81
+ parser (>= 3.2.0.0)
82
+ rainbow (>= 2.2.2, < 4.0)
83
+ regexp_parser (>= 1.8, < 3.0)
84
+ rexml (>= 3.2.5, < 4.0)
85
+ rubocop-ast (>= 1.26.0, < 2.0)
86
+ ruby-progressbar (~> 1.7)
87
+ unicode-display_width (>= 2.4.0, < 3.0)
88
+ rubocop-ast (1.28.0)
89
+ parser (>= 3.2.1.0)
90
+ rubocop-capybara (2.17.1)
91
+ rubocop (~> 1.41)
92
+ rubocop-rspec (2.19.0)
93
+ rubocop (~> 1.33)
94
+ rubocop-capybara (~> 2.17)
95
+ ruby-progressbar (1.13.0)
96
+ rufus-scheduler (3.8.2)
97
+ fugit (~> 1.1, >= 1.1.6)
98
+ sequel (5.66.0)
99
+ sequel_pg (1.17.1)
100
+ pg (>= 0.18.0, != 1.2.0)
101
+ sequel (>= 4.38.0)
102
+ simplecov (0.22.0)
103
+ docile (~> 1.1)
104
+ simplecov-html (~> 0.11)
105
+ simplecov_json_formatter (~> 0.1)
106
+ simplecov-html (0.12.3)
107
+ simplecov_json_formatter (0.1.4)
108
+ sorbet-runtime (0.5.10736)
109
+ sord (4.0.0)
110
+ commander (~> 4.5)
111
+ parlour (~> 5.0)
112
+ sorbet-runtime
113
+ yard
114
+ thor (1.2.1)
115
+ tzinfo (2.0.6)
116
+ concurrent-ruby (~> 1.0)
117
+ unicode-display_width (2.4.2)
118
+ webrick (1.7.0)
119
+ yard (0.9.28)
120
+ webrick (~> 1.7.0)
121
+ zeitwerk (2.6.7)
122
+
123
+ PLATFORMS
124
+ x86_64-linux
125
+
126
+ DEPENDENCIES
127
+ ductr-postgres!
128
+ rake (~> 13.0)
129
+ redcarpet (~> 3.5)
130
+ rspec (~> 3.11)
131
+ rubocop (~> 1.29)
132
+ rubocop-rspec (~> 2.11)
133
+ simplecov (~> 0.21)
134
+ sord (~> 4.0)
135
+ yard (~> 0.9)
136
+
137
+ BUNDLED WITH
138
+ 2.4.8
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # PostgreSQL adapter for Ductr ETL
2
+ This gem provides useful controls to operate Ductr with PostgreSQL databases.
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's `Gemfile`:
7
+
8
+ ```ruby
9
+ gem 'ductr-postgres'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ ```bash
15
+ $ bundle install
16
+ ```
17
+
18
+ Require `ductr/postgres` in the `config/app.rb` file:
19
+
20
+ ```ruby
21
+ require "ductr/postgres"
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ You can now use the adapter in your YAML configuration:
27
+
28
+ ```yml
29
+ adapters:
30
+ some_postgres_database:
31
+ adapter: postgres
32
+ host: localhost
33
+ user: postgres
34
+ password: s3cr3t
35
+ database: example
36
+ ```
37
+
38
+ You can pass any option recognized by sequel.
39
+ See [the Sequel Sequel general options list](https://sequel.jeremyevans.net/rdoc/files/doc/opening_databases_rdoc.html#label-General+connection+options) for further details.
40
+ See [the Sequel PostgreSQL specific options list](https://sequel.jeremyevans.net/rdoc/files/doc/opening_databases_rdoc.html#label-postgres) for further details.
41
+
42
+ The configured adapter can now be used in Ductr jobs e.g.:
43
+
44
+ ```ruby
45
+ source :some_postgres_database, :streamed
46
+ def select_some_stuff(db, offset, limit)
47
+ db[:items].offset(offset).limit(limit)
48
+ end
49
+ ```
50
+
51
+ ## Development
52
+
53
+ 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.
54
+
55
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ductr-io/ductr. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ductr-io/ductr-postgres/blob/main/CODE_OF_CONDUCT.md).
60
+
61
+ ## Code of Conduct
62
+
63
+ Everyone interacting in the Ductr::PostgreSQL project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ductr-io/ductr-postgres/blob/main/CODE_OF_CONDUCT.md).
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [LGPLv3 or later](https://opensource.org/license/lgpl-3-0/).
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
13
+
14
+ require "sord"
15
+
16
+ desc "Generates RBS file from the yard documentation"
17
+ task :sord do
18
+ options = {
19
+ rbs: true,
20
+ sord_comments: true,
21
+ regenerate: true,
22
+ break_params: 4,
23
+ replace_errors_with_untyped: false,
24
+ replace_unresolved_with_untyped: false,
25
+ exclude_messages: nil,
26
+ include_messages: nil,
27
+ keep_original_comments: false,
28
+ skip_constants: false,
29
+ use_original_initialize_return: false,
30
+ exclude_untyped: false
31
+ }
32
+
33
+ plugin = Sord::ParlourPlugin.new(options)
34
+ plugin.parlour = Parlour::RbsGenerator.new(break_params: options[:break_params])
35
+ plugin.generate(plugin.parlour.root)
36
+
37
+ File.write("sig/ductr/postgres.rbs", plugin.parlour.send(:rbs))
38
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ductr/postgres/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ductr-postgres"
7
+ spec.version = Ductr::Postgres::VERSION
8
+ spec.authors = ["Mathieu MOREL"]
9
+ spec.email = ["mathieu@lamanufacture.dev"]
10
+ spec.licenses = ["LGPL-3.0-or-later"]
11
+
12
+ spec.summary = "PostgreSQL adapter for Ductr using the `sequel` gem"
13
+ spec.description = "Allows ductr to interact with PostgreSQL DMBS."
14
+ spec.homepage = "https://github.com/ductr-io/ductr"
15
+ spec.required_ruby_version = ">= 3.1.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+ spec.metadata["rubygems_mfa_required"] = "true"
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = spec.homepage
22
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/releases"
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
27
+ `git ls-files -z`.split("\x0").reject do |f|
28
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
29
+ end
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_dependency "ductr", "~> 0.1"
36
+ spec.add_dependency "pg", "~> 1.4"
37
+ spec.add_dependency "sequel", "~> 5.66"
38
+ spec.add_dependency "sequel_pg", "~> 1.8"
39
+
40
+ spec.add_development_dependency "rake", "~> 13.0"
41
+ spec.add_development_dependency "redcarpet", "~> 3.5"
42
+ spec.add_development_dependency "rspec", "~> 3.11"
43
+ spec.add_development_dependency "rubocop", "~> 1.29"
44
+ spec.add_development_dependency "rubocop-rspec", "~> 2.11"
45
+ spec.add_development_dependency "simplecov", "~> 0.21"
46
+ spec.add_development_dependency "sord", "~> 4.0"
47
+ spec.add_development_dependency "yard", "~> 0.9"
48
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ductr
4
+ module Postgres
5
+ #
6
+ # The PostgreSQL adapter implement the required #open! and #close! methods to handle the database connection.
7
+ # The adapter is registered as `:postgres` to use it, add `adapter: postgres` to the YAML configuration e.g.:
8
+ #
9
+ # ```yml
10
+ # # config/development.yml
11
+ # adapters:
12
+ # some_postgres_database:
13
+ # adapter: postgres
14
+ # host: localhost
15
+ # user: postgres
16
+ # password: s3cr3t
17
+ # database: example
18
+ # ```
19
+ #
20
+ # @see https://sequel.jeremyevans.net/rdoc/files/doc/opening_databases_rdoc.html#label-General+connection+options
21
+ # General sequel options
22
+ # @see https://sequel.jeremyevans.net/rdoc/files/doc/opening_databases_rdoc.html#label-postgres
23
+ # PostgreSQL specific options
24
+ #
25
+ class Adapter < Ductr::Adapter
26
+ Ductr.adapter_registry.add(self, as: :postgres)
27
+
28
+ # @return [Sequel::Database, nil] The database connection instance
29
+ attr_reader :db
30
+
31
+ #
32
+ # Opens the database connection with the adapter's configuration.
33
+ #
34
+ # @return [Sequel::Database] The database connection instance
35
+ #
36
+ def open!
37
+ @db = Sequel.postgres(**config)
38
+
39
+ @db.extension(:pg_streaming)
40
+ @db.stream_all_queries = true
41
+
42
+ @db
43
+ end
44
+
45
+ #
46
+ # Closes the database connection.
47
+ #
48
+ # @return [void]
49
+ #
50
+ def close!
51
+ @db.disconnect
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ductr
4
+ module Postgres
5
+ #
6
+ # A lookup control that execute one query per row, registered as `:basic`.
7
+ # The job's method must return a row which will merged with the current row:
8
+ #
9
+ # lookup :some_postgres_database, :basic
10
+ # def my_lookup(db, row)
11
+ # db[:items_bis].where(item: row[:id]).limit(1)
12
+ # end
13
+ #
14
+ # As the control merge the looked up row with the current row,
15
+ # ensure that column names are different or they will be overwritten.
16
+ #
17
+ # If the lookup returns a falsy value, nothing won't be merged with the current row.
18
+ #
19
+ class BasicLookup < Ductr::ETL::Transform
20
+ Adapter.lookup_registry.add(self, as: :basic)
21
+
22
+ #
23
+ # Calls the job's method to merge its result with the current row.
24
+ #
25
+ # @param [Hash<Symbol, Object>] row The current row, preferably a Hash
26
+ #
27
+ # @return [Hash<Symbol, Object>] The row merged with looked up row or the untouched row if nothing was found
28
+ #
29
+ def process(row)
30
+ matching_row = call_method(adapter.db, row).first
31
+ return row unless matching_row
32
+
33
+ row.merge matching_row
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ductr
4
+ module Postgres
5
+ #
6
+ # A destination control that accumulates rows in a buffer to write them by batch, registered as `:buffered`.
7
+ # Accept the `:buffer_size` option, default value is 10 000:
8
+ #
9
+ # destination :some_postgres_database, :buffered, buffer_size: 42
10
+ # def my_destination(db, buffer)
11
+ # db[:items].multi_insert(buffer)
12
+ # end
13
+ #
14
+ # @see more Ductr::ETL::BufferedDestination
15
+ #
16
+ class BufferedDestination < Ductr::ETL::BufferedDestination
17
+ Adapter.destination_registry.add(self, as: :buffered)
18
+
19
+ #
20
+ # Open the database if needed and call the job's method to run the query.
21
+ #
22
+ # @return [void]
23
+ #
24
+ def on_flush
25
+ call_method(adapter.db, buffer)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ductr
4
+ module Postgres
5
+ #
6
+ # A lookup control that execute the query for a bunch of rows, registered as `:buffered`.
7
+ # Accept the `:buffer_size` option, default value is 10 000.
8
+ # You have to implement your own row matching logic:
9
+ #
10
+ # lookup :some_postgres_database, :buffered, buffer_size: 42
11
+ # def my_lookup(db, buffer, &)
12
+ # ids = buffer.map {|row| row[:id]}
13
+ # db[:items].where(item: ids).each do |row|
14
+ # match = buffer.find { |r| r[:id] == row[:item] }
15
+ #
16
+ # next yield(row) unless match
17
+ #
18
+ # yield(row.merge match)
19
+ # end
20
+ # end
21
+ #
22
+ class BufferedLookup < Ductr::ETL::BufferedTransform
23
+ Adapter.lookup_registry.add(self, as: :buffered)
24
+
25
+ #
26
+ # Opens the database if needed, calls the job's method and pass the each block to it.
27
+ #
28
+ # @yield The each block
29
+ #
30
+ # @return [void]
31
+ #
32
+ def on_flush(&)
33
+ call_method(adapter.db, buffer, &)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ductr
4
+ module Postgres
5
+ #
6
+ # A destination control that accumulates rows in a buffer to upsert them by batch, registered as `:buffered_upsert`.
7
+ # Accept the `:buffer_size` option, default value is 10 000:
8
+ #
9
+ # destination :some_postgres_database, :buffered_upsert, buffer_size: 42
10
+ # def my_destination(buffer, excluded, db)
11
+ # db[:items].insert_conflict(target: :id, update: excluded).multi_insert(buffer)
12
+ # end
13
+ #
14
+ # @see more Ductr::ETL::BufferedDestination
15
+ #
16
+ class BufferedUpsertDestination < Ductr::ETL::BufferedDestination
17
+ Adapter.destination_registry.add(self, as: :buffered_upsert)
18
+
19
+ #
20
+ # Open the database if needed and call the job's method to run the query.
21
+ #
22
+ # @return [void]
23
+ #
24
+ def on_flush
25
+ call_method(adapter.db, excluded, buffer)
26
+ end
27
+
28
+ private
29
+
30
+ #
31
+ # Generate the excluded keys hash e.g.
32
+ #
33
+ # ```ruby
34
+ # {a: Sequel[:excluded][:a]}
35
+ # ```
36
+ #
37
+ # @return [Hash<Symbol, Sequel::SQL::QualifiedIdentifier>] The excluded keys hash
38
+ #
39
+ def excluded
40
+ keys = buffer.first.keys
41
+
42
+ excluded_keys = keys.map do |key|
43
+ Sequel[:excluded][key]
44
+ end
45
+
46
+ keys.zip(excluded_keys).to_h
47
+ end
48
+ end
49
+ end
50
+ end