From 687e33df96e234eda2025812b1713e1b028dc417 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 4 Sep 2025 22:36:04 -0400 Subject: [PATCH] database migrations --- .../down.sql | 6 +- .../up.sql | 8 ++- .../2025-09-05-003044_wallets/down.sql | 5 ++ .../2025-09-05-003044_wallets/up.sql | 55 +++++++++++++++++++ .../down.sql | 5 ++ .../up.sql | 42 ++++++++++++++ .../user_tables/user_subscriptions.sql | 2 +- 7 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 _sql/migrations/2025-09-05-003044_wallets/down.sql create mode 100644 _sql/migrations/2025-09-05-003044_wallets/up.sql create mode 100644 _sql/migrations/2025-09-05-003050_wallet_ledger_entries/down.sql create mode 100644 _sql/migrations/2025-09-05-003050_wallet_ledger_entries/up.sql diff --git a/_sql/migrations/2025-09-04-024100_alter_user_subscriptions_add_maybe_invoice_is_paid/down.sql b/_sql/migrations/2025-09-04-024100_alter_user_subscriptions_add_maybe_invoice_is_paid/down.sql index d9a93fe9a1..0ad6231061 100644 --- a/_sql/migrations/2025-09-04-024100_alter_user_subscriptions_add_maybe_invoice_is_paid/down.sql +++ b/_sql/migrations/2025-09-04-024100_alter_user_subscriptions_add_maybe_invoice_is_paid/down.sql @@ -1 +1,5 @@ --- This file should undo anything in `up.sql` +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +ALTER TABLE user_subscriptions DROP COLUMN maybe_stripe_invoice_is_paid; diff --git a/_sql/migrations/2025-09-04-024100_alter_user_subscriptions_add_maybe_invoice_is_paid/up.sql b/_sql/migrations/2025-09-04-024100_alter_user_subscriptions_add_maybe_invoice_is_paid/up.sql index b37f10d95f..e7ef62ff88 100644 --- a/_sql/migrations/2025-09-04-024100_alter_user_subscriptions_add_maybe_invoice_is_paid/up.sql +++ b/_sql/migrations/2025-09-04-024100_alter_user_subscriptions_add_maybe_invoice_is_paid/up.sql @@ -1 +1,7 @@ --- Your SQL goes here +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +ALTER TABLE user_subscriptions + ADD COLUMN maybe_stripe_invoice_is_paid BOOLEAN DEFAULT NULL + AFTER maybe_stripe_subscription_status; diff --git a/_sql/migrations/2025-09-05-003044_wallets/down.sql b/_sql/migrations/2025-09-05-003044_wallets/down.sql new file mode 100644 index 0000000000..b3a7368edb --- /dev/null +++ b/_sql/migrations/2025-09-05-003044_wallets/down.sql @@ -0,0 +1,5 @@ +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +DROP TABLE wallets; \ No newline at end of file diff --git a/_sql/migrations/2025-09-05-003044_wallets/up.sql b/_sql/migrations/2025-09-05-003044_wallets/up.sql new file mode 100644 index 0000000000..4054552c0c --- /dev/null +++ b/_sql/migrations/2025-09-05-003044_wallets/up.sql @@ -0,0 +1,55 @@ +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +CREATE TABLE wallets ( + -- Not used for anything except replication. + id BIGINT(20) NOT NULL AUTO_INCREMENT, + + -- Effective "primary key" (PUBLIC) + token VARCHAR(32) NOT NULL, + + -- Who owns the wallet + -- In the future, we might allow other users to use the same wallet, + -- but the wallet would still have an owner. + -- For now, we'll keep this 1:1 with users, but we may want 1:n in the future. + owner_user_token VARCHAR(32) NOT NULL, + + -- The durable credits bucket. + -- This bucket is persistent and is refilled by user credit purchases. + -- Max value unsigned: 4,294,967,295 + banked_credits INTEGER UNSIGNED NOT NULL DEFAULT 0, + + -- The monthly credits bucket. + -- This bucket is ephemeral and is refilled monthly. + -- Max value unsigned: 4,294,967,295 + monthly_credits INTEGER UNSIGNED NOT NULL DEFAULT 0, + + -- Whether the stripe subscription is active. + subscription_is_active BOOLEAN NOT NULL DEFAULT FALSE, + + -- Whether the stripe subscription is paid. + -- If the subscription is not paid, we should not allow usage of monthly credits. + subscription_is_paid BOOLEAN NOT NULL DEFAULT FALSE, + + -- ========== VECTOR CLOCK ========== + + -- Incremented with every update. + version INT NOT NULL DEFAULT 0, + + -- ========== RECORD TIMESTAMPS ========== + + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + + -- ========== SUBSCRIPTION TIMESTAMPS ========== + + -- When we last refilled the monthly bucket. + monthly_wallet_refilled_at TIMESTAMP DEFAULT NULL, + + -- ========== INDICES ========== + PRIMARY KEY (id), + UNIQUE KEY (token), + UNIQUE KEY (owner_user_token) + +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; diff --git a/_sql/migrations/2025-09-05-003050_wallet_ledger_entries/down.sql b/_sql/migrations/2025-09-05-003050_wallet_ledger_entries/down.sql new file mode 100644 index 0000000000..802f67b5a1 --- /dev/null +++ b/_sql/migrations/2025-09-05-003050_wallet_ledger_entries/down.sql @@ -0,0 +1,5 @@ +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +DROP TABLE wallet_ledger_entries; diff --git a/_sql/migrations/2025-09-05-003050_wallet_ledger_entries/up.sql b/_sql/migrations/2025-09-05-003050_wallet_ledger_entries/up.sql new file mode 100644 index 0000000000..b329944379 --- /dev/null +++ b/_sql/migrations/2025-09-05-003050_wallet_ledger_entries/up.sql @@ -0,0 +1,42 @@ +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile +-- noinspection SqlDialectInspectionForFile +-- noinspection SqlNoDataSourceInspectionForFile +-- noinspection SqlResolveForFile + +CREATE TABLE wallet_ledger_entries ( + -- Not used for anything except replication. + id BIGINT(20) NOT NULL AUTO_INCREMENT, + + -- Effective "primary key" (PUBLIC) + token VARCHAR(32) NOT NULL, + + -- Which wallet this entry corresponds to. + wallet_token VARCHAR(32) NOT NULL, + + -- The type of ledger entry this is. + entry_type VARCHAR(32) NOT NULL, + + -- Balance of banked credits before the event. + banked_credits_before INTEGER UNSIGNED NOT NULL DEFAULT 0, + + -- Balance of banked credits after the event. + banked_credits_after INTEGER UNSIGNED NOT NULL DEFAULT 0, + + -- Balance of monthly credits before the event. + monthly_credits_before INTEGER UNSIGNED NOT NULL DEFAULT 0, + + -- Balance of monthly credits after the event. + monthly_credits_after INTEGER UNSIGNED NOT NULL DEFAULT 0, + + -- ========== RECORD TIMESTAMPS ========== + + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + + -- ========== INDICES ========== + PRIMARY KEY (id), + UNIQUE KEY (token), + KEY index_wallet_token (wallet_token) + +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; diff --git a/_sql/migrations_squashed/user_tables/user_subscriptions.sql b/_sql/migrations_squashed/user_tables/user_subscriptions.sql index 86083ded54..df01df111e 100644 --- a/_sql/migrations_squashed/user_tables/user_subscriptions.sql +++ b/_sql/migrations_squashed/user_tables/user_subscriptions.sql @@ -55,7 +55,7 @@ CREATE TABLE user_subscriptions ( -- Primarily managed by the `invoice.paid` and `invoice.payment_failed` webhooks. -- Not applicable to FakeYou subscriptions. - maybe_invoice_is_paid BOOLEAN DEFAULT NULL, + maybe_stripe_invoice_is_paid BOOLEAN DEFAULT NULL, maybe_stripe_is_production BOOLEAN DEFAULT NULL,