Files
mercadodevida/project/migrations/012_pricing.js
2026-08-17 22:23:10 +02:00

53 lines
2.2 KiB
JavaScript

/**
* Pricing variant prices and append-only price history.
*/
/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
export const up = (pgm) => {
pgm.sql(`
CREATE TABLE pricing_variant_prices (
variant_id uuid PRIMARY KEY,
net_unit_amount_cents integer NOT NULL,
vat_rate text NOT NULL,
currency text NOT NULL DEFAULT 'EUR',
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT pricing_variant_prices_amount_non_negative CHECK (net_unit_amount_cents >= 0),
CONSTRAINT pricing_variant_prices_vat_rate_check CHECK (vat_rate IN ('general', 'reduced')),
CONSTRAINT pricing_variant_prices_currency_check CHECK (currency = 'EUR')
)
`);
pgm.sql(`
CREATE TABLE pricing_price_history (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
variant_id uuid NOT NULL,
previous_net_unit_amount_cents integer,
previous_vat_rate text,
previous_currency text,
new_net_unit_amount_cents integer NOT NULL,
new_vat_rate text NOT NULL,
new_currency text NOT NULL DEFAULT 'EUR',
created_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT pricing_price_history_previous_amount_non_negative
CHECK (previous_net_unit_amount_cents IS NULL OR previous_net_unit_amount_cents >= 0),
CONSTRAINT pricing_price_history_new_amount_non_negative CHECK (new_net_unit_amount_cents >= 0),
CONSTRAINT pricing_price_history_previous_vat_rate_check
CHECK (previous_vat_rate IS NULL OR previous_vat_rate IN ('general', 'reduced')),
CONSTRAINT pricing_price_history_new_vat_rate_check CHECK (new_vat_rate IN ('general', 'reduced')),
CONSTRAINT pricing_price_history_previous_currency_check
CHECK (previous_currency IS NULL OR previous_currency = 'EUR'),
CONSTRAINT pricing_price_history_new_currency_check CHECK (new_currency = 'EUR')
)
`);
pgm.sql(
'CREATE INDEX pricing_price_history_variant_id_idx ON pricing_price_history (variant_id)',
);
};
/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
export const down = (pgm) => {
pgm.sql('DROP TABLE IF EXISTS pricing_price_history');
pgm.sql('DROP TABLE IF EXISTS pricing_variant_prices');
};