Module: ActiveRecord::ConnectionAdapters::CockroachDB::ReferentialIntegrity
- Included in:
- ActiveRecord::ConnectionAdapters::CockroachDBAdapter
- Defined in:
- lib/active_record/connection_adapters/cockroachdb/referential_integrity.rb
Instance Method Summary collapse
-
#check_all_foreign_keys_valid! ⇒ Object
CockroachDB will raise a
PG::ForeignKeyViolationwhen re-enabling referential integrity (e.g: adding a foreign key with invalid data raises). - #disable_referential_integrity ⇒ Object
Instance Method Details
#check_all_foreign_keys_valid! ⇒ Object
CockroachDB will raise a PG::ForeignKeyViolation when re-enabling
referential integrity (e.g: adding a foreign key with invalid data
raises).
So foreign keys should always be valid for that matter.
32 33 34 |
# File 'lib/active_record/connection_adapters/cockroachdb/referential_integrity.rb', line 32 def check_all_foreign_keys_valid! true end |
#disable_referential_integrity ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/active_record/connection_adapters/cockroachdb/referential_integrity.rb', line 36 def disable_referential_integrity if transaction_open? && query_value("SHOW autocommit_before_ddl") == "off" begin yield rescue ActiveRecord::InvalidForeignKey => e warn <<-WARNING WARNING: Rails was not able to disable referential integrity. This is due to CockroachDB's need of committing transactions before a schema change occurs. To bypass this, you can set `autocommit_before_ddl: "on"` in your database configuration. WARNING raise e end else foreign_keys = all_foreign_keys remove_foreign_keys(foreign_keys) # Prefixes and suffixes are added in add_foreign_key # in AR7+ so we need to temporarily disable them here, # otherwise prefixes/suffixes will be erroneously added. old_prefix = ActiveRecord::Base.table_name_prefix old_suffix = ActiveRecord::Base.table_name_suffix begin yield ensure ActiveRecord::Base.table_name_prefix = "" ActiveRecord::Base.table_name_suffix = "" add_foreign_keys(foreign_keys) # Never raises. ActiveRecord::Base.table_name_prefix = old_prefix if defined?(old_prefix) ActiveRecord::Base.table_name_suffix = old_suffix if defined?(old_suffix) end end end |