Skip to content

Commit

Permalink
Verify migrations have monotonically increasing version numbers (#2916)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nateowami authored Dec 17, 2024
1 parent dc0b712 commit 9fa9db8
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 21 deletions.
18 changes: 18 additions & 0 deletions src/RealtimeServer/common/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,21 @@ export abstract class DocMigration implements Migration {
// do nothing
}
}

/**
* Verifies that the specified migrations are have version numbers monotonically increasing by 1 and that the class
* names include the version number. Throws an error if any of the migrations violate this rule. Otherwise, returns the
* migrations.
*/
export function monotonicallyIncreasingMigrationList(migrations: MigrationConstructor[]): MigrationConstructor[] {
for (const [index, migration] of migrations.entries()) {
const expectedVersion = index + 1;
if (migration.VERSION !== expectedVersion) {
throw new Error(`Migration version mismatch: expected ${expectedVersion}, got ${migration.VERSION}`);
}
if (!migration.name.includes(migration.VERSION.toString())) {
throw new Error(`Migration class name must include the version number: ${migration.name}`);
}
}
return migrations;
}
4 changes: 2 additions & 2 deletions src/RealtimeServer/common/services/user-migrations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Doc, Op } from 'sharedb/lib/client';
import { submitMigrationOp } from '../../common/realtime-server';
import { DocMigration, MigrationConstructor } from '../migration';
import { DocMigration, MigrationConstructor, monotonicallyIncreasingMigrationList } from '../migration';

class UserMigration1 extends DocMigration {
static readonly VERSION = 1;
Expand All @@ -21,4 +21,4 @@ class UserMigration1 extends DocMigration {
}
}

export const USER_MIGRATIONS: MigrationConstructor[] = [UserMigration1];
export const USER_MIGRATIONS: MigrationConstructor[] = monotonicallyIncreasingMigrationList([UserMigration1]);
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { MigrationConstructor } from '../../common/migration';
import { MigrationConstructor, monotonicallyIncreasingMigrationList } from '../../common/migration';

export const BIBLICAL_TERM_MIGRATIONS: MigrationConstructor[] = [];
export const BIBLICAL_TERM_MIGRATIONS: MigrationConstructor[] = monotonicallyIncreasingMigrationList([]);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Doc, Op } from 'sharedb/lib/client';
import { DocMigration, MigrationConstructor } from '../../common/migration';
import { DocMigration, MigrationConstructor, monotonicallyIncreasingMigrationList } from '../../common/migration';
import { submitMigrationOp } from '../../common/realtime-server';

class NoteThreadMigration1 extends DocMigration {
Expand Down Expand Up @@ -69,9 +69,9 @@ class NoteThreadMigration4 extends DocMigration {
}
}

export const NOTE_THREAD_MIGRATIONS: MigrationConstructor[] = [
export const NOTE_THREAD_MIGRATIONS: MigrationConstructor[] = monotonicallyIncreasingMigrationList([
NoteThreadMigration1,
NoteThreadMigration2,
NoteThreadMigration3,
NoteThreadMigration4
];
]);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Doc, Op } from 'sharedb/lib/client';
import { DocMigration, MigrationConstructor } from '../../common/migration';
import { DocMigration, MigrationConstructor, monotonicallyIncreasingMigrationList } from '../../common/migration';
import { submitMigrationOp } from '../../common/realtime-server';

class QuestionMigration1 extends DocMigration {
Expand All @@ -24,4 +24,4 @@ class QuestionMigration1 extends DocMigration {
}
}

export const QUESTION_MIGRATIONS: MigrationConstructor[] = [QuestionMigration1];
export const QUESTION_MIGRATIONS: MigrationConstructor[] = monotonicallyIncreasingMigrationList([QuestionMigration1]);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Doc, Op } from 'sharedb/lib/client';
import { DocMigration, MigrationConstructor } from '../../common/migration';
import { DocMigration, MigrationConstructor, monotonicallyIncreasingMigrationList } from '../../common/migration';
import { Operation } from '../../common/models/project-rights';
import { submitMigrationOp } from '../../common/realtime-server';
import { NoteTag } from '../models/note-tag';
Expand Down Expand Up @@ -387,7 +387,7 @@ class SFProjectMigration21 extends DocMigration {
}
}

export const SF_PROJECT_MIGRATIONS: MigrationConstructor[] = [
export const SF_PROJECT_MIGRATIONS: MigrationConstructor[] = monotonicallyIncreasingMigrationList([
SFProjectMigration1,
SFProjectMigration2,
SFProjectMigration3,
Expand All @@ -409,4 +409,4 @@ export const SF_PROJECT_MIGRATIONS: MigrationConstructor[] = [
SFProjectMigration19,
SFProjectMigration20,
SFProjectMigration21
];
]);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Doc, ObjectDeleteOp, ObjectInsertOp } from 'sharedb/lib/client';
import { DocMigration, MigrationConstructor } from '../../common/migration';
import { DocMigration, MigrationConstructor, monotonicallyIncreasingMigrationList } from '../../common/migration';
import { submitMigrationOp } from '../../common/realtime-server';

class SFProjectUserConfigMigration1 extends DocMigration {
Expand Down Expand Up @@ -85,12 +85,12 @@ class SFProjectUserConfigMigration7 extends DocMigration {
}
}

export const SF_PROJECT_USER_CONFIG_MIGRATIONS: MigrationConstructor[] = [
export const SF_PROJECT_USER_CONFIG_MIGRATIONS: MigrationConstructor[] = monotonicallyIncreasingMigrationList([
SFProjectUserConfigMigration1,
SFProjectUserConfigMigration2,
SFProjectUserConfigMigration3,
SFProjectUserConfigMigration4,
SFProjectUserConfigMigration5,
SFProjectUserConfigMigration6,
SFProjectUserConfigMigration7
];
]);
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { MigrationConstructor } from '../../common/migration';
import { MigrationConstructor, monotonicallyIncreasingMigrationList } from '../../common/migration';

export const TEXT_AUDIO_MIGRATIONS: MigrationConstructor[] = [];
export const TEXT_AUDIO_MIGRATIONS: MigrationConstructor[] = monotonicallyIncreasingMigrationList([]);
4 changes: 2 additions & 2 deletions src/RealtimeServer/scriptureforge/services/text-migrations.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { MigrationConstructor } from '../../common/migration';
import { MigrationConstructor, monotonicallyIncreasingMigrationList } from '../../common/migration';

export const TEXT_MIGRATIONS: MigrationConstructor[] = [];
export const TEXT_MIGRATIONS: MigrationConstructor[] = monotonicallyIncreasingMigrationList([]);
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { MigrationConstructor } from '../../common/migration';
import { MigrationConstructor, monotonicallyIncreasingMigrationList } from '../../common/migration';

export const TRAINING_DATA_MIGRATIONS: MigrationConstructor[] = [];
export const TRAINING_DATA_MIGRATIONS: MigrationConstructor[] = monotonicallyIncreasingMigrationList([]);

0 comments on commit 9fa9db8

Please sign in to comment.