From 9b3e7b396eab9914ba96c0b8a3b3cbbdd35c0361 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Fri, 26 Dec 2014 17:45:12 +0900 Subject: [PATCH] Dump the default `nil` for PostgreSQL UUID primary key. --- activerecord/CHANGELOG.md | 4 ++++ activerecord/lib/active_record/schema_dumper.rb | 2 +- activerecord/test/cases/adapters/postgresql/uuid_test.rb | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index f282029d22..e9a66b4c66 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Dump the default `nil` for PostgreSQL UUID primary key. + + *Ryuta Kamizono* + * Add a `:foreign_key` option to `references` and associated migration methods. The model and migration generators now use this option, rather than the `add_foreign_key` form. diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index 3c44a625cc..2a570e1323 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -121,7 +121,7 @@ def table(table, stream) tbl.print ", id: :bigserial" elsif pkcol.sql_type == 'uuid' tbl.print ", id: :uuid" - tbl.print %Q(, default: "#{pkcol.default_function}") if pkcol.default_function + tbl.print %Q(, default: #{pkcol.default_function.inspect}) end else tbl.print ", id: false" diff --git a/activerecord/test/cases/adapters/postgresql/uuid_test.rb b/activerecord/test/cases/adapters/postgresql/uuid_test.rb index d6deb6fb1f..3c967d73da 100644 --- a/activerecord/test/cases/adapters/postgresql/uuid_test.rb +++ b/activerecord/test/cases/adapters/postgresql/uuid_test.rb @@ -215,6 +215,7 @@ def test_schema_dumper_for_uuid_primary_key_with_custom_default class PostgresqlUUIDTestNilDefault < ActiveRecord::TestCase include PostgresqlUUIDHelper + include SchemaDumpingHelper setup do enable_extension!('uuid-ossp', connection) @@ -238,6 +239,11 @@ def test_id_allows_default_override_via_nil WHERE a.attname='id' AND a.attrelid = 'pg_uuids'::regclass").first assert_nil col_desc["default"] end + + 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 end end