1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- #---
- # Excerpted from "Agile Web Development with Rails",
- # published by The Pragmatic Bookshelf.
- # Copyrights apply to this code. It may not be used to create training material,
- # courses, books, articles, and the like. Contact us if you are in doubt.
- # We make no guarantees that this code is fit for any purpose.
- # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
- #---
- #---
- # Excerpted from "Agile Web Development with Rails, 4rd Ed.",
- # published by The Pragmatic Bookshelf.
- # Copyrights apply to this code. It may not be used to create training material,
- # courses, books, articles, and the like. Contact us if you are in doubt.
- # We make no guarantees that this code is fit for any purpose.
- # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
- #---
- module MigrationHelpers
- def foreign_key(from_table, from_column, to_table)
- constraint_name = "fk_#{from_table}_#{to_table}"
- execute %{
- CREATE TRIGGER #{constraint_name}_insert
- BEFORE INSERT ON #{from_table}
- FOR EACH ROW BEGIN
- SELECT
- RAISE(ABORT, "constraint violation: #{constraint_name}")
- WHERE
- (SELECT id FROM #{to_table} WHERE id = NEW.#{from_column}) IS NULL;
- END;
- }
- execute %{
- CREATE TRIGGER #{constraint_name}_update
- BEFORE UPDATE ON #{from_table}
- FOR EACH ROW BEGIN
- SELECT
- RAISE(ABORT, "constraint violation: #{constraint_name}")
- WHERE
- (SELECT id FROM #{to_table} WHERE id = NEW.#{from_column}) IS NULL;
- END;
- }
- execute %{
- CREATE TRIGGER #{constraint_name}_delete
- BEFORE DELETE ON #{to_table}
- FOR EACH ROW BEGIN
- SELECT
- RAISE(ABORT, "constraint violation: #{constraint_name}")
- WHERE
- (SELECT id FROM #{from_table} WHERE #{from_column} = OLD.id) IS NOT NULL;
- END;
- }
- end
- end
|