activerecord-redshift-adapter 8.0.0.beta1 → 8.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d981a65a30765fe36e5e58616007f33103f238596de10188107d5e7bbe677093
4
- data.tar.gz: 78f40be0d77ab87f6e4b3b1f6857f84aae35d0b5184a6fe1b320b14c080bbce9
3
+ metadata.gz: 692d2587480e3e8474c03b2f00acbf455a7e59c9f2e95c746b9fb67ee7edc8b3
4
+ data.tar.gz: e4e9f46485d980b6e52a052f60b9c01f6758e374b7be1beba575e811705a6dc8
5
5
  SHA512:
6
- metadata.gz: f8a9a3801675594bc2a3400c3a4d4657247614b250558c7c169c1f720c0b8f8ce333b29e6a1e621bd3557539c69c8a5ecb160d059049e80ab24d3c531350aecb
7
- data.tar.gz: f75478c7a7df07526ad528e562978bae1918b8146562d42bab6fcf45c8f8eff749b67a3efa5dc08530285642d5ed1003247c2fa64bf3ce60f3482b9824a79580
6
+ metadata.gz: 498a1d46c28dbe7432209ca5632756878709442396dd048b5785c4c06ef364113f03e075ffe05c0ce83f483383ae130f082011e24e983f78bc586e7a256fe1a3
7
+ data.tar.gz: 7f3128a718ed1ae5069912c14a339bb9b2c15f4253fee0f19d6052e3d051ae59660907d957d5909f5d11d819a643569014de15a2d0b162ce01778fc8c171a848
data/README.md CHANGED
@@ -11,7 +11,7 @@ Thanks to the auhors.
11
11
  Usage
12
12
  -------------------
13
13
 
14
- For Rails, write following in Gemfile:
14
+ For Rails, add the following in the Gemfile:
15
15
 
16
16
  ```ruby
17
17
  gem 'activerecord-redshift-adapter'
@@ -30,7 +30,8 @@ development:
30
30
  encoding: utf8
31
31
  ```
32
32
 
