fix: add resource foreign key validation for tasks

Tasks also reference resources via assigned_resource_id and other fields.
Added validation for all task resource foreign keys:
- assigned_resource_id
- creator_resource_id
- completed_by_resource_id
- last_activity_resource_id

This completes the fix for tasks foreign key constraint violations.
This commit is contained in:
root 2026-01-24 08:13:18 -05:00
parent f1037d7964
commit 2f20b99195

View file

@ -274,16 +274,17 @@ export class EntitySyncService {
}
}
// Validate project foreign keys for tasks
// Validate project and resource foreign keys for tasks
if (entity === EntityType.TASKS) {
const initialCount = mappedRecords.length;
// Get all valid project IDs from database
// Get all valid project IDs and resource IDs from database
const validProjectIds = await this.getValidProjectIds();
const validResourceIds = await this.getValidResourceIds();
// Filter tasks with invalid project references
// Filter tasks with invalid foreign key references
mappedRecords = mappedRecords.map(task => {
// Set invalid project IDs to null instead of filtering out the entire task
// Set invalid project IDs to null
if (task.project_id && !validProjectIds.has(task.project_id)) {
entityLogger.debug(`Invalid project_id, setting to null`, {
taskId: task.id,
@ -291,12 +292,35 @@ export class EntitySyncService {
});
task.project_id = null;
}
// Set invalid resource IDs to null
if (task.assigned_resource_id && !validResourceIds.has(task.assigned_resource_id)) {
entityLogger.debug(`Invalid assigned_resource_id, setting to null`, {
taskId: task.id,
invalidResourceId: task.assigned_resource_id,
});
task.assigned_resource_id = null;
}
if (task.creator_resource_id && !validResourceIds.has(task.creator_resource_id)) {
task.creator_resource_id = null;
}
if (task.completed_by_resource_id && !validResourceIds.has(task.completed_by_resource_id)) {
task.completed_by_resource_id = null;
}
if (task.last_activity_resource_id && !validResourceIds.has(task.last_activity_resource_id)) {
task.last_activity_resource_id = null;
}
return task;
});
const nullifiedCount = initialCount - mappedRecords.filter(t => t.project_id).length;
if (nullifiedCount > 0) {
entityLogger.warn('Nullified invalid project references', { nullifiedCount });
const nullifiedProjectCount = initialCount - mappedRecords.filter(t => t.project_id).length;
const nullifiedResourceCount = initialCount - mappedRecords.filter(t => t.assigned_resource_id).length;
if (nullifiedProjectCount > 0 || nullifiedResourceCount > 0) {
entityLogger.warn('Nullified invalid foreign key references', {
nullifiedProjectCount,
nullifiedResourceCount
});
}
}