Skip to content

Commit

Permalink
fix(config): fix issue with --config cli arg (prisma#26336)
Browse files Browse the repository at this point in the history
The arg had to be "allowlisted" for each command individually.
  • Loading branch information
FGoessler authored Feb 17, 2025
1 parent bf427e3 commit cd29583
Show file tree
Hide file tree
Showing 31 changed files with 190 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/cli/src/CLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class CLI implements Command {
'-h': '--help',
'--version': Boolean,
'-v': '--version',
'--config': String,
'--json': Boolean, // for -v
'--experimental': Boolean,
'--preview-feature': Boolean,
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/DebugInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class DebugInfo implements Command {
'--help': Boolean,
'-h': '--help',
'--schema': String,
'--config': String,
'--telemetry-information': String,
})

Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Or specify a Prisma schema path
'--help': Boolean,
'-h': '--help',
'--schema': String,
'--config': String,
'--telemetry-information': String,
'--check': Boolean,
})
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/Generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ ${bold('Examples')}
'-h': '--help',
'--watch': Boolean,
'--schema': String,
'--config': String,
'--data-proxy': Boolean,
'--accelerate': Boolean,
'--no-engine': Boolean,
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/Validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ ${bold('Examples')}
'--help': Boolean,
'-h': '--help',
'--schema': String,
'--config': String,
'--telemetry-information': String,
})

Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/Version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class Version implements Command {
'-h': '--help',
'--version': Boolean,
'-v': '--version',
'--config': String,
'--json': Boolean,
'--telemetry-information': String,
})
Expand Down
48 changes: 48 additions & 0 deletions packages/cli/src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { jestConsoleContext, jestContext } from '@prisma/get-platform'

const ctx = jestContext.new().add(jestConsoleContext()).assemble()

function cleanSnapshot(str: string): string {
str = str.replace(/\\/g, '/').replace(/"\/.*(\/config\/prisma.config.ts)"/g, '"REDACTED_ROOT$1"')
str = str.replace(/\\/g, '/').replace(/"\/.*(\/prisma.config.ts)"/g, '"REDACTED_ROOT$1"')
return str
}

const COMMANDS = [
['validate'],
['migrate', 'dev'],
['migrate', 'status'],
['migrate', 'resolve'],
['migrate', 'reset'],
['migrate', 'deploy'],
['migrate', 'diff'],
['db', 'execute'],
['db', 'pull'],
['db', 'push'],
['db', 'seed'],
['studio'],
['generate'],
['version'],
['validate'],
['format'],
['debug'],
]

