Sha256: 56bc84d18bc1539e03dca0f85c3c96b851ebe188d423bee20e2aa8addd11bf90
Contents?: true
Size: 1.66 KB
Versions: 7
Compression:
Stored size: 1.66 KB
Contents
# Foreign Key support from http://wiki.rubyonrails.org/rails/pages/Foreign+Key+Schema+Dumper+Plugin module ActiveRecord module ConnectionAdapters class PostgreSQLAdapter < AbstractAdapter def supports_fetch_foreign_keys? true end 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 def remove_foreign_key_constraint(table_name, constraint_name) execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{constraint_name}" end end end end
Version data entries
7 entries across 7 versions & 2 rubygems