Migrations: restore 0027 as applied in production, the rest in 0028
0027 had already run in production in its first form; changing it made the backend refuse to start. It is back as it was, and the payment details and the invoices table come in 0028. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -1,74 +1,16 @@
|
|||||||
-- Stripe Checkout sessions started from the package cards. The package is
|
-- Stripe Checkout sessions started from the package cards. The package is
|
||||||
-- granted once, when the payment is confirmed (webhook, or the return page
|
-- granted once, when the payment is confirmed (webhook, or the return page
|
||||||
-- asking Stripe), and the session id keeps that from happening twice.
|
-- asking Stripe), and the session id keeps that from happening twice.
|
||||||
-- Card details never reach us: only what Stripe reports about the payment and
|
|
||||||
-- the buyer's billing details for the invoice.
|
|
||||||
CREATE TABLE stripe_checkouts (
|
CREATE TABLE stripe_checkouts (
|
||||||
-- Stripe's Checkout Session id (cs_test_… / cs_live_…).
|
-- Stripe's Checkout Session id (cs_test_… / cs_live_…).
|
||||||
id text PRIMARY KEY,
|
id text PRIMARY KEY,
|
||||||
-- Kept when the account is deleted: payments and invoices must be kept
|
user_id uuid NOT NULL REFERENCES users ON DELETE CASCADE,
|
||||||
-- for the accounting law's 8 years.
|
package text NOT NULL,
|
||||||
user_id uuid REFERENCES users ON DELETE SET NULL,
|
price_huf integer NOT NULL CHECK (price_huf > 0),
|
||||||
package text NOT NULL,
|
live boolean NOT NULL,
|
||||||
price_huf integer NOT NULL CHECK (price_huf > 0),
|
created_at timestamptz NOT NULL DEFAULT now(),
|
||||||
live boolean NOT NULL,
|
completed_at timestamptz,
|
||||||
created_at timestamptz NOT NULL DEFAULT now(),
|
order_id uuid REFERENCES package_orders ON DELETE SET NULL
|
||||||
-- Filled when the payment is confirmed.
|
|
||||||
completed_at timestamptz,
|
|
||||||
order_id uuid REFERENCES package_orders ON DELETE SET NULL,
|
|
||||||
payment_intent text,
|
|
||||||
-- What Stripe charged, in forints.
|
|
||||||
amount_huf integer,
|
|
||||||
-- Stripe's customer_details as it came: name, e-mail, address, and for a
|
|
||||||
-- company its name and tax ids.
|
|
||||||
customer jsonb
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX stripe_checkouts_user_idx ON stripe_checkouts (user_id, created_at DESC);
|
CREATE INDEX stripe_checkouts_user_idx ON stripe_checkouts (user_id, created_at DESC);
|
||||||
|
|
||||||
-- One invoice per paid checkout, issued in Billingo by the worker. Every
|
|
||||||
-- invoice detail is kept here (the buyer and the item as invoiced, Billingo's
|
|
||||||
-- whole answer); the PDF itself is fetched from Billingo when downloaded.
|
|
||||||
CREATE TABLE invoices (
|
|
||||||
id uuid PRIMARY KEY,
|
|
||||||
checkout_id text NOT NULL UNIQUE REFERENCES stripe_checkouts ON DELETE RESTRICT,
|
|
||||||
user_id uuid REFERENCES users ON DELETE SET NULL,
|
|
||||||
-- pending → issued; failed after the last retry (an admin looks at it).
|
|
||||||
status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'issued', 'failed')),
|
|
||||||
attempts integer NOT NULL DEFAULT 0,
|
|
||||||
next_attempt_at timestamptz NOT NULL DEFAULT now(),
|
|
||||||
last_error text,
|
|
||||||
-- The buyer as invoiced.
|
|
||||||
buyer_name text NOT NULL,
|
|
||||||
buyer_email text,
|
|
||||||
buyer_country text NOT NULL,
|
|
||||||
buyer_post_code text NOT NULL,
|
|
||||||
buyer_city text NOT NULL,
|
|
||||||
buyer_address text NOT NULL,
|
|
||||||
-- Billingo's partner tax type: HAS_TAX_NUMBER, NO_TAX_NUMBER or FOREIGN.
|
|
||||||
buyer_tax_type text NOT NULL CHECK (buyer_tax_type IN ('HAS_TAX_NUMBER', 'NO_TAX_NUMBER', 'FOREIGN')),
|
|
||||||
buyer_tax_number text,
|
|
||||||
-- The single item. vat_rate is Billingo's VAT value: AAM (alanyi
|
|
||||||
-- adómentes: net = gross, no VAT) or a rate like 27%.
|
|
||||||
item_name text NOT NULL,
|
|
||||||
gross_huf integer NOT NULL CHECK (gross_huf > 0),
|
|
||||||
net_huf integer NOT NULL,
|
|
||||||
vat_huf integer NOT NULL,
|
|
||||||
vat_rate text NOT NULL DEFAULT 'AAM',
|
|
||||||
payment_method text NOT NULL DEFAULT 'online_bankcard',
|
|
||||||
-- From Billingo.
|
|
||||||
billingo_partner_id bigint,
|
|
||||||
billingo_document_id bigint UNIQUE,
|
|
||||||
invoice_number text,
|
|
||||||
invoice_date date,
|
|
||||||
fulfillment_date date,
|
|
||||||
due_date date,
|
|
||||||
document jsonb,
|
|
||||||
-- When Billingo e-mailed it to the buyer.
|
|
||||||
sent_at timestamptz,
|
|
||||||
created_at timestamptz NOT NULL DEFAULT now(),
|
|
||||||
issued_at timestamptz
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX invoices_user_idx ON invoices (user_id, created_at DESC);
|
|
||||||
CREATE INDEX invoices_due_idx ON invoices (next_attempt_at) WHERE status = 'pending';
|
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
-- What a confirmed payment keeps for the invoice, and the invoices issued in
|
||||||
|
-- Billingo by the worker. Card details never reach us: only what Stripe
|
||||||
|
-- reports about the payment and the buyer's billing details.
|
||||||
|
|
||||||
|
-- Payments outlive a deleted account: the accounting law asks for 8 years.
|
||||||
|
ALTER TABLE stripe_checkouts ALTER COLUMN user_id DROP NOT NULL;
|
||||||
|
ALTER TABLE stripe_checkouts DROP CONSTRAINT stripe_checkouts_user_id_fkey;
|
||||||
|
ALTER TABLE stripe_checkouts ADD CONSTRAINT stripe_checkouts_user_id_fkey
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users ON DELETE SET NULL;
|
||||||
|
|
||||||
|
ALTER TABLE stripe_checkouts
|
||||||
|
ADD COLUMN payment_intent text,
|
||||||
|
-- What Stripe charged, in forints.
|
||||||
|
ADD COLUMN amount_huf integer,
|
||||||
|
-- Stripe's customer_details as it came: name, e-mail, address, and for a
|
||||||
|
-- company its name and tax ids.
|
||||||
|
ADD COLUMN customer jsonb;
|
||||||
|
|
||||||
|
-- One invoice per paid checkout. Every invoice detail is kept here (the buyer
|
||||||
|
-- and the item as invoiced, Billingo's whole answer); the PDF itself is
|
||||||
|
-- fetched from Billingo when downloaded.
|
||||||
|
CREATE TABLE invoices (
|
||||||
|
id uuid PRIMARY KEY,
|
||||||
|
checkout_id text NOT NULL UNIQUE REFERENCES stripe_checkouts ON DELETE RESTRICT,
|
||||||
|
user_id uuid REFERENCES users ON DELETE SET NULL,
|
||||||
|
-- pending → issued; failed after the last retry (an admin looks at it).
|
||||||
|
status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'issued', 'failed')),
|
||||||
|
attempts integer NOT NULL DEFAULT 0,
|
||||||
|
next_attempt_at timestamptz NOT NULL DEFAULT now(),
|
||||||
|
last_error text,
|
||||||
|
-- The buyer as invoiced.
|
||||||
|
buyer_name text NOT NULL,
|
||||||
|
buyer_email text,
|
||||||
|
buyer_country text NOT NULL,
|
||||||
|
buyer_post_code text NOT NULL,
|
||||||
|
buyer_city text NOT NULL,
|
||||||
|
buyer_address text NOT NULL,
|
||||||
|
-- Billingo's partner tax type: HAS_TAX_NUMBER, NO_TAX_NUMBER or FOREIGN.
|
||||||
|
buyer_tax_type text NOT NULL CHECK (buyer_tax_type IN ('HAS_TAX_NUMBER', 'NO_TAX_NUMBER', 'FOREIGN')),
|
||||||
|
buyer_tax_number text,
|
||||||
|
-- The single item. vat_rate is Billingo's VAT value: AAM (alanyi
|
||||||
|
-- adómentes: net = gross, no VAT) or a rate like 27%.
|
||||||
|
item_name text NOT NULL,
|
||||||
|
gross_huf integer NOT NULL CHECK (gross_huf > 0),
|
||||||
|
net_huf integer NOT NULL,
|
||||||
|
vat_huf integer NOT NULL,
|
||||||
|
vat_rate text NOT NULL DEFAULT 'AAM',
|
||||||
|
payment_method text NOT NULL DEFAULT 'online_bankcard',
|
||||||
|
-- From Billingo.
|
||||||
|
billingo_partner_id bigint,
|
||||||
|
billingo_document_id bigint UNIQUE,
|
||||||
|
invoice_number text,
|
||||||
|
invoice_date date,
|
||||||
|
fulfillment_date date,
|
||||||
|
due_date date,
|
||||||
|
document jsonb,
|
||||||
|
-- When Billingo e-mailed it to the buyer.
|
||||||
|
sent_at timestamptz,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT now(),
|
||||||
|
issued_at timestamptz
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX invoices_user_idx ON invoices (user_id, created_at DESC);
|
||||||
|
CREATE INDEX invoices_due_idx ON invoices (next_attempt_at) WHERE status = 'pending';
|
||||||
Reference in New Issue
Block a user