riverqueue-sequel 0.12.0 → 0.14.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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/driver.rb +47 -15
  3. metadata +9 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8672d5a5d5c01059a17a307704e5512496aecc5e62f57afe2dfcd5ddd8569a34
4
- data.tar.gz: abca68b736d841506449226a45e7b3d049884b40accdfb2c0e5fecf8cbbf896a
3
+ metadata.gz: 7118a23feeb5d7f021caf92736f9e2ae743abcb17b9271cca7cb028d2840b0b4
4
+ data.tar.gz: 4282e30b7d4fe282f9600cf10b4ed86cb415930d861070d0bf4c80086e99827e
5
5
  SHA512:
6
- metadata.gz: 8af058f89ffef579beb2cc91f428a4e004a7d844c61fd2ae588c8214e6fd54e7dc3b22acf7a01feca0dd2474945c5a6ec408378b42918e4f7bd29359c861c8fe
7
- data.tar.gz: 1d16f622b3df74b166ffccabee0c2374d810b43d026d344529df4f7d9eb2fd66a7f0bc2448a4e12619d00632c99e119a312f487635074dc14f35c47a44fbffd3
6
+ metadata.gz: 1c7688d17b1cf81710aaa87774cab9cec264f4692fb7af4819636625b3f77bc187c87a7f23322e71f492e29e8d5438d441ed323f23cf280d1a767be824ed5111
7
+ data.tar.gz: 21a5b271f12cbdedc4bf93803db4bab136ea35777cd101eb4723fd39b333a2dd54b81389c891017404d2f7c5b0c6bee929bff011b3f75cbd53eae35c996ad979
data/lib/driver.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  require "securerandom"
4
4
 
5
5
  module River::Driver
6
- # Provides a Sequel driver for River supporting PostgreSQL, YugabyteDB, and SQLite.
6
+ # Provides a Sequel driver for River supporting Postgres, YugabyteDB, and SQLite.
7
7
  #
8
8
  # Used in conjunction with a River client like:
9
9
  #
@@ -18,7 +18,7 @@ module River::Driver
18
18
  class Sequel
19
19
  include River::Driver::Runtime
20
20
 
21
- # Creates a driver backed by a connected Sequel::Database for PostgreSQL or
21
+ # Creates a driver backed by a connected Sequel::Database for Postgres or
22
22
  # SQLite.
23
23
  def initialize(db)
24
24
  @db = db
@@ -31,6 +31,16 @@ module River::Driver
31
31
  end
32
32
  end
33
33
 
34
+ # Runs the callback only if this transaction and its savepoints commit.
35
+ def after_commit(&block)
36
+ @db.after_commit(savepoint: true, &block)
37
+ end
38
+
39
+ # Reports whether this thread has a transaction on the configured database.
40
+ def in_transaction?
41
+ @db.in_transaction?
42
+ end
43
+
34
44
  def job_get_by_id(id)
35
45
  if @is_sqlite
36
46
  row = sqlite_job_rows("WHERE id = ? LIMIT 1", id).first
@@ -79,10 +89,24 @@ module River::Driver
79
89
  end
80
90
  end
81
91
 
82
- # Runs the block in a Sequel transaction or savepoint and returns the
83
- # block's result.
84
- def transaction(&)
85
- @db.transaction(savepoint: true, &)
92
+ # Runs the block in a Sequel transaction and returns the block's result.
93
+ # Joins an open transaction instead of opening a savepoint, unless that
94
+ # transaction uses Sequel's `auto_savepoint`.
95
+ def transaction
96
+ block_error = nil
97
+ options = runtime_legacy_subtransactions? ? {savepoint: true} : {}
98
+ @db.transaction(**options) do
99
+ yield
100
+ rescue ArgumentError => error
101
+ block_error = error
102
+ raise
103
+ end
104
+ rescue ::Sequel::DatabaseError => error
105
+ # SQLite treats ArgumentError as an adapter error, even when it came
106
+ # from an application callback. Preserve that error after rollback.
107
+ raise block_error if block_error && error.wrapped_exception.equal?(block_error)
108
+
109
+ raise
86
110
  end
87
111
 
88
112
  SQLITE_CONFLICT_WHERE = <<~SQL.chomp.freeze
@@ -133,7 +157,7 @@ module River::Driver
133
157
  private_constant :SQLITE_CONFLICT_WHERE, :SQLITE_JOB_COLUMNS, :UNIQUE_INSERT_METADATA_KEY
134
158
 
135
159
  private def format_time(time)
136
- time.getutc.round(3).strftime("%Y-%m-%d %H:%M:%S.%3N")
160
+ normalize_time(time).strftime("%Y-%m-%d %H:%M:%S.%3N")
137
161
  end
