jade-sql 0.9.0 → 0.9.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a1a8c1496c50e9d5ddc41779759904608bd05c8b92a19e2c7a4be8434d0c0af
4
- data.tar.gz: 4dae8fec72bcf27bf3f0dca7de137173e0edc96cdeefe4423be258f04862ea4a
3
+ metadata.gz: 1d437d0d6f0732ff240bc1833dd724ab0ddb17f11f048f64484971d4986c189a
4
+ data.tar.gz: 7570a5c8616b4281d64535e577dd9c70a504c285197ede6bda39e07b1061e397
5
5
  SHA512:
6
- metadata.gz: b82de8f2b0d3b8286235a6387aed031c32df9303ea7adc7ae1413f402d328fa7da8a6bd9262ba188c3920b6a79409c4fd597cc4250ed356f5304cc4a19ca0b11
7
- data.tar.gz: d65bea1279860d61c7c1d321fdaf3c352797bf0470a124ade49769fc697c745053d417d535ee394329f6fea003857d391bcf98df9d199a01269d095a21bac219
6
+ metadata.gz: 88fd20a47981bd6735bee0d63fcb65d950c34da7b4deaa5f4d132f6d8342a2fe7c0f9db78628fbdfe043e001262e1751eec24528eef449234f0746076b791304
7
+ data.tar.gz: aab1dbb9f6a251c3b61827bd605cb89ac81ca783f13f421db0093831c294556a863e3321e75e39531cc204a25693be8434a7caf3535b978b0712e100cc6454ff
@@ -171,52 +171,51 @@ module JadeSql
171
171
  raw == "NULL" ? nil : raw
172
172
  end
173
173
 
174
- # ActiveRecord already tells these apart by SQLSTATE, and dropping that
175
- # into a message is what forced callers to match text.
174
+ # Read from Postgres rather than from ActiveRecord's classes for these,
175
+ # which lag it: `CheckViolation` arrived in Rails 8.
176
+ #
177
+ # https://www.postgresql.org/docs/current/errcodes-appendix.html
178
+ BY_SQLSTATE = {
179
+ '23505' => ->(e) { SqlErrors.unique_violation(constraint_name(e)) },
180
+ '23503' => ->(e) { SqlErrors.foreign_key_violation(constraint_name(e)) },
181
+ '23514' => ->(e) { SqlErrors.check_violation(constraint_name(e)) },
182
+ '23P01' => ->(e) { SqlErrors.exclusion_violation(constraint_name(e)) },
183
+ '23502' => ->(e) { SqlErrors.not_null_violation(column_name(e)) },
184
+ '40P01' => ->(_e) { SqlErrors.deadlock },
185
+ '40001' => ->(_e) { SqlErrors.serialization_failure },
186
+ '57014' => ->(_e) { SqlErrors.statement_timeout },
187
+ '55P03' => ->(_e) { SqlErrors.lock_timeout },
188
+ }.freeze
189
+
176
190
  def self.translate(error)
177
- case error
178
- when ::ActiveRecord::RecordNotUnique
179
- JadeSql::SqlErrors.unique_violation(constraint_name(error))
180
- when ::ActiveRecord::InvalidForeignKey
181
- JadeSql::SqlErrors.foreign_key_violation(constraint_name(error))
182
- when ::ActiveRecord::CheckViolation
183
- JadeSql::SqlErrors.check_violation(constraint_name(error))
184
- when ::ActiveRecord::ExclusionViolation
185
- JadeSql::SqlErrors.exclusion_violation(constraint_name(error))
186
- when ::ActiveRecord::NotNullViolation
187
- JadeSql::SqlErrors.not_null_violation(column_name(error))
188
- when ::ActiveRecord::Deadlocked
189
- JadeSql::SqlErrors.deadlock
190
- when ::ActiveRecord::SerializationFailure
191
- JadeSql::SqlErrors.serialization_failure
192
- when ::ActiveRecord::LockWaitTimeout
193
- JadeSql::SqlErrors.lock_timeout
194
- when ::ActiveRecord::QueryCanceled, ::ActiveRecord::StatementTimeout
195
- JadeSql::SqlErrors.statement_timeout
196
- else
197
- JadeSql::SqlErrors.db_error(error.message)
198
- end
191
+ BY_SQLSTATE[sqlstate(error)]
192
+ &.call(error) || SqlErrors.db_error(error.message)
193
+ end
194
+
195
+ # Nil for anything that did not come back from Postgres.
196
+ def self.sqlstate(error)
197
+ diagnostic(error, ::PG::Result::PG_DIAG_SQLSTATE)
199
198
  end
200
199
 
201
200
  # The constraint/index name behind a violation, so callers can route by
202
201
  # which one it was. PG reports it in the error's diagnostics; other
203
202
  # adapters (or a missing name) fall back to "".
204
203
  def self.constraint_name(error)
205
- cause = error.cause
206
- return "" unless defined?(::PG::Result) && cause.respond_to?(:result) && cause.result
207
-
208
- cause.result.error_field(::PG::Result::PG_DIAG_CONSTRAINT_NAME) || ""
209
- rescue StandardError
210
- ""
204
+ diagnostic(error, ::PG::Result::PG_DIAG_CONSTRAINT_NAME) || ""
211
205
  end
212
206
 
213
207
  def self.column_name(error)
208
+ diagnostic(error, ::PG::Result::PG_DIAG_COLUMN_NAME) || ""
209
+ end
210
+
211
+
212
+ def self.diagnostic(error, field)
214
213
  cause = error.cause
215
- return "" unless defined?(::PG::Result) && cause.respond_to?(:result) && cause.result
214
+ return nil unless defined?(::PG::Result) && cause.respond_to?(:result) && cause.result
216
215
 
217
- cause.result.error_field(::PG::Result::PG_DIAG_COLUMN_NAME) || ""
216
+ cause.result.error_field(field)
218
217
  rescue StandardError
219
- ""
218
+ nil
220
219
  end
221
220
 
222
221
  # Sql.Write.timestamped emits "$JADE_SQL_NOW$" where created_at /
@@ -1,3 +1,3 @@
1
1
  module JadeSql
2
- VERSION = '0.9.0'
2
+ VERSION = '0.9.1'
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jade-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - agustin
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2026-09-17 00:00:00.000000000 Z
10
+ date: 2026-09-18 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: jade-lang