Use included hook to properly call adapter helpers from a module

This commit is contained in:
Akira Matsuda 2023-01-01 18:02:57 +09:00
parent 2ead5132ea
commit 4ba87aae1f
No known key found for this signature in database

@ -856,168 +856,58 @@ def migration_class
end
module LegacyPrimaryKeyTestCases
extend ActiveSupport::Concern
include SchemaDumpingHelper
class LegacyPrimaryKey < ActiveRecord::Base
end
def setup
@migration = nil
@verbose_was = ActiveRecord::Migration.verbose
ActiveRecord::Migration.verbose = false
end
def teardown
@migration.migrate(:down) if @migration
ActiveRecord::Migration.verbose = @verbose_was
ActiveRecord::Base.connection.schema_migration.delete_all_versions rescue nil
LegacyPrimaryKey.reset_column_information
end
def test_legacy_primary_key_should_be_auto_incremented
@migration = Class.new(migration_class) {
def change
create_table :legacy_primary_keys do |t|
t.references :legacy_ref
end
end
}.new
@migration.migrate(:up)
assert_legacy_primary_key
legacy_ref = LegacyPrimaryKey.columns_hash["legacy_ref_id"]
assert_not_predicate legacy_ref, :bigint?
record1 = LegacyPrimaryKey.create!
assert_not_nil record1.id
record1.destroy
record2 = LegacyPrimaryKey.create!
assert_not_nil record2.id
assert_operator record2.id, :>, record1.id
end
def test_legacy_integer_primary_key_should_not_be_auto_incremented
skip if current_adapter?(:SQLite3Adapter)
@migration = Class.new(migration_class) {
def change
create_table :legacy_primary_keys, id: :integer do |t|
end
end
}.new
@migration.migrate(:up)
assert_raises(ActiveRecord::NotNullViolation) do
LegacyPrimaryKey.create!
included do
def setup
@migration = nil
@verbose_was = ActiveRecord::Migration.verbose
ActiveRecord::Migration.verbose = false
end
schema = dump_table_schema "legacy_primary_keys"
assert_match %r{create_table "legacy_primary_keys", id: :integer, default: nil}, schema
end
def teardown
@migration.migrate(:down) if @migration
ActiveRecord::Migration.verbose = @verbose_was
ActiveRecord::Base.connection.schema_migration.delete_all_versions rescue nil
LegacyPrimaryKey.reset_column_information
end
def test_legacy_primary_key_in_create_table_should_be_integer
@migration = Class.new(migration_class) {
def change
create_table :legacy_primary_keys, id: false do |t|
t.primary_key :id
end
end
}.new
@migration.migrate(:up)
assert_legacy_primary_key
end
def test_legacy_primary_key_in_change_table_should_be_integer
@migration = Class.new(migration_class) {
def change
create_table :legacy_primary_keys, id: false do |t|
t.integer :dummy
end
change_table :legacy_primary_keys do |t|
t.primary_key :id
end
end
}.new
@migration.migrate(:up)
assert_legacy_primary_key
end
def test_add_column_with_legacy_primary_key_should_be_integer
@migration = Class.new(migration_class) {
def change
create_table :legacy_primary_keys, id: false do |t|
t.integer :dummy
end
add_column :legacy_primary_keys, :id, :primary_key
end
}.new
@migration.migrate(:up)
assert_legacy_primary_key
end
def test_legacy_join_table_foreign_keys_should_be_integer
@migration = Class.new(migration_class) {
def change
create_join_table :apples, :bananas do |t|
end
end
}.new
@migration.migrate(:up)
schema = dump_table_schema "apples_bananas"
assert_match %r{integer "apple_id", null: false}, schema
assert_match %r{integer "banana_id", null: false}, schema
end
def test_legacy_join_table_column_options_should_be_overwritten
@migration = Class.new(migration_class) {
def change
create_join_table :apples, :bananas, column_options: { type: :bigint } do |t|
end
end
}.new
@migration.migrate(:up)
schema = dump_table_schema "apples_bananas"
assert_match %r{bigint "apple_id", null: false}, schema
assert_match %r{bigint "banana_id", null: false}, schema
end
if current_adapter?(:Mysql2Adapter)
def test_legacy_bigint_primary_key_should_be_auto_incremented
def test_legacy_primary_key_should_be_auto_incremented
@migration = Class.new(migration_class) {
def change
create_table :legacy_primary_keys, id: :bigint
create_table :legacy_primary_keys do |t|
t.references :legacy_ref
end
end
}.new
@migration.migrate(:up)
legacy_pk = LegacyPrimaryKey.columns_hash["id"]
assert_predicate legacy_pk, :bigint?
assert_predicate legacy_pk, :auto_increment?
assert_legacy_primary_key
schema = dump_table_schema "legacy_primary_keys"
assert_match %r{create_table "legacy_primary_keys", (?!id: :bigint, default: nil)}, schema
legacy_ref = LegacyPrimaryKey.columns_hash["legacy_ref_id"]
assert_not_predicate legacy_ref, :bigint?
record1 = LegacyPrimaryKey.create!
assert_not_nil record1.id
record1.destroy
record2 = LegacyPrimaryKey.create!
assert_not_nil record2.id
assert_operator record2.id, :>, record1.id
end
else
def test_legacy_bigint_primary_key_should_not_be_auto_incremented
def test_legacy_integer_primary_key_should_not_be_auto_incremented
skip if current_adapter?(:SQLite3Adapter)
@migration = Class.new(migration_class) {
def change
create_table :legacy_primary_keys, id: :bigint do |t|
create_table :legacy_primary_keys, id: :integer do |t|
end
end
}.new
@ -1029,25 +919,138 @@ def change
end
schema = dump_table_schema "legacy_primary_keys"
assert_match %r{create_table "legacy_primary_keys", id: :bigint, default: nil}, schema
assert_match %r{create_table "legacy_primary_keys", id: :integer, default: nil}, schema
end
end
private
def assert_legacy_primary_key
assert_equal "id", LegacyPrimaryKey.primary_key
def test_legacy_primary_key_in_create_table_should_be_integer
@migration = Class.new(migration_class) {
def change
create_table :legacy_primary_keys, id: false do |t|
t.primary_key :id
end
end
}.new
legacy_pk = LegacyPrimaryKey.columns_hash["id"]
@migration.migrate(:up)
assert_equal :integer, legacy_pk.type
assert_not_predicate legacy_pk, :bigint?
assert_not legacy_pk.null
assert_legacy_primary_key
end
def test_legacy_primary_key_in_change_table_should_be_integer
@migration = Class.new(migration_class) {
def change
create_table :legacy_primary_keys, id: false do |t|
t.integer :dummy
end
change_table :legacy_primary_keys do |t|
t.primary_key :id
end
end
}.new
@migration.migrate(:up)
assert_legacy_primary_key
end
def test_add_column_with_legacy_primary_key_should_be_integer
@migration = Class.new(migration_class) {
def change
create_table :legacy_primary_keys, id: false do |t|
t.integer :dummy
end
add_column :legacy_primary_keys, :id, :primary_key
end
}.new
@migration.migrate(:up)
assert_legacy_primary_key
end
def test_legacy_join_table_foreign_keys_should_be_integer
@migration = Class.new(migration_class) {
def change
create_join_table :apples, :bananas do |t|
end
end
}.new
@migration.migrate(:up)
schema = dump_table_schema "apples_bananas"
assert_match %r{integer "apple_id", null: false}, schema
assert_match %r{integer "banana_id", null: false}, schema
end
def test_legacy_join_table_column_options_should_be_overwritten
@migration = Class.new(migration_class) {
def change
create_join_table :apples, :bananas, column_options: { type: :bigint } do |t|
end
end
}.new
@migration.migrate(:up)
schema = dump_table_schema "apples_bananas"
assert_match %r{bigint "apple_id", null: false}, schema
assert_match %r{bigint "banana_id", null: false}, schema
end
if current_adapter?(:Mysql2Adapter)
def test_legacy_bigint_primary_key_should_be_auto_incremented
@migration = Class.new(migration_class) {
def change
create_table :legacy_primary_keys, id: :bigint
end
}.new
@migration.migrate(:up)
legacy_pk = LegacyPrimaryKey.columns_hash["id"]
assert_predicate legacy_pk, :bigint?
assert_predicate legacy_pk, :auto_increment?
if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter)
schema = dump_table_schema "legacy_primary_keys"
assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)}, schema
assert_match %r{create_table "legacy_primary_keys", (?!id: :bigint, default: nil)}, schema
end
else
def test_legacy_bigint_primary_key_should_not_be_auto_incremented
@migration = Class.new(migration_class) {
def change
create_table :legacy_primary_keys, id: :bigint do |t|
end
end
}.new
@migration.migrate(:up)
assert_raises(ActiveRecord::NotNullViolation) do
LegacyPrimaryKey.create!
end
schema = dump_table_schema "legacy_primary_keys"
assert_match %r{create_table "legacy_primary_keys", id: :bigint, default: nil}, schema
end
end
private
def assert_legacy_primary_key
assert_equal "id", LegacyPrimaryKey.primary_key
legacy_pk = LegacyPrimaryKey.columns_hash["id"]
assert_equal :integer, legacy_pk.type
assert_not_predicate legacy_pk, :bigint?
assert_not legacy_pk.null
if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter)
schema = dump_table_schema "legacy_primary_keys"
assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)}, schema
end
end
end
end
module LegacyPrimaryKeyTest