/
Baseline.test.ts
146 lines (107 loc) · 5.22 KB
/
Baseline.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { jestConsoleContext, jestContext } from '@prisma/get-platform'
import fs from 'fs-jetpack'
import prompt from 'prompts'
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'
// Disable generate
process.env.PRISMA_MIGRATE_SKIP_GENERATE = '1'
// Covered in docs: https://www.prisma.io/docs/guides/database/developing-with-prisma-migrate/add-prisma-migrate-to-a-project
// We have a dev and prod database
// And we want to baseline the production database with a baseline migration
// For that, we use `db pull`, `migrate dev`, `migrate resolve` and `migrate deploy` commands
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 }
})
it('SQLite: should succeed', async () => {
ctx.fixture('baseline-sqlite')
fs.remove('prisma/migrations')
fs.copy('prisma/dev.db', 'prisma/prod.db')
// Start with the dev database
process.env.DATABASE_URL = 'file:./dev.db'
// db pull
const dbPull = DbPull.new().parse([])
await expect(dbPull).resolves.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.
`)
captureStdout.clearCaptureText()
// migrate dev --create-only
prompt.inject(['y'])
const migrateDevCreateOnly = MigrateDev.new().parse(['--create-only'])
await expect(migrateDevCreateOnly).resolves.toMatchInlineSnapshot(`
Prisma Migrate created the following migration without applying it 20201231000000_
You can now edit it and apply it by running prisma migrate dev.
`)
expect(captureStdout.getCapturedText().join('')).toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:./dev.db"
Drift detected: Your database schema is not in sync with your migration history.
The following is a summary of the differences between the expected database schema given your migrations files, and the actual schema of the database.
It should be understood as the set of changes to get from the expected schema to the actual schema.
If you are running this the first time on an existing database, please make sure to read this documentation page:
https://www.prisma.io/docs/guides/database/developing-with-prisma-migrate/troubleshooting-development
[+] Added tables
- Blog
We need to reset the SQLite database "dev.db" at "file:./dev.db"
Do you want to continue? All data will be lost.
`)
captureStdout.clearCaptureText()
// migrate dev
captureStdout.startCapture()
const migrateDev = MigrateDev.new().parse([])
await expect(migrateDev).resolves.toMatchInlineSnapshot(``)
expect(captureStdout.getCapturedText().join('')).toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:./dev.db"
Applying migration \`20201231000000_\`
The following migration(s) have been applied:
migrations/
└─ 20201231000000_/
└─ migration.sql
Your database is now in sync with your schema.
`)
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(``)
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.
`)
captureStdout.clearCaptureText()
// migrate deploy
const migrateDeployProd = MigrateDeploy.new().parse([])
await expect(migrateDeployProd).resolves.toMatchInlineSnapshot(`No pending migrations to apply.`)
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([])
expect(ctx.mocked['console.error'].mock.calls).toEqual([])
})
})