COMMANDS.forEach((command) => {
it(`test 'prisma ${command.join(' ')}' automatically detects config file`, async () => {
ctx.fixture('prisma-config')

// Running with --help to not run further actions beyond config loading
const res = await ctx.cli(...command, '--help')
expect(cleanSnapshot(res.stdout)).toContain(`Loaded Prisma config from "REDACTED_ROOT/prisma.config.ts".`)
})

it(`test 'prisma ${command.join(' ')}' picks up custom --config option`, async () => {
ctx.fixture('prisma-config-nested')

// Running with --help to not run further actions beyond config loading
const res = await ctx.cli(...command, '--config=./config/prisma.config.ts', '--help')
console.log(res.stdout)
expect(cleanSnapshot(res.stdout)).toContain(`Loaded Prisma config from "REDACTED_ROOT/config/prisma.config.ts".`)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from '@prisma/config/src'

export default defineConfig({
earlyAccess: true,
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgres"
url = "postgresql://foo:[email protected]"
}

model User {
id Int @id @default(autoincrement())
name String
}
13 changes: 13 additions & 0 deletions packages/cli/src/__tests__/fixtures/prisma-config/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgres"
url = "postgresql://foo:[email protected]"
}

model User {
id Int @id @default(autoincrement())
name String
}
1 change: 1 addition & 0 deletions packages/cli/src/utils/checkpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export const SENSITIVE_CLI_OPTIONS = [
'--to-url',
// 2. Paths
'--schema',
'--config',
'--file',
'--from-schema-datamodel',
'--to-schema-datamodel',
Expand Down
2 changes: 2 additions & 0 deletions packages/config/src/loadConfigFromFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ export async function loadConfigFromFile({
}
}

process.stdout.write(`Loaded Prisma config from "${resolvedPath}".\n`)

// Success case
const prismaConfig = transformPathsInConfigToAbsolute(parseResultEither.right, resolvedPath)

Expand Down
1 change: 1 addition & 0 deletions packages/migrate/src/CLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class CLI implements Command {
const args = arg(argv, {
'--help': Boolean,
'-h': '--help',
'--config': String,
'--json': Boolean, // for -v
'--experimental': Boolean,
'--preview-feature': Boolean,
Expand Down
52 changes: 52 additions & 0 deletions packages/migrate/src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { jestConsoleContext, jestContext } from '@prisma/get-platform'
import execa from 'execa'
import path from 'path'

const ctx = jestContext.new().add(jestConsoleContext()).assemble()

function cleanSnapshot(str: string): string {
str = str.replace(/\\/g, '/').replace(/"\/.*(\/config\/prisma.config.ts)"/g, '"REDACTED_ROOT$1"')
str = str.replace(/\\/g, '/').replace(/"\/.*(\/prisma.config.ts)"/g, '"REDACTED_ROOT$1"')
return str
}

const originalCwd = process.cwd()

const migrateCli = (...input: string[]) => {
return execa.node(path.join(originalCwd, '../migrate/dist/bin.js'), input, {
cwd: ctx.fs.cwd(),
stdio: 'pipe',
all: true,
})
}

const COMMANDS = [
['migrate', 'dev'],
['migrate', 'status'],
['migrate', 'resolve'],
['migrate', 'reset'],
['migrate', 'deploy'],
['migrate', 'diff'],
['db', 'execute'],
['db', 'pull'],
['db', 'push'],
['db', 'seed'],
]

COMMANDS.forEach((command) => {
it(`test 'prisma ${command.join(' ')}' automatically detects config file`, async () => {
ctx.fixture('prisma-config')

// Running with --help to not run further actions beyond config loading
const res = await migrateCli(...command, '--help')
expect(cleanSnapshot(res.stdout)).toContain(`Loaded Prisma config from "REDACTED_ROOT/prisma.config.ts".`)
})

it(`test 'prisma ${command.join(' ')}' picks up custom --config option`, async () => {
ctx.fixture('prisma-config-nested')

// Running with --help to not run further actions beyond config loading
const res = await migrateCli(...command, '--config=./config/prisma.config.ts', '--help')
expect(cleanSnapshot(res.stdout)).toContain(`Loaded Prisma config from "REDACTED_ROOT/config/prisma.config.ts".`)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from '@prisma/config/src'

export default defineConfig({
earlyAccess: true,
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgres"
url = "postgresql://foo:[email protected]"
}

model User {
id Int @id @default(autoincrement())
name String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from '@prisma/config/src'

export default defineConfig({
earlyAccess: true,
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgres"
url = "postgresql://foo:[email protected]"
}

model User {
id Int @id @default(autoincrement())
name String
}
1 change: 1 addition & 0 deletions packages/migrate/src/commands/DbCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ ${bold('Examples')}
const args = arg(argv, {
'--help': Boolean,
'-h': '--help',
'--config': String,
'--preview-feature': Boolean,
'--telemetry-information': String,
})
Expand Down
1 change: 1 addition & 0 deletions packages/migrate/src/commands/DbDrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ ${bold('Examples')}
'--force': Boolean,
'-f': '--force',
'--schema': String,
'--config': String,
'--telemetry-information': String,
})

Expand Down
1 change: 1 addition & 0 deletions packages/migrate/src/commands/DbExecute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ ${bold('Examples')}
{
'--help': Boolean,
'-h': '--help',
'--config': String,
'--stdin': Boolean,
'--file': String,
'--schema': String,
Expand Down
1 change: 1 addition & 0 deletions packages/migrate/src/commands/DbPull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Set composite types introspection depth to 2 levels
'--url': String,
'--print': Boolean,
'--schema': String,
'--config': String,
'--schemas': String,
'--force': Boolean,
'--composite-type-depth': Number, // optional, only on mongodb
Expand Down
1 change: 1 addition & 0 deletions packages/migrate/src/commands/DbPush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ ${bold('Examples')}
'--force-reset': Boolean,
'--skip-generate': Boolean,
'--schema': String,
'--config': String,
'--telemetry-information': String,
},
false,
Expand Down
1 change: 1 addition & 0 deletions packages/migrate/src/commands/DbSeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ ${bold('Examples')}
'--help': Boolean,
'-h': '--help',
'--schema': String,
'--config': String,
'--telemetry-information': String,
},
false,
Expand Down
1 change: 1 addition & 0 deletions packages/migrate/src/commands/MigrateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ ${bold('Examples')}
const args = arg(argv, {
'--help': Boolean,
'-h': '--help',
'--config': String,
'--preview-feature': Boolean,
'--telemetry-information': String,
})
Expand Down
1 change: 1 addition & 0 deletions packages/migrate/src/commands/MigrateDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ ${bold('Examples')}
'--help': Boolean,
'-h': '--help',
'--schema': String,
'--config': String,
'--telemetry-information': String,
},
false,
Expand Down
1 change: 1 addition & 0 deletions packages/migrate/src/commands/MigrateDev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ ${bold('Examples')}
// '-f': '--force',
'--create-only': Boolean,
'--schema': String,
'--config': String,
'--skip-generate': Boolean,
'--skip-seed': Boolean,
'--telemetry-information': String,
Expand Down
1 change: 1 addition & 0 deletions packages/migrate/src/commands/MigrateDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ ${bold('Examples')}
'--script': Boolean,
'--exit-code': Boolean,
'--telemetry-information': String,
'--config': String,
},
false,
)
Expand Down
1 change: 1 addition & 0 deletions packages/migrate/src/commands/MigrateReset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ ${bold('Examples')}
'--skip-generate': Boolean,
'--skip-seed': Boolean,
'--schema': String,
'--config': String,
'--telemetry-information': String,
})

Expand Down
1 change: 1 addition & 0 deletions packages/migrate/src/commands/MigrateResolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ ${bold('Examples')}
'--applied': String,
'--rolled-back': String,
'--schema': String,
'--config': String,
'--telemetry-information': String,
},
false,
Expand Down
1 change: 1 addition & 0 deletions packages/migrate/src/commands/MigrateStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Check the status of your database migrations
'--help': Boolean,
'-h': '--help',
'--schema': String,
'--config': String,
'--telemetry-information': String,
},
false,
Expand Down

0 comments on commit cd29583

Please sign in to comment.