Skip to content

Commit eb2e63d

Browse files
the-undefinedmatthewtusker
authored andcommitted
Allow unmanaged autogenerated sequence names
To still support primary keys through db triggers, without adding back in support for generating and maintaining them with migrations, add an configuration boolen: unmanaged_autogenerated_sequences = false # default Which can be used in an initializer file. When set to `true` there will not be an exception raised when the sequence name is defined in the model with the previously supported value: self.sequence_name = :autogenerated This option allows for dbs managed outside of the application to use triggers for primary keys. Without the `autogenerated` option an error is raised when inserting the record: ORA-02289: sequence does not exist
1 parent cb82744 commit eb2e63d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib/active_record/connection_adapters/oracle_enhanced_adapter.rb

+13
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@ class OracleEnhancedAdapter < AbstractAdapter
205205
cattr_accessor :default_sequence_start_value
206206
self.default_sequence_start_value = 1
207207

208+
##
209+
# :singleton-method:
210+
# By default trigger based sequences are not supported.
211+
# Enable this if you wish to manage your own primary key triggers, then you can avoid an
212+
# exception being raised when using autogenerated sequence names in your ActiveRecord models:
213+
#
214+
# self.sequence_name = :autogenerated
215+
#
216+
cattr_accessor :unmanaged_autogenerated_sequences
217+
self.unmanaged_autogenerated_sequences = false
218+
208219
##
209220
# :singleton-method:
210221
# By default, OracleEnhanced adapter will use longer 128 bytes identifier
@@ -475,8 +486,10 @@ def discard!
475486
# called directly; used by ActiveRecord to get the next primary key value
476487
# when inserting a new database record (see #prefetch_primary_key?).
477488
def next_sequence_value(sequence_name)
489+
return nil if unmanaged_autogenerated_sequences
478490
# if sequence_name is set to :autogenerated then it means that primary key will be populated by trigger
479491
raise ArgumentError "Trigger based primary key is not supported" if sequence_name == AUTOGENERATED_SEQUENCE_NAME
492+
480493
# call directly connection method to avoid prepared statement which causes fetching of next sequence value twice
481494
select_value(<<~SQL.squish, "SCHEMA")
482495
SELECT #{quote_table_name(sequence_name)}.NEXTVAL FROM dual

0 commit comments

Comments
 (0)