@friggframework/core 2.0.0--canary.464.f9d3fc0.0 → 2.0.0--canary.454.25d396a.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 +28 -0
- package/database/prisma.js +2 -2
- package/database/use-cases/run-database-migration-use-case.js +137 -0
- package/database/use-cases/run-database-migration-use-case.test.js +310 -0
- package/database/utils/prisma-runner.js +313 -0
- package/database/utils/prisma-runner.test.js +486 -0
- package/handlers/routers/integration-webhook-routers.js +2 -2
- package/handlers/workers/db-migration.js +208 -0
- package/handlers/workers/db-migration.test.js +437 -0
- package/package.json +79 -66
- package/prisma-mongodb/schema.prisma +22 -20
- package/prisma-postgresql/schema.prisma +16 -14
|
@@ -0,0 +1,313 @@
|
|
|
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
|
+
};
|