| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Subscriptions\Migrations; |
| 4 |
|
| 5 |
use Give\Framework\Database\DB; |
| 6 |
use Give\Framework\Database\Exceptions\DatabaseQueryException; |
| 7 |
use Give\Framework\Migrations\Contracts\Migration; |
| 8 |
use Give\Framework\Migrations\Contracts\ReversibleMigration; |
| 9 |
use Give\Framework\Migrations\Exceptions\DatabaseMigrationException; |
| 10 |
|
| 11 |
/** |
| 12 |
* @since 4.11.0 |
| 13 |
*/ |
| 14 |
class AddCampaignId extends Migration implements ReversibleMigration |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @inheritDoc |
| 18 |
*/ |
| 19 |
public static function id(): string |
| 20 |
{ |
| 21 |
return 'add_campaign_id_to_subscriptions'; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @inheritDoc |
| 26 |
*/ |
| 27 |
public static function title(): string |
| 28 |
{ |
| 29 |
return 'Add campaign id to subscriptions'; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @inheritdoc |
| 34 |
*/ |
| 35 |
public static function timestamp(): string |
| 36 |
{ |
| 37 |
return strtotime('2025-10-16 00:00:00'); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @inheritDoc |
| 42 |
* |
| 43 |
* @throws DatabaseMigrationException |
| 44 |
*/ |
| 45 |
public function run() |
| 46 |
{ |
| 47 |
try { |
| 48 |
$query = <<<SQL |
| 49 |
UPDATE %s AS subscriptions |
| 50 |
JOIN %s campaignForms |
| 51 |
ON subscriptions.product_id = campaignForms.form_id |
| 52 |
SET subscriptions.campaign_id = campaignForms.campaign_id |
| 53 |
SQL; |
| 54 |
|
| 55 |
DB::query(sprintf( |
| 56 |
$query, |
| 57 |
DB::prefix('give_subscriptions'), |
| 58 |
DB::prefix('give_campaign_forms') |
| 59 |
)); |
| 60 |
} catch (DatabaseQueryException $exception) { |
| 61 |
throw new DatabaseMigrationException("An error occurred while adding campaign ID to the give_subscriptions table", |
| 62 |
0, $exception); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
/** |
| 68 |
* @inheritDoc |
| 69 |
*/ |
| 70 |
public function reverse(): void |
| 71 |
{ |
| 72 |
DB::table('give_subscriptions')->update(['campaign_id' => null]); |
| 73 |
} |
| 74 |
} |
| 75 |
|