From 044ca74ea9b849c27131d1a1df34add05b46dd74 Mon Sep 17 00:00:00 2001 From: Joe James <191664+the-undefined@users.noreply.github.com> Date: Fri, 19 Jun 2020 13:37:37 +0100 Subject: [PATCH] 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 --- .../connection_adapters/oracle_enhanced_adapter.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb b/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb index eca568235..749a189ec 100644 --- a/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb +++ b/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb @@ -212,6 +212,17 @@ class OracleEnhancedAdapter < AbstractAdapter cattr_accessor :default_sequence_start_value self.default_sequence_start_value = 1 + ## + # :singleton-method: + # By default trigger based sequences are not supported. + # Enable this if you wish to manage your own primary key triggers, then you can avoid an + # exception being raised when using autogenerated sequence names in your ActiveRecord models: + # + # self.sequence_name = :autogenerated + # + cattr_accessor :unmanaged_autogenerated_sequences + self.unmanaged_autogenerated_sequences = false + ## # :singleton-method: # By default, OracleEnhanced adapter will use longer 128 bytes identifier @@ -493,6 +504,7 @@ def discard! # called directly; used by ActiveRecord to get the next primary key value # when inserting a new database record (see #prefetch_primary_key?). def next_sequence_value(sequence_name) + return nil if unmanaged_autogenerated_sequences # if sequence_name is set to :autogenerated then it means that primary key will be populated by trigger raise ArgumentError.new "Trigger based primary key is not supported" if sequence_name == AUTOGENERATED_SEQUENCE_NAME # call directly connection method to avoid prepared statement which causes fetching of next sequence value twice