Füge Spalte product_quality zur Tabelle stock hinzu und erstelle Migration für weather_type_id in production

This commit is contained in:
Torsten Schulz (local)
2025-12-16 13:00:29 +01:00
parent 43d86cce18
commit ee4b0ee7c2
11 changed files with 325 additions and 16 deletions

View File

@@ -0,0 +1,11 @@
-- Migration script: add_product_quality_to_stock.sql
-- Fügt die Spalte product_quality zur Tabelle falukant_data.stock hinzu (nullable, idempotent)
BEGIN;
ALTER TABLE IF EXISTS falukant_data.stock
ADD COLUMN IF NOT EXISTS product_quality integer;
COMMIT;
-- Ende

View File

@@ -0,0 +1,38 @@
-- Migration script: add_weather_type_to_production.sql
-- Legt die Spalte weather_type_id in falukant_data.production an,
-- fügt optional einen Foreign Key zu falukant_type.weather(id) hinzu
-- und erstellt einen Index. Idempotent (mehrfaches Ausführen ist unproblematisch).
BEGIN;
-- 1) Spalte anlegen (nullable, idempotent)
ALTER TABLE IF EXISTS falukant_data.production
ADD COLUMN IF NOT EXISTS weather_type_id integer;
-- 2) Fremdschlüssel nur hinzufügen, falls noch kein FK für diese Spalte existiert
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
ON kcu.constraint_name = tc.constraint_name
AND kcu.constraint_schema = tc.constraint_schema
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.constraint_schema = 'falukant_data'
AND tc.table_name = 'production'
AND kcu.column_name = 'weather_type_id'
) THEN
ALTER TABLE falukant_data.production
ADD CONSTRAINT fk_production_weather_type
FOREIGN KEY (weather_type_id) REFERENCES falukant_type.weather(id);
END IF;
END$$;
-- 3) Index (Postgres: CREATE INDEX IF NOT EXISTS)
CREATE INDEX IF NOT EXISTS idx_production_weather_type_id
ON falukant_data.production (weather_type_id);
COMMIT;
-- Ende