33
- or use it directly in the URL when establishing a connection:
33
+ or use it directly in the connection or `DATABASE_URL`:
34
+
34
35
  ```ruby
35
36
  class SomeModel < ApplicationRecord
36
37
  establish_connection('redshift://username:password@host/database')
@@ -207,6 +207,11 @@ module ActiveRecord
207
207
  end
208
208
  end
209
209
 
210
+ # Returns the current ID of a table's sequence.
211
+ def last_insert_id_result(sequence_name)
212
+ internal_exec_query("SELECT currval(#{quote(sequence_name)})", "SQL")
213
+ end
214
+
210
215
  # Begins a transaction.
211
216
  def begin_db_transaction
212
217
  execute 'BEGIN'
@@ -220,17 +220,17 @@ module ActiveRecord
220
220
  end
221
221
 
222
222
  # Returns the sequence name for a table's primary key or some other specified key.
223
- def default_sequence_name(table_name, pk = nil) # :nodoc:
224
- result = serial_sequence(table_name, pk || 'id')
223
+ def default_sequence_name(table_name, pk = "id") # :nodoc:
224
+ result = serial_sequence(table_name, pk)
225
225
  return nil unless result
226
226
 
227
227
  Utils.extract_schema_qualified_name(result).to_s
228
228
  rescue ActiveRecord::StatementInvalid
229
- Redshift::Name.new(nil, "#{table_name}_#{pk || 'id'}_seq").to_s
229
+ Redshift::Name.new(nil, "#{table_name}_#{pk}_seq").to_s
230
230
  end
231
231
 
232
232
  def serial_sequence(table, column)
233
- select_value("SELECT pg_get_serial_sequence('#{table}', '#{column}')", 'SCHEMA')
233
+ select_value("SELECT pg_get_serial_sequence(#{quote(table)}, #{quote(column)})", 'SCHEMA')
234
234
  end
235
235
 
236
236
  def set_pk_sequence!(table, value); end
@@ -330,11 +330,8 @@ module ActiveRecord
330
330
  def translate_exception(exception, message:, sql:, binds:)
331
331
  return exception unless exception.respond_to?(:result)
332
332
 
333
- case exception.message
334
- when /duplicate key value violates unique constraint/
335
- RecordNotUnique.new(message, exception)
336
- when /violates foreign key constraint/
337
- InvalidForeignKey.new(message, exception)
333
+ if exception.is_a?(PG::DuplicateDatabase)
334
+ DatabaseAlreadyExists.new(message, sql: sql, binds: binds)
338
335
  else
339
336
  super
340
337
  end
@@ -619,10 +616,6 @@ module ActiveRecord
619
616
  end
620
617
  end
621
618
 
622
- def last_insert_id_result(sequence_name) # :nodoc:
623
- exec_query("SELECT currval('#{sequence_name}')", 'SQL')
624
- end
625
-
626
619
  # Returns the list of a table's column names, data types, and default values.
627
620
  #
628
621
  # The underlying query is roughly:
@@ -654,8 +647,9 @@ module ActiveRecord
654
647
  end
655
648
 
656
649
  def extract_table_ref_from_insert_sql(sql)
657
- sql[/into\s("[A-Za-z0-9_."\[\]\s]+"|[A-Za-z0-9_."\[\]]+)\s*/im]
658
- Regexp.last_match(1)&.strip
650
+ if sql =~ /into\s("[A-Za-z0-9_."\[\]\s]+"|[A-Za-z0-9_."\[\]]+)\s*/im
651
+ $1.delete('"').strip
652
+ end
659
653
  end
660
654
 
661
655
  def arel_visitor
@@ -220,17 +220,17 @@ module ActiveRecord
220
220
  end
221
221
 
222
222
  # Returns the sequence name for a table's primary key or some other specified key.
223
- def default_sequence_name(table_name, pk = nil) # :nodoc:
224
- result = serial_sequence(table_name, pk || 'id')
223
+ def default_sequence_name(table_name, pk = 'id') # :nodoc:
224
+ result = serial_sequence(table_name, pk)
225
225
  return nil unless result
226
226
 
227
227
  Utils.extract_schema_qualified_name(result).to_s
228
228
  rescue ActiveRecord::StatementInvalid
229
- Redshift::Name.new(nil, "#{table_name}_#{pk || 'id'}_seq").to_s
229
+ Redshift::Name.new(nil, "#{table_name}_#{pk}_seq").to_s
230
230
  end
231
231
 
232
232
  def serial_sequence(table, column)
233
- select_value("SELECT pg_get_serial_sequence('#{table}', '#{column}')", 'SCHEMA')
233
+ select_value("SELECT pg_get_serial_sequence(#{quote(table)}, #{quote(column)})", 'SCHEMA')
234
234
  end
235
235
 
236
236
  def set_pk_sequence!(table, value); end
@@ -353,11 +353,8 @@ module ActiveRecord
353
353
  def translate_exception(exception, message:, sql:, binds:)
354
354
  return exception unless exception.respond_to?(:result)
355
355
 
356
- case exception.message
357
- when /duplicate key value violates unique constraint/
358
- RecordNotUnique.new(message, exception)
359
- when /violates foreign key constraint/
360
- InvalidForeignKey.new(message, exception)
356
+ if exception.is_a?(PG::DuplicateDatabase)
357
+ DatabaseAlreadyExists.new(message, sql: sql, binds: binds)
361
358
  else
362
359
  super
363
360
  end
@@ -692,11 +689,6 @@ module ActiveRecord
692
689
  end
693
690
  end
694
691
 
695
- def last_insert_id_result(sequence_name)
696
- # :nodoc:
697
- exec_query("SELECT currval('#{sequence_name}')", 'SQL')
698
- end
699
-
700
692
  # Returns the list of a table's column names, data types, and default values.
701
693
  #
702
694
  # The underlying query is roughly:
@@ -728,11 +720,6 @@ module ActiveRecord
728
720
  END_SQL
729
721
  end
730
722
 
731
- def extract_table_ref_from_insert_sql(sql)
732
- sql[/into\s("[A-Za-z0-9_."\[\]\s]+"|[A-Za-z0-9_."\[\]]+)\s*/im]
733
- Regexp.last_match(1)&.strip
734
- end
735
-
736
723
  def arel_visitor
737
724
  Arel::Visitors::PostgreSQL.new(self)
738
725
  end
@@ -220,17 +220,17 @@ module ActiveRecord
220
220
  end
221
221
 
222
222
  # Returns the sequence name for a table's primary key or some other specified key.
223
- def default_sequence_name(table_name, pk = nil) # :nodoc:
224
- result = serial_sequence(table_name, pk || 'id')
223
+ def default_sequence_name(table_name, pk = 'id') # :nodoc:
224
+ result = serial_sequence(table_name, pk)
225
225
  return nil unless result
226
226
 
227
227
  Utils.extract_schema_qualified_name(result).to_s
228
228
  rescue ActiveRecord::StatementInvalid
229
- Redshift::Name.new(nil, "#{table_name}_#{pk || 'id'}_seq").to_s
229
+ Redshift::Name.new(nil, "#{table_name}_#{pk}_seq").to_s
230
230
  end
231
231
 
232
232
  def serial_sequence(table, column)
233
- select_value("SELECT pg_get_serial_sequence('#{table}', '#{column}')", 'SCHEMA')
233
+ select_value("SELECT pg_get_serial_sequence(#{quote(table)}, #{quote(column)})".tap { puts _1 }, 'SCHEMA')
234
234
  end
235
235
 
236
236
  def set_pk_sequence!(table, value); end
@@ -353,11 +353,8 @@ module ActiveRecord
353
353
  def translate_exception(exception, message:, sql:, binds:)
354
354
  return exception unless exception.respond_to?(:result)
355
355
 
356
- case exception.message
357
- when /duplicate key value violates unique constraint/
358
- RecordNotUnique.new(message, exception)
359
- when /violates foreign key constraint/
360
- InvalidForeignKey.new(message, exception)
356
+ if exception.is_a?(PG::DuplicateDatabase)
357
+ DatabaseAlreadyExists.new(message, sql: sql, binds: binds)
361
358
  else
362
359
  super
363
360
  end
@@ -692,11 +689,6 @@ module ActiveRecord
692
689
  end
693
690
  end
694
691
 
695
- def last_insert_id_result(sequence_name)
696
- # :nodoc:
697
- exec_query("SELECT currval('#{sequence_name}')", 'SQL')
698
- end
699
-
700
692
  # Returns the list of a table's column names, data types, and default values.
701
693
  #
702
694
  # The underlying query is roughly:
@@ -728,11 +720,6 @@ module ActiveRecord
728
720
  END_SQL
729
721
  end
730
722
 
731
- def extract_table_ref_from_insert_sql(sql)
732
- sql[/into\s("[A-Za-z0-9_."\[\]\s]+"|[A-Za-z0-9_."\[\]]+)\s*/im]
733
- Regexp.last_match(1)&.strip
734
- end
735
-
736
723
  def arel_visitor
737
724
  Arel::Visitors::PostgreSQL.new(self)
738
725
  end
@@ -220,17 +220,17 @@ module ActiveRecord
220
220
  end
221
221
 
222
222
  # Returns the sequence name for a table's primary key or some other specified key.
223
- def default_sequence_name(table_name, pk = nil) # :nodoc:
224
- result = serial_sequence(table_name, pk || 'id')
223
+ def default_sequence_name(table_name, pk = 'id') # :nodoc:
224
+ result = serial_sequence(table_name, pk)
225
225
  return nil unless result
226
226
 
227
227
  Utils.extract_schema_qualified_name(result).to_s
228
228
  rescue ActiveRecord::StatementInvalid
229
- Redshift::Name.new(nil, "#{table_name}_#{pk || 'id'}_seq").to_s
229
+ Redshift::Name.new(nil, "#{table_name}_#{pk}_seq").to_s
230
230
  end
231
231
 
232
232
  def serial_sequence(table, column)
233
- select_value("SELECT pg_get_serial_sequence('#{table}', '#{column}')", 'SCHEMA')
233
+ select_value("SELECT pg_get_serial_sequence(#{quote(table)}, #{quote(column)})".tap { puts _1 }, 'SCHEMA')
234
234
  end
235
235
 
236
236
  def set_pk_sequence!(table, value); end
@@ -353,11 +353,8 @@ module ActiveRecord
353
353
  def translate_exception(exception, message:, sql:, binds:)
354
354
  return exception unless exception.respond_to?(:result)
355
355
 
356
- case exception.message
357
- when /duplicate key value violates unique constraint/
358
- RecordNotUnique.new(message, exception)
359
- when /violates foreign key constraint/
360
- InvalidForeignKey.new(message, exception)
356
+ if exception.is_a?(PG::DuplicateDatabase)
357
+ DatabaseAlreadyExists.new(message, sql: sql, binds: binds)
361
358
  else
362
359
  super
363
360
  end
@@ -691,11 +688,6 @@ module ActiveRecord
691
688
  end
692
689
  end
693
690
 
694
- def last_insert_id_result(sequence_name)
695
- # :nodoc:
696
- exec_query("SELECT currval('#{sequence_name}')", 'SQL')
697
- end
698
-
699
691
  # Returns the list of a table's column names, data types, and default values.
700
692
  #
701
693
  # The underlying query is roughly:
@@ -727,11 +719,6 @@ module ActiveRecord
727
719
  END_SQL
728
720
  end
729
721
 
730
- def extract_table_ref_from_insert_sql(sql)
731
- sql[/into\s("[A-Za-z0-9_."\[\]\s]+"|[A-Za-z0-9_."\[\]]+)\s*/im]
732
- Regexp.last_match(1)&.strip
733
- end
734
-
735
722
  def arel_visitor
736
723
  Arel::Visitors::PostgreSQL.new(self)
737
724
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-redshift-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.0.0.beta1
4
+ version: 8.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janusz Mordarski
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2024-11-25 00:00:00.000000000 Z
17
+ date: 2024-12-05 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: activerecord
@@ -152,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  - !ruby/object:Gem::Version
153
153
  version: '0'
154
154
  requirements: []
155
- rubygems_version: 3.5.22
155
+ rubygems_version: 3.5.23
156
156
  signing_key:
157
157
  specification_version: 4
158
158
  summary: Amazon Redshift adapter for ActiveRecord