meteor_detection_system/meteor-web-backend/migrations/1766300000011_analysis-result-relations.js
grabbit f557c06771 feat: complete database schema migration to UUID primary keys
## Database Migrations (18 new)
- Migrate all primary keys from SERIAL to UUID
- Add soft delete (deleted_at) to all 19 entities
- Add missing indexes for performance optimization
- Add CHECK constraints for data validation
- Add user audit fields (last_login_at, timezone, locale)
- Add weather station location fields (latitude, longitude, elevation)
- Add foreign key relationships (CameraDevice→Device, ValidatedEvent→WeatherStation)
- Prepare private key encryption fields

## Backend Entity Updates
- All entities updated with UUID primary keys
- Added @DeleteDateColumn for soft delete support
- Updated relations and foreign key types

## Backend Service/Controller Updates
- Changed ID parameters from number to string (UUID)
- Removed ParseIntPipe from controllers
- Updated TypeORM queries for string IDs

## Frontend Updates
- Updated all service interfaces to use string IDs
- Fixed CameraDevice.location as JSONB object
- Updated weather.ts with new fields (elevation, timezone)
- Added Supabase integration hooks and lib
- Fixed chart components for new data structure

## Cleanup
- Removed deprecated .claude/agents configuration files

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-21 03:33:26 +08:00

151 lines
4.0 KiB
JavaScript

/**
* Migration: Add AnalysisResult relations
*
* Fixes: AnalysisResult is completely orphaned with no relationships
* Changes:
* - Add validated_event_id FK (nullable)
* - Add device_id FK (nullable)
* - Add user_profile_id FK (nullable)
*
* @type {import('node-pg-migrate').ColumnDefinitions | undefined}
*/
export const shorthands = undefined;
/**
* @param pgm {import('node-pg-migrate').MigrationBuilder}
* @param run {() => void | undefined}
* @returns {Promise<void> | void}
*/
export const up = (pgm) => {
console.log('Adding AnalysisResult relations...');
// Add relationship columns
pgm.addColumns('analysis_results', {
validated_event_id: {
type: 'uuid',
notNull: false,
comment: 'FK to validated_events - the event this analysis is for',
},
device_id: {
type: 'uuid',
notNull: false,
comment: 'FK to devices - the device that captured the analyzed event',
},
user_profile_id: {
type: 'uuid',
notNull: false,
comment: 'FK to user_profiles - the owner of the analyzed event',
},
raw_event_id: {
type: 'uuid',
notNull: false,
comment: 'FK to raw_events - the original raw event',
},
});
// Add FK constraints
pgm.addConstraint('analysis_results', 'analysis_results_validated_event_id_fkey', {
foreignKeys: {
columns: 'validated_event_id',
references: 'validated_events(id)',
onDelete: 'SET NULL',
},
});
pgm.addConstraint('analysis_results', 'analysis_results_device_id_fkey', {
foreignKeys: {
columns: 'device_id',
references: 'devices(id)',
onDelete: 'SET NULL',
},
});
pgm.addConstraint('analysis_results', 'analysis_results_user_profile_id_fkey', {
foreignKeys: {
columns: 'user_profile_id',
references: 'user_profiles(id)',
onDelete: 'SET NULL',
},
});
pgm.addConstraint('analysis_results', 'analysis_results_raw_event_id_fkey', {
foreignKeys: {
columns: 'raw_event_id',
references: 'raw_events(id)',
onDelete: 'SET NULL',
},
});
// Add indexes
pgm.createIndex('analysis_results', 'validated_event_id', {
name: 'idx_analysis_results_validated_event_id',
where: 'validated_event_id IS NOT NULL',
});
pgm.createIndex('analysis_results', 'device_id', {
name: 'idx_analysis_results_device_id',
where: 'device_id IS NOT NULL',
});
pgm.createIndex('analysis_results', 'user_profile_id', {
name: 'idx_analysis_results_user_profile_id',
where: 'user_profile_id IS NOT NULL',
});
pgm.createIndex('analysis_results', 'raw_event_id', {
name: 'idx_analysis_results_raw_event_id',
where: 'raw_event_id IS NOT NULL',
});
// Add composite index for common query pattern
pgm.createIndex('analysis_results', ['analysis_type', 'user_profile_id'], {
name: 'idx_analysis_results_type_user',
});
console.log('AnalysisResult relations added.');
};
/**
* @param pgm {import('node-pg-migrate').MigrationBuilder}
* @param run {() => void | undefined}
* @returns {Promise<void> | void}
*/
export const down = (pgm) => {
console.log('Rolling back AnalysisResult relations...');
// Drop indexes
const indexes = [
'idx_analysis_results_validated_event_id',
'idx_analysis_results_device_id',
'idx_analysis_results_user_profile_id',
'idx_analysis_results_raw_event_id',
'idx_analysis_results_type_user',
];
indexes.forEach((name) => {
pgm.dropIndex('analysis_results', [], { name, ifExists: true });
});
// Drop FK constraints
const constraints = [
'analysis_results_validated_event_id_fkey',
'analysis_results_device_id_fkey',
'analysis_results_user_profile_id_fkey',
'analysis_results_raw_event_id_fkey',
];
constraints.forEach((name) => {
pgm.dropConstraint('analysis_results', name, { ifExists: true });
});
// Drop columns
pgm.dropColumns('analysis_results', [
'validated_event_id',
'device_id',
'user_profile_id',
'raw_event_id',
]);
console.log('AnalysisResult relations rollback complete.');
};