diff --git a/src/cli/commands/generate.ts b/src/cli/commands/generate.ts index 616b50c..c3d63e2 100644 --- a/src/cli/commands/generate.ts +++ b/src/cli/commands/generate.ts @@ -2,7 +2,7 @@ import chalk from 'chalk' import { Command, CommandArguments } from '../../shared/types' import { runLocalPrisma, runDistantPrisma, spawnShell } from '../../shared/shell' -import { setManagementProviderInSchema } from '../../shared/env' +import { setManagementProviderInSchema, setManagementProviderInMigration } from '../../shared/env' class Generate implements Command { name = 'generate' @@ -47,6 +47,7 @@ class Generate implements Command { async generateManagement(prismaArgs: string = '') { // This is a workaround until Prisma allows for multi-provider datasources await setManagementProviderInSchema() + await setManagementProviderInMigration() await runLocalPrisma(`generate ${prismaArgs}`) } diff --git a/src/shared/env.ts b/src/shared/env.ts index f02f419..b6b4e53 100644 --- a/src/shared/env.ts +++ b/src/shared/env.ts @@ -76,3 +76,32 @@ export const setManagementProviderInSchema = async (): Promise => { // 4. Write content to file return writeFile(schemaPath, content) } + +export const setManagementProviderInMigration = async (): Promise => { + if (!process.env.MANAGEMENT_PROVIDER) { + throw new PmtError('missing-env', { name: 'MANAGEMENT_PROVIDER' }) + } + + const nodeModules = getNodeModules() + + // 1. Find migration steps file + const stepsPath = path.join( + nodeModules, + 'prisma-multi-tenant/build/cli/prisma/migrations/20200411135513-alpha/steps.json' + ) + + if (!(await fileExists(stepsPath))) { + throw new PmtError('management-migration-not-found') + } + + // 2. Read content of file + const content = JSON.parse(await readFile(stepsPath)) + + // 3. Change provider of datasource + content.steps.find( + (step: any) => step.argument == 'provider' + ).value = `\"${process.env.MANAGEMENT_PROVIDER}\"` + + // 4. Write content to file + return writeFile(stepsPath, JSON.stringify(content, null, 2)) +}