Skip to content

Commit

Permalink
refactor(cli,migrate): rename introspect to db pull (#5867)
Browse files Browse the repository at this point in the history
Closes #5836
  • Loading branch information
Jolg42 committed Feb 26, 2021
1 parent 529942d commit e72fb11
Show file tree
Hide file tree
Showing 31 changed files with 101 additions and 101 deletions.
18 changes: 9 additions & 9 deletions src/packages/cli/fixtures/introspection-warnings/schema.prisma
Expand Up @@ -10,9 +10,8 @@ model column_name_that_becomes_empty_string {
}

model unsupported_type {
field1 Int @id @default(autoincrement())
// This type is currently not supported by the Prisma Client.
// unsupported Unsupported("binary(50)")?
field1 Int @id @default(autoincrement())
unsupported Unsupported("binary(50)")?
}

model column_name_that_becomes_empty_string2 {
Expand Down Expand Up @@ -43,9 +42,10 @@ model column_name_that_becomes_empty_string2 {
// 999999999999 Int? @map("999999999999")
}

// The underlying table does not contain a valid unique identifier and can therefore currently not be handled.
// model no_unique_identifier {
// This type is currently not supported by the Prisma Client.
// field1 Unsupported("integer key")
// field2 Int?
// }
/// The underlying table does not contain a valid unique identifier and can therefore currently not be handled by the Prisma Client.
model no_unique_identifier {
field1 Unsupported("integer key")
field2 Int?
@@ignore
}
7 changes: 3 additions & 4 deletions src/packages/cli/src/CLI.ts
Expand Up @@ -114,7 +114,6 @@ export class CLI implements Command {
${chalk.bold('Commands')}
init Setup Prisma for your app
introspect Get the datamodel of your database
generate Generate artifacts (e.g. Prisma Client)
studio Open Prisma Studio
format Format your schema
Expand All @@ -132,9 +131,6 @@ export class CLI implements Command {
Setup a new Prisma project
${chalk.dim('$')} prisma init
Introspect an existing database
${chalk.dim('$')} prisma introspect
Generate artifacts (e.g. Prisma Client)
${chalk.dim('$')} prisma generate
Expand All @@ -144,6 +140,9 @@ export class CLI implements Command {
Create migrations from your Prisma schema, apply them to the database, generate artifacts (e.g. Prisma Client)
${chalk.dim('$')} prisma migrate dev --preview-feature
Introspect an existing database
${chalk.dim('$')} prisma db pull
Push the Prisma schema state to the database
${chalk.dim('$')} prisma db push --preview-feature
`)
Expand Down
4 changes: 1 addition & 3 deletions src/packages/cli/src/Dev.ts
Expand Up @@ -17,9 +17,7 @@ export class Dev implements Command {
If you want to generate the Prisma Client, use ${chalk.green(
'prisma generate',
)} (or ${chalk.green('prisma generate --watch')})
If you want to update your schema, use ${chalk.green(
'prisma introspect',
)}.
If you want to update your schema, use ${chalk.green('prisma db pull')}.
If you want to migrate your database, use ${chalk.green(
'prisma migrate',
)}.
Expand Down
2 changes: 1 addition & 1 deletion src/packages/cli/src/Init.ts
Expand Up @@ -180,7 +180,7 @@ export class Init implements Command {

const steps = [
`Run ${chalk.green(
getCommandWithExecutor('prisma introspect'),
getCommandWithExecutor('prisma db pull'),
)} to turn your database schema into a Prisma data model.`,
`Run ${chalk.green(
getCommandWithExecutor('prisma generate'),
Expand Down
Expand Up @@ -9,7 +9,7 @@ Environment variables loaded from .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
2. Set the provider of the datasource block in schema.prisma to match your database: postgresql, mysql or sqlite.
3. Run prisma introspect to turn your database schema into a Prisma data model.
3. Run prisma db pull to turn your database schema into a Prisma data model.
4. Run prisma generate to install Prisma Client. You can then start querying your database.
More information in our documentation:
Expand Down Expand Up @@ -38,7 +38,7 @@ exports[`is schema and env written on disk replace 1`] = `
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
2. Set the provider of the datasource block in schema.prisma to match your database: postgresql, mysql or sqlite.
3. Run prisma introspect to turn your database schema into a Prisma data model.
3. Run prisma db pull to turn your database schema into a Prisma data model.
4. Run prisma generate to install Prisma Client. You can then start querying your database.
More information in our documentation:
Expand All @@ -57,7 +57,7 @@ warn Prisma would have added DATABASE_URL="postgresql://johndoe:randompassword@l
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
2. Set the provider of the datasource block in schema.prisma to match your database: postgresql, mysql or sqlite.
3. Run prisma introspect to turn your database schema into a Prisma data model.
3. Run prisma db pull to turn your database schema into a Prisma data model.
4. Run prisma generate to install Prisma Client. You can then start querying your database.
More information in our documentation:
Expand All @@ -71,7 +71,7 @@ exports[`works with url param 1`] = `
You can now open it in your favorite editor.
Next steps:
1. Run prisma introspect to turn your database schema into a Prisma data model.
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:
Expand Down
Binary file not shown.
6 changes: 3 additions & 3 deletions src/packages/cli/src/bin.ts
Expand Up @@ -89,6 +89,7 @@ import {
MigrateReset,
MigrateDeploy,
DbPush,
DbPull,
// DbDrop,
DbSeed,
DbCommand,
Expand All @@ -97,7 +98,6 @@ import {

import { CLI } from './CLI'
import { Init } from './Init'
import { Introspect } from './Introspect'
import { Dev } from './Dev'
import { Version } from './Version'
import { Generate } from './Generate'
Expand Down Expand Up @@ -156,12 +156,12 @@ async function main(): Promise<number> {
deploy: MigrateDeploy.new(),
}),
db: DbCommand.new({
pull: Introspect.new(),
pull: DbPull.new(),
push: DbPush.new(),
// drop: DbDrop.new(),
seed: DbSeed.new(),
}),
introspect: Introspect.new(),
introspect: DbPull.new(),
dev: Dev.new(),
studio: Studio.new(aliases),
generate: Generate.new(),
Expand Down
@@ -1,11 +1,10 @@
import path from 'path'
import { Introspect } from '../Introspect'
import { DbPull } from '../commands/DbPull'
import { consoleContext, Context } from './__helpers__/context'
import {
SetupParams,
setupPostgres,
tearDownPostgres,
} from '../utils/setupPostgres'
// import
// SetupParams,
// setupPostgres,
// tearDownPostgres,
// } from '../utils/setupPostgres'

const ctx = Context.new().add(consoleContext()).assemble()

Expand All @@ -26,7 +25,7 @@ const ctx = Context.new().add(consoleContext()).assemble()

// ctx.fixture('introspection/postgresql')

// const introspect = new Introspect()
// const introspect = new DbPull()

// try {
// const result = await introspect.parse(['--print'])
Expand All @@ -49,28 +48,28 @@ const ctx = Context.new().add(consoleContext()).assemble()

test('basic introspection', async () => {
ctx.fixture('introspection/sqlite')
const introspect = new Introspect()
const introspect = new DbPull()
await introspect.parse(['--print'])
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchSnapshot()
})

test('introspection --force', async () => {
ctx.fixture('introspection/sqlite')
const introspect = new Introspect()
const introspect = new DbPull()
await introspect.parse(['--print', '--force'])
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchSnapshot()
})

test('basic introspection with --url', async () => {
ctx.fixture('introspection/sqlite')
const introspect = new Introspect()
const introspect = new DbPull()
await introspect.parse(['--print', '--url', 'file:dev.db'])
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchSnapshot()
})

it('should succeed when schema and db do match', async () => {
ctx.fixture('introspect/prisma')
const result = Introspect.new().parse([])
const result = DbPull.new().parse([])
await expect(result).resolves.toMatchInlineSnapshot(``)

console.log(ctx.mocked['console.log'].mock.calls)
Expand All @@ -93,7 +92,7 @@ it('should succeed when schema and db do match', async () => {

it('should succeed when schema and db do match using --url', async () => {
ctx.fixture('introspect/prisma')
const result = Introspect.new().parse(['--url=file:./dev.db'])
const result = DbPull.new().parse(['--url=file:./dev.db'])
await expect(result).resolves.toMatchInlineSnapshot(``)

console.log(ctx.mocked['console.log'].mock.calls)
Expand All @@ -117,7 +116,7 @@ it('should succeed when schema and db do match using --url', async () => {
it('should succeed and keep changes to valid schema and output warnings', async () => {
ctx.fixture('introspect')
const originalSchema = ctx.fs.read('prisma/reintrospection.prisma')
const result = Introspect.new().parse([
const result = DbPull.new().parse([
'--schema=./prisma/reintrospection.prisma',
])
await expect(result).resolves.toMatchInlineSnapshot(``)
Expand Down Expand Up @@ -150,12 +149,12 @@ it('should succeed and keep changes to valid schema and output warnings', async
expect(ctx.fs.read('prisma/reintrospection.prisma')).toMatchInlineSnapshot(`
generator client {
provider = "prisma-client-js"
output = "***"
output = "../generated/client"
}
datasource db {
provider = "sqlite"
url = "***"
url = "file:dev.db"
}
model AwesomeUser {
Expand Down Expand Up @@ -195,7 +194,7 @@ it('should succeed and keep changes to valid schema and output warnings', async
it('should succeed and keep changes to valid schema and output warnings when using --print', async () => {
ctx.fixture('introspect')
const originalSchema = ctx.fs.read('prisma/reintrospection.prisma')
const result = Introspect.new().parse([
const result = DbPull.new().parse([
'--print',
'--schema=./prisma/reintrospection.prisma',
])
Expand All @@ -210,82 +209,81 @@ it('should succeed and keep changes to valid schema and output warnings when usi
expect(ctx.mocked['console.error'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
*** WARNING ***
*** WARNING ***
These models were enriched with \`@@map\` information taken from the previous Prisma schema.
- Model "AwesomeNewPost"
- Model "AwesomeProfile"
- Model "AwesomeUser"
These models were enriched with \`@@map\` information taken from the previous Prisma schema.
- Model "AwesomeNewPost"
- Model "AwesomeProfile"
- Model "AwesomeUser"
`)
`)

expect(ctx.fs.read('prisma/reintrospection.prisma')).toStrictEqual(
originalSchema,
)
})

it('should succeed when schema and db do not match', async () => {
ctx.fixture('schema-db-out-of-sync')
const result = Introspect.new().parse([])
ctx.fixture('existing-db-histories-diverge')
const result = DbPull.new().parse([])
await expect(result).resolves.toMatchInlineSnapshot(``)

expect(
ctx.mocked['console.log'].mock.calls
.join('\n')
.replace(/\d{2,3}ms/, 'in XXms'),
).toMatchInlineSnapshot(`
Prisma schema loaded from schema.prisma
Prisma schema loaded from prisma/schema.prisma
Introspecting based on datasource defined in schema.prisma …
Introspecting based on datasource defined in prisma/schema.prisma …
✔ Introspected 3 models and wrote them into schema.prisma in in XXms
✔ Introspected 3 models and wrote them into prisma/schema.prisma in in XXms
Run prisma generate to generate Prisma Client.
`)
})

it('should fail when db is missing', async () => {
ctx.fixture('schema-db-out-of-sync')
ctx.fs.remove('dev.db')
const result = Introspect.new().parse([])
ctx.fixture('schema-only-sqlite')
const result = DbPull.new().parse([])
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
P4001 The introspected database was empty:
prisma introspect could not create any models in your schema.prisma file and you will not be able to generate Prisma Client with the prisma generate command.
prisma db pull could not create any models in your schema.prisma file and you will not be able to generate Prisma Client with the prisma generate command.
To fix this, you have two options:
- manually create a table in your database (using SQL).
- make sure the database connection URL inside the datasource block in schema.prisma points to a database that is not empty (it must contain at least one table).
Then you can run prisma introspect again.
Then you can run prisma db pull again.
`)
})

it('should fail when db is empty', async () => {
ctx.fixture('schema-db-out-of-sync')
ctx.fs.write('dev.db', '')
const result = Introspect.new().parse([])
ctx.fixture('schema-only-sqlite')
ctx.fs.write('prisma/dev.db', '')
const result = DbPull.new().parse([])
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
P4001 The introspected database was empty:
prisma introspect could not create any models in your schema.prisma file and you will not be able to generate Prisma Client with the prisma generate command.
prisma db pull could not create any models in your schema.prisma file and you will not be able to generate Prisma Client with the prisma generate command.
To fix this, you have two options:
- manually create a table in your database (using SQL).
- make sure the database connection URL inside the datasource block in schema.prisma points to a database that is not empty (it must contain at least one table).
Then you can run prisma introspect again.
Then you can run prisma db pull again.
`)
})

it('should fail when Prisma schema is missing', async () => {
const result = Introspect.new().parse([])
const result = DbPull.new().parse([])
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
Could not find a schema.prisma file that is required for this command.
You can either provide it with --schema, set it as \`prisma.schema\` in your package.json or put it into the default location ./prisma/schema.prisma https://pris.ly/d/prisma-schema-location
Expand All @@ -294,7 +292,7 @@ it('should fail when Prisma schema is missing', async () => {

it('should fail when schema is invalid', async () => {
ctx.fixture('introspect')
const result = Introspect.new().parse(['--schema=./prisma/invalid.prisma'])
const result = DbPull.new().parse(['--schema=./prisma/invalid.prisma'])
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
P1012 Introspection failed as your current Prisma schema file is invalid
Expand All @@ -307,7 +305,7 @@ it('should fail when schema is invalid', async () => {
it('should succeed when schema is invalid and using --force', async () => {
ctx.fixture('introspect')

const result = Introspect.new().parse([
const result = DbPull.new().parse([
'--schema=./prisma/invalid.prisma',
'--force',
])
Expand Down

0 comments on commit e72fb11

Please sign in to comment.