Repository: rails/rails
File: activerecord/test/cases/migration/compatibility_test.rb (Ruby)

Code before:
          ActiveRecord::Migrator.new(:up, [migration], @schema_migration, @internal_metadata).migrate

          foreign_keys = Testing.lease_connection.foreign_keys("sub_testings")
          assert_equal 1, foreign_keys.size
          assert_equal :immediate, foreign_keys.first.deferrable
        ensure
          connection.drop_table(:sub_testings, if_exists: true)
          ActiveRecord::Base.clear_cache!
        end
      end

      if current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
        def test_change_column_on_7_0
          migration = Class.new(ActiveRecord::Migration[7.0]) do
            def up
              execute("ALTER TABLE testings CONVERT TO CHARACTER SET utf32")
              change_column(:testings, :foo, "varchar(255) CHARACTER SET ascii")
            end
          end
          assert_nothing_raised do
            ActiveRecord::Migrator.new(:up, [migration], @schema_migration, @internal_metadata).migrate
          end
        end
      end

      if current_adapter?(:SQLite3Adapter)
        def test_remove_foreign_key_on_8_1
          connection.create_table(:sub_testings) do |t|
            t.references :testing, foreign_key: true, type: :bigint
            t.references :experiment, foreign_key: { to_table: :testings }, type: :bigint
          end

          migration = Class.new(ActiveRecord::Migration[8.1]) do
            def migrate(x)
              change_table(:sub_testings) do |t|
                t.remove_foreign_key :testings
                t.remove_foreign_key :testings, column: :experiment_id
              end
            end
          end

          assert_raise(StandardError, match: /Table 'sub_testings' has no foreign key for testings$/) {
            ActiveRecord::Migrator.new(:up, [migration], @schema_migration, @internal_metadata).migrate
          }

          foreign_keys = @connection.foreign_keys("sub_testings")
          assert_equal 2, foreign_keys.size
        ensure
          connection.drop_table(:sub_testings, if_exists: true)
          ActiveRecord::Base.clear_cache!
        end

Review comment (suggestion):
If you will fix for sqlite too, I would suggest instead of these `if`s and removing blocks of code in the future PRs, and calling `skip if sqlite adapter` inline in tests. Then only a single line will be removed.

 

Code after:
          ActiveRecord::Migrator.new(:up, [migration], @schema_migration, @internal_metadata).migrate

          foreign_keys = Testing.lease_connection.foreign_keys("sub_testings")
          assert_equal 1, foreign_keys.size
          assert_equal :immediate, foreign_keys.first.deferrable
        ensure
          connection.drop_table(:sub_testings, if_exists: true)
          ActiveRecord::Base.clear_cache!
        end
      end

      if current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
        def test_change_column_on_7_0
          migration = Class.new(ActiveRecord::Migration[7.0]) do
            def up
              execute("ALTER TABLE testings CONVERT TO CHARACTER SET utf32")
              change_column(:testings, :foo, "varchar(255) CHARACTER SET ascii")
            end
          end
          assert_nothing_raised do
            ActiveRecord::Migrator.new(:up, [migration], @schema_migration, @internal_metadata).migrate
          end
        end
      end

      def test_remove_foreign_key_on_8_1
        connection.create_table(:sub_testings) do |t|
          t.references :testing, foreign_key: true, type: :bigint
          t.references :experiment, foreign_key: { to_table: :testings }, type: :bigint
        end

        migration = Class.new(ActiveRecord::Migration[8.1]) do
          def up
            change_table(:sub_testings) do |t|
              t.remove_foreign_key :testings
              t.remove_foreign_key :testings, column: :experiment_id
            end
          end
        end

        ActiveRecord::Migrator.new(:up, [migration], @schema_migration, @internal_metadata).migrate

        foreign_keys = @connection.foreign_keys("sub_testings")
        assert_equal 0, foreign_keys.size
      ensure
        connection.drop_table(:sub_testings, if_exists: true)
        ActiveRecord::Base.clear_cache!
      end

      def test_remove_foreign_key_on_8_0
        connection.create_table(:sub_testings) do |t|