seed stuff

This commit is contained in:
Jake Kasper
2025-09-03 10:56:17 -04:00
parent 6dbdba0e38
commit e7cbaf844f
8 changed files with 271 additions and 28 deletions

View File

@@ -0,0 +1,41 @@
-- Migration: add JSONB grass types to lawn_sections, enable seed products, and store seeding type on logs
-- 1) Lawn sections: add grass_types JSONB and backfill from grass_type CSV
ALTER TABLE lawn_sections
ADD COLUMN IF NOT EXISTS grass_types JSONB;
-- Backfill: split grass_type CSV to JSON array when present
UPDATE lawn_sections
SET grass_types = (
CASE
WHEN grass_type IS NULL OR trim(grass_type) = '' THEN NULL
ELSE (
SELECT jsonb_agg(trim(x)) FROM (
SELECT regexp_split_to_table(grass_type, '\s*,\s*') AS x
) s WHERE trim(x) <> ''
)
END
)
WHERE grass_types IS NULL;
-- 2) Products: allow 'seed' type and add seed_blend JSONB
DO $$
DECLARE cname text;
BEGIN
SELECT conname INTO cname
FROM pg_constraint
WHERE conrelid = 'products'::regclass AND contype='c' AND conname LIKE '%product_type%';
IF cname IS NOT NULL THEN
EXECUTE format('ALTER TABLE products DROP CONSTRAINT %I', cname);
END IF;
END $$;
ALTER TABLE products
ADD CONSTRAINT products_product_type_check CHECK (product_type IN ('granular','liquid','seed'));
ALTER TABLE products
ADD COLUMN IF NOT EXISTS seed_blend JSONB;
-- 3) Application logs: store seeding type
ALTER TABLE application_logs
ADD COLUMN IF NOT EXISTS seeding_type VARCHAR(20) CHECK (seeding_type IN ('overseed','new_seed'));