提交 584fc8b3 编写于 作者: A Andrey Novikov

Dump PostgreSQL primary key with custom function as a default.

For example, if use pgcrypto extension in PostgreSQL 9.4 beta 1, where
uuid-ossp extension isn't available for moment of writing, and thus to
use a gen_random_uuid() method as a primary key default.

In this case schema dumper wasn't able to correctly reconstruct
create_table statement and lost primary key constraint on schema load.

Fixes #16111.
上级 6e76031e
* Fix the schema dump generated for tables without constraints and with
primary key with default value of custom PostgreSQL function result.
Fixes #16111
*Andrey Novikov*
* Fix the SQL generated when a `delete_all` is run on an association to not
produce an `IN` statements.
......
......@@ -120,7 +120,8 @@ def table(table, stream)
# first dump primary key column
if @connection.respond_to?(:pk_and_sequence_for)
pk, _ = @connection.pk_and_sequence_for(table)
elsif @connection.respond_to?(:primary_key)
end
if !pk && @connection.respond_to?(:primary_key)
pk = @connection.primary_key(table)
end
......
......@@ -87,10 +87,26 @@ class UUID < ActiveRecord::Base
t.string 'name'
t.uuid 'other_uuid', default: 'uuid_generate_v4()'
end
# Create custom PostgreSQL function to generate UUIDs
# to test dumping tables which columns have defaults with custom functions
connection.execute <<-SQL
CREATE OR REPLACE FUNCTION my_uuid_generator() RETURNS uuid
AS $$ SELECT * FROM uuid_generate_v4() $$
LANGUAGE SQL VOLATILE;
SQL
# Create such a table with custom function as default value generator
connection.create_table('pg_uuids_2', id: :uuid, default: 'my_uuid_generator()') do |t|
t.string 'name'
t.uuid 'other_uuid_2', default: 'my_uuid_generator()'
end
end
teardown do
drop_table "pg_uuids"
drop_table 'pg_uuids_2'
connection.execute 'DROP FUNCTION IF EXISTS my_uuid_generator();'
end
if ActiveRecord::Base.connection.supports_extensions?
......@@ -122,6 +138,13 @@ def test_schema_dumper_for_uuid_primary_key
assert_match(/\bcreate_table "pg_uuids", id: :uuid, default: "uuid_generate_v1\(\)"/, schema.string)
assert_match(/t\.uuid "other_uuid", default: "uuid_generate_v4\(\)"/, schema.string)
end
def test_schema_dumper_for_uuid_primary_key_with_custom_default
schema = StringIO.new
ActiveRecord::SchemaDumper.dump(connection, schema)
assert_match(/\bcreate_table "pg_uuids_2", id: :uuid, default: "my_uuid_generator\(\)"/, schema.string)
assert_match(/t\.uuid "other_uuid_2", default: "my_uuid_generator\(\)"/, schema.string)
end
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册