77
88namespace craft \commerce \stripe \migrations ;
99
10+ use craft \commerce \stripe \db \Table ;
1011use craft \db \Migration ;
1112use craft \helpers \MigrationHelper ;
1213
@@ -23,7 +24,8 @@ class Install extends Migration
2324 */
2425 public function safeUp (): bool
2526 {
26- $ this ->createTable ('{{%stripe_customers}} ' , [
27+ $ this ->archiveTableIfExists (Table::CUSTOMERS );
28+ $ this ->createTable (Table::CUSTOMERS , [
2729 'id ' => $ this ->primaryKey (),
2830 'userId ' => $ this ->integer ()->notNull (),
2931 'gatewayId ' => $ this ->integer ()->notNull (),
@@ -34,7 +36,8 @@ public function safeUp(): bool
3436 'uid ' => $ this ->uid (),
3537 ]);
3638
37- $ this ->createTable ('{{%stripe_invoices}} ' , [
39+ $ this ->archiveTableIfExists (Table::INVOICES );
40+ $ this ->createTable (Table::INVOICES , [
3841 'id ' => $ this ->primaryKey (),
3942 'reference ' => $ this ->string (),
4043 'subscriptionId ' => $ this ->integer ()->notNull (),
@@ -44,7 +47,8 @@ public function safeUp(): bool
4447 'uid ' => $ this ->uid (),
4548 ]);
4649
47- $ this ->createTable ('{{%stripe_paymentintents}} ' , [
50+ $ this ->archiveTableIfExists (Table::PAYMENTINTENTS );
51+ $ this ->createTable (Table::PAYMENTINTENTS , [
4852 'id ' => $ this ->primaryKey (),
4953 'reference ' => $ this ->string (),
5054 'gatewayId ' => $ this ->integer ()->notNull (),
@@ -56,19 +60,19 @@ public function safeUp(): bool
5660 'uid ' => $ this ->uid (),
5761 ]);
5862
59- $ this ->addForeignKey (null , ' {{%stripe_customers}} ' , 'gatewayId ' , '{{%commerce_gateways}} ' , 'id ' , 'CASCADE ' );
60- $ this ->addForeignKey (null , ' {{%stripe_customers}} ' , 'userId ' , '{{%users}} ' , 'id ' , 'CASCADE ' );
61- $ this ->addForeignKey (null , ' {{%stripe_invoices}} ' , 'subscriptionId ' , '{{%commerce_subscriptions}} ' , 'id ' , 'CASCADE ' );
62- $ this ->addForeignKey (null , ' {{%stripe_paymentintents}} ' , 'gatewayId ' , '{{%commerce_gateways}} ' , 'id ' , 'CASCADE ' );
63- $ this ->addForeignKey (null , ' {{%stripe_paymentintents}} ' , 'customerId ' , ' {{%stripe_customers}} ' , 'id ' , 'CASCADE ' );
63+ $ this ->addForeignKey (null , Table:: CUSTOMERS , 'gatewayId ' , '{{%commerce_gateways}} ' , 'id ' , 'CASCADE ' );
64+ $ this ->addForeignKey (null , Table:: CUSTOMERS , 'userId ' , '{{%users}} ' , 'id ' , 'CASCADE ' );
65+ $ this ->addForeignKey (null , Table:: INVOICES , 'subscriptionId ' , '{{%commerce_subscriptions}} ' , 'id ' , 'CASCADE ' );
66+ $ this ->addForeignKey (null , Table:: PAYMENTINTENTS , 'gatewayId ' , '{{%commerce_gateways}} ' , 'id ' , 'CASCADE ' );
67+ $ this ->addForeignKey (null , Table:: PAYMENTINTENTS , 'customerId ' , Table:: CUSTOMERS , 'id ' , 'CASCADE ' );
6468
65- $ this ->createIndex (null , ' {{%stripe_customers}} ' , 'gatewayId ' , false );
66- $ this ->createIndex (null , ' {{%stripe_customers}} ' , 'userId ' , false );
67- $ this ->createIndex (null , ' {{%stripe_customers}} ' , 'reference ' , true );
68- $ this ->createIndex (null , ' {{%stripe_invoices}} ' , 'subscriptionId ' , false );
69- $ this ->createIndex (null , ' {{%stripe_invoices}} ' , 'reference ' , true );
70- $ this ->createIndex (null , ' {{%stripe_paymentintents}} ' , 'reference ' , true );
71- $ this ->createIndex (null , ' {{%stripe_paymentintents}} ' , ['gatewayId ' , 'customerId ' , 'transactionHash ' ], true );
69+ $ this ->createIndex (null , Table:: CUSTOMERS , 'gatewayId ' , false );
70+ $ this ->createIndex (null , Table:: CUSTOMERS , 'userId ' , false );
71+ $ this ->createIndex (null , Table:: CUSTOMERS , 'reference ' , true );
72+ $ this ->createIndex (null , Table:: INVOICES , 'subscriptionId ' , false );
73+ $ this ->createIndex (null , Table:: INVOICES , 'reference ' , true );
74+ $ this ->createIndex (null , Table:: PAYMENTINTENTS , 'reference ' , true );
75+ $ this ->createIndex (null , Table:: PAYMENTINTENTS , ['gatewayId ' , 'customerId ' , 'transactionHash ' ], true );
7276
7377 return true ;
7478 }
@@ -78,12 +82,12 @@ public function safeUp(): bool
7882 */
7983 public function safeDown (): bool
8084 {
81- MigrationHelper::dropAllForeignKeysOnTable (' {{%stripe_invoices}} ' , $ this );
82- MigrationHelper::dropAllForeignKeysOnTable (' {{%stripe_customers}} ' , $ this );
83- MigrationHelper::dropAllForeignKeysOnTable (' {{%stripe_paymentintents}} ' , $ this );
84- $ this ->dropTable (' {{%stripe_customers}} ' );
85- $ this ->dropTable (' {{%stripe_invoices}} ' );
86- $ this ->dropTable (' {{%stripe_paymentintents}} ' );
85+ MigrationHelper::dropAllForeignKeysOnTable (Table:: INVOICES , $ this );
86+ MigrationHelper::dropAllForeignKeysOnTable (Table:: CUSTOMERS , $ this );
87+ MigrationHelper::dropAllForeignKeysOnTable (Table:: PAYMENTINTENTS , $ this );
88+ $ this ->dropTable (Table:: CUSTOMERS );
89+ $ this ->dropTable (Table:: INVOICES );
90+ $ this ->dropTable (Table:: PAYMENTINTENTS );
8791
8892 return true ;
8993 }
0 commit comments