From ee69c6c99a58da50f85e7b5575e2a73cbee70967 Mon Sep 17 00:00:00 2001 From: Jake Kasper Date: Fri, 5 Sep 2025 11:48:03 -0400 Subject: [PATCH] asdfas --- backend/src/routes/properties.js | 21 ++----------------- ...__application_logs_section_fk_set_null.sql | 10 +++++++++ 2 files changed, 12 insertions(+), 19 deletions(-) create mode 100644 database/migrations/V14__application_logs_section_fk_set_null.sql diff --git a/backend/src/routes/properties.js b/backend/src/routes/properties.js index b0fa6d2..495da4f 100644 --- a/backend/src/routes/properties.js +++ b/backend/src/routes/properties.js @@ -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]); diff --git a/database/migrations/V14__application_logs_section_fk_set_null.sql b/database/migrations/V14__application_logs_section_fk_set_null.sql new file mode 100644 index 0000000..12fc68f --- /dev/null +++ b/database/migrations/V14__application_logs_section_fk_set_null.sql @@ -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; +