Sha256: 0f78fea6b6058b85cfe7f90ad502bf86961ce46607b40fd0e1bd051574f5100a

Contents?: true

Size: 1.89 KB

Versions: 8

Compression:

Stored size: 1.89 KB

Contents

require "cases/helper"

class SchemaMigrationsTest < ActiveRecord::Mysql2TestCase
  def test_renaming_index_on_foreign_key
    connection.add_index "engines", "car_id"
    connection.add_foreign_key :engines, :cars, name: "fk_engines_cars"

    connection.rename_index("engines", "index_engines_on_car_id", "idx_renamed")
    assert_equal ["idx_renamed"], connection.indexes("engines").map(&:name)
  ensure
    connection.remove_foreign_key :engines, name: "fk_engines_cars"
  end

  def test_initializes_schema_migrations_for_encoding_utf8mb4
    with_encoding_utf8mb4 do
      table_name = ActiveRecord::SchemaMigration.table_name
      connection.drop_table table_name, if_exists: true

      connection.initialize_schema_migrations_table

      assert connection.column_exists?(table_name, :version, :string, collation: 'utf8_general_ci')
    end
  end

  def test_initializes_internal_metadata_for_encoding_utf8mb4
    with_encoding_utf8mb4 do
      table_name = ActiveRecord::InternalMetadata.table_name
      connection.drop_table table_name, if_exists: true

      connection.initialize_internal_metadata_table

      assert connection.column_exists?(table_name, :key, :string, collation: 'utf8_general_ci')
    end
  end

  private

  def with_encoding_utf8mb4
    database_name = connection.current_database
    database_info = connection.select_one("SELECT * FROM information_schema.schemata WHERE schema_name = '#{database_name}'")

    original_charset = database_info["DEFAULT_CHARACTER_SET_NAME"]
    original_collation = database_info["DEFAULT_COLLATION_NAME"]

    execute("ALTER DATABASE #{database_name} DEFAULT CHARACTER SET utf8mb4")

    yield
  ensure
    execute("ALTER DATABASE #{database_name} DEFAULT CHARACTER SET #{original_charset} COLLATE #{original_collation}")
  end

  def connection
    @connection ||= ActiveRecord::Base.connection
  end

  def execute(sql)
    connection.execute(sql)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ibm_db-5.2.0 test/cases/adapters/mysql2/schema_migrations_test.rb
ibm_db-5.1.0 test/cases/adapters/mysql2/schema_migrations_test.rb
ibm_db-5.0.5 test/cases/adapters/mysql2/schema_migrations_test.rb
ibm_db-5.0.4 test/cases/adapters/mysql2/schema_migrations_test.rb
ibm_db-5.0.3 test/cases/adapters/mysql2/schema_migrations_test.rb
ibm_db-5.0.2 test/cases/adapters/mysql2/schema_migrations_test.rb
ibm_db-4.0.0-x86-mingw32 test/cases/adapters/mysql2/schema_migrations_test.rb
ibm_db-4.0.0 test/cases/adapters/mysql2/schema_migrations_test.rb