|
| 1 | +<?php |
| 2 | + |
| 3 | +use Phinx\Migration\AbstractMigration; |
| 4 | + |
| 5 | +class RewriteTwitterUrls extends AbstractMigration |
| 6 | +{ |
| 7 | + /** |
| 8 | + * Change Method. |
| 9 | + * |
| 10 | + * Write your reversible migrations using this method. |
| 11 | + * |
| 12 | + * More information on writing migrations is available here: |
| 13 | + * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class |
| 14 | + * |
| 15 | + * The following commands can be used in this method and Phinx will |
| 16 | + * automatically reverse them when rolling back: |
| 17 | + * |
| 18 | + * createTable |
| 19 | + * renameTable |
| 20 | + * addColumn |
| 21 | + * renameColumn |
| 22 | + * addIndex |
| 23 | + * addForeignKey |
| 24 | + * |
| 25 | + * Remember to call "create()" or "update()" and NOT "save()" when working |
| 26 | + * with the Table class. |
| 27 | + */ |
| 28 | + public function up() |
| 29 | + { |
| 30 | + // phpcs:disable |
| 31 | + $sql = "UPDATE posts INNER JOIN messages on messages.post_id = posts.id INNER JOIN contacts on messages.contact_id=contacts.id " . |
| 32 | + "SET posts.content=" . |
| 33 | + "REPLACE(posts.content, concat('https://twitter.com/statuses/', messages.data_source_message_id), " . |
| 34 | + "concat('https://twitter.com/', contacts.contact, '/status/', messages.data_source_message_id)) " . |
| 35 | + "WHERE `messages`.`type` = 'twitter' |
| 36 | + AND messages.message=posts.content"; |
| 37 | + $this->execute($sql); |
| 38 | + |
| 39 | + $sql = "UPDATE messages INNER JOIN contacts on contacts.id = messages.contact_id " . |
| 40 | + "SET messages.message = " . |
| 41 | + "REPLACE(messages.message, concat('https://twitter.com/statuses/', messages.data_source_message_id), " . |
| 42 | + "concat('https://twitter.com/', contacts.contact, '/status/', messages.data_source_message_id)) " . |
| 43 | + "WHERE `messages`.`type` = 'twitter'"; |
| 44 | + $this->execute($sql); |
| 45 | + } |
| 46 | + |
| 47 | + public function down() |
| 48 | + { |
| 49 | + // phpcs:disable |
| 50 | + $sql = "UPDATE posts INNER JOIN messages on messages.post_id = posts.id INNER JOIN contacts on messages.contact_id=contacts.id " . |
| 51 | + "SET posts.content=" . |
| 52 | + "REPLACE(messages.message, concat('https://twitter.com/', contacts.contact, '/status/', messages.data_source_message_id), " . |
| 53 | + "concat('https://twitter.com/statuses/', messages.data_source_message_id)) " . |
| 54 | + "WHERE `messages`.`type` = 'twitter' AND messages.message=posts.content"; |
| 55 | + $this->execute($sql); |
| 56 | + $sql = "UPDATE messages INNER JOIN contacts on contacts.id = messages.contact_id " . |
| 57 | + "SET messages.message = REPLACE(messages.message, " . |
| 58 | + "concat('https://twitter.com/', contacts.contact, '/status/', messages.data_source_message_id), " . |
| 59 | + "concat('https://twitter.com/statuses/', messages.data_source_message_id)) " . |
| 60 | + "WHERE `messages`.`type` = 'twitter'"; |
| 61 | + $this->execute($sql); |
| 62 | + } |
| 63 | +} |
0 commit comments