Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 2317

Show
Ignore:
Timestamp:
09/23/05 17:27:59 (3 years ago)
Author:
minam
Message:

Allow the postgresql adapter to work with the SchemaDumper.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r2312 r2317  
    11*SVN* 
     2 
     3* Allow the postgresql adapter to work with the SchemaDumper. 
    24 
    35* 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. 
  • trunk/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

    r2316 r2317  
    9090      end 
    9191 
     92      # Return the list of all tables in the schema search path. 
     93      def tables(name = nil) 
     94        schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',') 
     95        query(<<-SQL, name).map { |row| row[0] } 
     96          SELECT tablename 
     97            FROM pg_tables 
     98           WHERE schemaname IN (#{schemas}) 
     99        SQL 
     100      end 
     101 
     102      def indexes(table_name, name = nil) 
     103        result = query(<<-SQL, name) 
     104          SELECT i.relname, d.indisunique, a.attname 
     105            FROM pg_class t, pg_class i, pg_index d, pg_attribute a 
     106           WHERE i.relkind = 'i' 
     107             AND d.indexrelid = i.oid 
     108             AND d.indisprimary = 'f' 
     109             AND t.oid = d.indrelid 
     110             AND t.relname = '#{table_name}' 
     111             AND a.attrelid = t.oid 
     112             AND ( d.indkey[0]=a.attnum OR d.indkey[1]=a.attnum 
     113                OR d.indkey[2]=a.attnum OR d.indkey[3]=a.attnum 
     114                OR d.indkey[4]=a.attnum OR d.indkey[5]=a.attnum 
     115                OR d.indkey[6]=a.attnum OR d.indkey[7]=a.attnum 
     116                OR d.indkey[8]=a.attnum OR d.indkey[9]=a.attnum ) 
     117          ORDER BY i.relname 
     118        SQL 
     119 
     120        current_index = nil 
     121        indexes = [] 
     122 
     123        result.each do |row| 
     124          if current_index != row[0] 
     125            indexes << IndexDefinition.new(table_name, row[0], row[1] == "t", []) 
     126            current_index = row[0] 
     127          end 
     128 
     129          indexes.last.columns << row[2] 
     130        end 
     131 
     132        indexes 
     133      end 
     134 
    92135      def columns(table_name, name = nil) 
    93         column_definitions(table_name).collect do |name, type, default| 
    94           Column.new(name, default_value(default), translate_field_type(type)) 
     136        column_definitions(table_name).collect do |name, type, default, notnull| 
     137          Column.new(name, default_value(default), translate_field_type(type), 
     138            notnull == "f") 
    95139        end 
    96140      end 
     
    214258          s.gsub(/\\([0-9][0-9][0-9])/) { $1.oct.chr }.gsub(/\\\\/) { '\\' } unless s.nil? 
    215259        end 
    216  
     260         
    217261        # Query a table's column names, default values, and types. 
    218262        # 
     
    235279        def column_definitions(table_name) 
    236280          query <<-end_sql 
    237             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc 
     281            SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull 
    238282              FROM pg_attribute a LEFT JOIN pg_attrdef d 
    239283                ON a.attrelid = d.adrelid AND a.attnum = d.adnum