From 36417bfdf3e7e3f79c8cde30693b3fe631b756c8 Mon Sep 17 00:00:00 2001 From: Lars Date: Thu, 9 Apr 2026 21:42:11 +0200 Subject: [PATCH] refactor: Rename csv_import to data_import and update foreign key references - Changed feature ID from 'csv_import' to 'data_import' in the features table. - Updated foreign key references in tier_limits, user_feature_restrictions, user_feature_usage, and widget_feature_requirements. - Removed the old 'csv_import' feature entry after ensuring all references are updated. - Simplified the migration process by using an INSERT with ON CONFLICT for the new feature entry. --- backend/migrations/v9c_cleanup_features.sql | 48 +++++++++++++-------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/backend/migrations/v9c_cleanup_features.sql b/backend/migrations/v9c_cleanup_features.sql index 9acfbde..27072b0 100644 --- a/backend/migrations/v9c_cleanup_features.sql +++ b/backend/migrations/v9c_cleanup_features.sql @@ -12,30 +12,48 @@ -- ============================================================================ -- ============================================================================ --- 1. Rename csv_import to data_import +-- 1. csv_import → data_import (FK-sicher) -- ============================================================================ -UPDATE features -SET - id = 'data_import', - name = 'Daten importieren', - description = 'CSV-Import (FDDB, Apple Health) + ZIP-Backup-Import' -WHERE id = 'csv_import'; +-- Zuerst Ziel-Feature-Zeile sichern, dann alle FKs umhängen, dann csv_import +-- entfernen. PK direkt per UPDATE ändern scheitert, solange tier_limits noch +-- feature_id = 'csv_import' referenziert (tier_limits_feature_id_fkey). + +INSERT INTO features (id, name, description, category, limit_type, reset_period, default_limit, active) +VALUES ('data_import', 'Daten importieren', 'CSV-Import (FDDB, Apple Health) + ZIP-Backup-Import', 'import', 'count', 'monthly', 0, true) +ON CONFLICT (id) DO UPDATE SET + name = EXCLUDED.name, + description = EXCLUDED.description, + category = EXCLUDED.category, + limit_type = EXCLUDED.limit_type, + reset_period = EXCLUDED.reset_period; --- Update tier_limits references UPDATE tier_limits SET feature_id = 'data_import' WHERE feature_id = 'csv_import'; --- Update user_feature_restrictions references UPDATE user_feature_restrictions SET feature_id = 'data_import' WHERE feature_id = 'csv_import'; --- Update user_feature_usage references UPDATE user_feature_usage SET feature_id = 'data_import' WHERE feature_id = 'csv_import'; +-- Widget-Gateway (Migration 041): sonst blockiert FK beim Löschen von csv_import +DO $$ +BEGIN + IF EXISTS ( + SELECT 1 FROM information_schema.tables + WHERE table_schema = 'public' AND table_name = 'widget_feature_requirements' + ) THEN + UPDATE widget_feature_requirements + SET feature_id = 'data_import' + WHERE feature_id = 'csv_import'; + END IF; +END $$; + +DELETE FROM features WHERE id = 'csv_import'; + -- ============================================================================ -- 2. Remove old export_csv/json/zip features -- ============================================================================ @@ -69,16 +87,8 @@ ON CONFLICT (id) DO UPDATE SET reset_period = EXCLUDED.reset_period; -- ============================================================================ --- 4. Ensure data_import exists and is properly configured +-- 4. data_import: in Schritt 1 angelegt; kein zweites INSERT nötig -- ============================================================================ -INSERT INTO features (id, name, description, category, limit_type, reset_period, default_limit, active) -VALUES ('data_import', 'Daten importieren', 'CSV-Import (FDDB, Apple Health) + ZIP-Backup-Import', 'import', 'count', 'monthly', 0, true) -ON CONFLICT (id) DO UPDATE SET - name = EXCLUDED.name, - description = EXCLUDED.description, - category = EXCLUDED.category, - limit_type = EXCLUDED.limit_type, - reset_period = EXCLUDED.reset_period; -- ============================================================================ -- 5. Update tier_limits for data_export (consolidate from old features)