| 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\Exceptions\DatabaseMigrationException; |
| 9 |
|
| 10 |
class CreateSubscriptionTables extends Migration |
| 11 |
{ |
| 12 |
public static function id() |
| 13 |
{ |
| 14 |
return 'create_subscription_tables'; |
| 15 |
} |
| 16 |
|
| 17 |
public function run() |
| 18 |
{ |
| 19 |
global $wpdb; |
| 20 |
|
| 21 |
$charset_collate = $wpdb->get_charset_collate(); |
| 22 |
$subscriptionTableName = "{$wpdb->prefix}give_subscriptions"; |
| 23 |
$metaTableName = "{$wpdb->prefix}give_subscriptionmeta"; |
| 24 |
|
| 25 |
try { |
| 26 |
DB::delta( |
| 27 |
" |
| 28 |
CREATE TABLE `$subscriptionTableName` ( |
| 29 |
`id` bigint(20) NOT NULL AUTO_INCREMENT, |
| 30 |
`customer_id` bigint(20) NOT NULL, |
| 31 |
`period` varchar(20) NOT NULL, |
| 32 |
`frequency` bigint(20) NOT NULL DEFAULT '1', |
| 33 |
`initial_amount` decimal(18,10) NOT NULL, |
| 34 |
`recurring_amount` decimal(18,10) NOT NULL, |
| 35 |
`recurring_fee_amount` decimal(18,10) NOT NULL, |
| 36 |
`bill_times` bigint(20) NOT NULL, |
| 37 |
`transaction_id` varchar(60) NOT NULL, |
| 38 |
`parent_payment_id` bigint(20) NOT NULL, |
| 39 |
`product_id` bigint(20) NOT NULL, |
| 40 |
`created` datetime NOT NULL, |
| 41 |
`expiration` datetime NOT NULL, |
| 42 |
`status` varchar(20) NOT NULL, |
| 43 |
`profile_id` varchar(60) NOT NULL, |
| 44 |
`notes` longtext NOT NULL, |
| 45 |
PRIMARY KEY (`id`), |
| 46 |
KEY `profile_id` (`profile_id`), |
| 47 |
KEY `customer` (`customer_id`), |
| 48 |
KEY `transaction` (`transaction_id`), |
| 49 |
KEY `customer_and_status` (`customer_id`,`status`) |
| 50 |
) $charset_collate; |
| 51 |
" |
| 52 |
); |
| 53 |
|
| 54 |
DB::delta( |
| 55 |
" |
| 56 |
CREATE TABLE `$metaTableName` ( |
| 57 |
`meta_id` bigint(20) NOT NULL AUTO_INCREMENT, |
| 58 |
`subscription_id` bigint(20) NOT NULL, |
| 59 |
`meta_key` varchar(255) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL, |
| 60 |
`meta_value` longtext COLLATE utf8mb4_unicode_520_ci, |
| 61 |
PRIMARY KEY (`meta_id`), |
| 62 |
KEY `subscription_id` (`subscription_id`), |
| 63 |
KEY `meta_key` (`meta_key`(191)) |
| 64 |
) $charset_collate; |
| 65 |
" |
| 66 |
); |
| 67 |
} catch (DatabaseQueryException $exception) { |
| 68 |
throw new DatabaseMigrationException('An error occurred creating the subscription tables', 0, $exception); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public static function timestamp() |
| 73 |
{ |
| 74 |
return strtotime('2022-03-30'); |
| 75 |
} |
| 76 |
} |
| 77 |
|