@friggframework/core 2.0.0--canary.454.e2a280d.0 → 2.0.0--canary.459.51231dd.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.
- package/README.md +0 -28
- package/database/prisma.js +2 -2
- package/docs/PROCESS_MANAGEMENT_QUEUE_SPEC.md +517 -0
- package/handlers/routers/user.js +3 -1
- package/integrations/repositories/process-repository-factory.js +46 -0
- package/integrations/repositories/process-repository-interface.js +90 -0
- package/integrations/repositories/process-repository-mongo.js +190 -0
- package/integrations/repositories/process-repository-postgres.js +190 -0
- package/integrations/use-cases/create-process.js +128 -0
- package/integrations/use-cases/create-process.test.js +178 -0
- package/integrations/use-cases/get-process.js +87 -0
- package/integrations/use-cases/get-process.test.js +190 -0
- package/integrations/use-cases/index.js +8 -0
- package/integrations/use-cases/update-process-metrics.js +165 -0
- package/integrations/use-cases/update-process-metrics.test.js +308 -0
- package/integrations/use-cases/update-process-state.js +119 -0
- package/integrations/use-cases/update-process-state.test.js +256 -0
- package/modules/module.js +1 -1
- package/modules/use-cases/process-authorization-callback.js +2 -1
- package/package.json +66 -79
- package/prisma-mongodb/schema.prisma +64 -25
- package/prisma-postgresql/migrations/20251010000000_remove_unused_entity_reference_map/migration.sql +3 -0
- package/prisma-postgresql/schema.prisma +14 -19
- package/database/use-cases/run-database-migration-use-case.js +0 -137
- package/database/use-cases/run-database-migration-use-case.test.js +0 -310
- package/database/utils/prisma-runner.js +0 -313
- package/database/utils/prisma-runner.test.js +0 -486
- package/handlers/workers/db-migration.js +0 -208
- package/handlers/workers/db-migration.test.js +0 -437
|
@@ -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('./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,208 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Database Migration Lambda Handler
|
|
3
|
-
*
|
|
4
|
-
* Lambda function that runs Prisma database migrations from within the VPC,
|
|
5
|
-
* enabling CI/CD pipelines to migrate databases without requiring public access.
|
|
6
|
-
*
|
|
7
|
-
* This handler uses the prisma-runner utilities from @friggframework/core,
|
|
8
|
-
* ensuring consistency with the `frigg db:setup` command.
|
|
9
|
-
*
|
|
10
|
-
* Environment Variables Required:
|
|
11
|
-
* - DATABASE_URL: PostgreSQL connection string (automatically set from Secrets Manager)
|
|
12
|
-
* - DB_TYPE: Database type ('postgresql' or 'mongodb')
|
|
13
|
-
* - STAGE: Deployment stage (determines migration command: 'dev' or 'deploy')
|
|
14
|
-
*
|
|
15
|
-
* Invocation:
|
|
16
|
-
* aws lambda invoke \
|
|
17
|
-
* --function-name my-app-production-dbMigrate \
|
|
18
|
-
* --region us-east-1 \
|
|
19
|
-
* response.json
|
|
20
|
-
*
|
|
21
|
-
* Success Response:
|
|
22
|
-
* {
|
|
23
|
-
* "statusCode": 200,
|
|
24
|
-
* "body": {
|
|
25
|
-
* "success": true,
|
|
26
|
-
* "message": "Database migration completed successfully",
|
|
27
|
-
* "dbType": "postgresql",
|
|
28
|
-
* "stage": "production",
|
|
29
|
-
* "migrationCommand": "deploy"
|
|
30
|
-
* }
|
|
31
|
-
* }
|
|
32
|
-
*
|
|
33
|
-
* Error Response:
|
|
34
|
-
* {
|
|
35
|
-
* "statusCode": 500,
|
|
36
|
-
* "body": {
|
|
37
|
-
* "success": false,
|
|
38
|
-
* "error": "Migration failed: ...",
|
|
39
|
-
* "stack": "Error: ..."
|
|
40
|
-
* }
|
|
41
|
-
* }
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
const {
|
|
45
|
-
RunDatabaseMigrationUseCase,
|
|
46
|
-
MigrationError,
|
|
47
|
-
ValidationError,
|
|
48
|
-
} = require('../../database/use-cases/run-database-migration-use-case');
|
|
49
|
-
|
|
50
|
-
// Inject prisma-runner as dependency
|
|
51
|
-
const prismaRunner = require('../../database/utils/prisma-runner');
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Sanitizes error messages to prevent credential leaks
|
|
55
|
-
* @param {string} errorMessage - Error message that might contain credentials
|
|
56
|
-
* @returns {string} Sanitized error message
|
|
57
|
-
*/
|
|
58
|
-
function sanitizeError(errorMessage) {
|
|
59
|
-
if (!errorMessage) return 'Unknown error';
|
|
60
|
-
|
|
61
|
-
return String(errorMessage)
|
|
62
|
-
// Remove PostgreSQL connection strings
|
|
63
|
-
.replace(/postgresql:\/\/[^@\s]+@[^\s/]+/gi, 'postgresql://***:***@***')
|
|
64
|
-
// Remove MongoDB connection strings
|
|
65
|
-
.replace(/mongodb(\+srv)?:\/\/[^@\s]+@[^\s/]+/gi, 'mongodb$1://***:***@***')
|
|
66
|
-
// Remove password parameters
|
|
67
|
-
.replace(/password[=:]\s*[^\s,;)]+/gi, 'password=***')
|
|
68
|
-
// Remove API keys
|
|
69
|
-
.replace(/apikey[=:]\s*[^\s,;)]+/gi, 'apikey=***')
|
|
70
|
-
.replace(/api[_-]?key[=:]\s*[^\s,;)]+/gi, 'api_key=***')
|
|
71
|
-
// Remove tokens
|
|
72
|
-
.replace(/token[=:]\s*[^\s,;)]+/gi, 'token=***')
|
|
73
|
-
.replace(/bearer\s+[^\s,;)]+/gi, 'bearer ***');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Sanitizes DATABASE_URL for safe logging
|
|
78
|
-
* @param {string} url - Database URL
|
|
79
|
-
* @returns {string} Sanitized URL
|
|
80
|
-
*/
|
|
81
|
-
function sanitizeDatabaseUrl(url) {
|
|
82
|
-
if (!url) return '';
|
|
83
|
-
|
|
84
|
-
// Replace credentials in connection string
|
|
85
|
-
return url.replace(/(:\/\/)([^:]+):([^@]+)@/, '$1***:***@');
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Lambda handler entry point
|
|
90
|
-
* @param {Object} event - Lambda event (not used, migrations don't need input)
|
|
91
|
-
* @param {Object} context - Lambda context (contains AWS request ID, timeout info)
|
|
92
|
-
* @returns {Promise<Object>} Response with statusCode and body
|
|
93
|
-
*/
|
|
94
|
-
exports.handler = async (event, context) => {
|
|
95
|
-
console.log('========================================');
|
|
96
|
-
console.log('Database Migration Lambda Started');
|
|
97
|
-
console.log('========================================');
|
|
98
|
-
console.log('Event:', JSON.stringify(event, null, 2));
|
|
99
|
-
console.log('Context:', JSON.stringify({
|
|
100
|
-
requestId: context.requestId,
|
|
101
|
-
functionName: context.functionName,
|
|
102
|
-
remainingTimeInMillis: context.getRemainingTimeInMillis(),
|
|
103
|
-
}, null, 2));
|
|
104
|
-
|
|
105
|
-
// Get environment variables
|
|
106
|
-
const databaseUrl = process.env.DATABASE_URL;
|
|
107
|
-
const dbType = process.env.DB_TYPE || 'postgresql';
|
|
108
|
-
const stage = process.env.STAGE || 'production';
|
|
109
|
-
|
|
110
|
-
try {
|
|
111
|
-
// Validate DATABASE_URL is set
|
|
112
|
-
if (!databaseUrl) {
|
|
113
|
-
const error = 'DATABASE_URL environment variable is not set';
|
|
114
|
-
console.error('❌ Validation failed:', error);
|
|
115
|
-
return {
|
|
116
|
-
statusCode: 500,
|
|
117
|
-
body: JSON.stringify({
|
|
118
|
-
success: false,
|
|
119
|
-
error,
|
|
120
|
-
}),
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
console.log('✓ Environment validated');
|
|
125
|
-
console.log(` Database Type: ${dbType}`);
|
|
126
|
-
console.log(` Stage: ${stage}`);
|
|
127
|
-
console.log(` Database URL: ${sanitizeDatabaseUrl(databaseUrl)}`);
|
|
128
|
-
|
|
129
|
-
// Create use case with dependencies (Dependency Injection)
|
|
130
|
-
const runDatabaseMigrationUseCase = new RunDatabaseMigrationUseCase({
|
|
131
|
-
prismaRunner,
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
console.log('\n========================================');
|
|
135
|
-
console.log('Executing Database Migration');
|
|
136
|
-
console.log('========================================');
|
|
137
|
-
|
|
138
|
-
// Execute use case (business logic layer)
|
|
139
|
-
const result = await runDatabaseMigrationUseCase.execute({
|
|
140
|
-
dbType,
|
|
141
|
-
stage,
|
|
142
|
-
verbose: true, // Enable verbose output for Lambda CloudWatch logs
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
console.log('✓ Database migration completed successfully');
|
|
146
|
-
console.log('\n========================================');
|
|
147
|
-
console.log('Migration Summary');
|
|
148
|
-
console.log('========================================');
|
|
149
|
-
console.log(` Status: Success`);
|
|
150
|
-
console.log(` Database: ${result.dbType}`);
|
|
151
|
-
console.log(` Stage: ${result.stage}`);
|
|
152
|
-
console.log(` Command: ${result.command}`);
|
|
153
|
-
console.log('========================================');
|
|
154
|
-
|
|
155
|
-
// Return success response (adapter layer - HTTP mapping)
|
|
156
|
-
return {
|
|
157
|
-
statusCode: 200,
|
|
158
|
-
body: JSON.stringify({
|
|
159
|
-
success: true,
|
|
160
|
-
message: result.message,
|
|
161
|
-
dbType: result.dbType,
|
|
162
|
-
stage: result.stage,
|
|
163
|
-
migrationCommand: result.command,
|
|
164
|
-
timestamp: new Date().toISOString(),
|
|
165
|
-
}),
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
} catch (error) {
|
|
169
|
-
console.error('\n========================================');
|
|
170
|
-
console.error('Migration Failed');
|
|
171
|
-
console.error('========================================');
|
|
172
|
-
console.error('Error:', error.name, error.message);
|
|
173
|
-
|
|
174
|
-
// Log full stack trace to CloudWatch (only visible to developers)
|
|
175
|
-
if (error.stack) {
|
|
176
|
-
console.error('Stack:', error.stack);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// Log context if available (from MigrationError)
|
|
180
|
-
if (error.context) {
|
|
181
|
-
console.error('Context:', JSON.stringify(error.context, null, 2));
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// Map domain errors to HTTP status codes (adapter layer)
|
|
185
|
-
let statusCode = 500;
|
|
186
|
-
let errorMessage = error.message || 'Unknown error occurred';
|
|
187
|
-
|
|
188
|
-
if (error instanceof ValidationError) {
|
|
189
|
-
statusCode = 400; // Bad Request for validation errors
|
|
190
|
-
} else if (error instanceof MigrationError) {
|
|
191
|
-
statusCode = 500; // Internal Server Error for migration failures
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Sanitize error message before returning
|
|
195
|
-
const sanitizedError = sanitizeError(errorMessage);
|
|
196
|
-
|
|
197
|
-
return {
|
|
198
|
-
statusCode,
|
|
199
|
-
body: JSON.stringify({
|
|
200
|
-
success: false,
|
|
201
|
-
error: sanitizedError,
|
|
202
|
-
errorType: error.name || 'Error',
|
|
203
|
-
// Only include stack traces in development environments
|
|
204
|
-
...(stage === 'dev' || stage === 'local' || stage === 'test' ? { stack: error.stack } : {}),
|
|
205
|
-
}),
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
};
|