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

Code before:
            end
            add_foreign_key :classes, :schools, validate: true
          end
        end

        def test_add_foreign_key_with_prefix
          ActiveRecord::Base.table_name_prefix = "p_"
          migration = CreateSchoolsAndClassesMigration.new
          silence_stream($stdout) { migration.migrate(:up) }
          assert_equal 1, @connection.foreign_keys("p_classes").size
        ensure
          silence_stream($stdout) { migration.migrate(:down) }
          ActiveRecord::Base.table_name_prefix = nil
        end

        def test_add_foreign_key_with_suffix
          ActiveRecord::Base.table_name_suffix = "_s"
          migration = CreateSchoolsAndClassesMigration.new
          silence_stream($stdout) { migration.migrate(:up) }
          assert_equal 1, @connection.foreign_keys("classes_s").size
        ensure
          silence_stream($stdout) { migration.migrate(:down) }
          ActiveRecord::Base.table_name_suffix = nil
        end

        def test_remove_foreign_key_with_prefix
          ActiveRecord::Base.table_name_prefix = "p_"

          migration = CreateSchoolsAndClassesMigration.new
          silence_stream($stdout) { migration.migrate(:up) }
          assert_equal 1, @connection.foreign_keys("p_classes").size

          @connection.remove_foreign_key :p_classes, to_table: :p_schools

          assert_empty @connection.foreign_keys("p_classes")
        ensure
          @connection.drop_table :p_classes
          @connection.drop_table :p_schools
          ActiveRecord::Base.table_name_prefix = nil
        end

        def test_remove_foreign_key_with_suffix
          ActiveRecord::Base.table_name_suffix = "_s"

          migration = CreateSchoolsAndClassesMigration.new
          silence_stream($stdout) { migration.migrate(:up) }
          assert_equal 1, @connection.foreign_keys("classes_s").size

          @connection.remove_foreign_key :classes_s, to_table: :schools_s

          assert_empty @connection.foreign_keys("classes_s")

Review comment (bug):
Before this change, this test would fail like this only for sqlite:

```
ActiveRecord::Migration::ForeignKeyTest#test_remove_foreign_key_with_suffix:
ArgumentError: Table 'classes_s' has no foreign key for {}
    lib/active_record/connection_adapters/sqlite3/schema_statements.rb:81:in 'ActiveRecord::ConnectionAdapters::SQLite3::SchemaStatements#remove_foreign_key'
    test/cases/migration/foreign_key_test.rb:813:in 'ActiveRecord::Migration::ForeignKeyTest#test_remove_foreign_key_with_suffix'
```

Code after:
            end
            add_foreign_key :classes, :schools, validate: true
          end
        end

        def test_add_foreign_key_with_prefix
          ActiveRecord::Base.table_name_prefix = "p_"
          migration = CreateSchoolsAndClassesMigration.new
          silence_stream($stdout) { migration.migrate(:up) }
          assert_equal 1, @connection.foreign_keys("p_classes").size
        ensure
          silence_stream($stdout) { migration.migrate(:down) }
          ActiveRecord::Base.table_name_prefix = nil
        end

        def test_add_foreign_key_with_suffix
          ActiveRecord::Base.table_name_suffix = "_s"
          migration = CreateSchoolsAndClassesMigration.new
          silence_stream($stdout) { migration.migrate(:up) }
          assert_equal 1, @connection.foreign_keys("classes_s").size
        ensure
          silence_stream($stdout) { migration.migrate(:down) }
          ActiveRecord::Base.table_name_suffix = nil
        end

        def test_remove_foreign_key_with_prefix
          ActiveRecord::Base.table_name_prefix = "p_"

          migration = CreateSchoolsAndClassesMigration.new
          silence_stream($stdout) { migration.migrate(:up) }
          assert_equal 1, @connection.foreign_keys("p_classes").size

          @connection.remove_foreign_key :p_classes

          assert_empty @connection.foreign_keys("p_classes")
        ensure
          @connection.drop_table :p_classes
          @connection.drop_table :p_schools
          ActiveRecord::Base.table_name_prefix = nil
        end

        def test_remove_foreign_key_with_suffix
          ActiveRecord::Base.table_name_suffix = "_s"

          migration = CreateSchoolsAndClassesMigration.new
          silence_stream($stdout) { migration.migrate(:up) }
          assert_equal 1, @connection.foreign_keys("classes_s").size

          @connection.remove_foreign_key :classes_s

          assert_empty @connection.foreign_keys("classes_s")