@friggframework/devtools 2.0.0--canary.454.1aa5d6e.0 → 2.0.0--canary.454.83d7e99.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -23,7 +23,7 @@ const mockErrorMessages = {
|
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
jest.mock('../../../utils/database-validator', () => mockValidator);
|
|
26
|
-
jest.mock('
|
|
26
|
+
jest.mock('@friggframework/core/database/utils/prisma-runner', () => mockRunner);
|
|
27
27
|
jest.mock('../../../utils/error-messages', () => mockErrorMessages);
|
|
28
28
|
jest.mock('dotenv');
|
|
29
29
|
|
|
@@ -12,7 +12,7 @@ const {
|
|
|
12
12
|
runPrismaMigrate,
|
|
13
13
|
runPrismaDbPush,
|
|
14
14
|
getMigrationCommand
|
|
15
|
-
} = require('
|
|
15
|
+
} = require('@friggframework/core/database/utils/prisma-runner');
|
|
16
16
|
const {
|
|
17
17
|
getDatabaseUrlMissingError,
|
|
18
18
|
getDatabaseTypeNotConfiguredError,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@friggframework/devtools",
|
|
3
3
|
"prettier": "@friggframework/prettier-config",
|
|
4
|
-
"version": "2.0.0--canary.454.
|
|
4
|
+
"version": "2.0.0--canary.454.83d7e99.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@aws-sdk/client-ec2": "^3.835.0",
|
|
7
7
|
"@aws-sdk/client-kms": "^3.835.0",
|
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
"@babel/eslint-parser": "^7.18.9",
|
|
12
12
|
"@babel/parser": "^7.25.3",
|
|
13
13
|
"@babel/traverse": "^7.25.3",
|
|
14
|
-
"@friggframework/schemas": "2.0.0--canary.454.
|
|
15
|
-
"@friggframework/test": "2.0.0--canary.454.
|
|
14
|
+
"@friggframework/schemas": "2.0.0--canary.454.83d7e99.0",
|
|
15
|
+
"@friggframework/test": "2.0.0--canary.454.83d7e99.0",
|
|
16
16
|
"@hapi/boom": "^10.0.1",
|
|
17
17
|
"@inquirer/prompts": "^5.3.8",
|
|
18
18
|
"axios": "^1.7.2",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"serverless-http": "^2.7.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@friggframework/eslint-config": "2.0.0--canary.454.
|
|
38
|
-
"@friggframework/prettier-config": "2.0.0--canary.454.
|
|
37
|
+
"@friggframework/eslint-config": "2.0.0--canary.454.83d7e99.0",
|
|
38
|
+
"@friggframework/prettier-config": "2.0.0--canary.454.83d7e99.0",
|
|
39
39
|
"aws-sdk-client-mock": "^4.1.0",
|
|
40
40
|
"aws-sdk-client-mock-jest": "^4.1.0",
|
|
41
41
|
"jest": "^30.1.3",
|
|
@@ -70,5 +70,5 @@
|
|
|
70
70
|
"publishConfig": {
|
|
71
71
|
"access": "public"
|
|
72
72
|
},
|
|
73
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "83d7e994447c49b992fa56820b205594683e463f"
|
|
74
74
|
}
|
|
@@ -1,486 +0,0 @@
|
|
|
1
|
-
// Mock dependencies BEFORE requiring modules
|
|
2
|
-
jest.mock('child_process', () => ({
|
|
3
|
-
execSync: jest.fn(),
|
|
4
|
-
spawn: jest.fn()
|
|
5
|
-
}));
|
|
6
|
-
jest.mock('fs', () => ({
|
|
7
|
-
existsSync: jest.fn(),
|
|
8
|
-
readFileSync: jest.fn(),
|
|
9
|
-
writeFileSync: jest.fn()
|
|
10
|
-
}));
|
|
11
|
-
|
|
12
|
-
const { execSync, spawn } = require('child_process');
|
|
13
|
-
const fs = require('fs');
|
|
14
|
-
const {
|
|
15
|
-
getPrismaSchemaPath,
|
|
16
|
-
runPrismaGenerate,
|
|
17
|
-
checkDatabaseState,
|
|
18
|
-
runPrismaMigrate,
|
|
19
|
-
runPrismaDbPush,
|
|
20
|
-
getMigrationCommand
|
|
21
|
-
} = require('../../../utils/prisma-runner');
|
|
22
|
-
|
|
23
|
-
describe('Prisma Runner Utility', () => {
|
|
24
|
-
beforeEach(() => {
|
|
25
|
-
jest.clearAllMocks();
|
|
26
|
-
delete process.env.STAGE;
|
|
27
|
-
delete process.env.PRISMA_HIDE_UPDATE_MESSAGE;
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
afterEach(() => {
|
|
31
|
-
delete process.env.STAGE;
|
|
32
|
-
delete process.env.PRISMA_HIDE_UPDATE_MESSAGE;
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
describe('getPrismaSchemaPath()', () => {
|
|
36
|
-
it('should return correct path for MongoDB', () => {
|
|
37
|
-
fs.existsSync.mockReturnValue(true);
|
|
38
|
-
|
|
39
|
-
const path = getPrismaSchemaPath('mongodb');
|
|
40
|
-
|
|
41
|
-
expect(path).toContain('prisma-mongodb');
|
|
42
|
-
expect(path).toContain('schema.prisma');
|
|
43
|
-
expect(path).toContain('@friggframework/core');
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should return correct path for PostgreSQL', () => {
|
|
47
|
-
fs.existsSync.mockReturnValue(true);
|
|
48
|
-
|
|
49
|
-
const path = getPrismaSchemaPath('postgresql');
|
|
50
|
-
|
|
51
|
-
expect(path).toContain('prisma-postgresql');
|
|
52
|
-
expect(path).toContain('schema.prisma');
|
|
53
|
-
expect(path).toContain('@friggframework/core');
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('should throw error when schema file does not exist', () => {
|
|
57
|
-
fs.existsSync.mockReturnValue(false);
|
|
58
|
-
|
|
59
|
-
expect(() => getPrismaSchemaPath('mongodb')).toThrow('Prisma schema not found');
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('should include helpful error message when schema missing', () => {
|
|
63
|
-
fs.existsSync.mockReturnValue(false);
|
|
64
|
-
|
|
65
|
-
expect(() => getPrismaSchemaPath('mongodb')).toThrow('@friggframework/core');
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('should use process.cwd() for base path', () => {
|
|
69
|
-
fs.existsSync.mockReturnValue(true);
|
|
70
|
-
const originalCwd = process.cwd();
|
|
71
|
-
|
|
72
|
-
const path = getPrismaSchemaPath('mongodb');
|
|
73
|
-
|
|
74
|
-
expect(path).toContain(originalCwd);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should accept custom project root', () => {
|
|
78
|
-
fs.existsSync.mockReturnValue(true);
|
|
79
|
-
const customRoot = '/custom/project';
|
|
80
|
-
|
|
81
|
-
const path = getPrismaSchemaPath('mongodb', customRoot);
|
|
82
|
-
|
|
83
|
-
expect(path).toContain(customRoot);
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
describe('runPrismaGenerate()', () => {
|
|
88
|
-
beforeEach(() => {
|
|
89
|
-
fs.existsSync.mockReturnValue(true);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('should execute prisma generate successfully', async () => {
|
|
93
|
-
execSync.mockReturnValue('Generated successfully');
|
|
94
|
-
|
|
95
|
-
const result = await runPrismaGenerate('mongodb');
|
|
96
|
-
|
|
97
|
-
expect(result.success).toBe(true);
|
|
98
|
-
expect(execSync).toHaveBeenCalled();
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('should use correct schema path for MongoDB', async () => {
|
|
102
|
-
execSync.mockReturnValue('');
|
|
103
|
-
|
|
104
|
-
await runPrismaGenerate('mongodb');
|
|
105
|
-
|
|
106
|
-
const call = execSync.mock.calls[0][0];
|
|
107
|
-
expect(call).toContain('prisma generate');
|
|
108
|
-
expect(call).toContain('--schema');
|
|
109
|
-
expect(call).toContain('prisma-mongodb');
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it('should use correct schema path for PostgreSQL', async () => {
|
|
113
|
-
execSync.mockReturnValue('');
|
|
114
|
-
|
|
115
|
-
await runPrismaGenerate('postgresql');
|
|
116
|
-
|
|
117
|
-
const call = execSync.mock.calls[0][0];
|
|
118
|
-
expect(call).toContain('prisma-postgresql');
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it('should suppress telemetry when verbose false', async () => {
|
|
122
|
-
execSync.mockReturnValue('');
|
|
123
|
-
|
|
124
|
-
await runPrismaGenerate('mongodb', false);
|
|
125
|
-
|
|
126
|
-
const options = execSync.mock.calls[0][1];
|
|
127
|
-
expect(options.env.PRISMA_HIDE_UPDATE_MESSAGE).toBe('1');
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('should show output when verbose true', async () => {
|
|
131
|
-
execSync.mockReturnValue('Generated successfully');
|
|
132
|
-
|
|
133
|
-
await runPrismaGenerate('mongodb', true);
|
|
134
|
-
|
|
135
|
-
const options = execSync.mock.calls[0][1];
|
|
136
|
-
expect(options.stdio).toBe('inherit');
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it('should hide output when verbose false', async () => {
|
|
140
|
-
execSync.mockReturnValue('');
|
|
141
|
-
|
|
142
|
-
await runPrismaGenerate('mongodb', false);
|
|
143
|
-
|
|
144
|
-
const options = execSync.mock.calls[0][1];
|
|
145
|
-
expect(options.stdio).toBe('pipe');
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
it('should handle generation failures with error details', async () => {
|
|
149
|
-
const error = new Error('Generation failed');
|
|
150
|
-
error.stdout = 'Schema validation error';
|
|
151
|
-
execSync.mockImplementation(() => {
|
|
152
|
-
throw error;
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
const result = await runPrismaGenerate('mongodb');
|
|
156
|
-
|
|
157
|
-
expect(result.success).toBe(false);
|
|
158
|
-
expect(result.error).toContain('Generation failed');
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it('should include stdout in error output', async () => {
|
|
162
|
-
const error = new Error('Failed');
|
|
163
|
-
error.stdout = Buffer.from('Detailed error info');
|
|
164
|
-
execSync.mockImplementation(() => {
|
|
165
|
-
throw error;
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
const result = await runPrismaGenerate('mongodb');
|
|
169
|
-
|
|
170
|
-
expect(result.output).toContain('Detailed error info');
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
it('should handle schema syntax errors', async () => {
|
|
174
|
-
execSync.mockImplementation(() => {
|
|
175
|
-
throw new Error('Schema parsing failed');
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
const result = await runPrismaGenerate('mongodb');
|
|
179
|
-
|
|
180
|
-
expect(result.success).toBe(false);
|
|
181
|
-
expect(result.error).toBeDefined();
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
describe('checkDatabaseState()', () => {
|
|
186
|
-
beforeEach(() => {
|
|
187
|
-
fs.existsSync.mockReturnValue(true);
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
it('should return upToDate: true when migrations current (PostgreSQL)', async () => {
|
|
191
|
-
execSync.mockReturnValue('Database schema is up to date');
|
|
192
|
-
|
|
193
|
-
const result = await checkDatabaseState('postgresql');
|
|
194
|
-
|
|
195
|
-
expect(result.upToDate).toBe(true);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
it('should return upToDate: true for MongoDB (N/A)', async () => {
|
|
199
|
-
const result = await checkDatabaseState('mongodb');
|
|
200
|
-
|
|
201
|
-
expect(result.upToDate).toBe(true);
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
it('should return pendingMigrations count when migrations pending', async () => {
|
|
205
|
-
execSync.mockReturnValue('3 migrations have not been applied');
|
|
206
|
-
|
|
207
|
-
const result = await checkDatabaseState('postgresql');
|
|
208
|
-
|
|
209
|
-
expect(result.upToDate).toBe(false);
|
|
210
|
-
expect(result.pendingMigrations).toBe(3);
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
it('should handle uninitialized database', async () => {
|
|
214
|
-
execSync.mockImplementation(() => {
|
|
215
|
-
throw new Error('No migrations found');
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
const result = await checkDatabaseState('postgresql');
|
|
219
|
-
|
|
220
|
-
expect(result.upToDate).toBe(false);
|
|
221
|
-
expect(result.error).toBeDefined();
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it('should handle migrate status command errors', async () => {
|
|
225
|
-
execSync.mockImplementation(() => {
|
|
226
|
-
throw new Error('Migration status failed');
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
const result = await checkDatabaseState('postgresql');
|
|
230
|
-
|
|
231
|
-
expect(result.upToDate).toBe(false);
|
|
232
|
-
expect(result.error).toContain('Migration status failed');
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
it('should not run migrate status for MongoDB', async () => {
|
|
236
|
-
await checkDatabaseState('mongodb');
|
|
237
|
-
|
|
238
|
-
expect(execSync).not.toHaveBeenCalled();
|
|
239
|
-
});
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
describe('runPrismaMigrate()', () => {
|
|
243
|
-
let mockChildProcess;
|
|
244
|
-
|
|
245
|
-
beforeEach(() => {
|
|
246
|
-
fs.existsSync.mockReturnValue(true);
|
|
247
|
-
mockChildProcess = {
|
|
248
|
-
on: jest.fn((event, callback) => {
|
|
249
|
-
if (event === 'close') {
|
|
250
|
-
callback(0);
|
|
251
|
-
}
|
|
252
|
-
}),
|
|
253
|
-
stdout: { on: jest.fn() },
|
|
254
|
-
stderr: { on: jest.fn() }
|
|
255
|
-
};
|
|
256
|
-
spawn.mockReturnValue(mockChildProcess);
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
it('should run migrate dev successfully', async () => {
|
|
260
|
-
const result = await runPrismaMigrate('dev');
|
|
261
|
-
|
|
262
|
-
expect(result.success).toBe(true);
|
|
263
|
-
expect(spawn).toHaveBeenCalled();
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
it('should run migrate deploy successfully', async () => {
|
|
267
|
-
const result = await runPrismaMigrate('deploy');
|
|
268
|
-
|
|
269
|
-
expect(result.success).toBe(true);
|
|
270
|
-
expect(spawn).toHaveBeenCalled();
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
it('should use correct command for dev mode', async () => {
|
|
274
|
-
await runPrismaMigrate('dev');
|
|
275
|
-
|
|
276
|
-
const args = spawn.mock.calls[0][1];
|
|
277
|
-
expect(args).toContain('migrate');
|
|
278
|
-
expect(args).toContain('dev');
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
it('should use correct command for deploy mode', async () => {
|
|
282
|
-
await runPrismaMigrate('deploy');
|
|
283
|
-
|
|
284
|
-
const args = spawn.mock.calls[0][1];
|
|
285
|
-
expect(args).toContain('migrate');
|
|
286
|
-
expect(args).toContain('deploy');
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
it('should handle migration failures with error', async () => {
|
|
290
|
-
mockChildProcess.on.mockImplementation((event, callback) => {
|
|
291
|
-
if (event === 'close') {
|
|
292
|
-
callback(1); // Exit code 1
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
const result = await runPrismaMigrate('dev');
|
|
297
|
-
|
|
298
|
-
expect(result.success).toBe(false);
|
|
299
|
-
expect(result.error).toContain('exited with code 1');
|
|
300
|
-
});
|
|
301
|
-
|
|
302
|
-
it('should respect verbose flag', async () => {
|
|
303
|
-
await runPrismaMigrate('dev', true);
|
|
304
|
-
|
|
305
|
-
const options = spawn.mock.calls[0][2];
|
|
306
|
-
expect(options.stdio).toBe('inherit');
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
it('should handle process spawn errors', async () => {
|
|
310
|
-
mockChildProcess.on.mockImplementation((event, callback) => {
|
|
311
|
-
if (event === 'error') {
|
|
312
|
-
callback(new Error('Spawn failed'));
|
|
313
|
-
}
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
const result = await runPrismaMigrate('dev');
|
|
317
|
-
|
|
318
|
-
expect(result.success).toBe(false);
|
|
319
|
-
});
|
|
320
|
-
|
|
321
|
-
it('should hide telemetry messages', async () => {
|
|
322
|
-
await runPrismaMigrate('dev');
|
|
323
|
-
|
|
324
|
-
const options = spawn.mock.calls[0][2];
|
|
325
|
-
expect(options.env.PRISMA_HIDE_UPDATE_MESSAGE).toBe('1');
|
|
326
|
-
});
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
describe('runPrismaDbPush()', () => {
|
|
330
|
-
let mockChildProcess;
|
|
331
|
-
|
|
332
|
-
beforeEach(() => {
|
|
333
|
-
fs.existsSync.mockReturnValue(true);
|
|
334
|
-
mockChildProcess = {
|
|
335
|
-
on: jest.fn((event, callback) => {
|
|
336
|
-
if (event === 'close') {
|
|
337
|
-
callback(0);
|
|
338
|
-
}
|
|
339
|
-
}),
|
|
340
|
-
stdout: { on: jest.fn() },
|
|
341
|
-
stderr: { on: jest.fn() }
|
|
342
|
-
};
|
|
343
|
-
spawn.mockReturnValue(mockChildProcess);
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
it('should push schema successfully for MongoDB', async () => {
|
|
347
|
-
const result = await runPrismaDbPush();
|
|
348
|
-
|
|
349
|
-
expect(result.success).toBe(true);
|
|
350
|
-
expect(spawn).toHaveBeenCalled();
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
it('should use --skip-generate flag', async () => {
|
|
354
|
-
await runPrismaDbPush();
|
|
355
|
-
|
|
356
|
-
const args = spawn.mock.calls[0][1];
|
|
357
|
-
expect(args).toContain('--skip-generate');
|
|
358
|
-
});
|
|
359
|
-
|
|
360
|
-
it('should use db push command', async () => {
|
|
361
|
-
await runPrismaDbPush();
|
|
362
|
-
|
|
363
|
-
const args = spawn.mock.calls[0][1];
|
|
364
|
-
expect(args).toContain('db');
|
|
365
|
-
expect(args).toContain('push');
|
|
366
|
-
});
|
|
367
|
-
|
|
368
|
-
it('should handle push failures with error', async () => {
|
|
369
|
-
mockChildProcess.on.mockImplementation((event, callback) => {
|
|
370
|
-
if (event === 'close') {
|
|
371
|
-
callback(1);
|
|
372
|
-
}
|
|
373
|
-
});
|
|
374
|
-
|
|
375
|
-
const result = await runPrismaDbPush();
|
|
376
|
-
|
|
377
|
-
expect(result.success).toBe(false);
|
|
378
|
-
expect(result.error).toContain('exited with code 1');
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
it('should respect verbose flag', async () => {
|
|
382
|
-
await runPrismaDbPush(true);
|
|
383
|
-
|
|
384
|
-
const options = spawn.mock.calls[0][2];
|
|
385
|
-
expect(options.stdio).toBe('inherit');
|
|
386
|
-
});
|
|
387
|
-
|
|
388
|
-
it('should use interactive mode (stdio: inherit)', async () => {
|
|
389
|
-
await runPrismaDbPush();
|
|
390
|
-
|
|
391
|
-
const options = spawn.mock.calls[0][2];
|
|
392
|
-
expect(options.stdio).toBe('inherit');
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
it('should handle schema validation errors', async () => {
|
|
396
|
-
mockChildProcess.on.mockImplementation((event, callback) => {
|
|
397
|
-
if (event === 'close') {
|
|
398
|
-
callback(1);
|
|
399
|
-
}
|
|
400
|
-
});
|
|
401
|
-
|
|
402
|
-
const result = await runPrismaDbPush();
|
|
403
|
-
|
|
404
|
-
expect(result.success).toBe(false);
|
|
405
|
-
});
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
describe('getMigrationCommand()', () => {
|
|
409
|
-
it('should return dev for development stage', () => {
|
|
410
|
-
const command = getMigrationCommand('development');
|
|
411
|
-
|
|
412
|
-
expect(command).toBe('dev');
|
|
413
|
-
});
|
|
414
|
-
|
|
415
|
-
it('should return dev for dev stage', () => {
|
|
416
|
-
const command = getMigrationCommand('dev');
|
|
417
|
-
|
|
418
|
-
expect(command).toBe('dev');
|
|
419
|
-
});
|
|
420
|
-
|
|
421
|
-
it('should return dev for local stage', () => {
|
|
422
|
-
const command = getMigrationCommand('local');
|
|
423
|
-
|
|
424
|
-
expect(command).toBe('dev');
|
|
425
|
-
});
|
|
426
|
-
|
|
427
|
-
it('should return dev for test stage', () => {
|
|
428
|
-
const command = getMigrationCommand('test');
|
|
429
|
-
|
|
430
|
-
expect(command).toBe('dev');
|
|
431
|
-
});
|
|
432
|
-
|
|
433
|
-
it('should return deploy for production stage', () => {
|
|
434
|
-
const command = getMigrationCommand('production');
|
|
435
|
-
|
|
436
|
-
expect(command).toBe('deploy');
|
|
437
|
-
});
|
|
438
|
-
|
|
439
|
-
it('should return deploy for prod stage', () => {
|
|
440
|
-
const command = getMigrationCommand('prod');
|
|
441
|
-
|
|
442
|
-
expect(command).toBe('deploy');
|
|
443
|
-
});
|
|
444
|
-
|
|
445
|
-
it('should return deploy for staging stage', () => {
|
|
446
|
-
const command = getMigrationCommand('staging');
|
|
447
|
-
|
|
448
|
-
expect(command).toBe('deploy');
|
|
449
|
-
});
|
|
450
|
-
|
|
451
|
-
it('should default to dev when stage undefined', () => {
|
|
452
|
-
const command = getMigrationCommand();
|
|
453
|
-
|
|
454
|
-
expect(command).toBe('dev');
|
|
455
|
-
});
|
|
456
|
-
|
|
457
|
-
it('should read from STAGE environment variable when no argument', () => {
|
|
458
|
-
process.env.STAGE = 'production';
|
|
459
|
-
|
|
460
|
-
const command = getMigrationCommand();
|
|
461
|
-
|
|
462
|
-
expect(command).toBe('deploy');
|
|
463
|
-
});
|
|
464
|
-
|
|
465
|
-
it('should prioritize argument over STAGE env var', () => {
|
|
466
|
-
process.env.STAGE = 'production';
|
|
467
|
-
|
|
468
|
-
const command = getMigrationCommand('development');
|
|
469
|
-
|
|
470
|
-
expect(command).toBe('dev');
|
|
471
|
-
});
|
|
472
|
-
|
|
473
|
-
it('should handle case-insensitive stage names', () => {
|
|
474
|
-
expect(getMigrationCommand('DEVELOPMENT')).toBe('dev');
|
|
475
|
-
expect(getMigrationCommand('PRODUCTION')).toBe('deploy');
|
|
476
|
-
expect(getMigrationCommand('Dev')).toBe('dev');
|
|
477
|
-
expect(getMigrationCommand('Prod')).toBe('deploy');
|
|
478
|
-
});
|
|
479
|
-
|
|
480
|
-
it('should return deploy for unknown stages', () => {
|
|
481
|
-
const command = getMigrationCommand('unknown-stage');
|
|
482
|
-
|
|
483
|
-
expect(command).toBe('deploy');
|
|
484
|
-
});
|
|
485
|
-
});
|
|
486
|
-
});
|
|
@@ -1,313 +0,0 @@
|
|
|
1
|
-
const { execSync, spawn } = require('child_process');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const chalk = require('chalk');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Prisma Command Runner Utility
|
|
8
|
-
* Handles execution of Prisma CLI commands for database setup
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Gets the path to the Prisma schema file for the database type
|
|
13
|
-
* @param {'mongodb'|'postgresql'} dbType - Database type
|
|
14
|
-
* @param {string} projectRoot - Project root directory
|
|
15
|
-
* @returns {string} Absolute path to schema file
|
|
16
|
-
* @throws {Error} If schema file doesn't exist
|
|
17
|
-
*/
|
|
18
|
-
function getPrismaSchemaPath(dbType, projectRoot = process.cwd()) {
|
|
19
|
-
// Try multiple locations for the schema file
|
|
20
|
-
// Priority order:
|
|
21
|
-
// 1. Local node_modules (where @friggframework/core is installed - production scenario)
|
|
22
|
-
// 2. Parent node_modules (workspace/monorepo setup)
|
|
23
|
-
const possiblePaths = [
|
|
24
|
-
// Check where Frigg is installed via npm (production scenario)
|
|
25
|
-
path.join(projectRoot, 'node_modules', '@friggframework', 'core', `prisma-${dbType}`, 'schema.prisma'),
|
|
26
|
-
path.join(projectRoot, '..', 'node_modules', '@friggframework', 'core', `prisma-${dbType}`, 'schema.prisma')
|
|
27
|
-
];
|
|
28
|
-
|
|
29
|
-
for (const schemaPath of possiblePaths) {
|
|
30
|
-
if (fs.existsSync(schemaPath)) {
|
|
31
|
-
return schemaPath;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// If not found in any location, throw error
|
|
36
|
-
throw new Error(
|
|
37
|
-
`Prisma schema not found at:\n${possiblePaths.join('\n')}\n\n` +
|
|
38
|
-
'Ensure @friggframework/core is installed.'
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Runs prisma generate for the specified database type
|
|
44
|
-
* @param {'mongodb'|'postgresql'} dbType - Database type
|
|
45
|
-
* @param {boolean} verbose - Enable verbose output
|
|
46
|
-
* @returns {Promise<Object>} { success: boolean, output?: string, error?: string }
|
|
47
|
-
*/
|
|
48
|
-
async function runPrismaGenerate(dbType, verbose = false) {
|
|
49
|
-
try {
|
|
50
|
-
const schemaPath = getPrismaSchemaPath(dbType);
|
|
51
|
-
|
|
52
|
-
if (verbose) {
|
|
53
|
-
console.log(chalk.gray(`Running: npx prisma generate --schema=${schemaPath}`));
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const output = execSync(
|
|
57
|
-
`npx prisma generate --schema=${schemaPath}`,
|
|
58
|
-
{
|
|
59
|
-
encoding: 'utf8',
|
|
60
|
-
stdio: verbose ? 'inherit' : 'pipe',
|
|
61
|
-
env: {
|
|
62
|
-
...process.env,
|
|
63
|
-
// Suppress Prisma telemetry prompts
|
|
64
|
-
PRISMA_HIDE_UPDATE_MESSAGE: '1'
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
return {
|
|
70
|
-
success: true,
|
|
71
|
-
output: verbose ? 'Generated successfully' : output
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
} catch (error) {
|
|
75
|
-
return {
|
|
76
|
-
success: false,
|
|
77
|
-
error: error.message,
|
|
78
|
-
output: error.stdout?.toString() || error.stderr?.toString()
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Checks database migration status
|
|
85
|
-
* @param {'mongodb'|'postgresql'} dbType - Database type
|
|
86
|
-
* @returns {Promise<Object>} { upToDate: boolean, pendingMigrations?: number, error?: string }
|
|
87
|
-
*/
|
|
88
|
-
async function checkDatabaseState(dbType) {
|
|
89
|
-
try {
|
|
90
|
-
// Only applicable for PostgreSQL (MongoDB uses db push)
|
|
91
|
-
if (dbType !== 'postgresql') {
|
|
92
|
-
return { upToDate: true };
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const schemaPath = getPrismaSchemaPath(dbType);
|
|
96
|
-
|
|
97
|
-
const output = execSync(
|
|
98
|
-
`npx prisma migrate status --schema=${schemaPath}`,
|
|
99
|
-
{
|
|
100
|
-
encoding: 'utf8',
|
|
101
|
-
stdio: 'pipe',
|
|
102
|
-
env: {
|
|
103
|
-
...process.env,
|
|
104
|
-
PRISMA_HIDE_UPDATE_MESSAGE: '1'
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
if (output.includes('Database schema is up to date')) {
|
|
110
|
-
return { upToDate: true };
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// Parse pending migrations count
|
|
114
|
-
const pendingMatch = output.match(/(\d+) migration/);
|
|
115
|
-
const pendingMigrations = pendingMatch ? parseInt(pendingMatch[1]) : 0;
|
|
116
|
-
|
|
117
|
-
return {
|
|
118
|
-
upToDate: false,
|
|
119
|
-
pendingMigrations
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
} catch (error) {
|
|
123
|
-
// If migrate status fails, database might not be initialized
|
|
124
|
-
return {
|
|
125
|
-
upToDate: false,
|
|
126
|
-
error: error.message
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Runs Prisma migrate for PostgreSQL
|
|
133
|
-
* @param {'dev'|'deploy'} command - Migration command (dev or deploy)
|
|
134
|
-
* @param {boolean} verbose - Enable verbose output
|
|
135
|
-
* @returns {Promise<Object>} { success: boolean, output?: string, error?: string }
|
|
136
|
-
*/
|
|
137
|
-
async function runPrismaMigrate(command = 'dev', verbose = false) {
|
|
138
|
-
return new Promise((resolve) => {
|
|
139
|
-
try {
|
|
140
|
-
const schemaPath = getPrismaSchemaPath('postgresql');
|
|
141
|
-
|
|
142
|
-
const args = [
|
|
143
|
-
'prisma',
|
|
144
|
-
'migrate',
|
|
145
|
-
command,
|
|
146
|
-
'--schema',
|
|
147
|
-
schemaPath
|
|
148
|
-
];
|
|
149
|
-
|
|
150
|
-
if (verbose) {
|
|
151
|
-
console.log(chalk.gray(`Running: npx ${args.join(' ')}`));
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const proc = spawn('npx', args, {
|
|
155
|
-
stdio: 'inherit',
|
|
156
|
-
env: {
|
|
157
|
-
...process.env,
|
|
158
|
-
PRISMA_HIDE_UPDATE_MESSAGE: '1'
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
proc.on('error', (error) => {
|
|
163
|
-
resolve({
|
|
164
|
-
success: false,
|
|
165
|
-
error: error.message
|
|
166
|
-
});
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
proc.on('close', (code) => {
|
|
170
|
-
if (code === 0) {
|
|
171
|
-
resolve({
|
|
172
|
-
success: true,
|
|
173
|
-
output: 'Migration completed successfully'
|
|
174
|
-
});
|
|
175
|
-
} else {
|
|
176
|
-
resolve({
|
|
177
|
-
success: false,
|
|
178
|
-
error: `Migration process exited with code ${code}`
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
} catch (error) {
|
|
184
|
-
resolve({
|
|
185
|
-
success: false,
|
|
186
|
-
error: error.message
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Runs Prisma db push for MongoDB
|
|
194
|
-
* @param {boolean} verbose - Enable verbose output
|
|
195
|
-
* @param {boolean} nonInteractive - Run in non-interactive mode (accepts data loss, for Lambda/CI)
|
|
196
|
-
* @returns {Promise<Object>} { success: boolean, output?: string, error?: string }
|
|
197
|
-
*/
|
|
198
|
-
async function runPrismaDbPush(verbose = false, nonInteractive = false) {
|
|
199
|
-
return new Promise((resolve) => {
|
|
200
|
-
try {
|
|
201
|
-
const schemaPath = getPrismaSchemaPath('mongodb');
|
|
202
|
-
|
|
203
|
-
const args = [
|
|
204
|
-
'prisma',
|
|
205
|
-
'db',
|
|
206
|
-
'push',
|
|
207
|
-
'--schema',
|
|
208
|
-
schemaPath,
|
|
209
|
-
'--skip-generate' // We generate separately
|
|
210
|
-
];
|
|
211
|
-
|
|
212
|
-
// Add non-interactive flag for Lambda/CI environments
|
|
213
|
-
if (nonInteractive) {
|
|
214
|
-
args.push('--accept-data-loss');
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (verbose) {
|
|
218
|
-
console.log(chalk.gray(`Running: npx ${args.join(' ')}`));
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if (nonInteractive) {
|
|
222
|
-
console.log(chalk.yellow('⚠️ Non-interactive mode: Data loss will be automatically accepted'));
|
|
223
|
-
} else {
|
|
224
|
-
console.log(chalk.yellow('⚠️ Interactive mode: You may be prompted if schema changes cause data loss'));
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const proc = spawn('npx', args, {
|
|
228
|
-
stdio: nonInteractive ? 'pipe' : 'inherit', // Use pipe for non-interactive to capture output
|
|
229
|
-
env: {
|
|
230
|
-
...process.env,
|
|
231
|
-
PRISMA_HIDE_UPDATE_MESSAGE: '1'
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
let stdout = '';
|
|
236
|
-
let stderr = '';
|
|
237
|
-
|
|
238
|
-
// Capture output in non-interactive mode
|
|
239
|
-
if (nonInteractive) {
|
|
240
|
-
if (proc.stdout) {
|
|
241
|
-
proc.stdout.on('data', (data) => {
|
|
242
|
-
stdout += data.toString();
|
|
243
|
-
if (verbose) {
|
|
244
|
-
process.stdout.write(data);
|
|
245
|
-
}
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
if (proc.stderr) {
|
|
249
|
-
proc.stderr.on('data', (data) => {
|
|
250
|
-
stderr += data.toString();
|
|
251
|
-
if (verbose) {
|
|
252
|
-
process.stderr.write(data);
|
|
253
|
-
}
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
proc.on('error', (error) => {
|
|
259
|
-
resolve({
|
|
260
|
-
success: false,
|
|
261
|
-
error: error.message
|
|
262
|
-
});
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
proc.on('close', (code) => {
|
|
266
|
-
if (code === 0) {
|
|
267
|
-
resolve({
|
|
268
|
-
success: true,
|
|
269
|
-
output: nonInteractive ? stdout || 'Database push completed successfully' : 'Database push completed successfully'
|
|
270
|
-
});
|
|
271
|
-
} else {
|
|
272
|
-
resolve({
|
|
273
|
-
success: false,
|
|
274
|
-
error: `Database push process exited with code ${code}`,
|
|
275
|
-
output: stderr || stdout
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
} catch (error) {
|
|
281
|
-
resolve({
|
|
282
|
-
success: false,
|
|
283
|
-
error: error.message
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Determines migration command based on STAGE environment variable
|
|
291
|
-
* @param {string} stage - Stage from CLI option or environment
|
|
292
|
-
* @returns {'dev'|'deploy'}
|
|
293
|
-
*/
|
|
294
|
-
function getMigrationCommand(stage) {
|
|
295
|
-
const normalizedStage = (stage || process.env.STAGE || 'development').toLowerCase();
|
|
296
|
-
|
|
297
|
-
const developmentStages = ['dev', 'local', 'test', 'development'];
|
|
298
|
-
|
|
299
|
-
if (developmentStages.includes(normalizedStage)) {
|
|
300
|
-
return 'dev';
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
return 'deploy';
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
module.exports = {
|
|
307
|
-
getPrismaSchemaPath,
|
|
308
|
-
runPrismaGenerate,
|
|
309
|
-
checkDatabaseState,
|
|
310
|
-
runPrismaMigrate,
|
|
311
|
-
runPrismaDbPush,
|
|
312
|
-
getMigrationCommand
|
|
313
|
-
};
|