138
162
 
139
163
  private def postgres_insert_params_to_hash(insert_params, nonce)
@@ -163,7 +187,7 @@ module River::Driver
163
187
  "unique_key IS NOT NULL AND unique_states IS NOT NULL AND river_job_state_in_bitmask(unique_states, state)"
164
188
  ),
165
189
  target: [:unique_key],
166
- update: {kind: ::Sequel[:excluded][:kind]}
190
+ update: {kind: ::Sequel[:river_job][:kind]}
167
191
  )
168
192
  .returning(::Sequel.lit("*, #{capabilities.unique_insert_sql} AS unique_skipped_as_duplicate"))
169
193
  .multi_insert(insert_params_array.map { |p| postgres_insert_params_to_hash(p, nonce) })
@@ -197,6 +221,15 @@ module River::Driver
197
221
  end
198
222
  end
199
223
 
224
+ private def runtime_notification_connection
225
+ params, schema = @db.synchronize do |connection|
226
+ [connection.conninfo_hash, connection.exec("SELECT current_schema()").getvalue(0, 0)]
227
+ end
228
+ params.compact!
229
+ params[:connect_timeout] = "5" unless params[:connect_timeout].to_i.positive?
230
+ [::PG.connect(params), schema]
231
+ end
232
+
200
233
  private def runtime_postgres?
201
234
  !@is_sqlite
202
235
  end
@@ -209,17 +242,16 @@ module River::Driver
209
242
  @db.literal(value)
210
243
  end
211
244
 
212
- private def runtime_unique_violation_class
213
- ::Sequel::UniqueConstraintViolation
214
- end
215
-
216
245
  private def runtime_value(row, key)
217
246
  row[key] || row[key.to_s]
218
247
  end
219
248
 
220
249
  private def sqlite_insert_params_to_hash(insert_params, nonce)
221
250
  {
222
- args: JSON.parse(insert_params.encoded_args),
251
+ # Embed the encoded args as a JSON string so they reach SQLite
252
+ # byte-for-byte. `json_extract` unwraps the string back to the
253
+ # encoded JSON text before `jsonb` parses it.
254
+ args: insert_params.encoded_args,
223
255
  kind: insert_params.kind,
224
256
  max_attempts: insert_params.max_attempts,
225
257
  metadata: {UNIQUE_INSERT_METADATA_KEY => nonce},
@@ -238,7 +270,7 @@ module River::Driver
238
270
  # matching migration 007 and newer River databases.
239
271
  private def sqlite_job_insert_many(insert_params_array)
240
272
  nonces = sqlite_insert_nonces(insert_params_array)
241
- @db.transaction(savepoint: true) do
273
+ transaction do
242
274
  jobs = insert_params_array.zip(nonces).map { |param, nonce| sqlite_insert_params_to_hash(param, nonce) }
243
275
  inserted_nonces = nonces.to_h { |nonce| [nonce, true] }
244
276
 
@@ -276,7 +308,7 @@ module River::Driver
276
308
  FROM json_each(cast(? AS blob))
277
309
  WHERE true
278
310
  ON CONFLICT (unique_key) WHERE #{SQLITE_CONFLICT_WHERE}
279
- DO UPDATE SET kind = EXCLUDED.kind
311
+ DO UPDATE SET kind = river_job.kind
280
312
  RETURNING #{SQLITE_JOB_COLUMNS}
281
313
  SQL
282
314
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riverqueue-sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Gentry
@@ -14,9 +14,9 @@ dependencies:
14
14
  name: sequel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '5.20'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '1000'
@@ -24,9 +24,9 @@ dependencies:
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ">"
27
+ - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: '5.20'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '1000'
@@ -36,15 +36,15 @@ dependencies:
36
36
  requirements:
37
37
  - - '='
38
38
  - !ruby/object:Gem::Version
39
- version: 0.12.0
39
+ version: 0.14.0
40
40
  type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - '='
45
45
  - !ruby/object:Gem::Version
46
- version: 0.12.0
47
- description: Sequel PostgreSQL and SQLite driver for inserting and working River jobs
46
+ version: 0.14.0
47
+ description: Sequel Postgres and SQLite driver for inserting and working River jobs
48
48
  in Ruby.
49
49
  email: brandur@brandur.org
50
50
  executables: []
@@ -73,5 +73,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
73
  requirements: []
74
74
  rubygems_version: 4.0.9
75
75
  specification_version: 4
76
- summary: Sequel PostgreSQL and SQLite driver for the River Ruby gem.
76
+ summary: Sequel Postgres and SQLite driver for the River Ruby gem.
77
77
  test_files: []