async function ensureProjectFileSizeColumn(pool) { await pool.query('ALTER TABLE project_files ADD COLUMN IF NOT EXISTS size BIGINT'); } async function backfillProjectFileSizes(pool, storage) { const { rows } = await pool.query('SELECT id, path FROM project_files WHERE size IS NULL ORDER BY id'); if (!rows.length) return 0; const objects = await storage.listAll(''); const sizes = new Map(objects.map(o => [o.key, o.size || 0])); let updated = 0; for (const row of rows) { const key = storage.keyFromPath(row.path); const size = key && sizes.has(key) ? sizes.get(key) : 0; const { rowCount } = await pool.query( 'UPDATE project_files SET size = $1 WHERE id = $2 AND size IS NULL', [size, row.id] ); updated += rowCount; } return updated; } module.exports = { ensureProjectFileSizeColumn, backfillProjectFileSizes };