migrate-mongo.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { execSync } from 'node:child_process';
  2. import { beforeAll } from 'vitest';
  3. import { getTestDbConfig } from './mongo';
  4. // Track if migrations have been run for this worker
  5. let migrationsRun = false;
  6. /**
  7. * Run database migrations using external process.
  8. * This uses the existing dev:migrate:up script which has ts-node and tsconfig-paths configured.
  9. */
  10. function runMigrations(mongoUri: string): void {
  11. // Run migrations using the existing script with custom MONGO_URI
  12. execSync('pnpm run dev:migrate:up', {
  13. cwd: process.cwd(),
  14. env: {
  15. ...process.env,
  16. MONGO_URI: mongoUri,
  17. },
  18. stdio: 'inherit',
  19. });
  20. }
  21. beforeAll(() => {
  22. // Skip if already run (setupFiles run per test file, but we only need to migrate once per worker)
  23. if (migrationsRun) {
  24. return;
  25. }
  26. const { dbName, mongoUri } = getTestDbConfig();
  27. // Only run migrations when using external MongoDB (CI environment)
  28. if (mongoUri == null) {
  29. return;
  30. }
  31. // biome-ignore lint/suspicious/noConsole: Allow logging
  32. console.log(`Running migrations for ${dbName}...`);
  33. runMigrations(mongoUri);
  34. migrationsRun = true;
  35. });