提交 0bc8d0fb 编写于 作者: K Kasper Timm Hansen 提交者: GitHub

Merge pull request #27618 from kamipo/fix_uuid_default_nil

Fix UUID primary key with default nil in legacy migration
......@@ -16,7 +16,7 @@ def self.find(version)
class V5_0 < V5_1
def create_table(table_name, options = {})
if adapter_name == "PostgreSQL"
if options[:id] == :uuid && !options[:default]
if options[:id] == :uuid && !options.key?(:default)
options[:default] = "uuid_generate_v4()"
end
end
......
......@@ -234,25 +234,23 @@ def test_schema_dumper_for_uuid_primary_key_default
end
end
if ActiveRecord::Base.connection.supports_pgcrypto_uuid?
def test_schema_dumper_for_uuid_primary_key_default_in_legacy_migration
@verbose_was = ActiveRecord::Migration.verbose
ActiveRecord::Migration.verbose = false
migration = Class.new(ActiveRecord::Migration[4.2]) do
def version; 101 end
def migrate(x)
create_table("pg_uuids_4", id: :uuid)
end
end.new
ActiveRecord::Migrator.new(:up, [migration]).migrate
schema = dump_table_schema "pg_uuids_4"
assert_match(/\bcreate_table "pg_uuids_4", id: :uuid, default: -> { "uuid_generate_v4\(\)" }/, schema)
ensure
drop_table "pg_uuids_4"
ActiveRecord::Migration.verbose = @verbose_was
end
def test_schema_dumper_for_uuid_primary_key_default_in_legacy_migration
@verbose_was = ActiveRecord::Migration.verbose
ActiveRecord::Migration.verbose = false
migration = Class.new(ActiveRecord::Migration[5.0]) do
def version; 101 end
def migrate(x)
create_table("pg_uuids_4", id: :uuid)
end
end.new
ActiveRecord::Migrator.new(:up, [migration]).migrate
schema = dump_table_schema "pg_uuids_4"
assert_match(/\bcreate_table "pg_uuids_4", id: :uuid, default: -> { "uuid_generate_v4\(\)" }/, schema)
ensure
drop_table "pg_uuids_4"
ActiveRecord::Migration.verbose = @verbose_was
end
end
end
......@@ -285,6 +283,25 @@ def test_schema_dumper_for_uuid_primary_key_with_default_override_via_nil
schema = dump_table_schema "pg_uuids"
assert_match(/\bcreate_table "pg_uuids", id: :uuid, default: nil/, schema)
end
def test_schema_dumper_for_uuid_primary_key_with_default_nil_in_legacy_migration
@verbose_was = ActiveRecord::Migration.verbose
ActiveRecord::Migration.verbose = false
migration = Class.new(ActiveRecord::Migration[5.0]) do
def version; 101 end
def migrate(x)
create_table("pg_uuids_4", id: :uuid, default: nil)
end
end.new
ActiveRecord::Migrator.new(:up, [migration]).migrate
schema = dump_table_schema "pg_uuids_4"
assert_match(/\bcreate_table "pg_uuids_4", id: :uuid, default: nil/, schema)
ensure
drop_table "pg_uuids_4"
ActiveRecord::Migration.verbose = @verbose_was
end
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册