@prairielearn/migrations 3.0.25 → 4.0.0

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 CHANGED
@@ -1,5 +1,12 @@
1
1
  # @prairielearn/migrations
2
2
 
3
+ ## 4.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - d4d5ce6: - `init(...)` now accepts an options object instead of positional arguments.
8
+ - `init(...)` now supports a `migrationFilters` option to control which migrations to run.
9
+
3
10
  ## 3.0.25
4
11
 
5
12
  ### Patch Changes
@@ -69,7 +69,7 @@ describe('BatchedMigrationExecutor', () => {
69
69
  await namedLocks.init(postgresTestUtils.getPoolConfig(), (err) => {
70
70
  throw err;
71
71
  });
72
- await init([SCHEMA_MIGRATIONS_PATH], 'prairielearn_migrations');
72
+ await init({ directories: [SCHEMA_MIGRATIONS_PATH], project: 'prairielearn_migrations' });
73
73
  });
74
74
  beforeEach(async () => {
75
75
  await postgresTestUtils.resetDatabase();
@@ -1 +1 @@
1
- {"version":3,"file":"batched-migration-runner.test.js","sourceRoot":"","sources":["../../src/batched-migrations/batched-migration-runner.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAE/E,OAAO,KAAK,KAAK,MAAM,qBAAqB,CAAC;AAC7C,OAAO,KAAK,UAAU,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,OAAO,EAAE,qBAAqB,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAE7F,OAAO,EAAE,sBAAsB,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAE3D,OAAO,EAAE,4BAA4B,EAAE,MAAM,4BAA4B,CAAC;AAC1E,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EACL,yBAAyB,EACzB,sBAAsB,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WAAW,CAAC,CAAC;QAClF,MAAM,WAAW,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,QAAQ,KAAK,CAAC,CAAC,CAAC;QAClE,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;QAC/B,MAAM,CAAC,QAAQ,CAAC,eAAe,EAAE,CAAC,CAAC,CAAC;QACpC,MAAM,CAAC,QAAQ,CAAC,mBAAmB,EAAE,EAAE,CAAC,CAAC;QACzC,MAAM,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC;QAChC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC;QAE3D,SAAS,GAAG,MAAM,mBAAmB,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;QACpD,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;QAE5C,oEAAoE;QACpE,uCAAuC;QACvC,MAAM,CAAC,KAAK,CAAC,uBAAuB,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;IAC3D,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { afterAll, assert, beforeAll, beforeEach, describe, it } from 'vitest';\n\nimport * as error from '@prairielearn/error';\nimport * as namedLocks from '@prairielearn/named-locks';\nimport { execute, makePostgresTestUtils, queryRow, queryRows } from '@prairielearn/postgres';\n\nimport { SCHEMA_MIGRATIONS_PATH, init } from '../index.js';\n\nimport { BatchedMigrationJobRowSchema } from './batched-migration-job.js';\nimport { BatchedMigrationRunner } from './batched-migration-runner.js';\nimport {\n BatchedMigrationRowSchema,\n insertBatchedMigration,\n makeBatchedMigration,\n updateBatchedMigrationStatus,\n} from './batched-migration.js';\n\nconst postgresTestUtils = makePostgresTestUtils({\n database: 'prairielearn_migrations',\n});\n\nfunction makeTestBatchMigration() {\n let executionCount = 0;\n let failingIds: bigint[] = [];\n\n return makeBatchedMigration({\n async getParameters() {\n return {\n min: 1n,\n max: 10000n,\n batchSize: 1000,\n };\n },\n async execute(start: bigint, end: bigint) {\n executionCount += 1;\n const shouldFail = failingIds.some((id) => id >= start && id <= end);\n if (shouldFail) {\n // Throw an error with some data to make sure it gets persisted. We\n // specifically use BigInt values here to make sure that they are\n // correctly serialized to strings.\n throw new error.AugmentedError('Execution failure', { data: { start, end } });\n }\n },\n setFailingIds(ids: bigint[]) {\n failingIds = ids;\n },\n get executionCount() {\n return executionCount;\n },\n });\n}\n\nasync function getBatchedMigration(migrationId: string) {\n return await queryRow(\n 'SELECT * FROM batched_migrations WHERE id = $id;',\n { id: migrationId },\n BatchedMigrationRowSchema,\n );\n}\n\nasync function getBatchedMigrationJobs(migrationId: string) {\n return await queryRows(\n 'SELECT * FROM batched_migration_jobs WHERE batched_migration_id = $batched_migration_id ORDER BY id ASC;',\n { batched_migration_id: migrationId },\n BatchedMigrationJobRowSchema,\n );\n}\n\nasync function resetFailedBatchedMigrationJobs(migrationId: string) {\n await execute(\n \"UPDATE batched_migration_jobs SET status = 'pending', updated_at = CURRENT_TIMESTAMP WHERE batched_migration_id = $batched_migration_id AND status = 'failed'\",\n { batched_migration_id: migrationId },\n );\n}\n\nasync function insertTestBatchedMigration() {\n const migrationImplementation = makeTestBatchMigration();\n const parameters = await migrationImplementation.getParameters();\n const migration = await insertBatchedMigration({\n project: 'test',\n filename: '20230406184103_test_batch_migration.js',\n timestamp: '20230406184103',\n batch_size: parameters.batchSize,\n min_value: parameters.min,\n max_value: parameters.max,\n status: 'running',\n });\n if (!migration) throw new Error('Failed to insert batched migration');\n return migration;\n}\n\ndescribe('BatchedMigrationExecutor', () => {\n beforeAll(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 beforeEach(async () => {\n await postgresTestUtils.resetDatabase();\n });\n\n afterAll(async () => {\n await namedLocks.close();\n await postgresTestUtils.dropDatabase();\n });\n\n it('runs one iteration of a batched migration', async () => {\n const migration = await insertTestBatchedMigration();\n\n const migrationImplementation = makeTestBatchMigration();\n const executor = new BatchedMigrationRunner(migration, migrationImplementation);\n await executor.run({ iterations: 1 });\n\n const jobs = await getBatchedMigrationJobs(migration.id);\n assert.lengthOf(jobs, 1);\n\n const finalMigration = await getBatchedMigration(migration.id);\n assert.equal(finalMigration.status, 'running');\n\n assert.equal(migrationImplementation.executionCount, 1);\n });\n\n it('runs an entire batched migration', async () => {\n const migration = await insertTestBatchedMigration();\n\n const migrationImplementation = makeTestBatchMigration();\n const runner = new BatchedMigrationRunner(migration, migrationImplementation);\n await runner.run();\n\n const jobs = await getBatchedMigrationJobs(migration.id);\n assert.lengthOf(jobs, 10);\n assert.equal(jobs[0].min_value, 1n);\n assert.equal(jobs[0].max_value, 1000n);\n assert.equal(jobs.at(-1)?.min_value, 9001n);\n assert.equal(jobs.at(-1)?.max_value, 10000n);\n assert.isTrue(jobs.every((job) => job.started_at !== null));\n assert.isTrue(jobs.every((job) => job.finished_at !== null));\n assert.isTrue(jobs.every((job) => job.status === 'succeeded'));\n assert.isTrue(jobs.every((job) => job.attempts === 1));\n\n const finalMigration = await getBatchedMigration(migration.id);\n assert.equal(finalMigration.status, 'succeeded');\n });\n\n it('handles failing execution', async () => {\n let migration = await insertTestBatchedMigration();\n\n const migrationImplementation = makeTestBatchMigration();\n migrationImplementation.setFailingIds([1n, 5010n]);\n const runner = new BatchedMigrationRunner(migration, migrationImplementation);\n await runner.run();\n\n const jobs = await getBatchedMigrationJobs(migration.id);\n const failedJobs = jobs.filter((job) => job.status === 'failed');\n const successfulJobs = jobs.filter((job) => job.status === 'succeeded');\n assert.lengthOf(jobs, 10);\n assert.lengthOf(failedJobs, 2);\n assert.lengthOf(successfulJobs, 8);\n assert.equal(migrationImplementation.executionCount, 10);\n assert.isTrue(jobs.every((job) => job.attempts === 1));\n failedJobs.forEach((job) => {\n const jobData = job.data as any;\n assert.isObject(jobData);\n assert.isObject(jobData.error);\n assert.hasAllKeys(jobData.error, ['name', 'message', 'stack', 'data', 'status']);\n assert.equal(jobData.error.name, 'Error');\n assert.equal(jobData.error.message, 'Execution failure');\n assert.equal(jobData.error.data.start, job.min_value.toString());\n assert.equal(jobData.error.data.end, job.max_value.toString());\n });\n\n const failedMigration = await getBatchedMigration(migration.id);\n assert.equal(failedMigration.status, 'failed');\n\n // Retry the failed jobs; ensure they succeed this time.\n await resetFailedBatchedMigrationJobs(migration.id);\n migration = await updateBatchedMigrationStatus(migration.id, 'running');\n\n migrationImplementation.setFailingIds([]);\n const retryRunner = new BatchedMigrationRunner(migration, migrationImplementation);\n await retryRunner.run();\n\n const finalJobs = await getBatchedMigrationJobs(migration.id);\n const finalFailedJobs = finalJobs.filter((job) => job.status === 'failed');\n const finalSuccessfulJobs = finalJobs.filter((job) => job.status === 'succeeded');\n const retriedJobs = finalJobs.filter((job) => job.attempts === 2);\n assert.lengthOf(finalJobs, 10);\n assert.lengthOf(finalFailedJobs, 0);\n assert.lengthOf(finalSuccessfulJobs, 10);\n assert.lengthOf(retriedJobs, 2);\n assert.isTrue(finalJobs.every((job) => job.data === null));\n\n migration = await getBatchedMigration(migration.id);\n assert.equal(migration.status, 'succeeded');\n\n // The runner should have run only the previously failed jobs, which\n // works out to 2 additional execution.\n assert.equal(migrationImplementation.executionCount, 12);\n });\n});\n"]}
1
+ {"version":3,"file":"batched-migration-runner.test.js","sourceRoot":"","sources":["../../src/batched-migrations/batched-migration-runner.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAE/E,OAAO,KAAK,KAAK,MAAM,qBAAqB,CAAC;AAC7C,OAAO,KAAK,UAAU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import * as error from '@prairielearn/error';\nimport * as namedLocks from '@prairielearn/named-locks';\nimport { execute, makePostgresTestUtils, queryRow, queryRows } from '@prairielearn/postgres';\n\nimport { SCHEMA_MIGRATIONS_PATH, init } from '../index.js';\n\nimport { BatchedMigrationJobRowSchema } from './batched-migration-job.js';\nimport { BatchedMigrationRunner } from './batched-migration-runner.js';\nimport {\n BatchedMigrationRowSchema,\n insertBatchedMigration,\n makeBatchedMigration,\n updateBatchedMigrationStatus,\n} from './batched-migration.js';\n\nconst postgresTestUtils = makePostgresTestUtils({\n database: 'prairielearn_migrations',\n});\n\nfunction makeTestBatchMigration() {\n let executionCount = 0;\n let failingIds: bigint[] = [];\n\n return makeBatchedMigration({\n async getParameters() {\n return {\n min: 1n,\n max: 10000n,\n batchSize: 1000,\n };\n },\n async execute(start: bigint, end: bigint) {\n executionCount += 1;\n const shouldFail = failingIds.some((id) => id >= start && id <= end);\n if (shouldFail) {\n // Throw an error with some data to make sure it gets persisted. We\n // specifically use BigInt values here to make sure that they are\n // correctly serialized to strings.\n throw new error.AugmentedError('Execution failure', { data: { start, end } });\n }\n },\n setFailingIds(ids: bigint[]) {\n failingIds = ids;\n },\n get executionCount() {\n return executionCount;\n },\n });\n}\n\nasync function getBatchedMigration(migrationId: string) {\n return await queryRow(\n 'SELECT * FROM batched_migrations WHERE id = $id;',\n { id: migrationId },\n BatchedMigrationRowSchema,\n );\n}\n\nasync function getBatchedMigrationJobs(migrationId: string) {\n return await queryRows(\n 'SELECT * FROM batched_migration_jobs WHERE batched_migration_id = $batched_migration_id ORDER BY id ASC;',\n { batched_migration_id: migrationId },\n BatchedMigrationJobRowSchema,\n );\n}\n\nasync function resetFailedBatchedMigrationJobs(migrationId: string) {\n await execute(\n \"UPDATE batched_migration_jobs SET status = 'pending', updated_at = CURRENT_TIMESTAMP WHERE batched_migration_id = $batched_migration_id AND status = 'failed'\",\n { batched_migration_id: migrationId },\n );\n}\n\nasync function insertTestBatchedMigration() {\n const migrationImplementation = makeTestBatchMigration();\n const parameters = await migrationImplementation.getParameters();\n const migration = await insertBatchedMigration({\n project: 'test',\n filename: '20230406184103_test_batch_migration.js',\n timestamp: '20230406184103',\n batch_size: parameters.batchSize,\n min_value: parameters.min,\n max_value: parameters.max,\n status: 'running',\n });\n if (!migration) throw new Error('Failed to insert batched migration');\n return migration;\n}\n\ndescribe('BatchedMigrationExecutor', () => {\n beforeAll(async () => {\n await postgresTestUtils.createDatabase();\n await namedLocks.init(postgresTestUtils.getPoolConfig(), (err) => {\n throw err;\n });\n await init({ directories: [SCHEMA_MIGRATIONS_PATH], project: 'prairielearn_migrations' });\n });\n\n beforeEach(async () => {\n await postgresTestUtils.resetDatabase();\n });\n\n afterAll(async () => {\n await namedLocks.close();\n await postgresTestUtils.dropDatabase();\n });\n\n it('runs one iteration of a batched migration', async () => {\n const migration = await insertTestBatchedMigration();\n\n const migrationImplementation = makeTestBatchMigration();\n const executor = new BatchedMigrationRunner(migration, migrationImplementation);\n await executor.run({ iterations: 1 });\n\n const jobs = await getBatchedMigrationJobs(migration.id);\n assert.lengthOf(jobs, 1);\n\n const finalMigration = await getBatchedMigration(migration.id);\n assert.equal(finalMigration.status, 'running');\n\n assert.equal(migrationImplementation.executionCount, 1);\n });\n\n it('runs an entire batched migration', async () => {\n const migration = await insertTestBatchedMigration();\n\n const migrationImplementation = makeTestBatchMigration();\n const runner = new BatchedMigrationRunner(migration, migrationImplementation);\n await runner.run();\n\n const jobs = await getBatchedMigrationJobs(migration.id);\n assert.lengthOf(jobs, 10);\n assert.equal(jobs[0].min_value, 1n);\n assert.equal(jobs[0].max_value, 1000n);\n assert.equal(jobs.at(-1)?.min_value, 9001n);\n assert.equal(jobs.at(-1)?.max_value, 10000n);\n assert.isTrue(jobs.every((job) => job.started_at !== null));\n assert.isTrue(jobs.every((job) => job.finished_at !== null));\n assert.isTrue(jobs.every((job) => job.status === 'succeeded'));\n assert.isTrue(jobs.every((job) => job.attempts === 1));\n\n const finalMigration = await getBatchedMigration(migration.id);\n assert.equal(finalMigration.status, 'succeeded');\n });\n\n it('handles failing execution', async () => {\n let migration = await insertTestBatchedMigration();\n\n const migrationImplementation = makeTestBatchMigration();\n migrationImplementation.setFailingIds([1n, 5010n]);\n const runner = new BatchedMigrationRunner(migration, migrationImplementation);\n await runner.run();\n\n const jobs = await getBatchedMigrationJobs(migration.id);\n const failedJobs = jobs.filter((job) => job.status === 'failed');\n const successfulJobs = jobs.filter((job) => job.status === 'succeeded');\n assert.lengthOf(jobs, 10);\n assert.lengthOf(failedJobs, 2);\n assert.lengthOf(successfulJobs, 8);\n assert.equal(migrationImplementation.executionCount, 10);\n assert.isTrue(jobs.every((job) => job.attempts === 1));\n failedJobs.forEach((job) => {\n const jobData = job.data as any;\n assert.isObject(jobData);\n assert.isObject(jobData.error);\n assert.hasAllKeys(jobData.error, ['name', 'message', 'stack', 'data', 'status']);\n assert.equal(jobData.error.name, 'Error');\n assert.equal(jobData.error.message, 'Execution failure');\n assert.equal(jobData.error.data.start, job.min_value.toString());\n assert.equal(jobData.error.data.end, job.max_value.toString());\n });\n\n const failedMigration = await getBatchedMigration(migration.id);\n assert.equal(failedMigration.status, 'failed');\n\n // Retry the failed jobs; ensure they succeed this time.\n await resetFailedBatchedMigrationJobs(migration.id);\n migration = await updateBatchedMigrationStatus(migration.id, 'running');\n\n migrationImplementation.setFailingIds([]);\n const retryRunner = new BatchedMigrationRunner(migration, migrationImplementation);\n await retryRunner.run();\n\n const finalJobs = await getBatchedMigrationJobs(migration.id);\n const finalFailedJobs = finalJobs.filter((job) => job.status === 'failed');\n const finalSuccessfulJobs = finalJobs.filter((job) => job.status === 'succeeded');\n const retriedJobs = finalJobs.filter((job) => job.attempts === 2);\n assert.lengthOf(finalJobs, 10);\n assert.lengthOf(finalFailedJobs, 0);\n assert.lengthOf(finalSuccessfulJobs, 10);\n assert.lengthOf(retriedJobs, 2);\n assert.isTrue(finalJobs.every((job) => job.data === null));\n\n migration = await getBatchedMigration(migration.id);\n assert.equal(migration.status, 'succeeded');\n\n // The runner should have run only the previously failed jobs, which\n // works out to 2 additional execution.\n assert.equal(migrationImplementation.executionCount, 12);\n });\n});\n"]}
@@ -14,7 +14,7 @@ describe('BatchedMigrationsRunner', () => {
14
14
  await namedLocks.init(postgresTestUtils.getPoolConfig(), (err) => {
15
15
  throw err;
16
16
  });
17
- await init([SCHEMA_MIGRATIONS_PATH], 'prairielearn_migrations');
17
+ await init({ directories: [SCHEMA_MIGRATIONS_PATH], project: 'prairielearn_migrations' });
18
18
  });
19
19
  afterEach(async () => {
20
20
  await postgresTestUtils.resetDatabase();
@@ -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,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAEtF,OAAO,KAAK,UAAU,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAE/D,OAAO,EAAE,sBAAsB,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAE3D,OAAO,EAAE,0BAA0B,EAAE,MAAM,wBAAwB,CAAC;AACpE,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAEzE,MAAM,iBAAiB,GAAG,qBAAqB,CAAC;IAC9C,QAAQ,EAAE,yBAAyB;CACpC,CAAC,CAAC;AAEH,QAAQ,CAAC,yBAAyB,EAAE,GAAG,EAAE;IACvC,SAAS,CAAC,KAAK,IAAI,EAAE;QACnB,MAAM,iBAAiB,CAAC,cAAc,EAAE,CAAC;QACzC,MAAM,UAAU,CAAC,IAAI,CAAC,iBAAiB,CAAC,aAAa,EAAE,EAAE,CAAC,GAAG,EAAE,EAAE;YAC/D,MAAM,GAAG,C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U,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 { afterAll, afterEach, assert, beforeAll, describe, expect, it } from 'vitest';\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\nconst postgresTestUtils = makePostgresTestUtils({\n database: 'prairielearn_migrations',\n});\n\ndescribe('BatchedMigrationsRunner', () => {\n beforeAll(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 afterAll(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 expect(\n runner.finalizeBatchedMigration('20230406184107_failing_migration', {\n logProgress: false,\n }),\n ).rejects.toThrow(\"but it is 'failed'\");\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
+ {"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,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,EA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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,CACV,MAAM,CAAC,wBAAwB,CAAC,kCAAkC,EAAE;YAClE,WAAW,EAAE,KAAK;SACnB,CAAC,CACH,CAAC,OAAO,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC;QACxC,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 { afterAll, afterEach, assert, beforeAll, describe, expect, it } from 'vitest';\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\nconst postgresTestUtils = makePostgresTestUtils({\n database: 'prairielearn_migrations',\n});\n\ndescribe('BatchedMigrationsRunner', () => {\n beforeAll(async () => {\n await postgresTestUtils.createDatabase();\n await namedLocks.init(postgresTestUtils.getPoolConfig(), (err) => {\n throw err;\n });\n await init({ directories: [SCHEMA_MIGRATIONS_PATH], project: 'prairielearn_migrations' });\n });\n\n afterEach(async () => {\n await postgresTestUtils.resetDatabase();\n });\n\n afterAll(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 expect(\n runner.finalizeBatchedMigration('20230406184107_failing_migration', {\n logProgress: false,\n }),\n ).rejects.toThrow(\"but it is 'failed'\");\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"]}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { init } from './migrations/index.js';
2
2
  export { type BatchedMigrationRow, type BatchedMigrationStatus, type BatchedMigrationJobRow, type BatchedMigrationJobStatus, makeBatchedMigration, initBatchedMigrations, startBatchedMigrations, stopBatchedMigrations, enqueueBatchedMigration, finalizeBatchedMigration, selectAllBatchedMigrations, selectBatchedMigration, selectBatchedMigrationForTimestamp, selectRecentJobsWithStatus, retryFailedBatchedMigrationJobs, } from './batched-migrations/index.js';
3
+ export { extractTimestampFromFilename } from './load-migrations.js';
3
4
  export declare const SCHEMA_MIGRATIONS_PATH: string;
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import path from 'path';
2
2
  export { init } from './migrations/index.js';
3
3
  export { makeBatchedMigration, initBatchedMigrations, startBatchedMigrations, stopBatchedMigrations, enqueueBatchedMigration, finalizeBatchedMigration, selectAllBatchedMigrations, selectBatchedMigration, selectBatchedMigrationForTimestamp, selectRecentJobsWithStatus, retryFailedBatchedMigrationJobs, } from './batched-migrations/index.js';
4
+ export { extractTimestampFromFilename } from './load-migrations.js';
4
5
  export const SCHEMA_MIGRATIONS_PATH = path.resolve(import.meta.dirname, '..', 'schema-migrations');
5
6
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAE7C,OAAO,EAKL,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,EACtB,qBAAqB,EACrB,uBAAuB,EACvB,wBAAwB,EACxB,0BAA0B,EAC1B,sBAAsB,EACtB,kCAAkC,EAClC,0BAA0B,EAC1B,+BAA+B,GAChC,MAAM,+BAA+B,CAAC;AAEvC,MAAM,CAAC,MAAM,sBAAsB,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,mBAAmB,CAAC,CAAC","sourcesContent":["import path from 'path';\n\nexport { init } from './migrations/index.js';\n\nexport {\n type BatchedMigrationRow,\n type BatchedMigrationStatus,\n type BatchedMigrationJobRow,\n type BatchedMigrationJobStatus,\n makeBatchedMigration,\n initBatchedMigrations,\n startBatchedMigrations,\n stopBatchedMigrations,\n enqueueBatchedMigration,\n finalizeBatchedMigration,\n selectAllBatchedMigrations,\n selectBatchedMigration,\n selectBatchedMigrationForTimestamp,\n selectRecentJobsWithStatus,\n retryFailedBatchedMigrationJobs,\n} from './batched-migrations/index.js';\n\nexport const SCHEMA_MIGRATIONS_PATH = path.resolve(import.meta.dirname, '..', 'schema-migrations');\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAE7C,OAAO,EAKL,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,EACtB,qBAAqB,EACrB,uBAAuB,EACvB,wBAAwB,EACxB,0BAA0B,EAC1B,sBAAsB,EACtB,kCAAkC,EAClC,0BAA0B,EAC1B,+BAA+B,GAChC,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EAAE,4BAA4B,EAAE,MAAM,sBAAsB,CAAC;AACpE,MAAM,CAAC,MAAM,sBAAsB,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,mBAAmB,CAAC,CAAC","sourcesContent":["import path from 'path';\n\nexport { init } from './migrations/index.js';\n\nexport {\n type BatchedMigrationRow,\n type BatchedMigrationStatus,\n type BatchedMigrationJobRow,\n type BatchedMigrationJobStatus,\n makeBatchedMigration,\n initBatchedMigrations,\n startBatchedMigrations,\n stopBatchedMigrations,\n enqueueBatchedMigration,\n finalizeBatchedMigration,\n selectAllBatchedMigrations,\n selectBatchedMigration,\n selectBatchedMigrationForTimestamp,\n selectRecentJobsWithStatus,\n retryFailedBatchedMigrationJobs,\n} from './batched-migrations/index.js';\n\nexport { extractTimestampFromFilename } from './load-migrations.js';\nexport const SCHEMA_MIGRATIONS_PATH = path.resolve(import.meta.dirname, '..', 'schema-migrations');\n"]}
@@ -3,6 +3,7 @@ export interface MigrationFile {
3
3
  filename: string;
4
4
  timestamp: string;
5
5
  }
6
+ export declare function extractTimestampFromFilename(filename: string): string;
6
7
  export declare function readAndValidateMigrationsFromDirectory(dir: string, extensions: string[]): Promise<MigrationFile[]>;
7
8
  export declare function readAndValidateMigrationsFromDirectories(directories: string[], extensions: string[]): Promise<MigrationFile[]>;
8
9
  export declare function sortMigrationFiles(migrationFiles: MigrationFile[]): MigrationFile[];
@@ -3,7 +3,7 @@ import fs from 'fs-extra';
3
3
  * Timestamp prefixes will be of the form `YYYYMMDDHHMMSS`, which will have 14 digits.
4
4
  * If this code is still around in the year 10000... good luck.
5
5
  */
6
- const MIGRATION_FILENAME_REGEX = /^([0-9]{14})_.+\.[a-z]+/;
6
+ const MIGRATION_FILENAME_REGEX = /^([0-9]{14})_.+/;
7
7
  /**
8
8
  * Annotations are expressed via the following:
9
9
  *
@@ -15,17 +15,25 @@ const MIGRATION_FILENAME_REGEX = /^([0-9]{14})_.+\.[a-z]+/;
15
15
  */
16
16
  const ANNOTATION_PREFIX = '-- prairielearn:migrations';
17
17
  const ALLOWED_ANNOTATIONS = new Set(['NO TRANSACTION']);
18
+ export function extractTimestampFromFilename(filename) {
19
+ const match = filename.match(MIGRATION_FILENAME_REGEX);
20
+ if (!match) {
21
+ throw new Error(`Invalid migration filename: ${filename}`);
22
+ }
23
+ const timestamp = match.at(1) ?? null;
24
+ if (timestamp === null) {
25
+ throw new Error(`Migration ${filename} does not have a timestamp`);
26
+ }
27
+ return timestamp;
28
+ }
18
29
  export async function readAndValidateMigrationsFromDirectory(dir, extensions) {
19
- const migrationFiles = (await fs.readdir(dir)).filter((m) => extensions.some((e) => m.endsWith(e)));
30
+ const migrationFiles = (await fs.readdir(dir)).filter((m) => {
31
+ // Get the full extension of the file (e.g. for `foo.test.ts`, return `.test.ts`).
32
+ const [_name, ...extensionParts] = m.split('.');
33
+ return extensions.includes('.' + extensionParts.join('.'));
34
+ });
20
35
  const migrations = migrationFiles.map((mf) => {
21
- const match = mf.match(MIGRATION_FILENAME_REGEX);
22
- if (!match) {
23
- throw new Error(`Invalid migration filename: ${mf}`);
24
- }
25
- const timestamp = match[1] ?? null;
26
- if (timestamp === null) {
27
- throw new Error(`Migration ${mf} does not have a timestamp`);
28
- }
36
+ const timestamp = extractTimestampFromFilename(mf);
29
37
  return {
30
38
  directory: dir,
31
39
  filename: mf,
@@ -1 +1 @@
1
- {"version":3,"file":"load-migrations.js","sourceRoot":"","sources":["../src/load-migrations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,UAAU,CAAC;AAE1B;;;GAGG;AACH,MAAM,wBAAwB,GAAG,yBAAyB,CAAC;AAE3D;;;;;;;;GAQG;AACH,MAAM,iBAAiB,GAAG,4BAA4B,CAAC;AACvD,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC;AAQxD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,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["import fs from 'fs-extra';\n\n/**\n * Timestamp prefixes will be of the form `YYYYMMDDHHMMSS`, which will have 14 digits.\n * If this code is still around in the year 10000... good luck.\n */\nconst MIGRATION_FILENAME_REGEX = /^([0-9]{14})_.+\\.[a-z]+/;\n\n/**\n * Annotations are expressed via the following:\n *\n * -- prairielearn:migrations NO TRANSACTION\n *\n * Currently, `NO TRANSACTION` is the only supported annotation. This will run\n * the migration without a transaction. This is useful for migrations that use\n * features that can't be run in transactions, such as `CREATE INDEX CONCURRENTLY`.\n */\nconst ANNOTATION_PREFIX = '-- prairielearn:migrations';\nconst ALLOWED_ANNOTATIONS = new Set(['NO TRANSACTION']);\n\nexport interface MigrationFile {\n directory: string;\n filename: string;\n timestamp: string;\n}\n\nexport async function readAndValidateMigrationsFromDirectory(\n dir: string,\n extensions: string[],\n): Promise<MigrationFile[]> {\n const migrationFiles = (await fs.readdir(dir)).filter((m) =>\n extensions.some((e) => m.endsWith(e)),\n );\n\n const migrations = migrationFiles.map((mf) => {\n const match = mf.match(MIGRATION_FILENAME_REGEX);\n\n if (!match) {\n throw new Error(`Invalid migration filename: ${mf}`);\n }\n\n const timestamp = match[1] ?? null;\n\n if (timestamp === null) {\n throw new Error(`Migration ${mf} does not have a timestamp`);\n }\n\n return {\n directory: dir,\n filename: mf,\n timestamp,\n };\n });\n\n // First pass: validate that all migrations have a unique timestamp prefix.\n // This will avoid data loss and conflicts in unexpected scenarios.\n const seenTimestamps = new Set();\n for (const migration of migrations) {\n const { filename, timestamp } = migration;\n\n if (timestamp !== null) {\n if (seenTimestamps.has(timestamp)) {\n throw new Error(`Duplicate migration timestamp: ${timestamp} (${filename})`);\n }\n seenTimestamps.add(timestamp);\n }\n }\n\n return migrations;\n}\n\nexport async function readAndValidateMigrationsFromDirectories(\n directories: string[],\n extensions: string[],\n): Promise<MigrationFile[]> {\n const allMigrations: MigrationFile[] = [];\n for (const directory of directories) {\n const migrations = await readAndValidateMigrationsFromDirectory(directory, extensions);\n allMigrations.push(...migrations);\n }\n return allMigrations;\n}\n\nexport function sortMigrationFiles(migrationFiles: MigrationFile[]): MigrationFile[] {\n return migrationFiles.sort((a, b) => {\n return a.timestamp.localeCompare(b.timestamp);\n });\n}\n\nexport function parseAnnotations(contents: string): Set<string> {\n const lines = contents.split('\\n');\n const annotations = new Set<string>();\n\n lines.forEach((line) => {\n if (line.startsWith(ANNOTATION_PREFIX)) {\n const annotation = line.slice(ANNOTATION_PREFIX.length).trim();\n if (!ALLOWED_ANNOTATIONS.has(annotation)) {\n throw new Error(`Invalid annotation: ${annotation}`);\n }\n annotations.add(annotation);\n }\n });\n\n return annotations;\n}\n"]}
1
+ {"version":3,"file":"load-migrations.js","sourceRoot":"","sources":["../src/load-migrations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,UAAU,CAAC;AAE1B;;;GAGG;AACH,MAAM,wBAAwB,GAAG,iBAAiB,CAAC;AAEnD;;;;;;;;GAQG;AACH,MAAM,iBAAiB,GAAG,4BAA4B,CAAC;AACvD,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC;AAQxD,MAAM,UAAU,4BAA4B,CAAC,QAAgB;IAC3D,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAC;IACvD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,MAAM,IAAI,KAAK,CAAC,+BAA+B,QAAQ,EAAE,CAAC,CAAC;IAC7D,CAAC;IACD,MAAM,SAAS,GAAG,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;IACtC,IAAI,SAAS,KAAK,IAAI,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,aAAa,QAAQ,4BAA4B,CAAC,CAAC;IACrE,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,MAAM,CAAC,KAAK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AAK,MAAM,SAAS,IAAI,WAAW,EAAE,CAAC;QACpC,MAAM,UAAU,GAAG,MAAM,sCAAsC,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;QACvF,aAAa,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC,CAAC;IACpC,CAAC;IACD,OAAO,aAAa,CAAC;AACvB,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,cAA+B;IAChE,OAAO,cAAc,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;QAClC,OAAO,CAAC,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;IAChD,CAAC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,QAAgB;IAC/C,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACnC,MAAM,WAAW,GAAG,IAAI,GAAG,EAAU,CAAC;IAEtC,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QACrB,IAAI,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC,EAAE,CAAC;YACvC,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC;YAC/D,IAAI,CAAC,mBAAmB,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC;gBACzC,MAAM,IAAI,KAAK,CAAC,uBAAuB,UAAU,EAAE,CAAC,CAAC;YACvD,CAAC;YACD,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QAC9B,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["import fs from 'fs-extra';\n\n/**\n * Timestamp prefixes will be of the form `YYYYMMDDHHMMSS`, which will have 14 digits.\n * If this code is still around in the year 10000... good luck.\n */\nconst MIGRATION_FILENAME_REGEX = /^([0-9]{14})_.+/;\n\n/**\n * Annotations are expressed via the following:\n *\n * -- prairielearn:migrations NO TRANSACTION\n *\n * Currently, `NO TRANSACTION` is the only supported annotation. This will run\n * the migration without a transaction. This is useful for migrations that use\n * features that can't be run in transactions, such as `CREATE INDEX CONCURRENTLY`.\n */\nconst ANNOTATION_PREFIX = '-- prairielearn:migrations';\nconst ALLOWED_ANNOTATIONS = new Set(['NO TRANSACTION']);\n\nexport interface MigrationFile {\n directory: string;\n filename: string;\n timestamp: string;\n}\n\nexport function extractTimestampFromFilename(filename: string): string {\n const match = filename.match(MIGRATION_FILENAME_REGEX);\n if (!match) {\n throw new Error(`Invalid migration filename: ${filename}`);\n }\n const timestamp = match.at(1) ?? null;\n if (timestamp === null) {\n throw new Error(`Migration ${filename} does not have a timestamp`);\n }\n return timestamp;\n}\n\nexport async function readAndValidateMigrationsFromDirectory(\n dir: string,\n extensions: string[],\n): Promise<MigrationFile[]> {\n const migrationFiles = (await fs.readdir(dir)).filter((m) => {\n // Get the full extension of the file (e.g. for `foo.test.ts`, return `.test.ts`).\n const [_name, ...extensionParts] = m.split('.');\n return extensions.includes('.' + extensionParts.join('.'));\n });\n\n const migrations = migrationFiles.map((mf) => {\n const timestamp = extractTimestampFromFilename(mf);\n\n return {\n directory: dir,\n filename: mf,\n timestamp,\n };\n });\n\n // First pass: validate that all migrations have a unique timestamp prefix.\n // This will avoid data loss and conflicts in unexpected scenarios.\n const seenTimestamps = new Set();\n for (const migration of migrations) {\n const { filename, timestamp } = migration;\n\n if (timestamp !== null) {\n if (seenTimestamps.has(timestamp)) {\n throw new Error(`Duplicate migration timestamp: ${timestamp} (${filename})`);\n }\n seenTimestamps.add(timestamp);\n }\n }\n\n return migrations;\n}\n\nexport async function readAndValidateMigrationsFromDirectories(\n directories: string[],\n extensions: string[],\n): Promise<MigrationFile[]> {\n const allMigrations: MigrationFile[] = [];\n for (const directory of directories) {\n const migrations = await readAndValidateMigrationsFromDirectory(directory, extensions);\n allMigrations.push(...migrations);\n }\n return allMigrations;\n}\n\nexport function sortMigrationFiles(migrationFiles: MigrationFile[]): MigrationFile[] {\n return migrationFiles.sort((a, b) => {\n return a.timestamp.localeCompare(b.timestamp);\n });\n}\n\nexport function parseAnnotations(contents: string): Set<string> {\n const lines = contents.split('\\n');\n const annotations = new Set<string>();\n\n lines.forEach((line) => {\n if (line.startsWith(ANNOTATION_PREFIX)) {\n const annotation = line.slice(ANNOTATION_PREFIX.length).trim();\n if (!ALLOWED_ANNOTATIONS.has(annotation)) {\n throw new Error(`Invalid annotation: ${annotation}`);\n }\n annotations.add(annotation);\n }\n });\n\n return annotations;\n}\n"]}
@@ -1,6 +1,28 @@
1
1
  import { type MigrationFile } from '../load-migrations.js';
2
- export declare function init(directories: string | string[], project: string): Promise<void>;
3
- export declare function getMigrationsToExecute(migrationFiles: MigrationFile[], executedMigrations: {
4
- timestamp: string | null;
5
- }[]): MigrationFile[];
6
- export declare function initWithLock(directories: string[], project: string): Promise<void>;
2
+ interface InitOptions {
3
+ directories: string[];
4
+ project: string;
5
+ migrationFilters?: {
6
+ beforeTimestamp?: string | null;
7
+ inclusiveBefore?: boolean;
8
+ };
9
+ }
10
+ export declare function init({ directories, project, migrationFilters }: InitOptions): Promise<void>;
11
+ /**
12
+ * Get the migrations to execute.
13
+ *
14
+ * @param migrationFiles The full list of migration files.
15
+ * @param options The options for the migration execution.
16
+ * @param options.excludeMigrations The list of migrations to exclude.
17
+ * @param options.beforeTimestamp All migrations with timestamps before this timestamp will be excluded.
18
+ * @param options.inclusiveBefore Whether to include the migration with the timestamp equal to the beforeTimestamp.
19
+ */
20
+ export declare function getMigrationsToExecute(migrationFiles: MigrationFile[], { excludeMigrations, beforeTimestamp, inclusiveBefore, }: {
21
+ excludeMigrations?: {
22
+ timestamp: string | null;
23
+ }[];
24
+ beforeTimestamp?: string | null;
25
+ inclusiveBefore?: boolean;
26
+ }): MigrationFile[];
27
+ export declare function initWithLock({ directories, project, migrationFilters }: InitOptions): Promise<void>;
28
+ export {};
@@ -6,7 +6,7 @@ import * as namedLocks from '@prairielearn/named-locks';
6
6
  import * as sqldb from '@prairielearn/postgres';
7
7
  import { parseAnnotations, readAndValidateMigrationsFromDirectories, sortMigrationFiles, } from '../load-migrations.js';
8
8
  const sql = sqldb.loadSqlEquiv(import.meta.filename);
9
- export async function init(directories, project) {
9
+ export async function init({ directories, project, migrationFilters = {} }) {
10
10
  const migrationDirectories = Array.isArray(directories) ? directories : [directories];
11
11
  const lockName = 'migrations';
12
12
  logger.verbose(`Waiting for lock ${lockName}`);
@@ -21,19 +21,37 @@ export async function init(directories, project) {
21
21
  autoRenew: true,
22
22
  }, async () => {
23
23
  logger.verbose(`Acquired lock ${lockName}`);
24
- await initWithLock(migrationDirectories, project);
24
+ await initWithLock({ directories: migrationDirectories, project, migrationFilters });
25
25
  });
26
26
  logger.verbose(`Released lock ${lockName}`);
27
27
  }
28
- export function getMigrationsToExecute(migrationFiles, executedMigrations) {
28
+ /**
29
+ * Get the migrations to execute.
30
+ *
31
+ * @param migrationFiles The full list of migration files.
32
+ * @param options The options for the migration execution.
33
+ * @param options.excludeMigrations The list of migrations to exclude.
34
+ * @param options.beforeTimestamp All migrations with timestamps before this timestamp will be excluded.
35
+ * @param options.inclusiveBefore Whether to include the migration with the timestamp equal to the beforeTimestamp.
36
+ */
37
+ export function getMigrationsToExecute(migrationFiles, { excludeMigrations = [], beforeTimestamp = null, inclusiveBefore = false, }) {
29
38
  // If no migrations have ever been run, run them all.
30
- if (executedMigrations.length === 0) {
39
+ if (excludeMigrations.length === 0 && beforeTimestamp === null) {
31
40
  return migrationFiles;
32
41
  }
33
- const executedMigrationTimestamps = new Set(executedMigrations.map((m) => m.timestamp));
34
- return migrationFiles.filter((m) => !executedMigrationTimestamps.has(m.timestamp));
42
+ const excludedMigrationTimestamps = new Set(excludeMigrations.map((m) => m.timestamp));
43
+ const remainingMigrationFiles = migrationFiles.filter((m) => !excludedMigrationTimestamps.has(m.timestamp));
44
+ if (beforeTimestamp === null) {
45
+ return remainingMigrationFiles;
46
+ }
47
+ return remainingMigrationFiles.filter((m) => inclusiveBefore ? m.timestamp <= beforeTimestamp : m.timestamp < beforeTimestamp);
35
48
  }
36
- export async function initWithLock(directories, project) {
49
+ export async function initWithLock({ directories, project, migrationFilters = {} }) {
50
+ const resolvedMigrationFilters = {
51
+ beforeTimestamp: null,
52
+ inclusiveBefore: false,
53
+ ...migrationFilters,
54
+ };
37
55
  logger.verbose('Starting DB schema migration');
38
56
  const oldSchema = sqldb.defaultPool.getSearchSchema();
39
57
  // Each postgres pool uses a unique schema every time the server starts up.
@@ -96,7 +114,10 @@ export async function initWithLock(directories, project) {
96
114
  // Sort the migration files into execution order.
97
115
  const sortedMigrationFiles = sortMigrationFiles(migrationFiles);
98
116
  // Figure out which migrations have to be applied.
99
- const migrationsToExecute = getMigrationsToExecute(sortedMigrationFiles, allMigrations.rows);
117
+ const migrationsToExecute = getMigrationsToExecute(sortedMigrationFiles, {
118
+ excludeMigrations: allMigrations.rows,
119
+ ...resolvedMigrationFilters,
120
+ });
100
121
  for (const { directory, filename, timestamp } of migrationsToExecute) {
101
122
  if (allMigrations.rows.length === 0) {
102
123
  // if we are running all the migrations then log at a lower level
@@ -1 +1 @@
1
- {"version":3,"file":"migrations.js","sourceRoot":"","sources":["../../src/migrations/migrations.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,MAAM,UAAU,CAAC;AAE1B,OAAO,KAAK,KAAK,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,KAAK,UAAU,MAAM,2BAA2B,CAAC;AACxD,OAAO,KAAK,KAAK,MAAM,wBAAwB,CAAC;AAEhD,OAAO,EAEL,gBAAgB,EAChB,wCAAwC,EACxC,kBAAkB,GACnB,MAAM,uBAAuB,CAAC;AAE/B,MAAM,GAAG,GAAG,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,C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CAAC;IACH,CAAC;YAAS,CAAC;QACT,4BAA4B;QAC5B,MAAM,KAAK,CAAC,WAAW,CAAC,eAAe,CAAC,SAAS,CAAC,CAAC;IACrD,CAAC;AACH,CAAC","sourcesContent":["import path from 'path';\n\nimport fs from 'fs-extra';\n\nimport * as error from '@prairielearn/error';\nimport { logger } from '@prairielearn/logger';\nimport * as namedLocks from '@prairielearn/named-locks';\nimport * as sqldb from '@prairielearn/postgres';\n\nimport {\n type MigrationFile,\n parseAnnotations,\n readAndValidateMigrationsFromDirectories,\n sortMigrationFiles,\n} from '../load-migrations.js';\n\nconst sql = sqldb.loadSqlEquiv(import.meta.filename);\n\nexport async function init(directories: string | string[], project: string) {\n const migrationDirectories = Array.isArray(directories) ? directories : [directories];\n const lockName = 'migrations';\n logger.verbose(`Waiting for lock ${lockName}`);\n await namedLocks.doWithLock(\n lockName,\n {\n // Migrations *might* take a long time to run, so we'll enable automatic\n // lock renewal so that our lock doesn't get killed by the Postgres\n // idle session timeout.\n //\n // That said, we should generally try to keep migrations executing as\n // quickly as possible. A long-running migration likely means that\n // Postgres is locking a whole table, which is unacceptable in production.\n autoRenew: true,\n },\n async () => {\n logger.verbose(`Acquired lock ${lockName}`);\n await initWithLock(migrationDirectories, project);\n },\n );\n logger.verbose(`Released lock ${lockName}`);\n}\n\nexport function getMigrationsToExecute(\n migrationFiles: MigrationFile[],\n executedMigrations: { timestamp: string | null }[],\n): MigrationFile[] {\n // If no migrations have ever been run, run them all.\n if (executedMigrations.length === 0) {\n return migrationFiles;\n }\n\n const executedMigrationTimestamps = new Set(executedMigrations.map((m) => m.timestamp));\n return migrationFiles.filter((m) => !executedMigrationTimestamps.has(m.timestamp));\n}\n\nexport async function initWithLock(directories: string[], project: string) {\n logger.verbose('Starting DB schema migration');\n\n const oldSchema = sqldb.defaultPool.getSearchSchema();\n // Each postgres pool uses a unique schema every time the server starts up.\n // After that code runs, the default schema is set to that schema instead of public, which\n // causes the 'create_migrations_table' query to fail.\n //\n // We'll set the default schema to public before running the migrations, and then restore it\n // after the migrations are run.\n await sqldb.defaultPool.setSearchSchema('public');\n try {\n // Create the migrations table if needed\n await sqldb.execute(sql.create_migrations_table);\n\n // Apply necessary changes to the migrations table as needed.\n try {\n await sqldb.execute('SELECT project FROM migrations;');\n } catch (err: any) {\n if (err.routine === 'errorMissingColumn') {\n logger.info('Altering migrations table');\n await sqldb.execute(sql.add_projects_column);\n } else {\n throw err;\n }\n }\n try {\n await sqldb.execute('SELECT timestamp FROM migrations;');\n } catch (err: any) {\n if (err.routine === 'errorMissingColumn') {\n logger.info('Altering migrations table again');\n await sqldb.execute(sql.add_timestamp_column);\n } else {\n throw err;\n }\n }\n\n let allMigrations = await sqldb.queryAsync(sql.get_migrations, { project });\n const migrationFiles = await readAndValidateMigrationsFromDirectories(directories, [\n '.sql',\n '.js',\n '.ts',\n '.mjs',\n ]);\n\n // Validation: if we not all previously-executed migrations have timestamps,\n // prompt the user to deploy an earlier version that includes both indexes\n // and timestamps.\n const migrationsMissingTimestamps = allMigrations.rows.filter((m) => !m.timestamp);\n if (migrationsMissingTimestamps.length > 0) {\n throw new Error(\n [\n 'The following migrations are missing timestamps:',\n migrationsMissingTimestamps.map((m) => ` ${m.filename}`),\n // This revision was the most recent commit to `master` before the\n // code handling indexes was removed.\n 'You must deploy revision 1aa43c7348fa24cf636413d720d06a2fa9e38ef2 first.',\n ].join('\\n'),\n );\n }\n\n // Refetch the list of migrations from the database.\n allMigrations = await sqldb.queryAsync(sql.get_migrations, { project });\n\n // Sort the migration files into execution order.\n const sortedMigrationFiles = sortMigrationFiles(migrationFiles);\n\n // Figure out which migrations have to be applied.\n const migrationsToExecute = getMigrationsToExecute(sortedMigrationFiles, allMigrations.rows);\n for (const { directory, filename, timestamp } of migrationsToExecute) {\n if (allMigrations.rows.length === 0) {\n // if we are running all the migrations then log at a lower level\n logger.verbose(`Running migration ${filename}`);\n } else {\n logger.info(`Running migration ${filename}`);\n }\n\n const migrationPath = path.join(directory, filename);\n if (filename.endsWith('.sql')) {\n const migrationSql = await fs.readFile(migrationPath, 'utf8');\n const annotations = parseAnnotations(migrationSql);\n try {\n if (annotations.has('NO TRANSACTION')) {\n await sqldb.execute(migrationSql);\n } else {\n await sqldb.runInTransactionAsync(async () => {\n await sqldb.execute(migrationSql);\n });\n }\n } catch (err) {\n error.addData(err, { sqlFile: filename });\n throw err;\n }\n } else {\n const migrationModule = await import(/* @vite-ignore */ migrationPath);\n const implementation = migrationModule.default;\n if (typeof implementation !== 'function') {\n throw new Error(`Migration ${filename} does not export a default function`);\n }\n await implementation();\n }\n\n // Record the migration.\n await sqldb.execute(sql.insert_migration, {\n filename,\n timestamp,\n project,\n });\n }\n } finally {\n // Restore the search schema\n await sqldb.defaultPool.setSearchSchema(oldSchema);\n }\n}\n"]}
1
+ {"version":3,"file":"migrations.js","sourceRoot":"","sources":["../../src/migrations/migrations.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,MAAM,UAAU,CAAC;AAE1B,OAAO,KAAK,KAAK,MAAM,qBAAqB,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,KAAK,UAAU,MAAM,2BAA2B,CAAC;AACxD,OAAO,KAAK,KAAK,MAAM,wBAAwB,CAAC;AAEhD,OAAO,EAEL,gBAAgB,EAChB,wCAAwC,EACxC,kBAAkB,GACnB,MAAM,uBAAuB,CAAC;AAE/B,MAAM,GAAG,GAAG,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AAWrD,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,EAAE,WAAW,EAAE,OAAO,EAAE,gBAAgB,GAAG,EAAE,EAAe;IACrF,MAAM,oBAAoB,GAAG,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;IACtF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AM,MAAM,CAAC,kBAAkB,CAAC,aAAa,CAAC,CAAC;gBACvE,MAAM,cAAc,GAAG,eAAe,CAAC,OAAO,CAAC;gBAC/C,IAAI,OAAO,cAAc,KAAK,UAAU,EAAE,CAAC;oBACzC,MAAM,IAAI,KAAK,CAAC,aAAa,QAAQ,qCAAqC,CAAC,CAAC;gBAC9E,CAAC;gBACD,MAAM,cAAc,EAAE,CAAC;YACzB,CAAC;YAED,wBAAwB;YACxB,MAAM,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE;gBACxC,QAAQ;gBACR,SAAS;gBACT,OAAO;aACR,CAAC,CAAC;QACL,CAAC;IACH,CAAC;YAAS,CAAC;QACT,4BAA4B;QAC5B,MAAM,KAAK,CAAC,WAAW,CAAC,eAAe,CAAC,SAAS,CAAC,CAAC;IACrD,CAAC;AACH,CAAC","sourcesContent":["import path from 'path';\n\nimport fs from 'fs-extra';\n\nimport * as error from '@prairielearn/error';\nimport { logger } from '@prairielearn/logger';\nimport * as namedLocks from '@prairielearn/named-locks';\nimport * as sqldb from '@prairielearn/postgres';\n\nimport {\n type MigrationFile,\n parseAnnotations,\n readAndValidateMigrationsFromDirectories,\n sortMigrationFiles,\n} from '../load-migrations.js';\n\nconst sql = sqldb.loadSqlEquiv(import.meta.filename);\n\ninterface InitOptions {\n directories: string[];\n project: string;\n migrationFilters?: {\n beforeTimestamp?: string | null;\n inclusiveBefore?: boolean;\n };\n}\n\nexport async function init({ directories, project, migrationFilters = {} }: InitOptions) {\n const migrationDirectories = Array.isArray(directories) ? directories : [directories];\n const lockName = 'migrations';\n logger.verbose(`Waiting for lock ${lockName}`);\n await namedLocks.doWithLock(\n lockName,\n {\n // Migrations *might* take a long time to run, so we'll enable automatic\n // lock renewal so that our lock doesn't get killed by the Postgres\n // idle session timeout.\n //\n // That said, we should generally try to keep migrations executing as\n // quickly as possible. A long-running migration likely means that\n // Postgres is locking a whole table, which is unacceptable in production.\n autoRenew: true,\n },\n async () => {\n logger.verbose(`Acquired lock ${lockName}`);\n await initWithLock({ directories: migrationDirectories, project, migrationFilters });\n },\n );\n logger.verbose(`Released lock ${lockName}`);\n}\n\n/**\n * Get the migrations to execute.\n *\n * @param migrationFiles The full list of migration files.\n * @param options The options for the migration execution.\n * @param options.excludeMigrations The list of migrations to exclude.\n * @param options.beforeTimestamp All migrations with timestamps before this timestamp will be excluded.\n * @param options.inclusiveBefore Whether to include the migration with the timestamp equal to the beforeTimestamp.\n */\nexport function getMigrationsToExecute(\n migrationFiles: MigrationFile[],\n {\n excludeMigrations = [],\n beforeTimestamp = null,\n inclusiveBefore = false,\n }: {\n excludeMigrations?: { timestamp: string | null }[];\n beforeTimestamp?: string | null;\n inclusiveBefore?: boolean;\n },\n): MigrationFile[] {\n // If no migrations have ever been run, run them all.\n if (excludeMigrations.length === 0 && beforeTimestamp === null) {\n return migrationFiles;\n }\n\n const excludedMigrationTimestamps = new Set(excludeMigrations.map((m) => m.timestamp));\n const remainingMigrationFiles = migrationFiles.filter(\n (m) => !excludedMigrationTimestamps.has(m.timestamp),\n );\n if (beforeTimestamp === null) {\n return remainingMigrationFiles;\n }\n return remainingMigrationFiles.filter((m) =>\n inclusiveBefore ? m.timestamp <= beforeTimestamp : m.timestamp < beforeTimestamp,\n );\n}\n\nexport async function initWithLock({ directories, project, migrationFilters = {} }: InitOptions) {\n const resolvedMigrationFilters = {\n beforeTimestamp: null,\n inclusiveBefore: false,\n ...migrationFilters,\n };\n\n logger.verbose('Starting DB schema migration');\n\n const oldSchema = sqldb.defaultPool.getSearchSchema();\n // Each postgres pool uses a unique schema every time the server starts up.\n // After that code runs, the default schema is set to that schema instead of public, which\n // causes the 'create_migrations_table' query to fail.\n //\n // We'll set the default schema to public before running the migrations, and then restore it\n // after the migrations are run.\n await sqldb.defaultPool.setSearchSchema('public');\n try {\n // Create the migrations table if needed\n await sqldb.execute(sql.create_migrations_table);\n\n // Apply necessary changes to the migrations table as needed.\n try {\n await sqldb.execute('SELECT project FROM migrations;');\n } catch (err: any) {\n if (err.routine === 'errorMissingColumn') {\n logger.info('Altering migrations table');\n await sqldb.execute(sql.add_projects_column);\n } else {\n throw err;\n }\n }\n try {\n await sqldb.execute('SELECT timestamp FROM migrations;');\n } catch (err: any) {\n if (err.routine === 'errorMissingColumn') {\n logger.info('Altering migrations table again');\n await sqldb.execute(sql.add_timestamp_column);\n } else {\n throw err;\n }\n }\n\n let allMigrations = await sqldb.queryAsync(sql.get_migrations, { project });\n const migrationFiles = await readAndValidateMigrationsFromDirectories(directories, [\n '.sql',\n '.js',\n '.ts',\n '.mjs',\n ]);\n\n // Validation: if we not all previously-executed migrations have timestamps,\n // prompt the user to deploy an earlier version that includes both indexes\n // and timestamps.\n const migrationsMissingTimestamps = allMigrations.rows.filter((m) => !m.timestamp);\n if (migrationsMissingTimestamps.length > 0) {\n throw new Error(\n [\n 'The following migrations are missing timestamps:',\n migrationsMissingTimestamps.map((m) => ` ${m.filename}`),\n // This revision was the most recent commit to `master` before the\n // code handling indexes was removed.\n 'You must deploy revision 1aa43c7348fa24cf636413d720d06a2fa9e38ef2 first.',\n ].join('\\n'),\n );\n }\n\n // Refetch the list of migrations from the database.\n allMigrations = await sqldb.queryAsync(sql.get_migrations, { project });\n\n // Sort the migration files into execution order.\n const sortedMigrationFiles = sortMigrationFiles(migrationFiles);\n\n // Figure out which migrations have to be applied.\n const migrationsToExecute = getMigrationsToExecute(sortedMigrationFiles, {\n excludeMigrations: allMigrations.rows,\n ...resolvedMigrationFilters,\n });\n for (const { directory, filename, timestamp } of migrationsToExecute) {\n if (allMigrations.rows.length === 0) {\n // if we are running all the migrations then log at a lower level\n logger.verbose(`Running migration ${filename}`);\n } else {\n logger.info(`Running migration ${filename}`);\n }\n\n const migrationPath = path.join(directory, filename);\n if (filename.endsWith('.sql')) {\n const migrationSql = await fs.readFile(migrationPath, 'utf8');\n const annotations = parseAnnotations(migrationSql);\n try {\n if (annotations.has('NO TRANSACTION')) {\n await sqldb.execute(migrationSql);\n } else {\n await sqldb.runInTransactionAsync(async () => {\n await sqldb.execute(migrationSql);\n });\n }\n } catch (err) {\n error.addData(err, { sqlFile: filename });\n throw err;\n }\n } else {\n const migrationModule = await import(/* @vite-ignore */ migrationPath);\n const implementation = migrationModule.default;\n if (typeof implementation !== 'function') {\n throw new Error(`Migration ${filename} does not export a default function`);\n }\n await implementation();\n }\n\n // Record the migration.\n await sqldb.execute(sql.insert_migration, {\n filename,\n timestamp,\n project,\n });\n }\n } finally {\n // Restore the search schema\n await sqldb.defaultPool.setSearchSchema(oldSchema);\n }\n}\n"]}
@@ -12,7 +12,8 @@ describe('migrations', () => {
12
12
  timestamp: '20220101010101',
13
13
  },
14
14
  ];
15
- assert.deepEqual(getMigrationsToExecute(migrationFiles, []), migrationFiles);
15
+ assert.deepEqual(getMigrationsToExecute(migrationFiles, { excludeMigrations: [] }), migrationFiles);
16
+ assert.deepEqual(getMigrationsToExecute(migrationFiles, {}), migrationFiles);
16
17
  });
17
18
  it('handles case where subset of migrations have been executed', () => {
18
19
  const migrationFiles = [
@@ -40,7 +41,7 @@ describe('migrations', () => {
40
41
  timestamp: '20220101010102',
41
42
  },
42
43
  ];
43
- assert.deepEqual(getMigrationsToExecute(migrationFiles, executedMigrations), [
44
+ assert.deepEqual(getMigrationsToExecute(migrationFiles, { excludeMigrations: executedMigrations }), [
44
45
  {
45
46
  directory: 'migrations',
46
47
  timestamp: '20220101010103',
@@ -49,6 +50,70 @@ describe('migrations', () => {
49
50
  ]);
50
51
  });
51
52
  });
53
+ it('handles case where beforeTimestamp is specified', () => {
54
+ const migrationFiles = [
55
+ {
56
+ directory: 'migrations',
57
+ filename: '20220101010101_testing_1.sql',
58
+ timestamp: '20220101010101',
59
+ },
60
+ {
61
+ directory: 'migrations',
62
+ filename: '20220101010102_testing_2.sql',
63
+ timestamp: '20220101010102',
64
+ },
65
+ {
66
+ directory: 'migrations',
67
+ filename: '20220101010103_testing_3.sql',
68
+ timestamp: '20220101010103',
69
+ },
70
+ ];
71
+ assert.deepEqual(getMigrationsToExecute(migrationFiles, {
72
+ excludeMigrations: [],
73
+ beforeTimestamp: '20220101010102',
74
+ }), [
75
+ {
76
+ directory: 'migrations',
77
+ filename: '20220101010101_testing_1.sql',
78
+ timestamp: '20220101010101',
79
+ },
80
+ ]);
81
+ });
82
+ it('handles case where inclusiveBefore is specified', () => {
83
+ const migrationFiles = [
84
+ {
85
+ directory: 'migrations',
86
+ filename: '20220101010101_testing_1.sql',
87
+ timestamp: '20220101010101',
88
+ },
89
+ {
90
+ directory: 'migrations',
91
+ filename: '20220101010102_testing_2.sql',
92
+ timestamp: '20220101010102',
93
+ },
94
+ {
95
+ directory: 'migrations',
96
+ filename: '20220101010103_testing_3.sql',
97
+ timestamp: '20220101010103',
98
+ },
99
+ ];
100
+ assert.deepEqual(getMigrationsToExecute(migrationFiles, {
101
+ excludeMigrations: [],
102
+ beforeTimestamp: '20220101010102',
103
+ inclusiveBefore: true,
104
+ }), [
105
+ {
106
+ directory: 'migrations',
107
+ filename: '20220101010101_testing_1.sql',
108
+ timestamp: '20220101010101',
109
+ },
110
+ {
111
+ directory: 'migrations',
112
+ filename: '20220101010102_testing_2.sql',
113
+ timestamp: '20220101010102',
114
+ },
115
+ ]);
116
+ });
52
117
  describe('initWithLock', () => {
53
118
  const postgresTestUtils = makePostgresTestUtils({
54
119
  database: 'prairielearn_migrations',
@@ -61,7 +126,7 @@ describe('migrations', () => {
61
126
  });
62
127
  it('runs both SQL and JavaScript migrations', async () => {
63
128
  const migrationDir = path.join(import.meta.dirname, 'fixtures');
64
- await initWithLock([migrationDir], 'prairielearn_migrations');
129
+ await initWithLock({ directories: [migrationDir], project: 'prairielearn_migrations' });
65
130
  // If both migrations ran successfully, there should be a single user
66
131
  // in the database.
67
132
  const users = await queryAsync('SELECT * FROM users', {});
@@ -1 +1 @@
1
- {"version":3,"file":"migrations.test.js","sourceRoot":"","sources":["../../src/migrations/migrations.test.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAA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,SAAS,EAAE,gBAAgB;oBAC3B,QAAQ,EAAE,8BAA8B;iBACzC;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;QAC5B,MAAM,iBAAiB,GAAG,qBAAqB,CAAC;YAC9C,QAAQ,EAAE,yBAAyB;SACpC,CAAC,CAAC;QAEH,SAAS,CAAC,KAAK,IAAI,EAAE;YACnB,MAAM,iBAAiB,CAAC,cAAc,EAAE,CAAC;QAC3C,CAAC,CAAC,CAAC;QAEH,QAAQ,CAAC,KAAK,IAAI,EAAE;YAClB,MAAM,iBAAiB,CAAC,YAAY,EAAE,CAAC;QACzC,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,yCAAyC,EAAE,KAAK,IAAI,EAAE;YACvD,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;YAChE,MAAM,YAAY,CAAC,CAAC,YAAY,CAAC,EAAE,yBAAyB,CAAC,CAAC;YAE9D,qEAAqE;YACrE,mBAAmB;YACnB,MAAM,KAAK,GAAG,MAAM,UAAU,CAAC,qBAAqB,EAAE,EAAE,CAAC,CAAC;YAC1D,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;YAC/B,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;QAChD,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import path from 'node:path';\n\nimport { afterAll, assert, beforeAll, describe, it } from 'vitest';\n\nimport { makePostgresTestUtils, queryAsync } from '@prairielearn/postgres';\n\nimport { getMigrationsToExecute, initWithLock } from './migrations.js';\n\ndescribe('migrations', () => {\n describe('getMigrationsToExecute', () => {\n it('handles the case of no executed migrations', () => {\n const migrationFiles = [\n {\n directory: 'migrations',\n filename: '001_testing.sql',\n timestamp: '20220101010101',\n },\n ];\n assert.deepEqual(getMigrationsToExecute(migrationFiles, []), migrationFiles);\n });\n\n it('handles case where subset of migrations have been executed', () => {\n const migrationFiles = [\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 const executedMigrations = [\n {\n timestamp: '20220101010101',\n },\n {\n timestamp: '20220101010102',\n },\n ];\n assert.deepEqual(getMigrationsToExecute(migrationFiles, executedMigrations), [\n {\n directory: 'migrations',\n timestamp: '20220101010103',\n filename: '20220101010103_testing_3.sql',\n },\n ]);\n });\n });\n\n describe('initWithLock', () => {\n const postgresTestUtils = makePostgresTestUtils({\n database: 'prairielearn_migrations',\n });\n\n beforeAll(async () => {\n await postgresTestUtils.createDatabase();\n });\n\n afterAll(async () => {\n await postgresTestUtils.dropDatabase();\n });\n\n it('runs both SQL and JavaScript migrations', async () => {\n const migrationDir = path.join(import.meta.dirname, 'fixtures');\n await initWithLock([migrationDir], 'prairielearn_migrations');\n\n // If both migrations ran successfully, there should be a single user\n // in the database.\n const users = await queryAsync('SELECT * FROM users', {});\n assert.lengthOf(users.rows, 1);\n assert.equal(users.rows[0].name, 'Test User');\n });\n });\n});\n"]}
1
+ {"version":3,"file":"migrations.test.js","sourceRoot":"","sources":["../../src/migrations/migrations.test.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAEnE,OAAO,EAAE,qBAAqB,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAE3E,OAAO,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E,8BAA8B;gBACxC,SAAS,EAAE,gBAAgB;aAC5B;YACD;gBACE,SAAS,EAAE,YAAY;gBACvB,QAAQ,EAAE,8BAA8B;gBACxC,SAAS,EAAE,gBAAgB;aAC5B;SACF,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;QAC5B,MAAM,iBAAiB,GAAG,qBAAqB,CAAC;YAC9C,QAAQ,EAAE,yBAAyB;SACpC,CAAC,CAAC;QAEH,SAAS,CAAC,KAAK,IAAI,EAAE;YACnB,MAAM,iBAAiB,CAAC,cAAc,EAAE,CAAC;QAC3C,CAAC,CAAC,CAAC;QAEH,QAAQ,CAAC,KAAK,IAAI,EAAE;YAClB,MAAM,iBAAiB,CAAC,YAAY,EAAE,CAAC;QACzC,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,yCAAyC,EAAE,KAAK,IAAI,EAAE;YACvD,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;YAChE,MAAM,YAAY,CAAC,EAAE,WAAW,EAAE,CAAC,YAAY,CAAC,EAAE,OAAO,EAAE,yBAAyB,EAAE,CAAC,CAAC;YAExF,qEAAqE;YACrE,mBAAmB;YACnB,MAAM,KAAK,GAAG,MAAM,UAAU,CAAC,qBAAqB,EAAE,EAAE,CAAC,CAAC;YAC1D,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;YAC/B,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;QAChD,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import path from 'node:path';\n\nimport { afterAll, assert, beforeAll, describe, it } from 'vitest';\n\nimport { makePostgresTestUtils, queryAsync } from '@prairielearn/postgres';\n\nimport { getMigrationsToExecute, initWithLock } from './migrations.js';\n\ndescribe('migrations', () => {\n describe('getMigrationsToExecute', () => {\n it('handles the case of no executed migrations', () => {\n const migrationFiles = [\n {\n directory: 'migrations',\n filename: '001_testing.sql',\n timestamp: '20220101010101',\n },\n ];\n assert.deepEqual(\n getMigrationsToExecute(migrationFiles, { excludeMigrations: [] }),\n migrationFiles,\n );\n assert.deepEqual(getMigrationsToExecute(migrationFiles, {}), migrationFiles);\n });\n\n it('handles case where subset of migrations have been executed', () => {\n const migrationFiles = [\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 const executedMigrations = [\n {\n timestamp: '20220101010101',\n },\n {\n timestamp: '20220101010102',\n },\n ];\n assert.deepEqual(\n getMigrationsToExecute(migrationFiles, { excludeMigrations: executedMigrations }),\n [\n {\n directory: 'migrations',\n timestamp: '20220101010103',\n filename: '20220101010103_testing_3.sql',\n },\n ],\n );\n });\n });\n\n it('handles case where beforeTimestamp is specified', () => {\n const migrationFiles = [\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 assert.deepEqual(\n getMigrationsToExecute(migrationFiles, {\n excludeMigrations: [],\n beforeTimestamp: '20220101010102',\n }),\n [\n {\n directory: 'migrations',\n filename: '20220101010101_testing_1.sql',\n timestamp: '20220101010101',\n },\n ],\n );\n });\n it('handles case where inclusiveBefore is specified', () => {\n const migrationFiles = [\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 assert.deepEqual(\n getMigrationsToExecute(migrationFiles, {\n excludeMigrations: [],\n beforeTimestamp: '20220101010102',\n inclusiveBefore: true,\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 );\n });\n\n describe('initWithLock', () => {\n const postgresTestUtils = makePostgresTestUtils({\n database: 'prairielearn_migrations',\n });\n\n beforeAll(async () => {\n await postgresTestUtils.createDatabase();\n });\n\n afterAll(async () => {\n await postgresTestUtils.dropDatabase();\n });\n\n it('runs both SQL and JavaScript migrations', async () => {\n const migrationDir = path.join(import.meta.dirname, 'fixtures');\n await initWithLock({ directories: [migrationDir], project: 'prairielearn_migrations' });\n\n // If both migrations ran successfully, there should be a single user\n // in the database.\n const users = await queryAsync('SELECT * FROM users', {});\n assert.lengthOf(users.rows, 1);\n assert.equal(users.rows[0].name, 'Test User');\n });\n });\n});\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prairielearn/migrations",
3
- "version": "3.0.25",
3
+ "version": "4.0.0",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,7 +17,7 @@
17
17
  "@prairielearn/error": "^2.0.19",
18
18
  "@prairielearn/logger": "^2.0.20",
19
19
  "@prairielearn/named-locks": "^3.0.23",
20
- "@prairielearn/postgres": "^4.2.0",
20
+ "@prairielearn/postgres": "^4.3.0",
21
21
  "fs-extra": "^11.3.1",
22
22
  "serialize-error": "^12.0.0",
23
23
  "zod": "^3.25.76"
@@ -95,7 +95,7 @@ describe('BatchedMigrationExecutor', () => {
95
95
  await namedLocks.init(postgresTestUtils.getPoolConfig(), (err) => {
96
96
  throw err;
97
97
  });
98
- await init([SCHEMA_MIGRATIONS_PATH], 'prairielearn_migrations');
98
+ await init({ directories: [SCHEMA_MIGRATIONS_PATH], project: 'prairielearn_migrations' });
99
99
  });
100
100
 
101
101
  beforeEach(async () => {
@@ -20,7 +20,7 @@ describe('BatchedMigrationsRunner', () => {
20
20
  await namedLocks.init(postgresTestUtils.getPoolConfig(), (err) => {
21
21
  throw err;
22
22
  });
23
- await init([SCHEMA_MIGRATIONS_PATH], 'prairielearn_migrations');
23
+ await init({ directories: [SCHEMA_MIGRATIONS_PATH], project: 'prairielearn_migrations' });
24
24
  });
25
25
 
26
26
  afterEach(async () => {
package/src/index.ts CHANGED
@@ -20,4 +20,5 @@ export {
20
20
  retryFailedBatchedMigrationJobs,
21
21
  } from './batched-migrations/index.js';
22
22
 
23
+ export { extractTimestampFromFilename } from './load-migrations.js';
23
24
  export const SCHEMA_MIGRATIONS_PATH = path.resolve(import.meta.dirname, '..', 'schema-migrations');
@@ -4,7 +4,7 @@ import fs from 'fs-extra';
4
4
  * Timestamp prefixes will be of the form `YYYYMMDDHHMMSS`, which will have 14 digits.
5
5
  * If this code is still around in the year 10000... good luck.
6
6
  */
7
- const MIGRATION_FILENAME_REGEX = /^([0-9]{14})_.+\.[a-z]+/;
7
+ const MIGRATION_FILENAME_REGEX = /^([0-9]{14})_.+/;
8
8
 
9
9
  /**
10
10
  * Annotations are expressed via the following:
@@ -24,26 +24,30 @@ export interface MigrationFile {
24
24
  timestamp: string;
25
25
  }
26
26
 
27
+ export function extractTimestampFromFilename(filename: string): string {
28
+ const match = filename.match(MIGRATION_FILENAME_REGEX);
29
+ if (!match) {
30
+ throw new Error(`Invalid migration filename: ${filename}`);
31
+ }
32
+ const timestamp = match.at(1) ?? null;
33
+ if (timestamp === null) {
34
+ throw new Error(`Migration ${filename} does not have a timestamp`);
35
+ }
36
+ return timestamp;
37
+ }
38
+
27
39
  export async function readAndValidateMigrationsFromDirectory(
28
40
  dir: string,
29
41
  extensions: string[],
30
42
  ): Promise<MigrationFile[]> {
31
- const migrationFiles = (await fs.readdir(dir)).filter((m) =>
32
- extensions.some((e) => m.endsWith(e)),
33
- );
43
+ const migrationFiles = (await fs.readdir(dir)).filter((m) => {
44
+ // Get the full extension of the file (e.g. for `foo.test.ts`, return `.test.ts`).
45
+ const [_name, ...extensionParts] = m.split('.');
46
+ return extensions.includes('.' + extensionParts.join('.'));
47
+ });
34
48
 
35
49
  const migrations = migrationFiles.map((mf) => {
36
- const match = mf.match(MIGRATION_FILENAME_REGEX);
37
-
38
- if (!match) {
39
- throw new Error(`Invalid migration filename: ${mf}`);
40
- }
41
-
42
- const timestamp = match[1] ?? null;
43
-
44
- if (timestamp === null) {
45
- throw new Error(`Migration ${mf} does not have a timestamp`);
46
- }
50
+ const timestamp = extractTimestampFromFilename(mf);
47
51
 
48
52
  return {
49
53
  directory: dir,
@@ -16,7 +16,11 @@ describe('migrations', () => {
16
16
  timestamp: '20220101010101',
17
17
  },
18
18
  ];
19
- assert.deepEqual(getMigrationsToExecute(migrationFiles, []), migrationFiles);
19
+ assert.deepEqual(
20
+ getMigrationsToExecute(migrationFiles, { excludeMigrations: [] }),
21
+ migrationFiles,
22
+ );
23
+ assert.deepEqual(getMigrationsToExecute(migrationFiles, {}), migrationFiles);
20
24
  });
21
25
 
22
26
  it('handles case where subset of migrations have been executed', () => {
@@ -45,14 +49,88 @@ describe('migrations', () => {
45
49
  timestamp: '20220101010102',
46
50
  },
47
51
  ];
48
- assert.deepEqual(getMigrationsToExecute(migrationFiles, executedMigrations), [
52
+ assert.deepEqual(
53
+ getMigrationsToExecute(migrationFiles, { excludeMigrations: executedMigrations }),
54
+ [
55
+ {
56
+ directory: 'migrations',
57
+ timestamp: '20220101010103',
58
+ filename: '20220101010103_testing_3.sql',
59
+ },
60
+ ],
61
+ );
62
+ });
63
+ });
64
+
65
+ it('handles case where beforeTimestamp is specified', () => {
66
+ const migrationFiles = [
67
+ {
68
+ directory: 'migrations',
69
+ filename: '20220101010101_testing_1.sql',
70
+ timestamp: '20220101010101',
71
+ },
72
+ {
73
+ directory: 'migrations',
74
+ filename: '20220101010102_testing_2.sql',
75
+ timestamp: '20220101010102',
76
+ },
77
+ {
78
+ directory: 'migrations',
79
+ filename: '20220101010103_testing_3.sql',
80
+ timestamp: '20220101010103',
81
+ },
82
+ ];
83
+ assert.deepEqual(
84
+ getMigrationsToExecute(migrationFiles, {
85
+ excludeMigrations: [],
86
+ beforeTimestamp: '20220101010102',
87
+ }),
88
+ [
49
89
  {
50
90
  directory: 'migrations',
51
- timestamp: '20220101010103',
52
- filename: '20220101010103_testing_3.sql',
91
+ filename: '20220101010101_testing_1.sql',
92
+ timestamp: '20220101010101',
53
93
  },
54
- ]);
55
- });
94
+ ],
95
+ );
96
+ });
97
+ it('handles case where inclusiveBefore is specified', () => {
98
+ const migrationFiles = [
99
+ {
100
+ directory: 'migrations',
101
+ filename: '20220101010101_testing_1.sql',
102
+ timestamp: '20220101010101',
103
+ },
104
+ {
105
+ directory: 'migrations',
106
+ filename: '20220101010102_testing_2.sql',
107
+ timestamp: '20220101010102',
108
+ },
109
+ {
110
+ directory: 'migrations',
111
+ filename: '20220101010103_testing_3.sql',
112
+ timestamp: '20220101010103',
113
+ },
114
+ ];
115
+ assert.deepEqual(
116
+ getMigrationsToExecute(migrationFiles, {
117
+ excludeMigrations: [],
118
+ beforeTimestamp: '20220101010102',
119
+ inclusiveBefore: true,
120
+ }),
121
+ [
122
+ {
123
+ directory: 'migrations',
124
+ filename: '20220101010101_testing_1.sql',
125
+ timestamp: '20220101010101',
126
+ },
127
+ {
128
+ directory: 'migrations',
129
+ filename: '20220101010102_testing_2.sql',
130
+ timestamp: '20220101010102',
131
+ },
132
+ ],
133
+ );
56
134
  });
57
135
 
58
136
  describe('initWithLock', () => {
@@ -70,7 +148,7 @@ describe('migrations', () => {
70
148
 
71
149
  it('runs both SQL and JavaScript migrations', async () => {
72
150
  const migrationDir = path.join(import.meta.dirname, 'fixtures');
73
- await initWithLock([migrationDir], 'prairielearn_migrations');
151
+ await initWithLock({ directories: [migrationDir], project: 'prairielearn_migrations' });
74
152
 
75
153
  // If both migrations ran successfully, there should be a single user
76
154
  // in the database.
@@ -16,7 +16,16 @@ import {
16
16
 
17
17
  const sql = sqldb.loadSqlEquiv(import.meta.filename);
18
18
 
19
- export async function init(directories: string | string[], project: string) {
19
+ interface InitOptions {
20
+ directories: string[];
21
+ project: string;
22
+ migrationFilters?: {
23
+ beforeTimestamp?: string | null;
24
+ inclusiveBefore?: boolean;
25
+ };
26
+ }
27
+
28
+ export async function init({ directories, project, migrationFilters = {} }: InitOptions) {
20
29
  const migrationDirectories = Array.isArray(directories) ? directories : [directories];
21
30
  const lockName = 'migrations';
22
31
  logger.verbose(`Waiting for lock ${lockName}`);
@@ -34,26 +43,57 @@ export async function init(directories: string | string[], project: string) {
34
43
  },
35
44
  async () => {
36
45
  logger.verbose(`Acquired lock ${lockName}`);
37
- await initWithLock(migrationDirectories, project);
46
+ await initWithLock({ directories: migrationDirectories, project, migrationFilters });
38
47
  },
39
48
  );
40
49
  logger.verbose(`Released lock ${lockName}`);
41
50
  }
42
51
 
52
+ /**
53
+ * Get the migrations to execute.
54
+ *
55
+ * @param migrationFiles The full list of migration files.
56
+ * @param options The options for the migration execution.
57
+ * @param options.excludeMigrations The list of migrations to exclude.
58
+ * @param options.beforeTimestamp All migrations with timestamps before this timestamp will be excluded.
59
+ * @param options.inclusiveBefore Whether to include the migration with the timestamp equal to the beforeTimestamp.
60
+ */
43
61
  export function getMigrationsToExecute(
44
62
  migrationFiles: MigrationFile[],
45
- executedMigrations: { timestamp: string | null }[],
63
+ {
64
+ excludeMigrations = [],
65
+ beforeTimestamp = null,
66
+ inclusiveBefore = false,
67
+ }: {
68
+ excludeMigrations?: { timestamp: string | null }[];
69
+ beforeTimestamp?: string | null;
70
+ inclusiveBefore?: boolean;
71
+ },
46
72
  ): MigrationFile[] {
47
73
  // If no migrations have ever been run, run them all.
48
- if (executedMigrations.length === 0) {
74
+ if (excludeMigrations.length === 0 && beforeTimestamp === null) {
49
75
  return migrationFiles;
50
76
  }
51
77
 
52
- const executedMigrationTimestamps = new Set(executedMigrations.map((m) => m.timestamp));
53
- return migrationFiles.filter((m) => !executedMigrationTimestamps.has(m.timestamp));
78
+ const excludedMigrationTimestamps = new Set(excludeMigrations.map((m) => m.timestamp));
79
+ const remainingMigrationFiles = migrationFiles.filter(
80
+ (m) => !excludedMigrationTimestamps.has(m.timestamp),
81
+ );
82
+ if (beforeTimestamp === null) {
83
+ return remainingMigrationFiles;
84
+ }
85
+ return remainingMigrationFiles.filter((m) =>
86
+ inclusiveBefore ? m.timestamp <= beforeTimestamp : m.timestamp < beforeTimestamp,
87
+ );
54
88
  }
55
89
 
56
- export async function initWithLock(directories: string[], project: string) {
90
+ export async function initWithLock({ directories, project, migrationFilters = {} }: InitOptions) {
91
+ const resolvedMigrationFilters = {
92
+ beforeTimestamp: null,
93
+ inclusiveBefore: false,
94
+ ...migrationFilters,
95
+ };
96
+
57
97
  logger.verbose('Starting DB schema migration');
58
98
 
59
99
  const oldSchema = sqldb.defaultPool.getSearchSchema();
@@ -121,7 +161,10 @@ export async function initWithLock(directories: string[], project: string) {
121
161
  const sortedMigrationFiles = sortMigrationFiles(migrationFiles);
122
162
 
123
163
  // Figure out which migrations have to be applied.
124
- const migrationsToExecute = getMigrationsToExecute(sortedMigrationFiles, allMigrations.rows);
164
+ const migrationsToExecute = getMigrationsToExecute(sortedMigrationFiles, {
165
+ excludeMigrations: allMigrations.rows,
166
+ ...resolvedMigrationFilters,
167
+ });
125
168
  for (const { directory, filename, timestamp } of migrationsToExecute) {
126
169
  if (allMigrations.rows.length === 0) {
127
170
  // if we are running all the migrations then log at a lower level