diff --git a/src/bin.ts b/src/bin.ts index a8204a9f..109ebd10 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -2409,9 +2409,11 @@ yargs(rawArgs) async (argv) => { await applyInsecureStorage(argv.insecureStorage); const { resolveOptionalApiKey } = await import('./lib/api-key.js'); + const { getActiveEnvironment } = await import('./lib/config-store.js'); const { getMigrationsPassthroughArgs, runMigrations } = await import('./commands/migrations.js'); const passthrough = getMigrationsPassthroughArgs(rawArgs); - await runMigrations(passthrough, resolveOptionalApiKey({ apiKey: argv.apiKey })); + const endpoint = getActiveEnvironment()?.endpoint; + await runMigrations(passthrough, resolveOptionalApiKey({ apiKey: argv.apiKey }), endpoint); }, ) .command( diff --git a/src/commands/migrations.spec.ts b/src/commands/migrations.spec.ts index 3d94b7ac..1dbc873f 100644 --- a/src/commands/migrations.spec.ts +++ b/src/commands/migrations.spec.ts @@ -13,6 +13,7 @@ describe('runMigrations', () => { beforeEach(() => { vi.clearAllMocks(); delete process.env.WORKOS_SECRET_KEY; + delete process.env.WORKOS_API_URL; }); it('sets WORKOS_SECRET_KEY from the provided API key', async () => { @@ -42,6 +43,16 @@ describe('runMigrations', () => { expect(mockParseAsync).toHaveBeenCalledWith(args, { from: 'user' }); }); + it('sets WORKOS_API_URL when apiBaseUrl is provided', async () => { + await runMigrations(['import', '--csv', 'users.csv'], 'sk_test_123', 'https://api.staging.workos.com'); + expect(process.env.WORKOS_API_URL).toBe('https://api.staging.workos.com'); + }); + + it('does not set WORKOS_API_URL when apiBaseUrl is undefined', async () => { + await runMigrations(['import', '--csv', 'users.csv'], 'sk_test_123'); + expect(process.env.WORKOS_API_URL).toBeUndefined(); + }); + it('removes WorkOS global flags from migrations passthrough args', () => { expect( getMigrationsPassthroughArgs([ diff --git a/src/commands/migrations.ts b/src/commands/migrations.ts index 475b911a..9e39e58b 100644 --- a/src/commands/migrations.ts +++ b/src/commands/migrations.ts @@ -43,10 +43,13 @@ export function getMigrationsPassthroughArgs(rawArgs: string[]): string[] { return passthrough; } -export async function runMigrations(args: string[], apiKey?: string): Promise { +export async function runMigrations(args: string[], apiKey?: string, apiBaseUrl?: string): Promise { if (apiKey) { process.env.WORKOS_SECRET_KEY = apiKey; } + if (apiBaseUrl) { + process.env.WORKOS_API_URL = apiBaseUrl; + } const { program } = (await import('@workos/migrations/dist/cli/index.js')) as { program: {