Update users
This commit is contained in:
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS units;
|
||||
@@ -0,0 +1,17 @@
|
||||
-- Units table
|
||||
CREATE TABLE units (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
outlet_id UUID REFERENCES outlets(id) ON DELETE CASCADE,
|
||||
name VARCHAR(50) NOT NULL,
|
||||
abbreviation VARCHAR(10),
|
||||
is_active BOOLEAN DEFAULT true,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_units_organization_id ON units(organization_id);
|
||||
CREATE INDEX idx_units_outlet_id ON units(outlet_id);
|
||||
CREATE INDEX idx_units_is_active ON units(is_active);
|
||||
CREATE INDEX idx_units_created_at ON units(created_at);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS ingredients;
|
||||
@@ -0,0 +1,23 @@
|
||||
-- Ingredients table
|
||||
CREATE TABLE ingredients (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
outlet_id UUID REFERENCES outlets(id) ON DELETE CASCADE,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
unit_id UUID NOT NULL REFERENCES units(id) ON DELETE CASCADE,
|
||||
cost DECIMAL(12,2) DEFAULT 0,
|
||||
stock DECIMAL(12,3) DEFAULT 0,
|
||||
is_semi_finished BOOLEAN DEFAULT false,
|
||||
is_active BOOLEAN DEFAULT true,
|
||||
metadata JSONB DEFAULT '{}',
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_ingredients_organization_id ON ingredients(organization_id);
|
||||
CREATE INDEX idx_ingredients_outlet_id ON ingredients(outlet_id);
|
||||
CREATE INDEX idx_ingredients_unit_id ON ingredients(unit_id);
|
||||
CREATE INDEX idx_ingredients_is_semi_finished ON ingredients(is_semi_finished);
|
||||
CREATE INDEX idx_ingredients_is_active ON ingredients(is_active);
|
||||
CREATE INDEX idx_ingredients_created_at ON ingredients(created_at);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS ingredient_compositions;
|
||||
@@ -0,0 +1,21 @@
|
||||
-- Ingredient compositions table
|
||||
CREATE TABLE ingredient_compositions (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
outlet_id UUID REFERENCES outlets(id) ON DELETE CASCADE,
|
||||
parent_ingredient_id UUID NOT NULL REFERENCES ingredients(id) ON DELETE CASCADE,
|
||||
child_ingredient_id UUID NOT NULL REFERENCES ingredients(id) ON DELETE CASCADE,
|
||||
quantity DECIMAL(12,3) NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_ingredient_compositions_organization_id ON ingredient_compositions(organization_id);
|
||||
CREATE INDEX idx_ingredient_compositions_outlet_id ON ingredient_compositions(outlet_id);
|
||||
CREATE INDEX idx_ingredient_compositions_parent_ingredient_id ON ingredient_compositions(parent_ingredient_id);
|
||||
CREATE INDEX idx_ingredient_compositions_child_ingredient_id ON ingredient_compositions(child_ingredient_id);
|
||||
CREATE INDEX idx_ingredient_compositions_created_at ON ingredient_compositions(created_at);
|
||||
|
||||
-- Unique constraint to prevent duplicate compositions
|
||||
CREATE UNIQUE INDEX idx_ingredient_compositions_unique ON ingredient_compositions(parent_ingredient_id, child_ingredient_id);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS product_ingredients;
|
||||
@@ -0,0 +1,21 @@
|
||||
-- Product ingredients table
|
||||
CREATE TABLE product_ingredients (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
outlet_id UUID REFERENCES outlets(id) ON DELETE CASCADE,
|
||||
product_id UUID NOT NULL REFERENCES products(id) ON DELETE CASCADE,
|
||||
ingredient_id UUID NOT NULL REFERENCES ingredients(id) ON DELETE CASCADE,
|
||||
quantity DECIMAL(12,3) NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_product_ingredients_organization_id ON product_ingredients(organization_id);
|
||||
CREATE INDEX idx_product_ingredients_outlet_id ON product_ingredients(outlet_id);
|
||||
CREATE INDEX idx_product_ingredients_product_id ON product_ingredients(product_id);
|
||||
CREATE INDEX idx_product_ingredients_ingredient_id ON product_ingredients(ingredient_id);
|
||||
CREATE INDEX idx_product_ingredients_created_at ON product_ingredients(created_at);
|
||||
|
||||
-- Unique constraint to prevent duplicate product-ingredient combinations
|
||||
CREATE UNIQUE INDEX idx_product_ingredients_unique ON product_ingredients(product_id, ingredient_id);
|
||||
@@ -0,0 +1,4 @@
|
||||
-- Remove unit_id and has_ingredients from products table
|
||||
ALTER TABLE products
|
||||
DROP COLUMN IF EXISTS unit_id,
|
||||
DROP COLUMN IF EXISTS has_ingredients;
|
||||
@@ -0,0 +1,8 @@
|
||||
-- Add unit_id and has_ingredients to products table
|
||||
ALTER TABLE products
|
||||
ADD COLUMN unit_id UUID REFERENCES units(id) ON DELETE SET NULL,
|
||||
ADD COLUMN has_ingredients BOOLEAN DEFAULT false;
|
||||
|
||||
-- Index for unit_id
|
||||
CREATE INDEX idx_products_unit_id ON products(unit_id);
|
||||
CREATE INDEX idx_products_has_ingredients ON products(has_ingredients);
|
||||
@@ -0,0 +1,30 @@
|
||||
-- Revert inventory_movements table changes
|
||||
-- Add back product_id column
|
||||
ALTER TABLE inventory_movements
|
||||
ADD COLUMN product_id UUID;
|
||||
|
||||
-- Copy item_id data back to product_id where item_type is 'PRODUCT'
|
||||
UPDATE inventory_movements
|
||||
SET product_id = item_id
|
||||
WHERE item_type = 'PRODUCT';
|
||||
|
||||
-- Drop the new columns
|
||||
ALTER TABLE inventory_movements
|
||||
DROP COLUMN item_id,
|
||||
DROP COLUMN item_type;
|
||||
|
||||
-- Revert quantity columns to integer
|
||||
ALTER TABLE inventory_movements
|
||||
ALTER COLUMN quantity TYPE INTEGER USING quantity::integer,
|
||||
ALTER COLUMN previous_quantity TYPE INTEGER USING previous_quantity::integer,
|
||||
ALTER COLUMN new_quantity TYPE INTEGER USING new_quantity::integer;
|
||||
|
||||
-- Revert cost columns to original precision
|
||||
ALTER TABLE inventory_movements
|
||||
ALTER COLUMN unit_cost TYPE DECIMAL(10,2),
|
||||
ALTER COLUMN total_cost TYPE DECIMAL(10,2);
|
||||
|
||||
-- Drop the new indexes
|
||||
DROP INDEX IF EXISTS idx_inventory_movements_item_id;
|
||||
DROP INDEX IF EXISTS idx_inventory_movements_item_type;
|
||||
DROP INDEX IF EXISTS idx_inventory_movements_item_id_type;
|
||||
@@ -0,0 +1,35 @@
|
||||
-- Update inventory_movements table to support ingredients
|
||||
ALTER TABLE inventory_movements
|
||||
ADD COLUMN item_id UUID,
|
||||
ADD COLUMN item_type VARCHAR(20);
|
||||
|
||||
-- Copy existing product_id data to item_id
|
||||
UPDATE inventory_movements
|
||||
SET item_id = product_id,
|
||||
item_type = 'PRODUCT'
|
||||
WHERE product_id IS NOT NULL;
|
||||
|
||||
-- Make item_id and item_type NOT NULL after data migration
|
||||
ALTER TABLE inventory_movements
|
||||
ALTER COLUMN item_id SET NOT NULL,
|
||||
ALTER COLUMN item_type SET NOT NULL;
|
||||
|
||||
-- Drop the old product_id column
|
||||
ALTER TABLE inventory_movements
|
||||
DROP COLUMN product_id;
|
||||
|
||||
-- Update quantity columns to support decimal
|
||||
ALTER TABLE inventory_movements
|
||||
ALTER COLUMN quantity TYPE DECIMAL(12,3),
|
||||
ALTER COLUMN previous_quantity TYPE DECIMAL(12,3),
|
||||
ALTER COLUMN new_quantity TYPE DECIMAL(12,3);
|
||||
|
||||
-- Update cost columns to support higher precision
|
||||
ALTER TABLE inventory_movements
|
||||
ALTER COLUMN unit_cost TYPE DECIMAL(12,2),
|
||||
ALTER COLUMN total_cost TYPE DECIMAL(12,2);
|
||||
|
||||
-- Add indexes for the new structure
|
||||
CREATE INDEX idx_inventory_movements_item_id ON inventory_movements(item_id);
|
||||
CREATE INDEX idx_inventory_movements_item_type ON inventory_movements(item_type);
|
||||
CREATE INDEX idx_inventory_movements_item_id_type ON inventory_movements(item_id, item_type);
|
||||
Reference in New Issue
Block a user