fix(maintenance): stop silent deletion of one-time maintenance windows

Maintenance windows could disappear seconds after creation when the API received
a payload with end dates already in the past. The repository auto-set
expiry = end for one-time windows (repeat = 0), and Mongo's TTL index on the
expiry field then deleted the row on its next sweep. This was particularly
visible with automation tools sending payloads in non-UTC timezones.

## Changes

### Validator hardening
- Add .strict() to create + edit body schemas so unknown keys return 400
  instead of being silently dropped.
- Refine end > start on create.
- Refine end > now for one-time create payloads (repeat = 0).
- Refine end > start on edit when both are provided.
- Remove the accepted expiry field from both schemas.

### Stop the auto-delete behaviour
- Repository.create no longer copies end into expiry.
- Schema's expiry field + TTL index removed.
- Migration 0007 drops the live TTL index and unsets stale expiry values on
  existing rows so legacy data is no longer at risk.

### Client
- Form submission no longer includes expiry.
This commit is contained in:
gorkem-bwl committed 2026-05-07 12:29:26 -04:00
1 parent a69f8a5ab5
commit 4db95cfa44
6 files changed
+86 -36

No files matched your search

@@ -0,0 +1,48 @@
import { MaintenanceWindowModel } from "../models/MaintenanceWindow.js";
import { logger } from "@/utils/logger.js";
/**
* Drops the TTL index on MaintenanceWindow.expiry and unsets the expiry field.
*
* Earlier versions auto-set expiry = end for one-time maintenance windows. Combined with the
* TTL index ({ expires: "0s" }), this caused Mongo to silently delete completed one-time
* windows shortly after their end-date passed — including windows whose end-date was already
* in the past at creation time (e.g. timezone-mismatched payloads from automation tools).
* The fix stops writing expiry on create, and this migration removes the index and stale field
* so legacy rows are no longer at risk of being deleted by the TTL monitor.
*/
export async function dropMaintenanceWindowExpiryIndex(): Promise<void> {
const SERVICE_NAME = "Migration:DropMaintenanceWindowExpiryIndex";
try {
const collection = MaintenanceWindowModel.collection;
const indexes = await collection.indexes();
const expiryIndex = indexes.find((idx) => idx.key && idx.key.expiry === 1);
if (expiryIndex?.name) {
await collection.dropIndex(expiryIndex.name);
logger.info({
service: SERVICE_NAME,
message: `Dropped TTL index ${expiryIndex.name} on MaintenanceWindow.expiry`,
});
} else {
logger.info({
service: SERVICE_NAME,
message: "No TTL index found on MaintenanceWindow.expiry",
});
}
const result = await collection.updateMany({ expiry: { $exists: true } }, { $unset: { expiry: "" } });
logger.info({
service: SERVICE_NAME,
message: `Unset expiry on ${result.modifiedCount} maintenance windows`,
});
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
logger.error({
service: SERVICE_NAME,
message: `Error dropping expiry index: ${errorMessage}`,
});
throw error;
}
}
+2
View File
@@ -5,6 +5,7 @@ import { fixInfrastructureThresholds } from "./0004_fixInfrastructureThresholds.
import MigrationModel from "../models/Migration.js";
import { migrateStatusPageTypeToArray } from "./0005_migrateStatusPageTypeToArray.js";
import { cleanupDuplicateMonitorStatsForUniqueIndex } from "./0006_cleanupDuplicateMonitorStatsForUniqueIndex.js";
import { dropMaintenanceWindowExpiryIndex } from "./0007_dropMaintenanceWindowExpiryIndex.js";
import type { ILogger } from "@/utils/logger.js";
type MigrationEntry = {
@@ -19,6 +20,7 @@ const migrations: MigrationEntry[] = [
{ name: "0004_fixInfrastructureThresholds", execute: fixInfrastructureThresholds },
{ name: "0005_migrateStatusPageTypeToArray", execute: migrateStatusPageTypeToArray },
{ name: "0006_cleanupDuplicateMonitorStatsForUniqueIndex", execute: cleanupDuplicateMonitorStatsForUniqueIndex },
{ name: "0007_dropMaintenanceWindowExpiryIndex", execute: dropMaintenanceWindowExpiryIndex },
];
const runMigrations = async (logger?: ILogger) => {
@@ -6,7 +6,6 @@ type MaintenanceWindowDocumentBase = Omit<MaintenanceWindow, "id" | "monitorId"
teamId: Types.ObjectId;
start: Date;
end: Date;
expiry?: Date;
createdAt: Date;
updatedAt: Date;
};
@@ -50,10 +49,6 @@ const MaintenanceWindowSchema = new Schema<MaintenanceWindowDocument>(
end: {
type: Date,
},
expiry: {
type: Date,
index: { expires: "0s" },
},
},
{
timestamps: true,