Skip to content

Commit

Permalink
feat(migrate diff): add -o --output option (#23715)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolg42 committed Apr 4, 2024
1 parent 9c27962 commit 2c09e72
Show file tree
Hide file tree
Showing 62 changed files with 1,424 additions and 1,194 deletions.
Expand Up @@ -3,4 +3,5 @@
exports[`should read .env file in root folder and custom-path 1`] = `
Environment variables loaded from .env
Environment variables loaded from custom-path/.env
`;
@@ -1,3 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should read .env file in prisma folder 1`] = `Environment variables loaded from prisma/.env`;
exports[`should read .env file in prisma folder 1`] = `
Environment variables loaded from prisma/.env
`;
@@ -1,3 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should read .env file in prisma folder when there is no schema 1`] = `Environment variables loaded from prisma/.env`;
exports[`should read .env file in prisma folder when there is no schema 1`] = `
Environment variables loaded from prisma/.env
`;
@@ -1,3 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should not load root .env file 1`] = `Environment variables loaded from .env`;
exports[`should not load root .env file 1`] = `
Environment variables loaded from .env
`;
@@ -1,3 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should read expanded env vars 1`] = `Environment variables loaded from expand/.env`;
exports[`should read expanded env vars 1`] = `
Environment variables loaded from expand/.env
`;
@@ -1,12 +1,13 @@
import { jestConsoleContext, jestContext } from '@prisma/get-platform'
import { jestContext, jestProcessContext } from '@prisma/get-platform'
import { loadEnvFile } from '@prisma/internals'

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

