Skip to content

Commit

Permalink
fix(cli): add --datasource-provider to init command (#7671)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Piotrowski <piotrowski+github@gmail.com>
  • Loading branch information
Jolg42 and janpio committed Jun 28, 2021
1 parent c8b8825 commit febfba9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
47 changes: 32 additions & 15 deletions src/packages/cli/src/Init.ts
Expand Up @@ -70,11 +70,11 @@ export const defaultURL = (
) => {
switch (provider) {
case 'postgresql':
return `${provider}://johndoe:randompassword@localhost:${port}/mydb?schema=${schema}`
return `postgresql://johndoe:randompassword@localhost:${port}/mydb?schema=${schema}`
case 'mysql':
return `${provider}://johndoe:randompassword@localhost:${port}/mydb`
return `mysql://johndoe:randompassword@localhost:${port}/mydb`
case 'sqlserver':
return `${provider}://localhost:${port};database=mydb;user=SA;password=randompassword;`
return `sqlserver://localhost:${port};database=mydb;user=SA;password=randompassword;`
case 'mongodb':
return `mongodb://johndoe:randompassword@localhost:${port}/mydb?authSource=admin`
case 'sqlite':
Expand All @@ -87,13 +87,30 @@ export class Init implements Command {
return new Init()
}

// TODO add --provider & --url
private static help = format(`
Setup a new Prisma project
Setup a new Prisma project
${chalk.bold('Usage')}
${chalk.bold('Usage')}
${chalk.dim('$')} prisma init
${chalk.dim('$')} prisma init [options]
${chalk.bold('Options')}
-h, --help Display this help message
--datasource-provider Define the datasource provider to use: PostgreSQL, MySQL, SQLServer or SQLite
--url Define a custom datasource url
${chalk.bold('Examples')}
Setup a new Prisma project with PostgreSQL (default)
${chalk.dim('$')} prisma init
Setup a new Prisma project and specify MySQL as the datasource provider to use
${chalk.dim('$')} prisma init --datasource-provider mysql
Setup a new Prisma project and specify the url that will be used
${chalk.dim(
'$',
)} prisma init --url mysql://user:password@localhost:3306/mydb
`)

// eslint-disable-next-line @typescript-eslint/require-await
Expand All @@ -102,7 +119,7 @@ export class Init implements Command {
'--help': Boolean,
'-h': '--help',
'--url': String,
// '--provider': String,
'--datasource-provider': String,
})

if (isError(args) || args['--help']) {
Expand Down Expand Up @@ -176,15 +193,15 @@ export class Init implements Command {
protocolToDatabaseType(`${args['--url'].split(':')[0]}:`),
)
url = args['--url']
} else if (args['--provider']) {
const providerLowercase = args['--provider'].toLowerCase()
} else if (args['--datasource-provider']) {
const providerLowercase = args['--datasource-provider'].toLowerCase()
if (
!['postgresql', 'mysql', 'sqlserver', 'sqlite', 'mongodb'].includes(
providerLowercase,
)
) {
throw new Error(
`Provider "${args['--provider']}" is invalid or not supported. Try again with "postgresql", "mysql", "sqlserver" or "sqlite".`,
`Provider "${args['--datasource-provider']}" is invalid or not supported. Try again with "postgresql", "mysql", "sqlserver" or "sqlite".`,
)
}
provider = providerLowercase as ConnectorType
Expand Down Expand Up @@ -222,7 +239,7 @@ export class Init implements Command {
warning = `${chalk.yellow('warn')} Prisma would have added ${defaultEnv(
url,
false,
)} but it already exists in ${chalk.bold(
)} but the variable already exists in ${chalk.bold(
path.relative(outputDir, envPath),
)}`
} else {
Expand All @@ -244,8 +261,8 @@ export class Init implements Command {
)} to install Prisma Client. You can then start querying your database.`,
]

if (!url || args['--provider']) {
if (!args['--provider']) {
if (!url || args['--datasource-provider']) {
if (!args['--datasource-provider']) {
steps.unshift(
`Set the ${chalk.green('provider')} of the ${chalk.green(
'datasource',
Expand All @@ -267,7 +284,7 @@ export class Init implements Command {
}

return `
✔ Your Prisma schema was created at ${chalk.green('prisma/schema.prisma')}.
✔ Your Prisma schema was created at ${chalk.green('prisma/schema.prisma')}
You can now open it in your favorite editor.
${warning && logger.should.warn ? '\n' + warning + '\n' : ''}
Next steps:
Expand Down
14 changes: 7 additions & 7 deletions src/packages/cli/src/__tests__/__snapshots__/init.test.ts.snap
Expand Up @@ -3,7 +3,7 @@
exports[`appends when .env present 1`] = `
Environment variables loaded from .env
✔ Your Prisma schema was created at prisma/schema.prisma.
✔ Your Prisma schema was created at prisma/schema.prisma
You can now open it in your favorite editor.
Next steps:
Expand Down Expand Up @@ -32,7 +32,7 @@ DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=pub

exports[`is schema and env written on disk replace 1`] = `
✔ Your Prisma schema was created at prisma/schema.prisma.
✔ Your Prisma schema was created at prisma/schema.prisma
You can now open it in your favorite editor.
Next steps:
Expand All @@ -49,10 +49,10 @@ https://pris.ly/d/getting-started
exports[`warns when DATABASE_URL present in .env 1`] = `
Environment variables loaded from .env
✔ Your Prisma schema was created at prisma/schema.prisma.
✔ Your Prisma schema was created at prisma/schema.prisma
You can now open it in your favorite editor.
warn Prisma would have added DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public" but it already exists in .env
warn Prisma would have added DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public" but the variable already exists in .env
Next steps:
1. Set the DATABASE_URL in the .env file to point to your existing database. If your database has no tables yet, read https://pris.ly/d/getting-started
Expand All @@ -67,7 +67,7 @@ https://pris.ly/d/getting-started

exports[`works with provider param - SQLITE 1`] = `
✔ Your Prisma schema was created at prisma/schema.prisma.
✔ Your Prisma schema was created at prisma/schema.prisma
You can now open it in your favorite editor.
Next steps:
Expand All @@ -82,7 +82,7 @@ https://pris.ly/d/getting-started

exports[`works with provider param - mysql 1`] = `
✔ Your Prisma schema was created at prisma/schema.prisma.
✔ Your Prisma schema was created at prisma/schema.prisma
You can now open it in your favorite editor.
Next steps:
Expand All @@ -97,7 +97,7 @@ https://pris.ly/d/getting-started

exports[`works with url param 1`] = `
✔ Your Prisma schema was created at prisma/schema.prisma.
✔ Your Prisma schema was created at prisma/schema.prisma
You can now open it in your favorite editor.
Next steps:
Expand Down
12 changes: 6 additions & 6 deletions src/packages/cli/src/__tests__/init.test.ts
Expand Up @@ -43,9 +43,9 @@ DATABASE_URL="file:dev.db"
`)
})

test.skip('works with provider param - mysql', async () => {
test('works with provider param - mysql', async () => {
ctx.fixture('init')
const result = await ctx.cli('init', '--provider', 'mysql')
const result = await ctx.cli('init', '--datasource-provider', 'mysql')
expect(stripAnsi(result.stdout)).toMatchSnapshot()

const schema = fs.readFileSync(
Expand All @@ -66,9 +66,9 @@ DATABASE_URL="mysql://johndoe:randompassword@localhost:3306/mydb"
`)
})

test.skip('works with provider param - SQLITE', async () => {
test('works with provider param - SQLITE', async () => {
ctx.fixture('init')
const result = await ctx.cli('init', '--provider', 'SQLITE')
const result = await ctx.cli('init', '--datasource-provider', 'SQLITE')
expect(stripAnsi(result.stdout)).toMatchSnapshot()

const schema = fs.readFileSync(
Expand All @@ -89,9 +89,9 @@ DATABASE_URL="file:./dev.db"
`)
})

test.skip('errors with invalid provider param', async () => {
test('errors with invalid provider param', async () => {
ctx.fixture('init')
const result = ctx.cli('init', '--provider', 'INVALID')
const result = ctx.cli('init', '--datasource-provider', 'INVALID')
await expect(result).rejects.toThrowError()
})

Expand Down

0 comments on commit febfba9

Please sign in to comment.