Skip to content

Commit

Permalink
feat(cli): Added support for provider argument in prisma init (#7481)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuryaElavazhagan committed Jun 9, 2021
1 parent 5a2b51c commit d932c14
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/packages/cli/src/Init.ts
Expand Up @@ -49,6 +49,35 @@ export const defaultEnv = (
return env
}

export const defaultPort = (provider = 'postgresql') => {
switch (provider) {
case 'mysql':
return 3306
case 'sqlserver':
return 1433
case 'postgresql':
default:
return 5432
}
}

export const defaultURL = (
provider = 'postgresql',
port = defaultPort(provider),
schema = 'public',
) => {
switch (provider) {
case 'postgresql':
return `${provider}://johndoe:randompassword@localhost:${port}/mydb?schema=${schema}`
case 'mysql':
return `${provider}://johndoe:randompassword@localhost:${port}/mydb`
case 'sqlserver':
return `${provider}://localhost:${port};database=mydb;user=SA;password=randompassword;`
case 'sqlite':
return 'file:./dev.db'
}
}

export class Init implements Command {
static new(): Init {
return new Init()
Expand All @@ -68,6 +97,7 @@ export class Init implements Command {
'--help': Boolean,
'-h': '--help',
'--url': String,
'--provider': String,
})

if (isError(args) || args['--help']) {
Expand Down Expand Up @@ -141,6 +171,9 @@ export class Init implements Command {
protocolToDatabaseType(`${args['--url'].split(':')[0]}:`),
)
url = args['--url']
} else if (args['--provider']) {
provider = args['--provider']
url = defaultURL(provider)
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/packages/cli/src/__tests__/__snapshots__/init.test.ts.snap
Expand Up @@ -65,6 +65,20 @@ https://pris.ly/d/getting-started
`;

exports[`works with provider param 1`] = `
✔ Your Prisma schema was created at prisma/schema.prisma.
You can now open it in your favorite editor.
Next steps:
1. Run prisma db pull to turn your database schema into a Prisma data model.
2. Run prisma generate to install Prisma Client. You can then start querying your database.
More information in our documentation:
https://pris.ly/d/getting-started
`;

exports[`works with url param 1`] = `
✔ Your Prisma schema was created at prisma/schema.prisma.
Expand Down
22 changes: 22 additions & 0 deletions src/packages/cli/src/__tests__/init.test.ts
Expand Up @@ -43,6 +43,28 @@ test('works with url param', async () => {
`)
})

test('works with provider param', async () => {
ctx.fixture('init')
const result = await ctx.cli('init', '--provider', 'mysql')
expect(stripAnsi(result.stdout)).toMatchSnapshot()
const schema = fs.readFileSync(
join(ctx.tmpDir, 'prisma', 'schema.prisma'),
'utf-8',
)
expect(schema).toMatch(defaultSchema('mysql'))

const env = fs.readFileSync(join(ctx.tmpDir, '.env'), 'utf-8')
expect(env).toMatchInlineSnapshot(`
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
# Prisma supports the native connection string format for PostgreSQL, MySQL and SQLite.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_URL="mysql://johndoe:randompassword@localhost:3306/mydb"
`)
})

test('warns when DATABASE_URL present in .env ', async () => {
fs.writeFileSync(
join(ctx.tmpDir, '.env'),
Expand Down

0 comments on commit d932c14

Please sign in to comment.