Skip to content

Commit 6441fee

Browse files
committed
Add Table class and make be more defensive about DB tables already existing
1 parent 739753d commit 6441fee

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- Stripe API version are now set on all client requests. ([#340](https://github.com/craftcms/commerce-stripe/issues/340))
66
- Added support for fonts in the Stripe Elements. ([#258](https://github.com/craftcms/commerce-stripe/issues/258))
7+
- Added `craft\commerce\stripe\db\Table`.
8+
- Stripe for Commerce is more defensive about DB tables already existing during install.
79

810
## 5.0.7 - 2025-03-19
911

src/db/Table.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* @link https://craftcms.com/
4+
* @copyright Copyright (c) Pixel & Tonic, Inc.
5+
* @license MIT
6+
*/
7+
8+
namespace craft\commerce\stripe\db;
9+
10+
/**
11+
* Table Class
12+
*
13+
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
14+
* @since 5.1
15+
*/
16+
abstract class Table
17+
{
18+
public const CUSTOMERS = '{{%stripe_customers}}';
19+
public const INVOICES = '{{%stripe_invoices}}';
20+
public const PAYMENTINTENTS = '{{%stripe_paymentintents}}';
21+
}

src/migrations/Install.php

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace craft\commerce\stripe\migrations;
99

10+
use craft\commerce\stripe\db\Table;
1011
use craft\db\Migration;
1112
use 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

Comments
 (0)