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

46 lines
1.7 KiB
JavaScript

/**
* Catalog product variants and rich product data.
*/
/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
export const up = (pgm) => {
pgm.sql(`
CREATE TABLE catalog_product_variants (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
product_id uuid NOT NULL REFERENCES catalog_products(id) ON DELETE CASCADE,
sku text NOT NULL UNIQUE,
ean text UNIQUE,
attributes jsonb NOT NULL DEFAULT '{}'::jsonb,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
)
`);
pgm.sql(
'CREATE INDEX catalog_product_variants_product_id_idx ON catalog_product_variants (product_id)',
);
pgm.sql(`
CREATE TABLE catalog_product_rich_data (
product_id uuid PRIMARY KEY REFERENCES catalog_products(id) ON DELETE CASCADE,
ingredients text,
allergens text[] NOT NULL DEFAULT ARRAY[]::text[],
nutrition jsonb,
nutrition_source text,
is_organic boolean NOT NULL DEFAULT false,
organic_certification text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT catalog_product_rich_data_nutrition_source_check
CHECK (nutrition_source IS NULL OR nutrition_source IN ('manual', 'manufacturer', 'openfoodfacts')),
CONSTRAINT catalog_product_rich_data_nutrition_source_required_check
CHECK (nutrition IS NULL OR nutrition_source IS NOT NULL)
)
`);
};
/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
export const down = (pgm) => {
pgm.sql('DROP TABLE IF EXISTS catalog_product_rich_data');
pgm.sql('DROP TABLE IF EXISTS catalog_product_variants');
};