This commit is contained in:
Jake Kasper
2025-09-05 11:48:03 -04:00
parent ea4f523008
commit ee69c6c99a
2 changed files with 12 additions and 19 deletions

View File

@@ -322,17 +322,7 @@ router.delete('/:id', validateParams(idParamSchema), async (req, res, next) => {
throw new AppError('Cannot delete property with active applications', 400);
}
// Prevent deletion if there are historical application logs referencing sections on this property
const histLogs = await pool.query(
`SELECT COUNT(*) as count
FROM application_logs al
JOIN lawn_sections ls ON al.lawn_section_id = ls.id
WHERE ls.property_id = $1`,
[propertyId]
);
if (parseInt(histLogs.rows[0].count) > 0) {
throw new AppError('Cannot delete property with historical application logs; delete logs first or archive data', 400);
}
// Historical logs are preserved by FK ON DELETE SET NULL; no hard block here
await pool.query('DELETE FROM properties WHERE id = $1', [propertyId]);
@@ -530,14 +520,7 @@ router.delete('/:propertyId/sections/:sectionId', async (req, res, next) => {
throw new AppError('Cannot delete section with active applications', 400);
}
// Prevent deletion if there are historical logs tied to this section
const logs = await pool.query(
`SELECT COUNT(*) as count FROM application_logs WHERE lawn_section_id = $1`,
[sectionId]
);
if (parseInt(logs.rows[0].count) > 0) {
throw new AppError('Cannot delete section with historical application logs; delete logs first', 400);
}
// Historical logs are preserved by FK ON DELETE SET NULL; proceed with delete
await pool.query('DELETE FROM lawn_sections WHERE id = $1', [sectionId]);

View File

@@ -0,0 +1,10 @@
-- Set application_logs.lawn_section_id to ON DELETE SET NULL to preserve history
ALTER TABLE application_logs
DROP CONSTRAINT IF EXISTS application_logs_lawn_section_id_fkey;
ALTER TABLE application_logs
ADD CONSTRAINT application_logs_lawn_section_id_fkey
FOREIGN KEY (lawn_section_id) REFERENCES lawn_sections(id) ON DELETE SET NULL;
SELECT 'application_logs.lawn_section_id now ON DELETE SET NULL' as migration_status;