40 lines
1.5 KiB
JavaScript
40 lines
1.5 KiB
JavaScript
/** Shipping zones and methods. */
|
|
|
|
/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
|
|
export const up = (pgm) => {
|
|
pgm.sql(`
|
|
CREATE TABLE shipping_zones (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
name text NOT NULL,
|
|
country text NOT NULL,
|
|
postal_code_prefix text,
|
|
active boolean NOT NULL DEFAULT true,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
updated_at timestamptz NOT NULL DEFAULT now()
|
|
)
|
|
`);
|
|
pgm.sql('CREATE INDEX shipping_zones_country_idx ON shipping_zones (country, active)');
|
|
|
|
pgm.sql(`
|
|
CREATE TABLE shipping_methods (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
zone_id uuid NOT NULL REFERENCES shipping_zones(id) ON DELETE CASCADE,
|
|
name text NOT NULL,
|
|
base_cost_cents integer NOT NULL,
|
|
free_shipping_threshold_cents integer,
|
|
active boolean NOT NULL DEFAULT true,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
CONSTRAINT shipping_methods_base_cost_non_negative CHECK (base_cost_cents >= 0),
|
|
CONSTRAINT shipping_methods_threshold_non_negative CHECK (free_shipping_threshold_cents IS NULL OR free_shipping_threshold_cents >= 0)
|
|
)
|
|
`);
|
|
pgm.sql('CREATE INDEX shipping_methods_zone_id_idx ON shipping_methods (zone_id)');
|
|
};
|
|
|
|
/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
|
|
export const down = (pgm) => {
|
|
pgm.sql('DROP TABLE IF EXISTS shipping_methods');
|
|
pgm.sql('DROP TABLE IF EXISTS shipping_zones');
|
|
};
|