diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-05-30 13:30:07 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-05-30 13:30:07 -0700 |
commit | 7278547a5d1ac8a7c20d0edf62bc76c5434da6f5 (patch) | |
tree | 1f999a5c30d66cad6191f19d9ee4f6e78cec3bb3 /activerecord | |
parent | 65858a4bedab5f1ace32059f401d71602056fef0 (diff) | |
parent | c44418ea4e09cc81da47edbc9ac5f31c7e32c1b4 (diff) | |
download | rails-7278547a5d1ac8a7c20d0edf62bc76c5434da6f5.tar.gz rails-7278547a5d1ac8a7c20d0edf62bc76c5434da6f5.tar.bz2 rails-7278547a5d1ac8a7c20d0edf62bc76c5434da6f5.zip |
Merge pull request #1410 from lucasts/pg_schema
a little step to better postgresql schema support in rails
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 8b48c055ac..b681871673 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -659,7 +659,10 @@ module ActiveRecord # Returns the list of all tables in the schema search path or a specified schema. def tables(name = nil) 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 WHERE schemaname = ANY (current_schemas(false)) SQL @@ -805,8 +808,8 @@ module ActiveRecord 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 +821,21 @@ module ActiveRecord # 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] + sequence = result.second == 'public' ? result.last : "#{result.second}.#{result.last}" + + [result.first, sequence] rescue nil end |