Class: SQL::Postgres::Table

Inherits:
Table
  • Object
show all
Defined in:
lib/dm-migrations/sql/postgres.rb

Instance Attribute Summary

Attributes inherited from Table

#columns, #name

Instance Method Summary collapse

Methods inherited from Table

#column, #to_s

Constructor Details

#initialize(adapter, table_name) ⇒ Table

Returns a new instance of Table.



40
41
42
43
44
45
46
47
48
# File 'lib/dm-migrations/sql/postgres.rb', line 40

def initialize(adapter, table_name)
  @adapter, @name = adapter, table_name
  @columns = []
  adapter.query_table(table_name).each do |col_struct|
    @columns << SQL::Postgres::Column.new(col_struct)
  end

  query_column_constraints
end

Instance Method Details

#query_column_constraintsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dm-migrations/sql/postgres.rb', line 50

def query_column_constraints
  @adapter.select(
    "SELECT * FROM information_schema.table_constraints WHERE table_name='#{@name}' AND table_schema=current_schema()"
  ).each do |table_constraint|
    @adapter.select(
      "SELECT * FROM information_schema.constraint_column_usage WHERE constraint_name='#{table_constraint.constraint_name}' AND table_schema=current_schema()"
    ).each do |constrained_column|
      @columns.each do |column|
        if column.name == constrained_column.column_name
          case table_constraint.constraint_type
          when "UNIQUE"       then column.unique = true
          when "PRIMARY KEY"  then column.primary_key = true
          end
        end
      end
    end
  end
end