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

47 lines
1.7 KiB
JavaScript

// migration: 024_product_channels_attributes
// Adds sales channels, featured flag, and attribute tags to the catalog_products table
/** @param {import('pg').Pool} pool */
exports.up = async (pool) => {
// Add channels column
await pool.query(`
ALTER TABLE catalog_products
ADD COLUMN IF NOT EXISTS channels TEXT NOT NULL DEFAULT 'all'
CHECK (channels IN ('online', 'offline', 'all'))
`);
// Add featured column
await pool.query(`
ALTER TABLE catalog_products
ADD COLUMN IF NOT EXISTS featured BOOLEAN NOT NULL DEFAULT FALSE
`);
// Add attributes column (stored as JSONB array of strings)
await pool.query(`
ALTER TABLE catalog_products
ADD COLUMN IF NOT EXISTS attributes JSONB NOT NULL DEFAULT '[]'::jsonb
`);
// Add offer_cents and cost_cents to pricing_variant_prices
await pool.query(`
ALTER TABLE pricing_variant_prices
ADD COLUMN IF NOT EXISTS offer_cents INTEGER,
ADD COLUMN IF NOT EXISTS cost_cents INTEGER
`);
// Create index for featured products (common query)
await pool.query(`
CREATE INDEX IF NOT EXISTS catalog_products_featured_idx
ON catalog_products (featured) WHERE featured = TRUE
`);
};
exports.down = async (pool) => {
await pool.query(`ALTER TABLE catalog_products DROP COLUMN IF EXISTS channels`);
await pool.query(`ALTER TABLE catalog_products DROP COLUMN IF EXISTS featured`);
await pool.query(`ALTER TABLE catalog_products DROP COLUMN IF EXISTS attributes`);
await pool.query(`ALTER TABLE pricing_variant_prices DROP COLUMN IF EXISTS offer_cents`);
await pool.query(`ALTER TABLE pricing_variant_prices DROP COLUMN IF EXISTS cost_cents`);
await pool.query(`DROP INDEX IF EXISTS catalog_products_featured_idx`);
};