Spaces:
Sleeping
Sleeping
Download supabase/schema.sql from Netrisyl/Lobels-Stores-Assistant: direct link, hf CLI and curl.
- Browser
- Download file 3.81 kB
-
https://huggingface.co/spaces/Netrisyl/Lobels-Stores-Assistant/resolve/main/supabase/schema.sql
- Command line
-
hf download hf://spaces/Netrisyl/Lobels-Stores-Assistant/supabase/schema.sql
-
curl -L -o schema.sql https://huggingface.co/spaces/Netrisyl/Lobels-Stores-Assistant/resolve/main/supabase/schema.sql
3.81 kB
| -- ===================================================================== | |
| -- Lobels Stores Intelligence Assistant -- Database Schema | |
| -- Stack: Supabase (PostgreSQL) | |
| -- Run in Supabase -> SQL Editor. Safe to re-run. | |
| -- | |
| -- Multi-client design: lobels_stores carries a client_id column, and the | |
| -- app filters every query on it (CLIENT_ID = "lobels" in app.py). | |
| -- lobels_materials is a global reference table with no client_id. | |
| -- | |
| -- Access: the app and the sheet sync both use the service key, which | |
| -- bypasses RLS. RLS is enabled with no policies, so the anon and | |
| -- authenticated roles cannot read or write these tables. | |
| -- ===================================================================== | |
| -- ββ Daily transactions (one row per material per day with issues) βββ | |
| CREATE TABLE IF NOT EXISTS lobels_stores ( | |
| id BIGSERIAL PRIMARY KEY, | |
| client_id TEXT NOT NULL DEFAULT 'lobels', | |
| txn_date DATE NOT NULL, | |
| stock_code TEXT NOT NULL, | |
| description TEXT NOT NULL, | |
| category TEXT, | |
| month TEXT, | |
| year INTEGER, | |
| daily_issues NUMERIC(14,3) DEFAULT 0, | |
| opening_stock NUMERIC(14,3) DEFAULT 0, | |
| receipts NUMERIC(14,3) DEFAULT 0, | |
| transfer_from_sweets NUMERIC(14,3) DEFAULT 0, | |
| transfer_to_sweets NUMERIC(14,3) DEFAULT 0, | |
| syrup NUMERIC(14,3) DEFAULT 0, | |
| canteen_others NUMERIC(14,3) DEFAULT 0, | |
| total_issues NUMERIC(14,3) DEFAULT 0, | |
| theoretical_closing NUMERIC(14,3) DEFAULT 0, | |
| physical_closing NUMERIC(14,3) DEFAULT 0, | |
| variance NUMERIC(14,3) DEFAULT 0, | |
| variance_flag TEXT, | |
| unit_cost_usd NUMERIC(14,4), | |
| value_usd NUMERIC(14,2) | |
| ); | |
| -- ββ Materials master (reference data; app reads unit_cost_usd, supplier, | |
| -- reorder_point, lead_time_days) βββββββββββββββββββββββββββββββββββ | |
| -- stock_code is NOT unique: the source Materials Master reuses 1602 and | |
| -- 1604 for different materials, and the app joins on description. | |
| CREATE TABLE IF NOT EXISTS lobels_materials ( | |
| id BIGSERIAL PRIMARY KEY, | |
| stock_code TEXT NOT NULL, | |
| description TEXT NOT NULL UNIQUE, | |
| category TEXT, | |
| unit TEXT DEFAULT 'kg', | |
| reorder_point NUMERIC(14,3), | |
| lead_time_days INTEGER, | |
| unit_cost_usd NUMERIC(14,4), | |
| supplier TEXT, | |
| notes TEXT | |
| ); | |
| -- ββ Indexes for chatbot queries βββββββββββββββββββββββββββββββββββββ | |
| CREATE INDEX IF NOT EXISTS idx_lobels_client ON lobels_stores (client_id); | |
| CREATE INDEX IF NOT EXISTS idx_lobels_date ON lobels_stores (txn_date DESC); | |
| CREATE INDEX IF NOT EXISTS idx_lobels_code ON lobels_stores (stock_code); | |
| CREATE INDEX IF NOT EXISTS idx_lobels_month ON lobels_stores (client_id, month); | |
| CREATE INDEX IF NOT EXISTS idx_lobels_category ON lobels_stores (category); | |
| CREATE INDEX IF NOT EXISTS idx_lobels_materials_desc ON lobels_materials (description); | |
| -- ββ Monthly totals per material (not used by app.py; handy for ad hoc SQL) | |
| CREATE OR REPLACE VIEW lobels_monthly_summary | |
| WITH (security_invoker = true) AS | |
| SELECT | |
| client_id, | |
| stock_code, | |
| description, | |
| category, | |
| month, | |
| year, | |
| SUM(daily_issues) AS month_issues, | |
| AVG(variance) AS avg_variance | |
| FROM lobels_stores | |
| GROUP BY client_id, stock_code, description, category, month, year; | |
| -- ββ Lock down: service key only βββββββββββββββββββββββββββββββββββββ | |
| ALTER TABLE lobels_stores ENABLE ROW LEVEL SECURITY; | |
| ALTER TABLE lobels_materials ENABLE ROW LEVEL SECURITY; | |