@prairielearn/migrations 3.0.11 → 3.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/dist/batched-migrations/batched-migration-runner.d.ts +1 -1
- package/dist/batched-migrations/batched-migration-runner.js +2 -2
- package/dist/batched-migrations/batched-migration-runner.js.map +1 -1
- package/dist/batched-migrations/batched-migration.js +1 -1
- package/dist/batched-migrations/batched-migration.js.map +1 -1
- package/dist/batched-migrations/batched-migrations-runner.js.map +1 -1
- package/dist/batched-migrations/batched-migrations-runner.test.js +1 -1
- package/dist/batched-migrations/batched-migrations-runner.test.js.map +1 -1
- package/dist/load-migrations.test.js +1 -1
- package/dist/load-migrations.test.js.map +1 -1
- package/package.json +13 -13
- package/src/batched-migrations/batched-migration-runner.ts +5 -5
- package/src/batched-migrations/batched-migration.ts +1 -1
- package/src/batched-migrations/batched-migrations-runner.test.ts +1 -1
- package/src/batched-migrations/batched-migrations-runner.ts +3 -3
- package/src/load-migrations.test.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @prairielearn/migrations
|
|
2
2
|
|
|
3
|
+
## 3.0.13
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 984dc62: Upgrade all JavaScript dependencies
|
|
8
|
+
- Updated dependencies [984dc62]
|
|
9
|
+
- @prairielearn/named-locks@3.0.13
|
|
10
|
+
- @prairielearn/postgres@2.1.9
|
|
11
|
+
- @prairielearn/logger@2.0.11
|
|
12
|
+
- @prairielearn/error@2.0.11
|
|
13
|
+
|
|
14
|
+
## 3.0.12
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- 49bb3fa: Upgrade all JavaScript dependencies
|
|
19
|
+
- Updated dependencies [49bb3fa]
|
|
20
|
+
- @prairielearn/named-locks@3.0.12
|
|
21
|
+
- @prairielearn/postgres@2.1.8
|
|
22
|
+
- @prairielearn/logger@2.0.10
|
|
23
|
+
- @prairielearn/error@2.0.10
|
|
24
|
+
|
|
3
25
|
## 3.0.11
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type BatchedMigrationImplementation, type BatchedMigrationRow } from './batched-migration.js';
|
|
2
2
|
interface BatchedMigrationRunnerOptions {
|
|
3
3
|
logProgress?: boolean;
|
|
4
4
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { serializeError } from 'serialize-error';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { logger } from '@prairielearn/logger';
|
|
4
|
-
import { loadSqlEquiv, queryAsync,
|
|
4
|
+
import { loadSqlEquiv, queryAsync, queryOptionalRow, queryRow } from '@prairielearn/postgres';
|
|
5
5
|
import { BatchedMigrationJobRowSchema, } from './batched-migration-job.js';
|
|
6
|
-
import {
|
|
6
|
+
import { BatchedMigrationStatusSchema, updateBatchedMigrationStatus, } from './batched-migration.js';
|
|
7
7
|
const sql = loadSqlEquiv(import.meta.filename);
|
|
8
8
|
export class BatchedMigrationRunner {
|
|
9
9
|
options;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"batched-migration-runner.js","sourceRoot":"","sources":["../../src/batched-migrations/batched-migration-runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE9F,OAAO,EACL,4BAA4B,GAG7B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAGL,4BAA4B,EAC5B,4BAA4B,GAE7B,MAAM,wBAAwB,CAAC;A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AAC;QACH,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,CAAC,sBAAsB,CAAC,SAAS,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,EACR,MAAM,EACN,UAAU,EACV,UAAU,MAC4D,EAAE;QACxE,IAAI,cAAc,GAAG,CAAC,CAAC;QACvB,MAAM,OAAO,GAAG,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC;QAC5D,OACE,CAAC,MAAM,EAAE,OAAO;YAChB,CAAC,UAAU,CAAC,CAAC,CAAC,cAAc,GAAG,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC;YACjD,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;YACvC,CAAC,IAAI,CAAC,eAAe,KAAK,SAAS,IAAI,IAAI,CAAC,eAAe,KAAK,YAAY,CAAC,EAC7E,CAAC;YACD,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,uBAAuB,CAAC,CAAC;YACzE,cAAc,IAAI,CAAC,CAAC;YACpB,yEAAyE;YACzE,gCAAgC;YAChC,MAAM,IAAI,CAAC,sBAAsB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACpD,CAAC;IACH,CAAC;CACF","sourcesContent":["import { serializeError } from 'serialize-error';\nimport { z } from 'zod';\n\nimport { logger } from '@prairielearn/logger';\nimport { loadSqlEquiv, queryAsync, queryRow, queryOptionalRow } from '@prairielearn/postgres';\n\nimport {\n BatchedMigrationJobRowSchema,\n type BatchedMigrationJobStatus,\n type BatchedMigrationJobRow,\n} from './batched-migration-job.js';\nimport {\n type BatchedMigrationStatus,\n type BatchedMigrationRow,\n updateBatchedMigrationStatus,\n BatchedMigrationStatusSchema,\n type BatchedMigrationImplementation,\n} from './batched-migration.js';\n\nconst sql = loadSqlEquiv(import.meta.filename);\n\ninterface BatchedMigrationRunnerOptions {\n logProgress?: boolean;\n}\n\nexport class BatchedMigrationRunner {\n private options: BatchedMigrationRunnerOptions;\n private migration: BatchedMigrationRow;\n private migrationImplementation: BatchedMigrationImplementation;\n private migrationStatus: BatchedMigrationStatus;\n\n constructor(\n migration: BatchedMigrationRow,\n migrationImplementation: BatchedMigrationImplementation,\n options: BatchedMigrationRunnerOptions = {},\n ) {\n this.options = options;\n this.migration = migration;\n this.migrationImplementation = migrationImplementation;\n this.migrationStatus = migration.status;\n }\n\n private log(message: string, ...meta: any[]) {\n if (this.options.logProgress) {\n logger.info(`[${this.migration.filename}] ${message}`, ...meta);\n }\n }\n\n private async hasIncompleteJobs(migration: BatchedMigrationRow): Promise<boolean> {\n return await queryRow(\n sql.batched_migration_has_incomplete_jobs,\n { batched_migration_id: migration.id },\n z.boolean(),\n );\n }\n\n private async hasFailedJobs(migration: BatchedMigrationRow): Promise<boolean> {\n return await queryRow(\n sql.batched_migration_has_failed_jobs,\n { batched_migration_id: migration.id },\n z.boolean(),\n );\n }\n\n private async refreshMigrationStatus(migration: BatchedMigrationRow) {\n this.migrationStatus = await queryRow(\n sql.get_migration_status,\n {\n id: migration.id,\n },\n BatchedMigrationStatusSchema,\n );\n }\n\n private async finishRunningMigration(migration: BatchedMigrationRow) {\n // Safety check: if there are any pending jobs, don't mark this\n // migration as finished.\n if (await this.hasIncompleteJobs(migration)) {\n this.log('Incomplete jobs found, not marking as finished');\n return;\n }\n\n const hasFailedJobs = await this.hasFailedJobs(migration);\n const finalStatus = hasFailedJobs ? 'failed' : 'succeeded';\n await updateBatchedMigrationStatus(migration.id, finalStatus);\n this.log(`Finished with status '${finalStatus}'`);\n }\n\n private async getNextBatchBounds(\n migration: BatchedMigrationRow,\n ): Promise<null | [bigint, bigint]> {\n const lastJob = await queryOptionalRow(\n sql.select_last_batched_migration_job,\n { batched_migration_id: migration.id },\n BatchedMigrationJobRowSchema,\n );\n\n const nextMin = lastJob ? lastJob.max_value + 1n : migration.min_value;\n if (nextMin > migration.max_value) return null;\n\n let nextMax = nextMin + BigInt(migration.batch_size) - 1n;\n if (nextMax > migration.max_value) nextMax = migration.max_value;\n\n return [nextMin, nextMax];\n }\n\n private async startJob(job: BatchedMigrationJobRow) {\n await queryAsync(sql.start_batched_migration_job, { id: job.id });\n const jobRange = `[${job.min_value}, ${job.max_value}]`;\n const migrationRange = `[${this.migration.min_value}, ${this.migration.max_value}]`;\n this.log(`Started job ${job.id} for range ${jobRange} in ${migrationRange}`);\n }\n\n private serializeJobData(data: unknown) {\n if (data == null) return null;\n\n // Return JSON-stringified data. Convert BigInts to strings.\n return JSON.stringify(data, (_key, value) => {\n if (typeof value === 'bigint') return value.toString();\n return value;\n });\n }\n\n private async finishJob(\n job: BatchedMigrationJobRow,\n status: Extract<BatchedMigrationJobStatus, 'failed' | 'succeeded'>,\n data?: unknown,\n ) {\n await queryAsync(sql.finish_batched_migration_job, {\n id: job.id,\n status,\n data: this.serializeJobData(data),\n });\n this.log(`Job ${job.id} finished with status '${status}'`);\n }\n\n private async getOrCreateNextMigrationJob(\n migration: BatchedMigrationRow,\n ): Promise<BatchedMigrationJobRow | null> {\n const nextBatchBounds = await this.getNextBatchBounds(migration);\n if (nextBatchBounds) {\n return await queryRow(\n sql.insert_batched_migration_job,\n {\n batched_migration_id: migration.id,\n min_value: nextBatchBounds[0],\n max_value: nextBatchBounds[1],\n },\n BatchedMigrationJobRowSchema,\n );\n } else {\n // Pick up any old pending jobs from this migration. These will only exist if\n // an admin manually elected to retry all failed jobs; we'll never automatically\n // transition failed jobs back to pending.\n return await queryOptionalRow(\n sql.select_first_pending_batched_migration_job,\n { batched_migration_id: migration.id },\n BatchedMigrationJobRowSchema,\n );\n }\n }\n\n private async runMigrationJob(\n migration: BatchedMigrationRow,\n migrationImplementation: BatchedMigrationImplementation,\n ) {\n const nextJob = await this.getOrCreateNextMigrationJob(migration);\n if (nextJob) {\n await this.startJob(nextJob);\n\n let error = null;\n try {\n // We'll only handle errors thrown by the migration itself. If any of\n // our own execution machinery throws an error, we'll let it bubble up.\n await migrationImplementation.execute(nextJob.min_value, nextJob.max_value);\n } catch (err) {\n error = err;\n }\n\n if (error) {\n logger.error(\n `Error running job ${nextJob.id} for batched migration ${migration.filename}`,\n error,\n );\n await this.finishJob(nextJob, 'failed', { error: serializeError(error) });\n } else {\n await this.finishJob(nextJob, 'succeeded');\n }\n } else {\n await this.finishRunningMigration(migration);\n }\n }\n\n async run({\n signal,\n iterations,\n durationMs,\n }: { signal?: AbortSignal; iterations?: number; durationMs?: number } = {}) {\n let iterationCount = 0;\n const endTime = durationMs ? Date.now() + durationMs : null;\n while (\n !signal?.aborted &&\n (iterations ? iterationCount < iterations : true) &&\n (endTime ? Date.now() < endTime : true) &&\n (this.migrationStatus === 'running' || this.migrationStatus === 'finalizing')\n ) {\n await this.runMigrationJob(this.migration, this.migrationImplementation);\n iterationCount += 1;\n // Always refresh the status so we can detect if the migration was marked\n // as paused by another process.\n await this.refreshMigrationStatus(this.migration);\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"batched-migration-runner.js","sourceRoot":"","sources":["../../src/batched-migrations/batched-migration-runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAE9F,OAAO,EAEL,4BAA4B,GAE7B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAIL,4BAA4B,EAC5B,4BAA4B,GAC7B,MAAM,wBAAwB,CAAC;AAEhC,MAAM,GAAG,GAAG,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AAM/C,MAAM,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AC;IACH,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,EACR,MAAM,EACN,UAAU,EACV,UAAU,MAC4D,EAAE;QACxE,IAAI,cAAc,GAAG,CAAC,CAAC;QACvB,MAAM,OAAO,GAAG,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC;QAC5D,OACE,CAAC,MAAM,EAAE,OAAO;YAChB,CAAC,UAAU,CAAC,CAAC,CAAC,cAAc,GAAG,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC;YACjD,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;YACvC,CAAC,IAAI,CAAC,eAAe,KAAK,SAAS,IAAI,IAAI,CAAC,eAAe,KAAK,YAAY,CAAC,EAC7E,CAAC;YACD,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,uBAAuB,CAAC,CAAC;YACzE,cAAc,IAAI,CAAC,CAAC;YACpB,yEAAyE;YACzE,gCAAgC;YAChC,MAAM,IAAI,CAAC,sBAAsB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACpD,CAAC;IACH,CAAC;CACF","sourcesContent":["import { serializeError } from 'serialize-error';\nimport { z } from 'zod';\n\nimport { logger } from '@prairielearn/logger';\nimport { loadSqlEquiv, queryAsync, queryOptionalRow, queryRow } from '@prairielearn/postgres';\n\nimport {\n type BatchedMigrationJobRow,\n BatchedMigrationJobRowSchema,\n type BatchedMigrationJobStatus,\n} from './batched-migration-job.js';\nimport {\n type BatchedMigrationImplementation,\n type BatchedMigrationRow,\n type BatchedMigrationStatus,\n BatchedMigrationStatusSchema,\n updateBatchedMigrationStatus,\n} from './batched-migration.js';\n\nconst sql = loadSqlEquiv(import.meta.filename);\n\ninterface BatchedMigrationRunnerOptions {\n logProgress?: boolean;\n}\n\nexport class BatchedMigrationRunner {\n private options: BatchedMigrationRunnerOptions;\n private migration: BatchedMigrationRow;\n private migrationImplementation: BatchedMigrationImplementation;\n private migrationStatus: BatchedMigrationStatus;\n\n constructor(\n migration: BatchedMigrationRow,\n migrationImplementation: BatchedMigrationImplementation,\n options: BatchedMigrationRunnerOptions = {},\n ) {\n this.options = options;\n this.migration = migration;\n this.migrationImplementation = migrationImplementation;\n this.migrationStatus = migration.status;\n }\n\n private log(message: string, ...meta: any[]) {\n if (this.options.logProgress) {\n logger.info(`[${this.migration.filename}] ${message}`, ...meta);\n }\n }\n\n private async hasIncompleteJobs(migration: BatchedMigrationRow): Promise<boolean> {\n return await queryRow(\n sql.batched_migration_has_incomplete_jobs,\n { batched_migration_id: migration.id },\n z.boolean(),\n );\n }\n\n private async hasFailedJobs(migration: BatchedMigrationRow): Promise<boolean> {\n return await queryRow(\n sql.batched_migration_has_failed_jobs,\n { batched_migration_id: migration.id },\n z.boolean(),\n );\n }\n\n private async refreshMigrationStatus(migration: BatchedMigrationRow) {\n this.migrationStatus = await queryRow(\n sql.get_migration_status,\n {\n id: migration.id,\n },\n BatchedMigrationStatusSchema,\n );\n }\n\n private async finishRunningMigration(migration: BatchedMigrationRow) {\n // Safety check: if there are any pending jobs, don't mark this\n // migration as finished.\n if (await this.hasIncompleteJobs(migration)) {\n this.log('Incomplete jobs found, not marking as finished');\n return;\n }\n\n const hasFailedJobs = await this.hasFailedJobs(migration);\n const finalStatus = hasFailedJobs ? 'failed' : 'succeeded';\n await updateBatchedMigrationStatus(migration.id, finalStatus);\n this.log(`Finished with status '${finalStatus}'`);\n }\n\n private async getNextBatchBounds(\n migration: BatchedMigrationRow,\n ): Promise<null | [bigint, bigint]> {\n const lastJob = await queryOptionalRow(\n sql.select_last_batched_migration_job,\n { batched_migration_id: migration.id },\n BatchedMigrationJobRowSchema,\n );\n\n const nextMin = lastJob ? lastJob.max_value + 1n : migration.min_value;\n if (nextMin > migration.max_value) return null;\n\n let nextMax = nextMin + BigInt(migration.batch_size) - 1n;\n if (nextMax > migration.max_value) nextMax = migration.max_value;\n\n return [nextMin, nextMax];\n }\n\n private async startJob(job: BatchedMigrationJobRow) {\n await queryAsync(sql.start_batched_migration_job, { id: job.id });\n const jobRange = `[${job.min_value}, ${job.max_value}]`;\n const migrationRange = `[${this.migration.min_value}, ${this.migration.max_value}]`;\n this.log(`Started job ${job.id} for range ${jobRange} in ${migrationRange}`);\n }\n\n private serializeJobData(data: unknown) {\n if (data == null) return null;\n\n // Return JSON-stringified data. Convert BigInts to strings.\n return JSON.stringify(data, (_key, value) => {\n if (typeof value === 'bigint') return value.toString();\n return value;\n });\n }\n\n private async finishJob(\n job: BatchedMigrationJobRow,\n status: Extract<BatchedMigrationJobStatus, 'failed' | 'succeeded'>,\n data?: unknown,\n ) {\n await queryAsync(sql.finish_batched_migration_job, {\n id: job.id,\n status,\n data: this.serializeJobData(data),\n });\n this.log(`Job ${job.id} finished with status '${status}'`);\n }\n\n private async getOrCreateNextMigrationJob(\n migration: BatchedMigrationRow,\n ): Promise<BatchedMigrationJobRow | null> {\n const nextBatchBounds = await this.getNextBatchBounds(migration);\n if (nextBatchBounds) {\n return await queryRow(\n sql.insert_batched_migration_job,\n {\n batched_migration_id: migration.id,\n min_value: nextBatchBounds[0],\n max_value: nextBatchBounds[1],\n },\n BatchedMigrationJobRowSchema,\n );\n } else {\n // Pick up any old pending jobs from this migration. These will only exist if\n // an admin manually elected to retry all failed jobs; we'll never automatically\n // transition failed jobs back to pending.\n return await queryOptionalRow(\n sql.select_first_pending_batched_migration_job,\n { batched_migration_id: migration.id },\n BatchedMigrationJobRowSchema,\n );\n }\n }\n\n private async runMigrationJob(\n migration: BatchedMigrationRow,\n migrationImplementation: BatchedMigrationImplementation,\n ) {\n const nextJob = await this.getOrCreateNextMigrationJob(migration);\n if (nextJob) {\n await this.startJob(nextJob);\n\n let error = null;\n try {\n // We'll only handle errors thrown by the migration itself. If any of\n // our own execution machinery throws an error, we'll let it bubble up.\n await migrationImplementation.execute(nextJob.min_value, nextJob.max_value);\n } catch (err) {\n error = err;\n }\n\n if (error) {\n logger.error(\n `Error running job ${nextJob.id} for batched migration ${migration.filename}`,\n error,\n );\n await this.finishJob(nextJob, 'failed', { error: serializeError(error) });\n } else {\n await this.finishJob(nextJob, 'succeeded');\n }\n } else {\n await this.finishRunningMigration(migration);\n }\n }\n\n async run({\n signal,\n iterations,\n durationMs,\n }: { signal?: AbortSignal; iterations?: number; durationMs?: number } = {}) {\n let iterationCount = 0;\n const endTime = durationMs ? Date.now() + durationMs : null;\n while (\n !signal?.aborted &&\n (iterations ? iterationCount < iterations : true) &&\n (endTime ? Date.now() < endTime : true) &&\n (this.migrationStatus === 'running' || this.migrationStatus === 'finalizing')\n ) {\n await this.runMigrationJob(this.migration, this.migrationImplementation);\n iterationCount += 1;\n // Always refresh the status so we can detect if the migration was marked\n // as paused by another process.\n await this.refreshMigrationStatus(this.migration);\n }\n }\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
import { loadSqlEquiv, queryAsync, queryRow, queryRows,
|
|
2
|
+
import { loadSqlEquiv, queryAsync, queryOptionalRow, queryRow, queryRows, } from '@prairielearn/postgres';
|
|
3
3
|
const sql = loadSqlEquiv(import.meta.filename);
|
|
4
4
|
export const BatchedMigrationStatusSchema = z.enum([
|
|
5
5
|
'pending',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"batched-migration.js","sourceRoot":"","sources":["../../src/batched-migrations/batched-migration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EACL,YAAY,EACZ,UAAU,EACV,QAAQ,EACR,SAAS,
|
|
1
|
+
{"version":3,"file":"batched-migration.js","sourceRoot":"","sources":["../../src/batched-migrations/batched-migration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EACL,YAAY,E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n queryAsync,\n queryOptionalRow,\n queryRow,\n queryRows,\n} from '@prairielearn/postgres';\n\nconst sql = loadSqlEquiv(import.meta.filename);\n\nexport const BatchedMigrationStatusSchema = z.enum([\n 'pending',\n 'paused',\n 'running',\n 'finalizing',\n 'failed',\n 'succeeded',\n]);\nexport type BatchedMigrationStatus = z.infer<typeof BatchedMigrationStatusSchema>;\n\nexport const BatchedMigrationRowSchema = z.object({\n id: z.string(),\n project: z.string(),\n filename: z.string(),\n timestamp: z.string(),\n batch_size: z.number(),\n min_value: z.bigint({ coerce: true }),\n max_value: z.bigint({ coerce: true }),\n status: BatchedMigrationStatusSchema,\n created_at: z.date(),\n updated_at: z.date(),\n started_at: z.date().nullable(),\n});\nexport type BatchedMigrationRow = z.infer<typeof BatchedMigrationRowSchema>;\n\nexport interface BatchedMigrationParameters {\n min?: bigint | string | null;\n max: bigint | string | null;\n batchSize?: number;\n}\n\nexport interface BatchedMigrationImplementation {\n getParameters(): Promise<BatchedMigrationParameters>;\n execute(start: bigint, end: bigint): Promise<void>;\n}\n\n/**\n * Identity function that helps to write correct batched migrations.\n */\nexport function makeBatchedMigration<T extends BatchedMigrationImplementation>(fns: T): T {\n validateBatchedMigrationImplementation(fns);\n return fns;\n}\n\nexport function validateBatchedMigrationImplementation(\n fns: BatchedMigrationImplementation,\n): asserts fns is BatchedMigrationImplementation {\n if (typeof fns.getParameters !== 'function') {\n throw new Error('getParameters() must be a function');\n }\n if (typeof fns.execute !== 'function') {\n throw new Error('execute() must be a function');\n }\n}\n\ntype NewBatchedMigration = Pick<\n BatchedMigrationRow,\n 'project' | 'filename' | 'timestamp' | 'batch_size' | 'min_value' | 'max_value' | 'status'\n>;\n\n/**\n * Inserts a new batched migration. If one already exists for the given\n * project/timestamp pair, returns null, otherwise returns the inserted row.\n */\nexport async function insertBatchedMigration(\n migration: NewBatchedMigration,\n): Promise<BatchedMigrationRow | null> {\n return await queryOptionalRow(sql.insert_batched_migration, migration, BatchedMigrationRowSchema);\n}\n\nexport async function selectAllBatchedMigrations(project: string) {\n return await queryRows(sql.select_all_batched_migrations, { project }, BatchedMigrationRowSchema);\n}\n\nexport async function selectBatchedMigration(\n project: string,\n id: string,\n): Promise<BatchedMigrationRow> {\n return await queryRow(sql.select_batched_migration, { project, id }, BatchedMigrationRowSchema);\n}\n\nexport async function selectBatchedMigrationForTimestamp(\n project: string,\n timestamp: string,\n): Promise<BatchedMigrationRow> {\n return await queryRow(\n sql.select_batched_migration_for_timestamp,\n { project, timestamp },\n BatchedMigrationRowSchema,\n );\n}\n\nexport async function updateBatchedMigrationStatus(\n id: string,\n status: BatchedMigrationStatus,\n): Promise<BatchedMigrationRow> {\n return await queryRow(\n sql.update_batched_migration_status,\n { id, status },\n BatchedMigrationRowSchema,\n );\n}\n\nexport async function retryFailedBatchedMigrationJobs(project: string, id: string): Promise<void> {\n await queryAsync(sql.retry_failed_jobs, { project, id });\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"batched-migrations-runner.js","sourceRoot":"","sources":["../../src/batched-migrations/batched-migrations-runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE3D,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAExE,OAAO,EAEL,wCAAwC,GACzC,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EACL,yBAAyB,EAEzB,sBAAsB,EAEtB,kCAAkC,EAClC,4BAA4B,EAE5B,sCAAsC,GACvC,MAAM,wBAAwB,CAAC;AAEhC,MAAM,GAAG,GAAG,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC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AAC;IACtE,MAAM,GAAG,IAAI,uBAAuB,CAAC,OAAO,CAAC,CAAC;IAC9C,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,UAAU,sBAAsB,CAAC,UAAwC,EAAE;IAC/E,YAAY,CAAC,MAAM,CAAC,CAAC;IACrB,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACtB,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,qBAAqB;IACzC,YAAY,CAAC,MAAM,CAAC,CAAC;IACrB,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;IACpB,MAAM,GAAG,IAAI,CAAC;AAChB,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAAC,UAAkB;IAC9D,YAAY,CAAC,MAAM,CAAC,CAAC;IACrB,MAAM,MAAM,CAAC,uBAAuB,CAAC,UAAU,CAAC,CAAC;AACnD,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAC5C,UAAkB,EAClB,OAAyC;IAEzC,YAAY,CAAC,MAAM,CAAC,CAAC;IACrB,MAAM,MAAM,CAAC,wBAAwB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;AAC7D,CAAC","sourcesContent":["import { EventEmitter } from 'node:events';\nimport path from 'node:path';\nimport { setTimeout as sleep } from 'node:timers/promises';\n\nimport { doWithLock } from '@prairielearn/named-locks';\nimport { loadSqlEquiv, queryOptionalRow } from '@prairielearn/postgres';\n\nimport {\n type MigrationFile,\n readAndValidateMigrationsFromDirectories,\n} from '../load-migrations.js';\n\nimport { BatchedMigrationRunner } from './batched-migration-runner.js';\nimport {\n BatchedMigrationRowSchema,\n type BatchedMigrationRow,\n insertBatchedMigration,\n type BatchedMigrationStatus,\n selectBatchedMigrationForTimestamp,\n updateBatchedMigrationStatus,\n type BatchedMigrationImplementation,\n validateBatchedMigrationImplementation,\n} from './batched-migration.js';\n\nconst sql = loadSqlEquiv(import.meta.filename);\n\nconst DEFAULT_MIN_VALUE = 1n;\nconst DEFAULT_BATCH_SIZE = 1_000;\nconst DEFAULT_WORK_DURATION_MS = 60_000;\nconst DEFAULT_SLEEP_DURATION_MS = 30_000;\nconst EXTENSIONS = ['.js', '.ts', '.mjs', '.mts'];\n\ninterface BatchedMigrationRunnerOptions {\n project: string;\n directories: string[];\n}\n\ninterface BatchedMigrationStartOptions {\n workDurationMs?: number;\n sleepDurationMs?: number;\n}\n\ninterface BatchedMigrationFinalizeOptions {\n logProgress?: boolean;\n}\n\nexport class BatchedMigrationsRunner extends EventEmitter {\n private readonly options: BatchedMigrationRunnerOptions;\n private readonly lockName: string;\n private running = false;\n private migrationFiles: MigrationFile[] | null = null;\n private abortController = new AbortController();\n\n constructor(options: BatchedMigrationRunnerOptions) {\n super();\n this.options = options;\n this.lockName = `batched-migrations:${this.options.project}`;\n }\n\n private lockNameForTimestamp(timestamp: string) {\n return `${this.lockName}:${timestamp}`;\n }\n\n private getMigrationFiles = async () => {\n if (!this.migrationFiles) {\n this.migrationFiles = await readAndValidateMigrationsFromDirectories(\n this.options.directories,\n EXTENSIONS,\n );\n }\n return this.migrationFiles;\n };\n\n private async getMigrationForIdentifier(identifier: string): Promise<MigrationFile | null> {\n const timestamp = identifier.split('_')[0];\n\n const migrationFiles = await this.getMigrationFiles();\n const migrationFile = migrationFiles.find((m) => m.timestamp === timestamp);\n return migrationFile ?? null;\n }\n\n /**\n * Loads the implementation for the migration with the given identifier. The identifier\n * must start with a 14-character timestamp. It may optionally be followed by\n * an underscore with additional characters, which are ignored. These should\n * typically be used to provide a human-readable name for the migration.\n */\n private async loadMigrationImplementation(migrationFile: MigrationFile) {\n // We use dynamic imports to handle both CJS and ESM modules.\n const migrationModulePath = path.join(migrationFile.directory, migrationFile.filename);\n const migrationModule = await import(migrationModulePath);\n\n const migrationImplementation = migrationModule.default as BatchedMigrationImplementation;\n validateBatchedMigrationImplementation(migrationImplementation);\n return migrationImplementation;\n }\n\n async enqueueBatchedMigration(identifier: string) {\n const migrationFile = await this.getMigrationForIdentifier(identifier);\n if (!migrationFile) {\n throw new Error(`No migration found for identifier ${identifier}`);\n }\n\n const migrationImplementation = await this.loadMigrationImplementation(migrationFile);\n const migrationParameters = await migrationImplementation.getParameters();\n\n // If `max` is null, that implies that there are no rows to process, so\n // we can immediately mark the migration as finished.\n const status: BatchedMigrationStatus =\n migrationParameters.max === null ? 'succeeded' : 'pending';\n\n const minValue = BigInt(migrationParameters.min ?? DEFAULT_MIN_VALUE);\n const maxValue = BigInt(migrationParameters.max ?? minValue);\n const batchSize = migrationParameters.batchSize ?? DEFAULT_BATCH_SIZE;\n\n await insertBatchedMigration({\n project: this.options.project,\n filename: migrationFile.filename,\n timestamp: migrationFile.timestamp,\n batch_size: batchSize,\n min_value: minValue,\n max_value: maxValue,\n status,\n });\n }\n\n async finalizeBatchedMigration(identifier: string, options?: BatchedMigrationFinalizeOptions) {\n const timestamp = identifier.split('_')[0];\n\n let migration = await selectBatchedMigrationForTimestamp(this.options.project, timestamp);\n\n if (migration.status === 'succeeded') return;\n\n // If the migration isn't already in the finalizing state, mark it as such.\n if (migration.status !== 'finalizing') {\n migration = await updateBatchedMigrationStatus(migration.id, 'finalizing');\n }\n\n await doWithLock(this.lockNameForTimestamp(timestamp), { autoRenew: true }, async () => {\n const migrationFile = await this.getMigrationForIdentifier(identifier);\n if (!migrationFile) {\n throw new Error(`No migration found for identifier ${identifier}`);\n }\n const migrationImplementation = await this.loadMigrationImplementation(migrationFile);\n\n const runner = new BatchedMigrationRunner(migration, migrationImplementation, {\n // Always log progress unless explicitly disabled.\n logProgress: options?.logProgress ?? true,\n });\n\n // Because we don't give any arguments to `run()`, it will run until it\n // has attempted every job.\n await runner.run();\n });\n\n migration = await selectBatchedMigrationForTimestamp(this.options.project, timestamp);\n\n if (migration.status === 'succeeded') return;\n\n throw new Error(\n `Expected batched migration with identifier ${identifier} to be marked as 'succeeded', but it is '${migration.status}'.`,\n );\n }\n\n start(options: BatchedMigrationStartOptions = {}) {\n if (this.running) {\n throw new Error('BatchedMigrationsRunner is already running');\n }\n\n this.loop(options);\n }\n\n async loop({ workDurationMs, sleepDurationMs }: BatchedMigrationStartOptions) {\n workDurationMs ??= DEFAULT_WORK_DURATION_MS;\n sleepDurationMs ??= DEFAULT_SLEEP_DURATION_MS;\n\n this.running = true;\n while (this.running) {\n if (this.abortController.signal.aborted) {\n // We assign this here so that `stop()` can tell when this loop is done\n // processing jobs.\n this.running = false;\n return;\n }\n\n let didWork = false;\n try {\n didWork = await this.maybePerformWork(workDurationMs);\n } catch (err) {\n this.emit('error', err);\n }\n\n // If we did work, we'll immediately try again since there's probably more\n // work to be done. If not, we'll sleep for a while - maybe some more work\n // will become available!\n if (!didWork) {\n // We provide the signal here so that we can more quickly stop things\n // when we're shutting down.\n try {\n await sleep(sleepDurationMs, null, { ref: false, signal: this.abortController.signal });\n } catch {\n // We don't care about errors here, they should only ever occur when\n // the AbortController is aborted. Continue to the next iteration of\n // the loop so we can shut down.\n continue;\n }\n }\n }\n }\n\n private async getOrStartMigration(): Promise<BatchedMigrationRow | null> {\n return doWithLock(\n this.lockName,\n {\n // Don't fail if the lock couldn't be acquired immediately.\n onNotAcquired: () => null,\n },\n async () => {\n let migration = await queryOptionalRow(\n sql.select_running_migration,\n { project: this.options.project },\n BatchedMigrationRowSchema,\n );\n\n if (!migration) {\n migration = await queryOptionalRow(\n sql.start_next_pending_migration,\n { project: this.options.project },\n BatchedMigrationRowSchema,\n );\n }\n\n return migration;\n },\n );\n }\n\n async maybePerformWork(durationMs: number): Promise<boolean> {\n const migration = await this.getOrStartMigration();\n if (!migration) {\n // No work to do. Handle this case.\n return false;\n }\n\n // This server may not yet know about the current running migration. If\n // that's the case, we'll just skip it for now.\n const migrationFile = await this.getMigrationForIdentifier(migration.timestamp);\n if (!migrationFile) {\n return false;\n }\n\n let didWork = false;\n await doWithLock(\n this.lockNameForTimestamp(migrationFile.timestamp),\n {\n autoRenew: true,\n // Do nothing if the lock could not immediately be acquired.\n onNotAcquired: () => null,\n },\n async () => {\n didWork = true;\n const migrationImplementation = await this.loadMigrationImplementation(migrationFile);\n\n const runner = new BatchedMigrationRunner(migration, migrationImplementation);\n\n try {\n await runner.run({ signal: this.abortController.signal, durationMs });\n } catch (err) {\n this.emit('error', err);\n }\n },\n );\n\n return didWork;\n }\n\n async stop() {\n this.abortController.abort();\n\n // Spin until we're no longer running.\n while (this.running) {\n await sleep(1000);\n }\n }\n}\n\nlet runner: BatchedMigrationsRunner | null = null;\n\nfunction assertRunner(\n runner: BatchedMigrationsRunner | null,\n): asserts runner is BatchedMigrationsRunner {\n if (!runner) throw new Error('Batched migrations not initialized');\n}\n\nexport function initBatchedMigrations(options: BatchedMigrationRunnerOptions) {\n if (runner) throw new Error('Batched migrations already initialized');\n runner = new BatchedMigrationsRunner(options);\n return runner;\n}\n\nexport function startBatchedMigrations(options: BatchedMigrationStartOptions = {}) {\n assertRunner(runner);\n runner.start(options);\n return runner;\n}\n\nexport async function stopBatchedMigrations() {\n assertRunner(runner);\n await runner.stop();\n runner = null;\n}\n\n/**\n * Given a batched migration identifier like `20230406184103_migration`,\n * enqueues it for execution by creating a row in the `batched_migrations`\n * table.\n *\n * Despite taking a full identifier, only the timestamp is used to uniquely\n * identify the batched migration. The remaining part is just used to make\n * calls more human-readable.\n *\n * @param identifier The identifier of the batched migration to enqueue.\n */\nexport async function enqueueBatchedMigration(identifier: string) {\n assertRunner(runner);\n await runner.enqueueBatchedMigration(identifier);\n}\n\n/**\n * Given a batched migration identifier like `20230406184103_migration`,\n * synchronously runs it to completion. An error will be thrown if the final\n * status of the migration is not `succeeded`.\n *\n * @param identifier The identifier of the batched migration to finalize.\n * @param options Options for finalizing the batched migration.\n */\nexport async function finalizeBatchedMigration(\n identifier: string,\n options?: BatchedMigrationFinalizeOptions,\n) {\n assertRunner(runner);\n await runner.finalizeBatchedMigration(identifier, options);\n}\n"]}
|
|
1
|
+
{"version":3,"file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;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAC5C,UAAkB,EAClB,OAAyC;IAEzC,YAAY,CAAC,MAAM,CAAC,CAAC;IACrB,MAAM,MAAM,CAAC,wBAAwB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;AAC7D,CAAC","sourcesContent":["import { EventEmitter } from 'node:events';\nimport path from 'node:path';\nimport { setTimeout as sleep } from 'node:timers/promises';\n\nimport { doWithLock } from '@prairielearn/named-locks';\nimport { loadSqlEquiv, queryOptionalRow } from '@prairielearn/postgres';\n\nimport {\n type MigrationFile,\n readAndValidateMigrationsFromDirectories,\n} from '../load-migrations.js';\n\nimport { BatchedMigrationRunner } from './batched-migration-runner.js';\nimport {\n type BatchedMigrationImplementation,\n type BatchedMigrationRow,\n BatchedMigrationRowSchema,\n type BatchedMigrationStatus,\n insertBatchedMigration,\n selectBatchedMigrationForTimestamp,\n updateBatchedMigrationStatus,\n validateBatchedMigrationImplementation,\n} from './batched-migration.js';\n\nconst sql = loadSqlEquiv(import.meta.filename);\n\nconst DEFAULT_MIN_VALUE = 1n;\nconst DEFAULT_BATCH_SIZE = 1_000;\nconst DEFAULT_WORK_DURATION_MS = 60_000;\nconst DEFAULT_SLEEP_DURATION_MS = 30_000;\nconst EXTENSIONS = ['.js', '.ts', '.mjs', '.mts'];\n\ninterface BatchedMigrationRunnerOptions {\n project: string;\n directories: string[];\n}\n\ninterface BatchedMigrationStartOptions {\n workDurationMs?: number;\n sleepDurationMs?: number;\n}\n\ninterface BatchedMigrationFinalizeOptions {\n logProgress?: boolean;\n}\n\nexport class BatchedMigrationsRunner extends EventEmitter {\n private readonly options: BatchedMigrationRunnerOptions;\n private readonly lockName: string;\n private running = false;\n private migrationFiles: MigrationFile[] | null = null;\n private abortController = new AbortController();\n\n constructor(options: BatchedMigrationRunnerOptions) {\n super();\n this.options = options;\n this.lockName = `batched-migrations:${this.options.project}`;\n }\n\n private lockNameForTimestamp(timestamp: string) {\n return `${this.lockName}:${timestamp}`;\n }\n\n private getMigrationFiles = async () => {\n if (!this.migrationFiles) {\n this.migrationFiles = await readAndValidateMigrationsFromDirectories(\n this.options.directories,\n EXTENSIONS,\n );\n }\n return this.migrationFiles;\n };\n\n private async getMigrationForIdentifier(identifier: string): Promise<MigrationFile | null> {\n const timestamp = identifier.split('_')[0];\n\n const migrationFiles = await this.getMigrationFiles();\n const migrationFile = migrationFiles.find((m) => m.timestamp === timestamp);\n return migrationFile ?? null;\n }\n\n /**\n * Loads the implementation for the migration with the given identifier. The identifier\n * must start with a 14-character timestamp. It may optionally be followed by\n * an underscore with additional characters, which are ignored. These should\n * typically be used to provide a human-readable name for the migration.\n */\n private async loadMigrationImplementation(migrationFile: MigrationFile) {\n // We use dynamic imports to handle both CJS and ESM modules.\n const migrationModulePath = path.join(migrationFile.directory, migrationFile.filename);\n const migrationModule = await import(migrationModulePath);\n\n const migrationImplementation = migrationModule.default as BatchedMigrationImplementation;\n validateBatchedMigrationImplementation(migrationImplementation);\n return migrationImplementation;\n }\n\n async enqueueBatchedMigration(identifier: string) {\n const migrationFile = await this.getMigrationForIdentifier(identifier);\n if (!migrationFile) {\n throw new Error(`No migration found for identifier ${identifier}`);\n }\n\n const migrationImplementation = await this.loadMigrationImplementation(migrationFile);\n const migrationParameters = await migrationImplementation.getParameters();\n\n // If `max` is null, that implies that there are no rows to process, so\n // we can immediately mark the migration as finished.\n const status: BatchedMigrationStatus =\n migrationParameters.max === null ? 'succeeded' : 'pending';\n\n const minValue = BigInt(migrationParameters.min ?? DEFAULT_MIN_VALUE);\n const maxValue = BigInt(migrationParameters.max ?? minValue);\n const batchSize = migrationParameters.batchSize ?? DEFAULT_BATCH_SIZE;\n\n await insertBatchedMigration({\n project: this.options.project,\n filename: migrationFile.filename,\n timestamp: migrationFile.timestamp,\n batch_size: batchSize,\n min_value: minValue,\n max_value: maxValue,\n status,\n });\n }\n\n async finalizeBatchedMigration(identifier: string, options?: BatchedMigrationFinalizeOptions) {\n const timestamp = identifier.split('_')[0];\n\n let migration = await selectBatchedMigrationForTimestamp(this.options.project, timestamp);\n\n if (migration.status === 'succeeded') return;\n\n // If the migration isn't already in the finalizing state, mark it as such.\n if (migration.status !== 'finalizing') {\n migration = await updateBatchedMigrationStatus(migration.id, 'finalizing');\n }\n\n await doWithLock(this.lockNameForTimestamp(timestamp), { autoRenew: true }, async () => {\n const migrationFile = await this.getMigrationForIdentifier(identifier);\n if (!migrationFile) {\n throw new Error(`No migration found for identifier ${identifier}`);\n }\n const migrationImplementation = await this.loadMigrationImplementation(migrationFile);\n\n const runner = new BatchedMigrationRunner(migration, migrationImplementation, {\n // Always log progress unless explicitly disabled.\n logProgress: options?.logProgress ?? true,\n });\n\n // Because we don't give any arguments to `run()`, it will run until it\n // has attempted every job.\n await runner.run();\n });\n\n migration = await selectBatchedMigrationForTimestamp(this.options.project, timestamp);\n\n if (migration.status === 'succeeded') return;\n\n throw new Error(\n `Expected batched migration with identifier ${identifier} to be marked as 'succeeded', but it is '${migration.status}'.`,\n );\n }\n\n start(options: BatchedMigrationStartOptions = {}) {\n if (this.running) {\n throw new Error('BatchedMigrationsRunner is already running');\n }\n\n this.loop(options);\n }\n\n async loop({ workDurationMs, sleepDurationMs }: BatchedMigrationStartOptions) {\n workDurationMs ??= DEFAULT_WORK_DURATION_MS;\n sleepDurationMs ??= DEFAULT_SLEEP_DURATION_MS;\n\n this.running = true;\n while (this.running) {\n if (this.abortController.signal.aborted) {\n // We assign this here so that `stop()` can tell when this loop is done\n // processing jobs.\n this.running = false;\n return;\n }\n\n let didWork = false;\n try {\n didWork = await this.maybePerformWork(workDurationMs);\n } catch (err) {\n this.emit('error', err);\n }\n\n // If we did work, we'll immediately try again since there's probably more\n // work to be done. If not, we'll sleep for a while - maybe some more work\n // will become available!\n if (!didWork) {\n // We provide the signal here so that we can more quickly stop things\n // when we're shutting down.\n try {\n await sleep(sleepDurationMs, null, { ref: false, signal: this.abortController.signal });\n } catch {\n // We don't care about errors here, they should only ever occur when\n // the AbortController is aborted. Continue to the next iteration of\n // the loop so we can shut down.\n continue;\n }\n }\n }\n }\n\n private async getOrStartMigration(): Promise<BatchedMigrationRow | null> {\n return doWithLock(\n this.lockName,\n {\n // Don't fail if the lock couldn't be acquired immediately.\n onNotAcquired: () => null,\n },\n async () => {\n let migration = await queryOptionalRow(\n sql.select_running_migration,\n { project: this.options.project },\n BatchedMigrationRowSchema,\n );\n\n if (!migration) {\n migration = await queryOptionalRow(\n sql.start_next_pending_migration,\n { project: this.options.project },\n BatchedMigrationRowSchema,\n );\n }\n\n return migration;\n },\n );\n }\n\n async maybePerformWork(durationMs: number): Promise<boolean> {\n const migration = await this.getOrStartMigration();\n if (!migration) {\n // No work to do. Handle this case.\n return false;\n }\n\n // This server may not yet know about the current running migration. If\n // that's the case, we'll just skip it for now.\n const migrationFile = await this.getMigrationForIdentifier(migration.timestamp);\n if (!migrationFile) {\n return false;\n }\n\n let didWork = false;\n await doWithLock(\n this.lockNameForTimestamp(migrationFile.timestamp),\n {\n autoRenew: true,\n // Do nothing if the lock could not immediately be acquired.\n onNotAcquired: () => null,\n },\n async () => {\n didWork = true;\n const migrationImplementation = await this.loadMigrationImplementation(migrationFile);\n\n const runner = new BatchedMigrationRunner(migration, migrationImplementation);\n\n try {\n await runner.run({ signal: this.abortController.signal, durationMs });\n } catch (err) {\n this.emit('error', err);\n }\n },\n );\n\n return didWork;\n }\n\n async stop() {\n this.abortController.abort();\n\n // Spin until we're no longer running.\n while (this.running) {\n await sleep(1000);\n }\n }\n}\n\nlet runner: BatchedMigrationsRunner | null = null;\n\nfunction assertRunner(\n runner: BatchedMigrationsRunner | null,\n): asserts runner is BatchedMigrationsRunner {\n if (!runner) throw new Error('Batched migrations not initialized');\n}\n\nexport function initBatchedMigrations(options: BatchedMigrationRunnerOptions) {\n if (runner) throw new Error('Batched migrations already initialized');\n runner = new BatchedMigrationsRunner(options);\n return runner;\n}\n\nexport function startBatchedMigrations(options: BatchedMigrationStartOptions = {}) {\n assertRunner(runner);\n runner.start(options);\n return runner;\n}\n\nexport async function stopBatchedMigrations() {\n assertRunner(runner);\n await runner.stop();\n runner = null;\n}\n\n/**\n * Given a batched migration identifier like `20230406184103_migration`,\n * enqueues it for execution by creating a row in the `batched_migrations`\n * table.\n *\n * Despite taking a full identifier, only the timestamp is used to uniquely\n * identify the batched migration. The remaining part is just used to make\n * calls more human-readable.\n *\n * @param identifier The identifier of the batched migration to enqueue.\n */\nexport async function enqueueBatchedMigration(identifier: string) {\n assertRunner(runner);\n await runner.enqueueBatchedMigration(identifier);\n}\n\n/**\n * Given a batched migration identifier like `20230406184103_migration`,\n * synchronously runs it to completion. An error will be thrown if the final\n * status of the migration is not `succeeded`.\n *\n * @param identifier The identifier of the batched migration to finalize.\n * @param options Options for finalizing the batched migration.\n */\nexport async function finalizeBatchedMigration(\n identifier: string,\n options?: BatchedMigrationFinalizeOptions,\n) {\n assertRunner(runner);\n await runner.finalizeBatchedMigration(identifier, options);\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
|
-
import { use as chaiUse
|
|
2
|
+
import { assert, use as chaiUse } from 'chai';
|
|
3
3
|
import chaiAsPromised from 'chai-as-promised';
|
|
4
4
|
import * as namedLocks from '@prairielearn/named-locks';
|
|
5
5
|
import { makePostgresTestUtils } from '@prairielearn/postgres';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"batched-migrations-runner.test.js","sourceRoot":"","sources":["../../src/batched-migrations/batched-migrations-runner.test.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"batched-migrations-runner.test.js","sourceRoot":"","sources":["../../src/batched-migrations/batched-migrations-runner.test.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,OAAO,E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CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IAClD,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,+BAA+B,EAAE,KAAK,IAAI,EAAE;QAC7C,MAAM,MAAM,GAAG,IAAI,uBAAuB,CAAC;YACzC,OAAO,EAAE,MAAM;YACf,WAAW,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;SAC1D,CAAC,CAAC;QAEH,MAAM,MAAM,CAAC,uBAAuB,CAAC,kCAAkC,CAAC,CAAC;QAEzE,MAAM,MAAM,CAAC,UAAU,CACrB,MAAM,CAAC,wBAAwB,CAAC,kCAAkC,EAAE;YAClE,WAAW,EAAE,KAAK;SACnB,CAAC,EACF,oBAAoB,CACrB,CAAC;QAEF,MAAM,UAAU,GAAG,MAAM,0BAA0B,CAAC,MAAM,CAAC,CAAC;QAC5D,MAAM,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC;QAC/B,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,gBAAgB,CAAC,CAAC;QACxD,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAC/C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import path from 'node:path';\n\nimport { assert, use as chaiUse } from 'chai';\nimport chaiAsPromised from 'chai-as-promised';\n\nimport * as namedLocks from '@prairielearn/named-locks';\nimport { makePostgresTestUtils } from '@prairielearn/postgres';\n\nimport { SCHEMA_MIGRATIONS_PATH, init } from '../index.js';\n\nimport { selectAllBatchedMigrations } from './batched-migration.js';\nimport { BatchedMigrationsRunner } from './batched-migrations-runner.js';\n\nchaiUse(chaiAsPromised);\n\nconst postgresTestUtils = makePostgresTestUtils({\n database: 'prairielearn_migrations',\n});\n\ndescribe('BatchedMigrationsRunner', () => {\n before(async () => {\n await postgresTestUtils.createDatabase();\n await namedLocks.init(postgresTestUtils.getPoolConfig(), (err) => {\n throw err;\n });\n await init([SCHEMA_MIGRATIONS_PATH], 'prairielearn_migrations');\n });\n\n afterEach(async () => {\n await postgresTestUtils.resetDatabase();\n });\n\n after(async () => {\n await namedLocks.close();\n await postgresTestUtils.dropDatabase();\n });\n\n it('enqueues migrations', async () => {\n const runner = new BatchedMigrationsRunner({\n project: 'test',\n directories: [path.join(import.meta.dirname, 'fixtures')],\n });\n\n await runner.enqueueBatchedMigration('20230406184103_successful_migration');\n await runner.enqueueBatchedMigration('20230406184107_failing_migration');\n await runner.enqueueBatchedMigration('20230407230446_no_rows_migration');\n\n const migrations = await selectAllBatchedMigrations('test');\n\n assert.lengthOf(migrations, 3);\n assert.equal(migrations[0].timestamp, '20230406184103');\n assert.equal(migrations[0].filename, '20230406184103_successful_migration.ts');\n assert.equal(migrations[0].status, 'pending');\n assert.equal(migrations[1].timestamp, '20230406184107');\n assert.equal(migrations[1].filename, '20230406184107_failing_migration.ts');\n assert.equal(migrations[1].status, 'pending');\n assert.equal(migrations[2].timestamp, '20230407230446');\n assert.equal(migrations[2].filename, '20230407230446_no_rows_migration.ts');\n assert.equal(migrations[2].status, 'succeeded');\n });\n\n it('safely enqueues migrations multiple times', async () => {\n const runner = new BatchedMigrationsRunner({\n project: 'test',\n directories: [path.join(import.meta.dirname, 'fixtures')],\n });\n\n await runner.enqueueBatchedMigration('20230406184103_successful_migration');\n await runner.enqueueBatchedMigration('20230406184103_successful_migration');\n await runner.enqueueBatchedMigration('20230406184103_successful_migration');\n\n const migrations = await selectAllBatchedMigrations('test');\n\n assert.lengthOf(migrations, 1);\n });\n\n it('finalizes a successful migration', async () => {\n const runner = new BatchedMigrationsRunner({\n project: 'test',\n directories: [path.join(import.meta.dirname, 'fixtures')],\n });\n\n await runner.enqueueBatchedMigration('20230406184103_successful_migration');\n await runner.finalizeBatchedMigration('20230406184103_successful_migration', {\n logProgress: false,\n });\n\n const migrations = await selectAllBatchedMigrations('test');\n assert.lengthOf(migrations, 1);\n assert.equal(migrations[0].timestamp, '20230406184103');\n assert.equal(migrations[0].status, 'succeeded');\n });\n\n it('finalizes a failing migration', async () => {\n const runner = new BatchedMigrationsRunner({\n project: 'test',\n directories: [path.join(import.meta.dirname, 'fixtures')],\n });\n\n await runner.enqueueBatchedMigration('20230406184107_failing_migration');\n\n await assert.isRejected(\n runner.finalizeBatchedMigration('20230406184107_failing_migration', {\n logProgress: false,\n }),\n \"but it is 'failed'\",\n );\n\n const migrations = await selectAllBatchedMigrations('test');\n assert.lengthOf(migrations, 1);\n assert.equal(migrations[0].timestamp, '20230406184107');\n assert.equal(migrations[0].status, 'failed');\n });\n});\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"load-migrations.test.js","sourceRoot":"","sources":["../src/load-migrations.test.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"load-migrations.test.js","sourceRoot":"","sources":["../src/load-migrations.test.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,MAAM,CAAC;AAC9C,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,MAAM,UAAU,CAAC;AAC1B,OAAO,GAAG,MAAM,aAAa,CAAC;AAE9B,OAAO,EACL,gBAAgB,EAChB,sCAAsC,EACtC,kBAAkB,GACnB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,CAAC,cAAc,CAAC,CAAC;AAExB,KAAK,UAAU,kBAAkB,CAAC,KAAe,EAAE,EAAqC;IACtF,MAAM,GAAG,CAAC,OAAO,CACf,KAAK,WAAW,MAAM;QACpB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,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EAAE,YAAY;oBACvB,QAAQ,EAAE,8BAA8B;oBACxC,SAAS,EAAE,gBAAgB;iBAC5B;gBACD;oBACE,SAAS,EAAE,YAAY;oBACvB,QAAQ,EAAE,8BAA8B;oBACxC,SAAS,EAAE,gBAAgB;iBAC5B;aACF,CAAC,EACF;gBACE;oBACE,SAAS,EAAE,YAAY;oBACvB,QAAQ,EAAE,8BAA8B;oBACxC,SAAS,EAAE,gBAAgB;iBAC5B;gBACD;oBACE,SAAS,EAAE,YAAY;oBACvB,QAAQ,EAAE,8BAA8B;oBACxC,SAAS,EAAE,gBAAgB;iBAC5B;gBACD;oBACE,SAAS,EAAE,YAAY;oBACvB,QAAQ,EAAE,8BAA8B;oBACxC,SAAS,EAAE,gBAAgB;iBAC5B;aACF,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,kBAAkB,EAAE,GAAG,EAAE;QAChC,EAAE,CAAC,oCAAoC,EAAE,GAAG,EAAE;YAC5C,MAAM,WAAW,GAAG,gBAAgB,CAAC,2CAA2C,CAAC,CAAC;YAClF,MAAM,CAAC,SAAS,CAAC,WAAW,EAAE,IAAI,GAAG,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC;QAC7D,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,2CAA2C,EAAE,GAAG,EAAE;YACnD,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE;gBACjB,gBAAgB,CAAC,oCAAoC,CAAC,CAAC;YACzD,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import path from 'path';\n\nimport { assert, use as chaiUse } from 'chai';\nimport chaiAsPromised from 'chai-as-promised';\nimport fs from 'fs-extra';\nimport tmp from 'tmp-promise';\n\nimport {\n parseAnnotations,\n readAndValidateMigrationsFromDirectory,\n sortMigrationFiles,\n} from './load-migrations.js';\n\nchaiUse(chaiAsPromised);\n\nasync function withMigrationFiles(files: string[], fn: (tmpDir: string) => Promise<void>) {\n await tmp.withDir(\n async function (tmpDir) {\n for (const file of files) {\n await fs.writeFile(path.join(tmpDir.path, file), '');\n }\n await fn(tmpDir.path);\n },\n { unsafeCleanup: true },\n );\n}\n\ndescribe('load-migrations', () => {\n describe('readAndValidateMigrationsFromDirectory', () => {\n it('handles migrations without a timestamp', async () => {\n await withMigrationFiles(['001_testing.sql'], async (tmpDir) => {\n await assert.isRejected(\n readAndValidateMigrationsFromDirectory(tmpDir, ['.sql']),\n 'Invalid migration filename: 001_testing.sql',\n );\n });\n });\n\n it('handles duplicate timestamps', async () => {\n await withMigrationFiles(\n ['20220101010101_testing.sql', '20220101010101_testing_again.sql'],\n async (tmpDir) => {\n await assert.isRejected(\n readAndValidateMigrationsFromDirectory(tmpDir, ['.sql']),\n 'Duplicate migration timestamp',\n );\n },\n );\n });\n });\n\n describe('sortMigrationFiles', () => {\n it('sorts by timestamp', () => {\n assert.deepEqual(\n sortMigrationFiles([\n {\n directory: 'migrations',\n filename: '20220101010103_testing_3.sql',\n timestamp: '20220101010103',\n },\n {\n directory: 'migrations',\n filename: '20220101010101_testing_1.sql',\n timestamp: '20220101010101',\n },\n {\n directory: 'migrations',\n filename: '20220101010102_testing_2.sql',\n timestamp: '20220101010102',\n },\n ]),\n [\n {\n directory: 'migrations',\n filename: '20220101010101_testing_1.sql',\n timestamp: '20220101010101',\n },\n {\n directory: 'migrations',\n filename: '20220101010102_testing_2.sql',\n timestamp: '20220101010102',\n },\n {\n directory: 'migrations',\n filename: '20220101010103_testing_3.sql',\n timestamp: '20220101010103',\n },\n ],\n );\n });\n });\n\n describe('parseAnnotations', () => {\n it('parses a NO TRANSACTION annotation', () => {\n const annotations = parseAnnotations('-- prairielearn:migrations NO TRANSACTION');\n assert.deepEqual(annotations, new Set(['NO TRANSACTION']));\n });\n\n it('throws an error for an invalid annotation', () => {\n assert.throws(() => {\n parseAnnotations('-- prairielearn:migrations INVALID');\n });\n });\n });\n});\n"]}
|
package/package.json
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prairielearn/migrations",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.13",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"main": "./dist/index.js",
|
|
6
5
|
"repository": {
|
|
7
6
|
"type": "git",
|
|
8
7
|
"url": "https://github.com/PrairieLearn/PrairieLearn.git",
|
|
9
8
|
"directory": "packages/migrations"
|
|
10
9
|
},
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
11
|
"scripts": {
|
|
12
12
|
"build": "tsc && tscp",
|
|
13
13
|
"dev": "tsc --watch --preserveWatchOutput & tscp --watch",
|
|
14
14
|
"test": "c8 mocha src/**/*.test.ts"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@prairielearn/error": "^2.0.
|
|
18
|
-
"@prairielearn/logger": "^2.0.
|
|
19
|
-
"@prairielearn/named-locks": "^3.0.
|
|
20
|
-
"@prairielearn/postgres": "^2.1.
|
|
17
|
+
"@prairielearn/error": "^2.0.11",
|
|
18
|
+
"@prairielearn/logger": "^2.0.11",
|
|
19
|
+
"@prairielearn/named-locks": "^3.0.13",
|
|
20
|
+
"@prairielearn/postgres": "^2.1.9",
|
|
21
21
|
"fs-extra": "^11.3.0",
|
|
22
|
-
"serialize-error": "^
|
|
23
|
-
"zod": "^3.24.
|
|
22
|
+
"serialize-error": "^12.0.0",
|
|
23
|
+
"zod": "^3.24.2"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@prairielearn/tsconfig": "^0.0.0",
|
|
27
27
|
"@types/fs-extra": "^11.0.4",
|
|
28
28
|
"@types/mocha": "^10.0.10",
|
|
29
|
-
"@types/node": "^20.17.
|
|
29
|
+
"@types/node": "^20.17.28",
|
|
30
30
|
"c8": "^10.1.3",
|
|
31
|
-
"chai": "^5.
|
|
31
|
+
"chai": "^5.2.0",
|
|
32
32
|
"chai-as-promised": "^8.0.1",
|
|
33
|
-
"mocha": "^
|
|
33
|
+
"mocha": "^11.1.0",
|
|
34
34
|
"tmp-promise": "^3.0.3",
|
|
35
|
-
"tsx": "^4.19.
|
|
36
|
-
"typescript": "^5.
|
|
35
|
+
"tsx": "^4.19.3",
|
|
36
|
+
"typescript": "^5.8.2",
|
|
37
37
|
"typescript-cp": "^0.1.9"
|
|
38
38
|
},
|
|
39
39
|
"c8": {
|
|
@@ -2,19 +2,19 @@ import { serializeError } from 'serialize-error';
|
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
|
|
4
4
|
import { logger } from '@prairielearn/logger';
|
|
5
|
-
import { loadSqlEquiv, queryAsync,
|
|
5
|
+
import { loadSqlEquiv, queryAsync, queryOptionalRow, queryRow } from '@prairielearn/postgres';
|
|
6
6
|
|
|
7
7
|
import {
|
|
8
|
+
type BatchedMigrationJobRow,
|
|
8
9
|
BatchedMigrationJobRowSchema,
|
|
9
10
|
type BatchedMigrationJobStatus,
|
|
10
|
-
type BatchedMigrationJobRow,
|
|
11
11
|
} from './batched-migration-job.js';
|
|
12
12
|
import {
|
|
13
|
-
type
|
|
13
|
+
type BatchedMigrationImplementation,
|
|
14
14
|
type BatchedMigrationRow,
|
|
15
|
-
|
|
15
|
+
type BatchedMigrationStatus,
|
|
16
16
|
BatchedMigrationStatusSchema,
|
|
17
|
-
|
|
17
|
+
updateBatchedMigrationStatus,
|
|
18
18
|
} from './batched-migration.js';
|
|
19
19
|
|
|
20
20
|
const sql = loadSqlEquiv(import.meta.filename);
|
|
@@ -12,13 +12,13 @@ import {
|
|
|
12
12
|
|
|
13
13
|
import { BatchedMigrationRunner } from './batched-migration-runner.js';
|
|
14
14
|
import {
|
|
15
|
-
|
|
15
|
+
type BatchedMigrationImplementation,
|
|
16
16
|
type BatchedMigrationRow,
|
|
17
|
-
|
|
17
|
+
BatchedMigrationRowSchema,
|
|
18
18
|
type BatchedMigrationStatus,
|
|
19
|
+
insertBatchedMigration,
|
|
19
20
|
selectBatchedMigrationForTimestamp,
|
|
20
21
|
updateBatchedMigrationStatus,
|
|
21
|
-
type BatchedMigrationImplementation,
|
|
22
22
|
validateBatchedMigrationImplementation,
|
|
23
23
|
} from './batched-migration.js';
|
|
24
24
|
|