Class: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- Defined in:
- lib/foreign_key_saver/foreign_key_saver_patches.rb
Class Method Summary collapse
Instance Method Summary collapse
- #foreign_key_constraints_on(table_name) ⇒ Object
- #remove_foreign_key_constraints_referencing(table_name) ⇒ Object
Class Method Details
.foreign_key_from_sql(name, foreign_key_sql) ⇒ Object
179 180 181 182 183 |
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 179 def self.foreign_key_from_sql(name, foreign_key_sql) # the clauses look like this: FOREIGN KEY (ac, bc) REFERENCES parent(ap, bp) ON UPDATE CASCADE ON DELETE SET NULL capture = foreign_key_sql.match(/FOREIGN KEY \(((?:\w+)(?:, \w+)*)\) REFERENCES (\w+)\(((?:\w+)(?:, \w+)*)\)(?: ON UPDATE (CASCADE|RESTRICT|NO ACTION|SET NULL|SET DEFAULT))?(?: ON DELETE (CASCADE|RESTRICT|NO ACTION|SET NULL|SET DEFAULT))?/) ForeignKeyConstraint.new(name, capture[1].split(', '), capture[2], capture[3].split(', '), capture[4], capture[5]) if capture end |
Instance Method Details
#foreign_key_constraints_on(table_name) ⇒ Object
169 170 171 172 173 174 175 176 177 |
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 169 def foreign_key_constraints_on(table_name) select_rows( "SELECT pg_constraint.conname, pg_get_constraintdef(pg_constraint.oid)" + " FROM pg_constraint, pg_class" + " WHERE pg_constraint.conrelid = pg_class.oid" + " AND pg_class.relname = #{quote(table_name)}").collect do |name, constraintdef| self.class.foreign_key_from_sql(name, constraintdef) end.compact end |
#remove_foreign_key_constraints_referencing(table_name) ⇒ Object
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/foreign_key_saver/foreign_key_saver_patches.rb', line 158 def remove_foreign_key_constraints_referencing(table_name) select_rows( "SELECT referenced.relname, pg_constraint.conname" + " FROM pg_constraint, pg_class, pg_class referenced" + " WHERE pg_constraint.confrelid = pg_class.oid" + " AND pg_class.relname = #{quote(table_name)}" + " AND referenced.oid = pg_constraint.conrelid").each do |table_name, constraint_name| remove_foreign_key_constraint(table_name, constraint_name) end end |