@friggframework/core 2.0.0--canary.461.5767fa4.0 → 2.0.0--canary.461.3d6d8ad.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.
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Check Migration Status Use Case
3
+ *
4
+ * Domain logic for checking if database has pending migrations.
5
+ * Does NOT trigger migrations, just reports status.
6
+ *
7
+ * Architecture: Hexagonal/Clean
8
+ * - Use Case (Domain Layer)
9
+ * - Depends on prismaRunner (Infrastructure abstraction)
10
+ * - Called by Router (Adapter Layer)
11
+ */
12
+
13
+ class ValidationError extends Error {
14
+ constructor(message) {
15
+ super(message);
16
+ this.name = 'ValidationError';
17
+ }
18
+ }
19
+
20
+ class CheckMigrationStatusUseCase {
21
+ /**
22
+ * @param {Object} dependencies
23
+ * @param {Object} dependencies.prismaRunner - Prisma runner utility
24
+ */
25
+ constructor({ prismaRunner }) {
26
+ if (!prismaRunner) {
27
+ throw new Error('prismaRunner dependency is required');
28
+ }
29
+ this.prismaRunner = prismaRunner;
30
+ }
31
+
32
+ /**
33
+ * Execute check migration status
34
+ *
35
+ * @param {string} dbType - Database type (postgresql or mongodb)
36
+ * @param {string} stage - Deployment stage (default: 'production')
37
+ * @returns {Promise<Object>} Migration status
38
+ */
39
+ async execute(dbType, stage = 'production') {
40
+ // Validate inputs
41
+ if (!dbType) {
42
+ throw new ValidationError('dbType is required');
43
+ }
44
+
45
+ if (!['postgresql', 'mongodb'].includes(dbType)) {
46
+ throw new ValidationError('dbType must be postgresql or mongodb');
47
+ }
48
+
49
+ console.log(`Checking migration status for ${dbType} in ${stage}`);
50
+
51
+ // Check database state using Prisma
52
+ const state = await this.prismaRunner.checkDatabaseState(dbType);
53
+
54
+ // Build response
55
+ const response = {
56
+ upToDate: state.upToDate,
57
+ pendingMigrations: state.pendingMigrations || 0,
58
+ dbType,
59
+ stage,
60
+ };
61
+
62
+ // Add error if present
63
+ if (state.error) {
64
+ response.error = state.error;
65
+ response.recommendation = 'Run POST /db-migrate to initialize database';
66
+ }
67
+
68
+ // Add recommendation if migrations pending
69
+ if (!state.upToDate && state.pendingMigrations > 0) {
70
+ response.recommendation = `Run POST /db-migrate to apply ${state.pendingMigrations} pending migration(s)`;
71
+ }
72
+
73
+ return response;
74
+ }
75
+ }
76
+
77
+ module.exports = {
78
+ CheckMigrationStatusUseCase,
79
+ ValidationError,
80
+ };
81
+
@@ -4,6 +4,7 @@
4
4
  * HTTP API for triggering and monitoring database migrations.
5
5
  *
6
6
  * Endpoints:
7
+ * - GET /db-migrate/status - Check if migrations are pending
7
8
  * - POST /db-migrate - Trigger async migration (queues job)
8
9
  * - GET /db-migrate/:processId - Check migration status
9
10
  *
@@ -27,6 +28,11 @@ const {
27
28
  ValidationError: GetValidationError,
28
29
  NotFoundError,
29
30
  } = require('../../database/use-cases/get-migration-status-use-case');
31
+ const {
32
+ CheckMigrationStatusUseCase,
33
+ ValidationError: CheckValidationError,
34
+ } = require('../../database/use-cases/check-migration-status-use-case');
35
+ const prismaRunner = require('../../database/utils/prisma-runner');
30
36
 
31
37
  const router = Router();
32
38
 
@@ -40,6 +46,7 @@ const triggerMigrationUseCase = new TriggerDatabaseMigrationUseCase({
40
46
  // Note: QueuerUtil is used directly in the use case (static utility)
41
47
  });
42
48
  const getStatusUseCase = new GetMigrationStatusUseCase({ migrationStatusRepository });
49
+ const checkMigrationStatusUseCase = new CheckMigrationStatusUseCase({ prismaRunner });
43
50
 
44
51
  /**
45
52
  * Admin API key validation middleware
@@ -118,6 +125,51 @@ router.post(
118
125
  })
119
126
  );
120
127
 
128
+ /**
129
+ * GET /db-migrate/status
130
+ *
131
+ * Check if database has pending migrations
132
+ *
133
+ * Query params:
134
+ * - stage: string (optional, defaults to STAGE env var or 'production')
135
+ *
136
+ * Response (200 OK):
137
+ * {
138
+ * upToDate: boolean,
139
+ * pendingMigrations: number,
140
+ * dbType: 'postgresql',
141
+ * stage: string,
142
+ * recommendation?: string (if migrations pending),
143
+ * error?: string (if database check failed)
144
+ * }
145
+ */
146
+ router.get(
147
+ '/db-migrate/status',
148
+ catchAsyncError(async (req, res) => {
149
+ const stage = req.query.stage || process.env.STAGE || 'production';
150
+ const dbType = process.env.DB_TYPE || 'postgresql'; // Hardcoded for PostgreSQL migrations
151
+
152
+ console.log(`Checking migration status: dbType=${dbType}, stage=${stage}`);
153
+
154
+ try {
155
+ const status = await checkMigrationStatusUseCase.execute(dbType, stage);
156
+
157
+ res.status(200).json(status);
158
+ } catch (error) {
159
+ // Handle validation errors (400 Bad Request)
160
+ if (error instanceof CheckValidationError) {
161
+ return res.status(400).json({
162
+ success: false,
163
+ error: error.message,
164
+ });
165
+ }
166
+
167
+ // Re-throw other errors for global error handler
168
+ throw error;
169
+ }
170
+ })
171
+ );
172
+
121
173
  /**
122
174
  * GET /db-migrate/:migrationId
123
175
  *
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@friggframework/core",
3
3
  "prettier": "@friggframework/prettier-config",
4
- "version": "2.0.0--canary.461.5767fa4.0",
4
+ "version": "2.0.0--canary.461.3d6d8ad.0",
5
5
  "dependencies": {
6
6
  "@aws-sdk/client-apigatewaymanagementapi": "^3.588.0",
7
7
  "@aws-sdk/client-kms": "^3.588.0",
@@ -37,9 +37,9 @@
37
37
  }
38
38
  },
39
39
  "devDependencies": {
40
- "@friggframework/eslint-config": "2.0.0--canary.461.5767fa4.0",
41
- "@friggframework/prettier-config": "2.0.0--canary.461.5767fa4.0",
42
- "@friggframework/test": "2.0.0--canary.461.5767fa4.0",
40
+ "@friggframework/eslint-config": "2.0.0--canary.461.3d6d8ad.0",
41
+ "@friggframework/prettier-config": "2.0.0--canary.461.3d6d8ad.0",
42
+ "@friggframework/test": "2.0.0--canary.461.3d6d8ad.0",
43
43
  "@prisma/client": "^6.17.0",
44
44
  "@types/lodash": "4.17.15",
45
45
  "@typescript-eslint/eslint-plugin": "^8.0.0",
@@ -79,5 +79,5 @@
79
79
  "publishConfig": {
80
80
  "access": "public"
81
81
  },
82
- "gitHead": "5767fa41c5e27a098d51bf4edff785f87de1d6bf"
82
+ "gitHead": "3d6d8ad065c2530fee34dcda5ee304979c718a65"
83
83
  }