AddMissingDonorIdToDonationComments.php
3 years ago
MoveDonationCommentToDonationMetaTable.php
3 years ago
SetAutomaticFormattingOption.php
3 years ago
MoveDonationCommentToDonationMetaTable.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donations\Migrations; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\Framework\Database\DB; |
| 7 | use Give\Framework\Migrations\Contracts\Migration; |
| 8 | use Give\Framework\Migrations\Exceptions\DatabaseMigrationException; |
| 9 | use Give\Log\Log; |
| 10 | |
| 11 | /** |
| 12 | * Class MoveDonationCommentToDonationMetaTable |
| 13 | * |
| 14 | * @since 2.27.0 |
| 15 | */ |
| 16 | class MoveDonationCommentToDonationMetaTable extends Migration |
| 17 | { |
| 18 | /** |
| 19 | * @inheritdoc |
| 20 | * @throws Exception |
| 21 | */ |
| 22 | public function run() |
| 23 | { |
| 24 | $commentMetaTable = DB::prefix('give_commentmeta'); |
| 25 | $commentTable = DB::prefix('give_comments'); |
| 26 | $donationMetaTable = DB::prefix('give_donationmeta'); |
| 27 | |
| 28 | $commentsCount = DB::get_var( |
| 29 | " |
| 30 | SELECT |
| 31 | COUNT(1) |
| 32 | FROM |
| 33 | $commentTable |
| 34 | WHERE |
| 35 | comment_type = 'donor_donation' |
| 36 | " |
| 37 | ); |
| 38 | |
| 39 | if (!intval($commentsCount)) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | DB::beginTransaction(); |
| 44 | |
| 45 | try { |
| 46 | $insertQuery = DB::query( |
| 47 | " |
| 48 | INSERT INTO $donationMetaTable (donation_id, meta_key, meta_value) |
| 49 | SELECT |
| 50 | comment_parent, |
| 51 | '_give_donation_comment', |
| 52 | comment_content |
| 53 | FROM |
| 54 | $commentTable |
| 55 | WHERE |
| 56 | comment_type = 'donor_donation' |
| 57 | " |
| 58 | ); |
| 59 | |
| 60 | if (!$insertQuery) { |
| 61 | throw new Exception('Failed to insert donation comment into donation meta table.'); |
| 62 | } |
| 63 | |
| 64 | DB::query( |
| 65 | " |
| 66 | DELETE FROM $commentMetaTable |
| 67 | WHERE give_comment_id IN( |
| 68 | SELECT |
| 69 | comment_ID FROM $commentTable |
| 70 | WHERE |
| 71 | comment_type = 'donor_donation' |
| 72 | ) |
| 73 | " |
| 74 | ); |
| 75 | |
| 76 | DB::query( |
| 77 | " |
| 78 | DELETE FROM $commentTable |
| 79 | WHERE comment_type = 'donor_donation' |
| 80 | " |
| 81 | ); |
| 82 | } catch (Exception $exception) { |
| 83 | DB::rollback(); |
| 84 | |
| 85 | Log::error('Failed running migration: ' . self::title()); |
| 86 | |
| 87 | throw new DatabaseMigrationException('Failed running migration: ' . self::title(), 0, $exception); |
| 88 | } |
| 89 | |
| 90 | DB::commit(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @inheritdoc |
| 95 | */ |
| 96 | public static function id() |
| 97 | { |
| 98 | return 'move-donation-comment-to-donation-meta-table'; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @inheritdoc |
| 103 | */ |
| 104 | public static function title() |
| 105 | { |
| 106 | return 'Move donation comment to donation meta table'; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @inheritdoc |
| 111 | */ |
| 112 | public static function timestamp() |
| 113 | { |
| 114 | return strtotime('2023-03-27'); |
| 115 | } |
| 116 | } |
| 117 |