Class: SQL::Postgresql::Table

Inherits:
Table show all
Defined in:
lib/gems/dm-migrations-0.9.9/lib/sql/postgresql.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.



36
37
38
39
40
41
42
43
44
# File 'lib/gems/dm-migrations-0.9.9/lib/sql/postgresql.rb', line 36

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

  query_column_constraints
end

Instance Method Details

#query_column_constraintsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gems/dm-migrations-0.9.9/lib/sql/postgresql.rb', line 46

def query_column_constraints
  @adapter.query(
    "SELECT * FROM information_schema.table_constraints WHERE table_name='#{@name}' AND table_schema=current_schema()"
  ).each do |table_constraint|
    @adapter.query(
      "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