fix(sqlite): tag per-migration database backups with version and prevent collisions

This commit is contained in:
Tyagiquamar
2026-09-18 21:31:00 +05:30
parent 803fbb2ea2
commit b74ded3a9c
5 changed files with 298 additions and 65 deletions
+23
View File
@@ -26,3 +26,26 @@ export function formatBackupTimestamp(date: Date = new Date()): string {
return `${datePart}_${timePart}`;
}
/**
* Builds the full database backup file name, including timestamp and optional version tag.
*
* When a migration version is provided, the filename includes `_v<version>`,
* preventing collisions between multiple migrations running in the same second and making it easy
* to identify the migration state contained in the backup.
*
* @param version Optional migration version being run.
* @param date The moment the backup is being taken. Defaults to now.
* @returns A filename of the form `db_YYYY-MM-DD_HH-MM-SS_v<version>.sqlite` or `db_YYYY-MM-DD_HH-MM-SS.sqlite`.
*/
export function formatBackupFileName(
version?: string,
date: Date = new Date()
): string {
const timestamp = formatBackupTimestamp(date);
if (version) {
const versionTag = version.startsWith("v") ? version : `v${version}`;
return `db_${timestamp}_${versionTag}.sqlite`;
}
return `db_${timestamp}.sqlite`;
}