it('should read .env file in root folder and custom-path', () => {
ctx.fixture('dotenv-1-custom-schema-path')

loadEnvFile({ schemaPath: './custom-path/schema.prisma', printMessage: true })
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['process.stdout.write'].mock.calls.join('\n')).toMatchSnapshot()

expect(process.env.DOTENV_PRISMA_WHEN_CUSTOM_SCHEMA_PATH_SHOULD_WORK).toEqual('file:dev.db')
expect(process.env.DOTENV_ROOT).toEqual('shouldbebread')
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/src/__tests__/dotenv-2-prisma-folder.test.ts
@@ -1,13 +1,14 @@
import { jestConsoleContext, jestContext } from '@prisma/get-platform'
import { jestContext, jestProcessContext } from '@prisma/get-platform'
import { loadEnvFile } from '@prisma/internals'

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

it('should read .env file in prisma folder', () => {
ctx.fixture('dotenv-2-prisma-folder')

loadEnvFile({ printMessage: true })

expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['process.stdout.write'].mock.calls.join('\n')).toMatchSnapshot()

expect(process.env.DOTENV_PRISMA_SHOULD_WORK).toEqual('file:dev.db')
expect(process.env.DOTENV_ROOT_SHOULD_BE_UNDEFINED).toEqual(undefined)
Expand Down
@@ -1,13 +1,14 @@
import { jestConsoleContext, jestContext } from '@prisma/get-platform'
import { jestContext, jestProcessContext } from '@prisma/get-platform'
import { loadEnvFile } from '@prisma/internals'

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

it('should read .env file in prisma folder when there is no schema', () => {
ctx.fixture('dotenv-4-prisma-no-schema')

loadEnvFile({ printMessage: true })

expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['process.stdout.write'].mock.calls.join('\n')).toMatchSnapshot()

expect(process.env.DOTENV_PRISMA_NO_SCHEMA_SHOULD_WORK).toEqual('file:dev.db')
expect(process.env.DOTENV_ROOT_SHOULD_BE_UNDEFINED).toEqual(undefined)
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/src/__tests__/dotenv-5-only-root.test.ts
@@ -1,13 +1,14 @@
import { jestConsoleContext, jestContext } from '@prisma/get-platform'
import { jestContext, jestProcessContext } from '@prisma/get-platform'
import { loadEnvFile } from '@prisma/internals'

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

it('should not load root .env file', () => {
ctx.fixture('dotenv-5-only-root')

loadEnvFile({ printMessage: true })

expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['process.stdout.write'].mock.calls.join('\n')).toMatchSnapshot()

expect(process.env.DOTENV_ROOT_SHOULD_BE_UNDEFINED).toEqual(undefined)
})
6 changes: 3 additions & 3 deletions packages/cli/src/__tests__/dotenv-6-expand.test.ts
@@ -1,13 +1,13 @@
import { jestConsoleContext, jestContext } from '@prisma/get-platform'
import { jestContext, jestProcessContext } from '@prisma/get-platform'
import { loadEnvFile } from '@prisma/internals'

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

it('should read expanded env vars', () => {
ctx.fixture('dotenv-6-expand')
loadEnvFile({ schemaPath: './expand/schema.prisma', printMessage: true })

expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['process.stdout.write'].mock.calls.join('\n')).toMatchSnapshot()

expect(process.env.DOTENV_PRISMA_EXPAND_DATABASE_URL_WITH_SCHEMA).toEqual(
'postgres://user:password@server.host:5432/database?ssl=1&schema=schema1234',
Expand Down
2 changes: 1 addition & 1 deletion packages/internals/src/utils/loadEnvFile.ts
Expand Up @@ -13,6 +13,6 @@ export function loadEnvFile({
const envData = tryLoadEnvs(envPaths, { conflictCheck: 'error' })

if (printMessage && envData && envData.message) {
console.info(envData.message)
process.stdout.write(envData.message + '\n')
}
}
2 changes: 1 addition & 1 deletion packages/migrate/src/Migrate.ts
Expand Up @@ -145,7 +145,7 @@ export class Migrate {

const message: string[] = []

console.info() // empty line
process.stdout.write('\n') // empty line
logUpdate(`Running generate... ${dim('(Use --skip-generate to skip the generators)')}`)

const generators = await getGenerators({
Expand Down
2 changes: 1 addition & 1 deletion packages/migrate/src/SchemaEngine.ts
Expand Up @@ -300,7 +300,7 @@ export class SchemaEngine {
if (result.method === 'print' && result.params?.content !== undefined) {
// Here we print the content from the Schema Engine to stdout directly
// (it is not returned to the caller)
console.info(result.params.content)
process.stdout.write(result.params.content + '\n')

// Send an empty response back as ACK.
const response: RpcSuccessResponse<{}> = {
Expand Down
41 changes: 31 additions & 10 deletions packages/migrate/src/__tests__/Baseline.test.ts
Expand Up @@ -6,8 +6,10 @@ import { DbPull } from '../commands/DbPull'
import { MigrateDeploy } from '../commands/MigrateDeploy'
import { MigrateDev } from '../commands/MigrateDev'
import { MigrateResolve } from '../commands/MigrateResolve'
import { CaptureStdout } from '../utils/captureStdout'

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

// Disable prompts
process.env.GITHUB_ACTIONS = '1'
Expand All @@ -22,7 +24,13 @@ describe('Baselining', () => {
// Backup env vars
const OLD_ENV = { ...process.env }

beforeEach(() => {
captureStdout.startCapture()
})

afterEach(() => {
captureStdout.clearCaptureText()
captureStdout.stopCapture()
// Restore env vars to backup state
process.env = { ...OLD_ENV }
})
Expand All @@ -38,11 +46,17 @@ describe('Baselining', () => {
// db pull
const dbPull = DbPull.new().parse([])
await expect(dbPull).resolves.toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(`
expect(captureStdout.getCapturedText().join('')).toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:./dev.db"
- Introspecting based on datasource defined in prisma/schema.prisma
✔ Introspected 1 model and wrote it into prisma/schema.prisma in XXXms
Run prisma generate to generate Prisma Client.
`)
ctx.mocked['console.info'].mockReset()
captureStdout.clearCaptureText()

// migrate dev --create-only
prompt.inject(['y'])
Expand All @@ -52,7 +66,7 @@ describe('Baselining', () => {
You can now edit it and apply it by running prisma migrate dev.
`)
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(`
expect(captureStdout.getCapturedText().join('')).toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:./dev.db"
Expand All @@ -71,13 +85,15 @@ describe('Baselining', () => {
We need to reset the SQLite database "dev.db" at "file:./dev.db"
Do you want to continue? All data will be lost.
`)
ctx.mocked['console.info'].mockReset()
captureStdout.clearCaptureText()

// migrate dev
captureStdout.startCapture()
const migrateDev = MigrateDev.new().parse([])
await expect(migrateDev).resolves.toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(`
expect(captureStdout.getCapturedText().join('')).toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:./dev.db"
Expand All @@ -90,33 +106,38 @@ describe('Baselining', () => {
└─ migration.sql
Your database is now in sync with your schema.
`)
ctx.mocked['console.info'].mockReset()
captureStdout.clearCaptureText()

// Switch to PROD database
process.env.DATABASE_URL = 'file:./prod.db'

// migrate resolve --applied migration_name
const migrationName = fs.list('prisma/migrations')![0]
const migrateResolveProd = MigrateResolve.new().parse(['--applied', migrationName])
await expect(migrateResolveProd).resolves.toMatchInlineSnapshot(`Migration 20201231000000_ marked as applied.`)
await expect(migrateResolveProd).resolves.toMatchInlineSnapshot(``)

expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(`
expect(captureStdout.getCapturedText().join('')).toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "prod.db" at "file:./prod.db"
Migration 20201231000000_ marked as applied.
`)
ctx.mocked['console.info'].mockReset()
captureStdout.clearCaptureText()

// migrate deploy
const migrateDeployProd = MigrateDeploy.new().parse([])
await expect(migrateDeployProd).resolves.toMatchInlineSnapshot(`No pending migrations to apply.`)
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(`
expect(captureStdout.getCapturedText().join('')).toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "prod.db" at "file:./prod.db"
1 migration found in prisma/migrations
`)

expect(ctx.mocked['console.log'].mock.calls).toEqual([])
Expand Down
38 changes: 34 additions & 4 deletions packages/migrate/src/__tests__/DbDrop.test.ts
Expand Up @@ -2,10 +2,25 @@ import { jestConsoleContext, jestContext } from '@prisma/get-platform'
import prompt from 'prompts'

import { DbDrop } from '../commands/DbDrop'
import { CaptureStdout } from '../utils/captureStdout'

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

describe('drop', () => {
const captureStdout = new CaptureStdout()

beforeEach(() => {
captureStdout.startCapture()
})

afterEach(() => {
captureStdout.clearCaptureText()
})

afterAll(() => {
captureStdout.stopCapture()
})

it('requires --preview-feature flag', async () => {
ctx.fixture('empty')

Expand Down Expand Up @@ -59,11 +74,15 @@ describe('drop', () => {

const result = DbDrop.new().parse(['--preview-feature'])
await expect(result).resolves.toContain(`The SQLite database "dev.db" from "file:dev.db" was successfully dropped.`)
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(`
expect(captureStdout.getCapturedText().join('\n')).toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
`)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
})
Expand All @@ -73,10 +92,13 @@ describe('drop', () => {

const result = DbDrop.new().parse(['--preview-feature', '--force'])
await expect(result).resolves.toContain(`The SQLite database "dev.db" from "file:dev.db" was successfully dropped.`)
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(`
expect(captureStdout.getCapturedText().join('\n')).toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
`)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
})
Expand All @@ -85,10 +107,13 @@ describe('drop', () => {
ctx.fixture('reset')
const result = DbDrop.new().parse(['--preview-feature', '-f'])
await expect(result).resolves.toContain(`The SQLite database "dev.db" from "file:dev.db" was successfully dropped.`)
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(`
expect(captureStdout.getCapturedText().join('\n')).toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
`)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
})
Expand All @@ -103,12 +128,17 @@ describe('drop', () => {

const result = DbDrop.new().parse(['--preview-feature'])
await expect(result).rejects.toMatchInlineSnapshot(`process.exit: 130`)
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(`
expect(captureStdout.getCapturedText().join('\n')).toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
Drop cancelled.
`)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
expect(mockExit).toHaveBeenCalledWith(130)
Expand Down
Expand Up @@ -35,6 +35,7 @@ enum Role {
ADMIN
}
`;
exports[`cockroachdb basic introspection (with cockroach provider) --url 2`] = `
Expand Down Expand Up @@ -72,6 +73,7 @@ enum Role {
ADMIN
}
`;
exports[`cockroachdb basic introspection (with cockroachdb provider) 2`] = `
Expand Down Expand Up @@ -109,6 +111,10 @@ enum Role {
ADMIN
}
`;
exports[`cockroachdb basic introspection (with postgresql provider) --url should fail 2`] = ``;
exports[`cockroachdb basic introspection (with postgresql provider) --url should fail 2`] = `
`;
Expand Up @@ -31,4 +31,5 @@ model users {
numberOrString1 Json
}
`;

0 comments on commit 2c09e72

Please sign in to comment.