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

124 lines
3.7 KiB
TypeScript

import { Client, ClientConfig } from 'pg';
import * as dotenv from 'dotenv';
dotenv.config();
// Target database configuration (Supabase)
const targetConfig: ClientConfig = {
host: 'aws-1-us-east-1.pooler.supabase.com',
port: 6543,
database: 'postgres',
user: 'postgres.ffbgowwvcqmdtvvabmnh',
password: '!a_KW.-6Grb-X?#',
ssl: { rejectUnauthorized: false },
};
async function verifyMigration() {
const client = new Client(targetConfig);
try {
console.log('Connecting to Supabase database...');
await client.connect();
console.log('Connected!\n');
// Tables to verify
const tables = [
'user_profiles',
'user_identities',
'inventory_devices',
'weather_stations',
'weather_forecasts',
'analysis_results',
'device_registrations',
'devices',
'raw_events',
'validated_events',
];
console.log('========================================');
console.log('Migration Verification Results:');
console.log('========================================\n');
console.log('Table Row Counts:');
console.log('--------------------------------');
let totalRows = 0;
for (const table of tables) {
try {
const result = await client.query(`SELECT count(*) FROM ${table}`);
const count = parseInt(result.rows[0].count, 10);
console.log(`${table.padEnd(25)} : ${count}`);
totalRows += count;
} catch (error: any) {
console.log(`${table.padEnd(25)} : ERROR - ${error.message}`);
}
}
console.log('--------------------------------');
console.log(`Total rows: ${totalRows}\n`);
// Verify user_profiles have supabase_user_id
console.log('\nUser Profile Verification:');
console.log('--------------------------------');
const userProfiles = await client.query(`
SELECT id, display_name, supabase_user_id
FROM user_profiles
`);
for (const profile of userProfiles.rows) {
const status = profile.supabase_user_id ? '✓' : '✗';
console.log(`${status} ${profile.display_name || 'N/A'} (${profile.id})`);
if (profile.supabase_user_id) {
console.log(` Supabase Auth ID: ${profile.supabase_user_id}`);
}
}
// Verify user_identities
console.log('\nUser Identity Verification:');
console.log('--------------------------------');
const userIdentities = await client.query(`
SELECT email, provider, provider_id
FROM user_identities
WHERE provider = 'email'
`);
for (const identity of userIdentities.rows) {
const status = identity.provider_id ? '✓' : '✗';
console.log(`${status} ${identity.email}`);
if (identity.provider_id) {
console.log(` Provider ID: ${identity.provider_id}`);
}
}
// Check Supabase Auth users
console.log('\nSupabase Auth Users (from auth.users):');
console.log('--------------------------------');
try {
const authUsers = await client.query(`
SELECT id, email, created_at
FROM auth.users
ORDER BY created_at DESC
`);
for (const user of authUsers.rows) {
console.log(`${user.email} (${user.id})`);
}
console.log(`\nTotal Supabase Auth users: ${authUsers.rows.length}`);
} catch (error: any) {
console.log(`Cannot access auth.users table: ${error.message}`);
}
console.log('\n========================================');
console.log('Verification Complete!');
console.log('========================================\n');
} catch (error: any) {
console.error('Error:', error.message);
throw error;
} finally {
await client.end();
}
}
verifyMigration().catch(console.error);