asdfas
This commit is contained in:
@@ -322,17 +322,7 @@ router.delete('/:id', validateParams(idParamSchema), async (req, res, next) => {
|
|||||||
throw new AppError('Cannot delete property with active applications', 400);
|
throw new AppError('Cannot delete property with active applications', 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevent deletion if there are historical application logs referencing sections on this property
|
// Historical logs are preserved by FK ON DELETE SET NULL; no hard block here
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
await pool.query('DELETE FROM properties WHERE id = $1', [propertyId]);
|
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);
|
throw new AppError('Cannot delete section with active applications', 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevent deletion if there are historical logs tied to this section
|
// Historical logs are preserved by FK ON DELETE SET NULL; proceed with delete
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
await pool.query('DELETE FROM lawn_sections WHERE id = $1', [sectionId]);
|
await pool.query('DELETE FROM lawn_sections WHERE id = $1', [sectionId]);
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
Reference in New Issue
Block a user