Skip to content

Commit

Permalink
Merge pull request #1410 from lucasts/pg_schema
Browse files Browse the repository at this point in the history
a little step to better postgresql schema support in rails
  • Loading branch information
tenderlove committed May 30, 2011
2 parents 65858a4 + c44418e commit 7278547
Showing 1 changed file with 11 additions and 5 deletions.
Expand Up @@ -659,7 +659,10 @@ def drop_database(name) #:nodoc:
# Returns the list of all tables in the schema search path or a specified schema. # Returns the list of all tables in the schema search path or a specified schema.
def tables(name = nil) def tables(name = nil)
query(<<-SQL, 'SCHEMA').map { |row| row[0] } query(<<-SQL, 'SCHEMA').map { |row| row[0] }
SELECT tablename SELECT case schemaname
when 'public' then tablename
else schemaname||'.'||tablename
end as tablename
FROM pg_tables FROM pg_tables
WHERE schemaname = ANY (current_schemas(false)) WHERE schemaname = ANY (current_schemas(false))
SQL SQL
Expand Down Expand Up @@ -805,8 +808,8 @@ def reset_pk_sequence!(table, pk = nil, sequence = nil) #:nodoc:
end end


if pk && sequence if pk && sequence
quoted_sequence = quote_column_name(sequence) quoted_sequence = quote_table_name(sequence)

select_value <<-end_sql, 'Reset 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) 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 end_sql
Expand All @@ -818,18 +821,21 @@ def pk_and_sequence_for(table) #:nodoc:
# 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 = exec_query(<<-end_sql, 'SCHEMA').rows.first result = exec_query(<<-end_sql, 'SCHEMA').rows.first
SELECT attr.attname, seq.relname SELECT attr.attname, ns.nspname, seq.relname
FROM pg_class seq FROM pg_class seq
INNER JOIN pg_depend dep ON seq.oid = dep.objid 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_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_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' WHERE seq.relkind = 'S'
AND cons.contype = 'p' AND cons.contype = 'p'
AND dep.refobjid = '#{quote_table_name(table)}'::regclass AND dep.refobjid = '#{quote_table_name(table)}'::regclass
end_sql end_sql


# [primary_key, sequence] # [primary_key, sequence]
[result.first, result.last] sequence = result.second == 'public' ? result.last : "#{result.second}.#{result.last}"

[result.first, sequence]
rescue rescue
nil nil
end end
Expand Down

0 comments on commit 7278547

Please sign in to comment.