Class: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- Defined in:
- lib/connection_adapters/postgresql_adapter.rb
Instance Method Summary collapse
- #foreign_key_constraints(table, name = nil) ⇒ Object
- #remove_foreign_key_constraint(table_name, constraint_name) ⇒ Object
- #supports_fetch_foreign_keys? ⇒ Boolean
Instance Method Details
#foreign_key_constraints(table, name = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/connection_adapters/postgresql_adapter.rb', line 11 def foreign_key_constraints(table, name = nil) sql = "SELECT conname, pg_catalog.pg_get_constraintdef(oid) AS consrc FROM pg_catalog.pg_constraint WHERE contype='f' " sql += "AND conrelid = (SELECT oid FROM pg_catalog.pg_class WHERE relname='#{table}')" result = query(sql, name) keys = [] re = /(?i)^FOREIGN KEY \((.+)\) REFERENCES (.+)\((.+)\)(?: ON UPDATE (\w+))?(?: ON DELETE (\w+))?$/ result.each do |row| # pg_catalog.pg_get_constraintdef returns a string like this: # FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE if match = re.match(row[1]) keys << ForeignKeyConstraint.new(row[0], table, match[1], match[2], match[3], symbolize_foreign_key_constraint_action(match[4]), symbolize_foreign_key_constraint_action(match[5])) end end keys end |
#remove_foreign_key_constraint(table_name, constraint_name) ⇒ Object
39 40 41 |
# File 'lib/connection_adapters/postgresql_adapter.rb', line 39 def remove_foreign_key_constraint(table_name, constraint_name) execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{constraint_name}" end |
#supports_fetch_foreign_keys? ⇒ Boolean
7 8 9 |
# File 'lib/connection_adapters/postgresql_adapter.rb', line 7 def supports_fetch_foreign_keys? true end |