ActiveRecord::Base.connection.execute <<-EOS ALTER TABLE `comments` ADD CONSTRAINT FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE restrict ON UPDATE cascade EOS Database Foreign Keys! miércoles 2 de noviembre de 2011
select * from posts where id = 1; +----+-------+--------------+ | id | title | lock_version | +----+-------+--------------+ | 1 | Title | 0 | +----+-------+--------------+ 1 row in set (0,00 sec) mysql> update posts set title = 'New Title' where lock_version = 0; Query OK, 0 rows affected (0,00 sec) Rows matched: 0 Changed: 0 Warnings: 0 mysql> select * from posts where id = 1; +----+-------+--------------+ | id | title | lock_version | +----+-------+--------------+ | 1 | Title | 0 | +----+-------+--------------+ 1 row in set (0,00 sec) miércoles 2 de noviembre de 2011
select * from posts where id = 1; +----+-------+--------------+ | id | title | lock_version | +----+-------+--------------+ | 1 | Title | 0 | +----+-------+--------------+ 1 row in set (0,00 sec) mysql> update posts set title = 'New Title' where lock_version = 0; Query OK, 0 rows affected (0,00 sec) Rows matched: 0 Changed: 0 Warnings: 0 mysql> select * from posts where id = 1; +----+-------+--------------+ | id | title | lock_version | +----+-------+--------------+ | 1 | Title | 0 | +----+-------+--------------+ 1 row in set (0,00 sec) miércoles 2 de noviembre de 2011
select * from posts where id = 1; +----+-------+--------------+ | id | title | lock_version | +----+-------+--------------+ | 1 | Title | 0 | +----+-------+--------------+ 1 row in set (0,00 sec) mysql> update posts set title = 'New Title' where lock_version = 0; Query OK, 0 rows affected (0,00 sec) Rows matched: 0 Changed: 0 Warnings: 0 // raises StaleObjectError mysql> select * from posts where id = 1; +----+-------+--------------+ | id | title | lock_version | +----+-------+--------------+ | 1 | Title | 0 | +----+-------+--------------+ 1 row in set (0,00 sec) miércoles 2 de noviembre de 2011
select * from posts where id = 1; +----+-------+--------------+ | id | title | lock_version | +----+-------+--------------+ | 1 | Title | 0 | +----+-------+--------------+ 1 row in set (0,00 sec) mysql> update posts set title = 'New Title' where lock_version = 0; Query OK, 0 rows affected (0,00 sec) Rows matched: 0 Changed: 0 Warnings: 0 // raises StaleObjectError mysql> select * from posts where id = 1; +----+-------+--------------+ | id | title | lock_version | +----+-------+--------------+ | 1 | Title | 0 | +----+-------+--------------+ 1 row in set (0,00 sec) miércoles 2 de noviembre de 2011
select * from posts where id = 1; +----+-------+--------------+ | id | title | lock_version | +----+-------+--------------+ | 1 | Title | 0 | +----+-------+--------------+ 1 row in set (0,00 sec) mysql> update posts set title = 'New Title' where lock_version = 0; Query OK, 0 rows affected (0,00 sec) Rows matched: 0 Changed: 0 Warnings: 0 // raises StaleObjectError mysql> select * from posts where id = 1; +----+-----------+--------------+ | id | title | lock_version | +----+-------+--------------+ | 1 | Title | 1 | +----+-------+--------------+ 1 row in set (0,00 sec) miércoles 2 de noviembre de 2011
:comments_count end end class Posts after_update :update_comments def update_comments comments.each {|c| c.published = self.published; c.save} end end miércoles 2 de noviembre de 2011
SELECT * FOR UPDATE FROM `posts` WHERE `id` = 1 SELECT * FOR UPDATE FROM `posts` WHERE `id` = 1 SELECT * FOR UPDATE FROM `comments` WHERE `id` = 1 Transaction 1 Transaction 2 DEADLOCK! locked.. miércoles 2 de noviembre de 2011
UPDATE `posts` SET `title` = “bar” WHERE `id` = 1 UPDATE `posts` SET `title` = “bar” WHERE `id` = 1 UPDATE `comments` SET `title` = “foo” WHERE `id` = 1 Transaction 1 Transaction 2 DEADLOCK! locked.. miércoles 2 de noviembre de 2011
:comments_count end end class Posts before_update :update_comments def update_comments comments.each {|c| c.published = self.published; c.save} end end miércoles 2 de noviembre de 2011