umami/prisma/migrations
iskandarsulaili 7c1db78050 feat: Add UserMapping constraints and schema improvements
## Database Schema Enhancements

### Migration 19 - Fixed
- Removed invalid composite index on session(visitor_id, user_id)
- visitor_id column doesn't exist in session table
- Fixed migration to only create valid indexes

### Migration 20 - New Constraints (with rollback support)
- Added websiteId foreign key to UserMapping table
- Added foreign key constraint to Website table for referential integrity
- Added composite index on UserMapping(websiteId, userId) for query performance
- Added composite index on Session(websiteId, userId) for join optimization

### Schema Updates
- Updated UserMapping model with websiteId field
- Added foreign key relationship: UserMapping -> Website
- Added userMappings relation to Website model
- Added composite indexes for better query performance

## Files Changed

### Modified
- prisma/schema.prisma - Added foreign keys and composite indexes
- prisma/migrations/19_add_user_id_mapping/migration.sql - Fixed invalid index

### New
- prisma/migrations/20_add_user_mapping_constraints/migration.sql - Forward migration
- prisma/migrations/20_add_user_mapping_constraints/rollback.sql - Rollback script

## Benefits
-  Referential integrity enforced at database level
-  Optimized queries for Umami sync operations
-  Better join performance on Session table
-  Rollback capability for safe schema changes

## Breaking Changes
- UserMapping table now requires websiteId field
- Existing data will be migrated to use first available website_id

## Migration Notes
Run the migration with:
```bash
npx prisma migrate deploy
```

To rollback if needed:
```bash
psql -d umami -f prisma/migrations/20_add_user_mapping_constraints/rollback.sql
```
2025-11-06 22:04:57 +08:00
..
01_init New schema for pixels and links. 2025-08-13 20:27:54 -07:00
02_report_schema_session_data New schema for pixels and links. 2025-08-13 20:27:54 -07:00
03_metric_performance_index New schema for pixels and links. 2025-08-13 20:27:54 -07:00
04_team_redesign New schema for pixels and links. 2025-08-13 20:27:54 -07:00
05_add_visit_id New schema for pixels and links. 2025-08-13 20:27:54 -07:00
06_session_data New schema for pixels and links. 2025-08-13 20:27:54 -07:00
07_add_tag New schema for pixels and links. 2025-08-13 20:27:54 -07:00
08_add_utm_clid New schema for pixels and links. 2025-08-13 20:27:54 -07:00
09_update_hostname_region New schema for pixels and links. 2025-08-13 20:27:54 -07:00
10_add_distinct_id New schema for pixels and links. 2025-08-13 20:27:54 -07:00
11_add_segment New schema for pixels and links. 2025-08-13 20:27:54 -07:00
12_update_report_parameter New schema for pixels and links. 2025-08-13 20:27:54 -07:00
13_add_revenue New schema for pixels and links. 2025-08-13 20:27:54 -07:00
14_add_link_and_pixel clean up funnel migration and report errors 2025-09-10 16:38:51 -07:00
15_add_woocommerce_fields feat: integrate First8 Marketing hyper-personalization system 2025-11-05 19:17:57 +08:00
16_create_recommendation_tables feat: integrate First8 Marketing hyper-personalization system 2025-11-05 19:17:57 +08:00
17_setup_apache_age feat: integrate First8 Marketing hyper-personalization system 2025-11-05 19:17:57 +08:00
18_setup_timescaledb feat: integrate First8 Marketing hyper-personalization system 2025-11-05 19:17:57 +08:00
19_add_user_id_mapping feat: Add UserMapping constraints and schema improvements 2025-11-06 22:04:57 +08:00
20_add_user_mapping_constraints feat: Add UserMapping constraints and schema improvements 2025-11-06 22:04:57 +08:00
migration_lock.toml New schema for pixels and links. 2025-08-13 20:27:54 -07:00