Module: ActiveRecord::MTI::ConnectionAdapters::PostgreSQL::SchemaStatements

Defined in:
lib/active_record/mti/connection_adapters/postgresql/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#create_table(table_name, options = {}) ⇒ Object

Creates a new table with the name table_name. table_name may either be a String or a Symbol.

Add :inherits options for Postgres table inheritance. If a table is inherited then the primary key column is also inherited. Therefore the :primary_key options is set to false so we don’t duplicate that colume.

However the primary key column from the parent is not inherited as primary key so we manually add it. Lastly we also create indexes on the child table to match those on the parent table since indexes are also not inherited.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/active_record/mti/connection_adapters/postgresql/schema_statements.rb', line 18

def create_table(table_name, options = {})
  if options[:inherits]
    options[:id] = false
    options.delete(:primary_key)
  end

  if (schema = options.delete(:schema))
    # If we specify a schema then we only create it if it doesn't exist
    # and we only force create it if only the specific schema is in the search path
    table_name = %Q("#{schema}"."#{table_name}")
  end

  if (inherited_table = options.delete(:inherits))
    # options[:options] = options[:options].sub("INHERITS", "() INHERITS") if td.columns.empty?
    options[:options] = [%Q(INHERITS ("#{inherited_table}")), options[:options]].compact.join
  end

  results = super(table_name, options)

  if inherited_table
    inherited_table_primary_key = primary_key(inherited_table)
    execute %Q(ALTER TABLE "#{table_name}" ADD PRIMARY KEY ("#{inherited_table_primary_key}"))

    indexes(inherited_table).each do |index|
      attributes = index.to_h.slice(:unique, :using, :where, :orders)

      # Why rails insists on being inconsistant with itself is beyond me.
      attributes[:order] = attributes.delete(:orders)

      add_index table_name, index.columns, attributes
    end
  end

  results
end

#parent_table(table_name) ⇒ Object



66
67
68
69
# File 'lib/active_record/mti/connection_adapters/postgresql/schema_statements.rb', line 66

def parent_table(table_name)
  parents = parent_tables(table_name)
  parents.first
end

#parent_tables(table_name) ⇒ Object

Parent of inherited table



55
56
57
58
59
60
61
62
63
64
# File 'lib/active_record/mti/connection_adapters/postgresql/schema_statements.rb', line 55

def parent_tables(table_name)
  result = exec_query(<<-SQL, "SCHEMA")
    SELECT pg_namespace.nspname, pg_class.relname
    FROM pg_catalog.pg_inherits
      INNER JOIN pg_catalog.pg_class ON (pg_inherits.inhparent = pg_class.oid)
      INNER JOIN pg_catalog.pg_namespace ON (pg_class.relnamespace = pg_namespace.oid)
    WHERE inhrelid = '#{table_name}'::regclass
  SQL
  result.map{|a| a['relname']}
end