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.



44
45
46
47
48
49
50
51
52
# File 'lib/dm-migrations/sql/postgres.rb', line 44

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dm-migrations/sql/postgres.rb', line 54

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