提交 c1537e89 编写于 作者: J Jeremy Kemper

PostgreSQL: more robust sequence name discovery. References #3087.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3235 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 bae97ef4
*SVN* *SVN*
* PostgreSQL: more robust sequence name discovery. #3087 [Rick Olson]
* Oracle: use syntax compatible with Oracle 8. #3131 [Michael Schoen] * Oracle: use syntax compatible with Oracle 8. #3131 [Michael Schoen]
* MySQL: work around ruby-mysql/mysql-ruby inconsistency with mysql.stat. Eliminate usage of mysql.ping because it doesn't guarantee reconnect. Explicitly close and reopen the connection instead. [Jeremy Kemper] * MySQL: work around ruby-mysql/mysql-ruby inconsistency with mysql.stat. Eliminate usage of mysql.ping because it doesn't guarantee reconnect. Explicitly close and reopen the connection instead. [Jeremy Kemper]
......
...@@ -251,7 +251,7 @@ def pk_and_sequence_for(table) ...@@ -251,7 +251,7 @@ def pk_and_sequence_for(table)
# First try looking for a sequence with a dependency on the # First try looking for a sequence with a dependency on the
# given table's primary key. # given table's primary key.
result = execute(<<-end_sql, 'PK and serial sequence')[0] result = execute(<<-end_sql, 'PK and serial sequence')[0]
SELECT attr.attname, (name.nspname || '.' || seq.relname) SELECT attr.attname, name.nspname, seq.relname
FROM pg_class seq, FROM pg_class seq,
pg_attribute attr, pg_attribute attr,
pg_depend dep, pg_depend dep,
...@@ -274,7 +274,7 @@ def pk_and_sequence_for(table) ...@@ -274,7 +274,7 @@ def pk_and_sequence_for(table)
# the 8.1+ nextval('foo'::regclass). # the 8.1+ nextval('foo'::regclass).
# TODO: assumes sequence is in same schema as table. # TODO: assumes sequence is in same schema as table.
result = execute(<<-end_sql, 'PK and custom sequence')[0] result = execute(<<-end_sql, 'PK and custom sequence')[0]
SELECT attr.attname, (name.nspname || '.' || split_part(def.adsrc, '\\\'', 2)) SELECT attr.attname, name.nspname, split_part(def.adsrc, '\\\'', 2)
FROM pg_class t FROM pg_class t
JOIN pg_namespace name ON (t.relnamespace = name.oid) JOIN pg_namespace name ON (t.relnamespace = name.oid)
JOIN pg_attribute attr ON (t.oid = attrelid) JOIN pg_attribute attr ON (t.oid = attrelid)
...@@ -285,7 +285,8 @@ def pk_and_sequence_for(table) ...@@ -285,7 +285,8 @@ def pk_and_sequence_for(table)
AND def.adsrc ~* 'nextval' AND def.adsrc ~* 'nextval'
end_sql end_sql
end end
result # check for existence of . in sequence name as in public.foo_sequence. if it does not exist, join the current namespace
result.last['.'] ? [result.first, result.last] : [result.first, "#{result[1]}.#{result[2]}"]
rescue rescue
nil nil
end end
......
CREATE SEQUENCE public.accounts_id_seq START 100;
CREATE TABLE accounts ( CREATE TABLE accounts (
id serial, id integer DEFAULT nextval('public.accounts_id_seq'),
firm_id integer, firm_id integer,
credit_limit integer, credit_limit integer,
PRIMARY KEY (id) PRIMARY KEY (id)
); );
SELECT setval('accounts_id_seq', 100);
CREATE SEQUENCE companies_nonstd_seq START 101; CREATE SEQUENCE companies_nonstd_seq START 101;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册