Files
website/drizzle/20261027225743_tools/migration.sql
T

58 lines
3.0 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
CREATE SCHEMA "tools";
--> statement-breakpoint
CREATE TABLE "tools"."items" (
"id" serial PRIMARY KEY,
"name" text NOT NULL,
"brand" text,
"model" text,
"category" text DEFAULT 'other' NOT NULL,
"quantity" integer DEFAULT 1 NOT NULL,
"location" text,
"purchased_on" date,
"price_eur" numeric(10,2),
"specs" jsonb DEFAULT '{}' NOT NULL,
"notes" text,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "items_name_check" CHECK (length(btrim("name")) > 0),
CONSTRAINT "items_quantity_check" CHECK ("quantity" >= 0),
CONSTRAINT "items_price_check" CHECK ("price_eur" >= 0)
);
--> statement-breakpoint
CREATE UNIQUE INDEX "items_name_key" ON "tools"."items" (lower("name"));--> statement-breakpoint
CREATE INDEX "items_category_idx" ON "tools"."items" ("category");
--> statement-breakpoint
-- Stan's first items (27 Oct 2026). Specs only where verified: CR2032/CR2016 are standard sizes;
-- the LV1R specs are from Kiprim's product page; every Bosch IXO generation is 3.6 V with a
-- built-in Li-ion battery (which generation Stan has isn't known yet).
INSERT INTO "tools"."items" (name, brand, model, category, quantity, specs, notes) VALUES
('AAA batteries', NULL, NULL, 'batteries', 32, '{"size": "AAA"}', NULL),
('CR2032 coin cells', NULL, 'CR2032', 'batteries', 14,
'{"chemistry": "lithium", "voltage": "3 V", "size": "20 × 3.2 mm"}', NULL),
('CR2016 coin cell', NULL, 'CR2016', 'batteries', 1,
'{"chemistry": "lithium", "voltage": "3 V", "size": "20 × 1.6 mm"}', NULL),
('Kiprim LV1R laser level', 'Kiprim', 'LV1R', 'measuring', 1,
'{"type": "self-levelling cross-line laser", "laser": "red, 635 nm, class 2", "accuracy": "±2.5 mm at 5 m", "self_levelling": "±4°", "power": "2× AA", "ip_rating": "IP54", "mount": "1/4\" tripod thread"}',
NULL),
('Bosch IXO cordless screwdriver', 'Bosch', 'IXO', 'power_tools', 1,
'{"type": "cordless screwdriver", "voltage": "3.6 V", "battery": "built-in Li-ion"}',
'Small green drill-shaped electric screwdriver. Which IXO generation is not recorded yet.')
ON CONFLICT ((lower(name))) DO NOTHING;
--> statement-breakpoint
-- Privacy: owner-only on the site (ownerOnlyPrefixes). Like radio, plant and notes, only the app
-- role can read or write (no personal_ro). Grants only run on prod (migrations as `personal`).
REVOKE ALL ON SCHEMA tools FROM PUBLIC;
--> statement-breakpoint
DO $$
BEGIN
IF current_user = 'personal' AND EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'personal_app') THEN
GRANT USAGE ON SCHEMA tools TO personal_app;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA tools TO personal_app;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA tools TO personal_app;
ALTER DEFAULT PRIVILEGES FOR ROLE personal IN SCHEMA tools
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO personal_app;
ALTER DEFAULT PRIVILEGES FOR ROLE personal IN SCHEMA tools
GRANT USAGE, SELECT ON SEQUENCES TO personal_app;
END IF;
END $$;