Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit 11411effded1e3b2c6cf4577be22e75ac4dd48b3 1 parent 9838a5f
@jamis jamis authored
View
2  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]
View
52 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -89,9 +89,53 @@ def select_one(sql, name = nil)
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 @@ def escape_bytea(s)
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 @@ def unescape_bytea(s)
# - ::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
Please sign in to comment.
Something went wrong with that request. Please try again.