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::ForeignKeyViolation` when 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 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/active_record/connection_adapters/cockroachdb/referential_integrity.rb', line 36 def disable_referential_integrity foreign_keys = all_foreign_keys statements = foreign_keys.map do |foreign_key| # We do not use the `#remove_foreign_key` method here because it # checks for foreign keys existance in the schema cache. This method # is performance critical and we know the foreign key exist. at = create_alter_table foreign_key.from_table at.drop_foreign_key foreign_key.name schema_creation.accept(at) end execute_batch(statements, "Disable referential integrity -> remove foreign keys") yield # 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 ActiveRecord::Base.table_name_prefix = "" ActiveRecord::Base.table_name_suffix = "" begin # Avoid having PG:DuplicateObject error if a test is ran in transaction. # TODO: verify that there is no cache issue related to running this (e.g: fk # still in cache but not in db) # # We avoid using `foreign_key_exists?` here because it checks the schema cache # for every key. This method is performance critical for the test suite, hence # we use the `#all_foreign_keys` method that only make one query to the database. already_inserted_foreign_keys = all_foreign_keys statements = foreign_keys.map do |foreign_key| next if already_inserted_foreign_keys.any? { |fk| fk.from_table == foreign_key.from_table && fk.[:name] == foreign_key.[:name] } = (foreign_key.from_table, foreign_key.to_table, foreign_key.) at = create_alter_table foreign_key.from_table at.add_foreign_key foreign_key.to_table, schema_creation.accept(at) end execute_batch(statements.compact, "Disable referential integrity -> add foreign keys") ensure ActiveRecord::Base.table_name_prefix = old_prefix ActiveRecord::Base.table_name_suffix = old_suffix end end |