import postgres from 'postgres' const databaseUrl = String(process.env.DATABASE_URL || '').trim() if (!databaseUrl) { throw new Error('DATABASE_URL_REQUIRED') } const parsedDatabaseUrl = new URL(databaseUrl) const allowedHosts = new Set(['127.0.0.1', 'localhost', '::1']) const expectedDatabase = decodeURIComponent(parsedDatabaseUrl.pathname.replace(/^\/+/, '')) if (process.env.NODE_ENV === 'production' || !allowedHosts.has(parsedDatabaseUrl.hostname)) { throw new Error('MEDIA_CATALOG_RECONCILE_LOCAL_DATABASE_ONLY') } const database = postgres(databaseUrl, { max: 1 }) try { const identityRows = await database` SELECT current_database() AS database_name ` const databaseName = identityRows[0]?.database_name if (!databaseName || databaseName !== expectedDatabase || /prod|production/i.test(databaseName)) { throw new Error('MEDIA_CATALOG_DATABASE_IDENTITY_MISMATCH') } const { reconcileMediaCatalog } = await import('../server/repositories/media-catalog-repository.js') const report = await reconcileMediaCatalog() console.info(JSON.stringify({ database: databaseName, destructiveFileChanges: false, report }, null, 2)) } finally { await database.end() }