|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "cases/helper_cockroachdb" |
| 4 | +require "support/connection_helper" # for #reset_connection |
| 5 | +require "support/copy_cat" |
| 6 | + |
| 7 | +class CockroachDBReferentialIntegrityTest < ActiveRecord::PostgreSQLTestCase |
| 8 | + include ConnectionHelper |
| 9 | + |
| 10 | + module ProgrammerMistake |
| 11 | + def execute_batch(sql, name = nil) |
| 12 | + raise ArgumentError, "something is not right." if name.match?(/referential integrity/) |
| 13 | + super |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + def setup |
| 18 | + @connection = ActiveRecord::Base.lease_connection |
| 19 | + end |
| 20 | + |
| 21 | + def teardown |
| 22 | + reset_connection |
| 23 | + end |
| 24 | + |
| 25 | + exclude_from_transactional_tests :test_only_catch_active_record_errors_others_bubble_up |
| 26 | + CopyCat.copy_methods(self, ::PostgreSQLReferentialIntegrityTest, :test_only_catch_active_record_errors_others_bubble_up) |
| 27 | + |
| 28 | + def test_should_reraise_invalid_foreign_key_exception_and_show_warning |
| 29 | + warning = capture(:stderr) do |
| 30 | + e = assert_raises(ActiveRecord::InvalidForeignKey) do |
| 31 | + @connection.disable_referential_integrity do |
| 32 | + @connection.execute("INSERT INTO authors (name, author_address_id) VALUES ('Mona Chollet', 42)") |
| 33 | + end |
| 34 | + end |
| 35 | + assert_match (/Key \(author_address_id\)=\(42\) is not present in table/), e.message |
| 36 | + end |
| 37 | + assert_match (/WARNING: Rails was not able to disable referential integrity/), warning |
| 38 | + assert_match (/autocommit_before_ddl/), warning |
| 39 | + end |
| 40 | + |
| 41 | + def test_no_warning_nor_error_with_autocommit_before_ddl |
| 42 | + @connection.execute("SET SESSION autocommit_before_ddl = 'on'") |
| 43 | + warning = capture(:stderr) do |
| 44 | + @connection.disable_referential_integrity do |
| 45 | + @connection.execute("INSERT INTO authors (name, author_address_id) VALUES ('Mona Chollet', 42)") |
| 46 | + @connection.truncate(:authors) |
| 47 | + end |
| 48 | + end |
| 49 | + assert_predicate warning, :blank?, "expected no warnings but got:\n#{warning}" |
| 50 | + end |
| 51 | + |
| 52 | + # def test_does_not_break_transactions |
| 53 | + # @connection.extend MissingSuperuserPrivileges |
| 54 | + |
| 55 | + # @connection.transaction do |
| 56 | + # @connection.disable_referential_integrity do |
| 57 | + # assert_transaction_is_not_broken |
| 58 | + # end |
| 59 | + # assert_transaction_is_not_broken |
| 60 | + # end |
| 61 | + # end |
| 62 | + |
| 63 | + # def test_does_not_break_nested_transactions |
| 64 | + # @connection.extend MissingSuperuserPrivileges |
| 65 | + |
| 66 | + # @connection.transaction do |
| 67 | + # @connection.transaction(requires_new: true) do |
| 68 | + # @connection.disable_referential_integrity do |
| 69 | + # assert_transaction_is_not_broken |
| 70 | + # end |
| 71 | + # end |
| 72 | + # assert_transaction_is_not_broken |
| 73 | + # end |
| 74 | + # end |
| 75 | + |
| 76 | + # def test_only_catch_active_record_errors_others_bubble_up |
| 77 | + # @connection.extend ProgrammerMistake |
| 78 | + |
| 79 | + # assert_raises ArgumentError do |
| 80 | + # @connection.disable_referential_integrity { } |
| 81 | + # end |
| 82 | + # end |
| 83 | + |
| 84 | + # def test_all_foreign_keys_valid_having_foreign_keys_in_multiple_schemas |
| 85 | + # @connection.execute <<~SQL |
| 86 | + # CREATE SCHEMA referential_integrity_test_schema; |
| 87 | + |
| 88 | + # CREATE TABLE referential_integrity_test_schema.nodes ( |
| 89 | + # id BIGSERIAL, |
| 90 | + # parent_id INT NOT NULL, |
| 91 | + # PRIMARY KEY(id), |
| 92 | + # CONSTRAINT fk_parent_node FOREIGN KEY(parent_id) |
| 93 | + # REFERENCES referential_integrity_test_schema.nodes(id) |
| 94 | + # ); |
| 95 | + # SQL |
| 96 | + |
| 97 | + # result = @connection.execute <<~SQL |
| 98 | + # SELECT count(*) AS count |
| 99 | + # FROM information_schema.table_constraints |
| 100 | + # WHERE constraint_schema = 'referential_integrity_test_schema' |
| 101 | + # AND constraint_type = 'FOREIGN KEY'; |
| 102 | + # SQL |
| 103 | + |
| 104 | + # assert_equal 1, result.first["count"], "referential_integrity_test_schema should have 1 foreign key" |
| 105 | + # @connection.check_all_foreign_keys_valid! |
| 106 | + # ensure |
| 107 | + # @connection.drop_schema "referential_integrity_test_schema", if_exists: true |
| 108 | + # end |
| 109 | + |
| 110 | + # private |
| 111 | + # def assert_transaction_is_not_broken |
| 112 | + # assert_equal 1, @connection.select_value("SELECT 1") |
| 113 | + # end |
| 114 | +end |
0 commit comments