Füge Spalte product_quality zur Tabelle stock hinzu und erstelle Migration für weather_type_id in production
This commit is contained in:
11
backend/sql/add_product_quality_to_stock.sql
Normal file
11
backend/sql/add_product_quality_to_stock.sql
Normal 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
|
||||
38
backend/sql/add_weather_type_to_production.sql
Normal file
38
backend/sql/add_weather_type_to_production.sql
Normal 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
|
||||
Reference in New Issue
Block a user