feat: Training Types → Focus Area Hierarchie
Migration 013: - Adds focus_area_id to training_types (context-specific types) - Migrates existing data to Karate focus area - Seeds focus-area-specific training types: * Karate: Dan-Vorbereitung * Gewaltschutz: Präventivkurse, Intensivtraining, Spezialkurse * Fitness: Gesundheitssport, Funktionelles Training - Updates unique constraint to (name, focus_area_id) Backend (catalogs.py): - list_training_types: Added focus_area_id filter, LEFT JOIN focus_areas - create_training_type: Added focus_area_id parameter - update_training_type: Added focus_area_id parameter - Enriched responses with focus_area_name and focus_area_icon Frontend (AdminCatalogsPage): - Added Fokusbereich dropdown to create form - Added Fokusbereich dropdown to edit form - Display shows focus_area_icon and focus_area_name - Training types now context-specific to focus areas Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9f44cff77b
commit
fe5d29e40e
3 files changed
+212
-10
No files matched your search
@@ -0,0 +1,141 @@
|
||||
-- Migration 013: Training Types → Focus Area Hierarchie
|
||||
-- Author: Claude Code
|
||||
-- Date: 2026-04-23
|
||||
-- Purpose: Training Types sind kontextabhängig vom Fokusbereich
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
|
||||
-- ============================================================================
|
||||
-- ADD focus_area_id TO training_types
|
||||
-- ============================================================================
|
||||
|
||||
-- Add focus_area_id column if not exists
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_name = 'training_types'
|
||||
AND column_name = 'focus_area_id'
|
||||
) THEN
|
||||
ALTER TABLE training_types
|
||||
ADD COLUMN focus_area_id INT REFERENCES focus_areas(id) ON DELETE CASCADE;
|
||||
|
||||
CREATE INDEX idx_training_types_focus_area ON training_types(focus_area_id);
|
||||
|
||||
RAISE NOTICE 'Added focus_area_id to training_types';
|
||||
END IF;
|
||||
|
||||
|
||||
-- ============================================================================
|
||||
-- MIGRATE EXISTING DATA (assign to Karate if exists)
|
||||
-- ============================================================================
|
||||
|
||||
-- Assign existing training types to Karate (if Karate focus area exists)
|
||||
DO $migrate$
|
||||
DECLARE
|
||||
karate_id INT;
|
||||
BEGIN
|
||||
-- Get Karate focus area ID
|
||||
SELECT id INTO karate_id FROM focus_areas WHERE name = 'Karate' LIMIT 1;
|
||||
|
||||
IF karate_id IS NOT NULL THEN
|
||||
-- Assign existing training types to Karate
|
||||
UPDATE training_types
|
||||
SET focus_area_id = karate_id
|
||||
WHERE focus_area_id IS NULL;
|
||||
|
||||
RAISE NOTICE 'Assigned existing training types to Karate (focus_area_id: %)', karate_id;
|
||||
ELSE
|
||||
RAISE NOTICE 'No Karate focus area found - skipping migration';
|
||||
END IF;
|
||||
END $migrate$;
|
||||
|
||||
|
||||
-- ============================================================================
|
||||
-- SEED EXAMPLE DATA (focus-area-specific training types)
|
||||
-- ============================================================================
|
||||
|
||||
DO $seed$
|
||||
DECLARE
|
||||
karate_id INT;
|
||||
gewaltschutz_id INT;
|
||||
fitness_id INT;
|
||||
BEGIN
|
||||
-- Get focus area IDs
|
||||
SELECT id INTO karate_id FROM focus_areas WHERE name = 'Karate' LIMIT 1;
|
||||
SELECT id INTO gewaltschutz_id FROM focus_areas WHERE name = 'Gewaltschutz' LIMIT 1;
|
||||
SELECT id INTO fitness_id FROM focus_areas WHERE name = 'Fitness' LIMIT 1;
|
||||
|
||||
-- Karate-specific training types (if Karate exists)
|
||||
IF karate_id IS NOT NULL THEN
|
||||
INSERT INTO training_types (name, abbreviation, description, focus_area_id, sort_order, status)
|
||||
VALUES
|
||||
('Dan-Vorbereitung', 'DAN', 'Vorbereitung auf Dan-Prüfungen', karate_id, 4, 'active')
|
||||
ON CONFLICT (name) DO NOTHING;
|
||||
END IF;
|
||||
|
||||
-- Gewaltschutz-specific training types (if exists)
|
||||
IF gewaltschutz_id IS NOT NULL THEN
|
||||
INSERT INTO training_types (name, abbreviation, description, focus_area_id, sort_order, status)
|
||||
VALUES
|
||||
('Präventivkurse', 'PREV', 'Präventive Selbstverteidigung und Deeskalation', gewaltschutz_id, 1, 'active'),
|
||||
('Intensivtraining', 'INT', 'Intensives Selbstverteidigungs-Training', gewaltschutz_id, 2, 'active'),
|
||||
('Spezialkurse', 'SPEC', 'Spezielle Kurse (Frauen, Sicherheitspersonal, etc.)', gewaltschutz_id, 3, 'active')
|
||||
ON CONFLICT (name) DO NOTHING;
|
||||
END IF;
|
||||
|
||||
-- Fitness-specific training types (if exists)
|
||||
IF fitness_id IS NOT NULL THEN
|
||||
INSERT INTO training_types (name, abbreviation, description, focus_area_id, sort_order, status)
|
||||
VALUES
|
||||
('Gesundheitssport', 'GES', 'Gesundheitsorientiertes Training', fitness_id, 1, 'active'),
|
||||
('Funktionelles Training', 'FUNKT', 'Funktionelle Fitness und Beweglichkeit', fitness_id, 2, 'active')
|
||||
ON CONFLICT (name) DO NOTHING;
|
||||
END IF;
|
||||
|
||||
RAISE NOTICE 'Seeded focus-area-specific training types';
|
||||
END $seed$;
|
||||
|
||||
|
||||
-- ============================================================================
|
||||
-- UPDATE UNIQUE CONSTRAINT (if needed)
|
||||
-- ============================================================================
|
||||
|
||||
-- Drop old unique constraint if exists
|
||||
DO $constraint$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM pg_constraint
|
||||
WHERE conname = 'training_types_name_key'
|
||||
) THEN
|
||||
ALTER TABLE training_types DROP CONSTRAINT training_types_name_key;
|
||||
RAISE NOTICE 'Dropped old unique constraint on name';
|
||||
END IF;
|
||||
END $constraint$;
|
||||
|
||||
-- Add new unique constraint (name per focus_area)
|
||||
DO $constraint$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_constraint
|
||||
WHERE conname = 'training_types_name_focus_area_key'
|
||||
) THEN
|
||||
ALTER TABLE training_types
|
||||
ADD CONSTRAINT training_types_name_focus_area_key
|
||||
UNIQUE (name, focus_area_id);
|
||||
|
||||
RAISE NOTICE 'Added unique constraint: name per focus_area';
|
||||
END IF;
|
||||
END $constraint$;
|
||||
|
||||
|
||||
-- ============================================================================
|
||||
-- MIGRATION TRACKING
|
||||
-- ============================================================================
|
||||
|
||||
INSERT INTO schema_migrations (version, description)
|
||||
VALUES (13, 'training_types focus_area hierarchy')
|
||||
ON CONFLICT (version) DO NOTHING;
|
||||
|
||||
RAISE NOTICE 'Migration 013 completed successfully';
|
||||
|
||||
END $$;
|
||||
Reference in new issue
Block a user