Skip to content
Browse files

find sequences with pg schemas properly

  • Loading branch information...
1 parent d1c7470 commit 055c6fb010417f7e5f8596a63075fc86ff35c54a @lucasts lucasts committed May 27, 2011
Showing with 11 additions and 4 deletions.
  1. +11 −4 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
View
15 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -805,8 +805,8 @@ def reset_pk_sequence!(table, pk = nil, sequence = nil) #:nodoc:
end
if pk && sequence
- quoted_sequence = quote_column_name(sequence)
-
+ quoted_sequence = quote_table_name(sequence)
+
select_value <<-end_sql, 'Reset sequence'
SELECT setval('#{quoted_sequence}', (SELECT COALESCE(MAX(#{quote_column_name pk})+(SELECT increment_by FROM #{quoted_sequence}), (SELECT min_value FROM #{quoted_sequence})) FROM #{quote_table_name(table)}), false)
end_sql
@@ -818,18 +818,25 @@ def pk_and_sequence_for(table) #:nodoc:
# First try looking for a sequence with a dependency on the
# given table's primary key.
result = exec_query(<<-end_sql, 'SCHEMA').rows.first
- SELECT attr.attname, seq.relname
+ SELECT attr.attname, ns.nspname, seq.relname
FROM pg_class seq
INNER JOIN pg_depend dep ON seq.oid = dep.objid
INNER JOIN pg_attribute attr ON attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid
INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
+ INNER JOIN pg_namespace ns ON seq.relnamespace = ns.oid
WHERE seq.relkind = 'S'
AND cons.contype = 'p'
AND dep.refobjid = '#{quote_table_name(table)}'::regclass
end_sql
# [primary_key, sequence]
- [result.first, result.last]
+ if result.second == 'public' then
+ sequence = result.last
+ else
+ sequence = result.second+'.'+result.last
+ end
+
+ [result.first, sequence]
rescue
nil
end

0 comments on commit 055c6fb

Please sign in to comment.
Something went wrong with that request. Please try again.