aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2005-09-23 17:27:59 +0000
committerJamis Buck <jamis@37signals.com>2005-09-23 17:27:59 +0000
commit11411effded1e3b2c6cf4577be22e75ac4dd48b3 (patch)
tree98880e721d63761c5e510f04f6b2c8f2c6c73067
parent9838a5f8b3f75f93d9d605cbf5f81e182c4bdb52 (diff)
downloadrails-11411effded1e3b2c6cf4577be22e75ac4dd48b3.tar.gz
rails-11411effded1e3b2c6cf4577be22e75ac4dd48b3.tar.bz2
rails-11411effded1e3b2c6cf4577be22e75ac4dd48b3.zip
Allow the postgresql adapter to work with the SchemaDumper.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2317 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb52
2 files changed, 50 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index a486e167ef..cce58f7587 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Allow the postgresql adapter to work with the SchemaDumper.
+
* Add ActiveRecord::SchemaDumper for dumping a DB schema to a pure-ruby file, making it easier to consolidate large migration lists and port database schemas between databases.
* Fixed migrations for Windows when using more than 10 [David Naseby]
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 165c09cead..74b4440329 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -89,9 +89,53 @@ module ActiveRecord
result.nil? ? nil : result.first
end
+ # Return the list of all tables in the schema search path.
+ def tables(name = nil)
+ schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
+ query(<<-SQL, name).map { |row| row[0] }
+ SELECT tablename
+ FROM pg_tables
+ WHERE schemaname IN (#{schemas})
+ SQL
+ end
+
+ def indexes(table_name, name = nil)
+ result = query(<<-SQL, name)
+ SELECT i.relname, d.indisunique, a.attname
+ FROM pg_class t, pg_class i, pg_index d, pg_attribute a
+ WHERE i.relkind = 'i'
+ AND d.indexrelid = i.oid
+ AND d.indisprimary = 'f'
+ AND t.oid = d.indrelid
+ AND t.relname = '#{table_name}'
+ AND a.attrelid = t.oid
+ AND ( d.indkey[0]=a.attnum OR d.indkey[1]=a.attnum
+ OR d.indkey[2]=a.attnum OR d.indkey[3]=a.attnum
+ OR d.indkey[4]=a.attnum OR d.indkey[5]=a.attnum
+ OR d.indkey[6]=a.attnum OR d.indkey[7]=a.attnum
+ OR d.indkey[8]=a.attnum OR d.indkey[9]=a.attnum )
+ ORDER BY i.relname
+ SQL
+
+ current_index = nil
+ indexes = []
+
+ result.each do |row|
+ if current_index != row[0]
+ indexes << IndexDefinition.new(table_name, row[0], row[1] == "t", [])
+ current_index = row[0]
+ end
+
+ indexes.last.columns << row[2]
+ end
+
+ indexes
+ end
+
def columns(table_name, name = nil)
- column_definitions(table_name).collect do |name, type, default|
- Column.new(name, default_value(default), translate_field_type(type))
+ column_definitions(table_name).collect do |name, type, default, notnull|
+ Column.new(name, default_value(default), translate_field_type(type),
+ notnull == "f")
end
end
@@ -213,7 +257,7 @@ module ActiveRecord
def unescape_bytea(s)
s.gsub(/\\([0-9][0-9][0-9])/) { $1.oct.chr }.gsub(/\\\\/) { '\\' } unless s.nil?
end
-
+
# Query a table's column names, default values, and types.
#
# The underlying query is roughly:
@@ -234,7 +278,7 @@ module ActiveRecord
# - ::regclass is a function that gives the id for a table name
def column_definitions(table_name)
query <<-end_sql
- SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc
+ SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '#{table_name}'::regclass