@prisma-next/cli 0.1.0-dev.20 → 0.1.0-dev.22

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/dist/cli.js CHANGED
@@ -1230,7 +1230,7 @@ function createDbInitCommand() {
1230
1230
  setCommandDescriptions(
1231
1231
  command,
1232
1232
  "Bootstrap a database to match the current contract and write the contract marker",
1233
- "Initializes a database to match your emitted contract using additive-only operations.\nCreates tables, columns, indexes, and constraints defined in your contract.\nWrites a contract marker to track the database state. This operation is idempotent.\n\nCurrently supports empty databases only. Use --plan to preview changes without applying."
1233
+ "Initializes a database to match your emitted contract using additive-only operations.\nCreates any missing tables, columns, indexes, and constraints defined in your contract.\nLeaves existing compatible structures in place, surfaces conflicts when destructive changes\nwould be required, and writes a contract marker to track the database state. Use --plan to\npreview changes without applying."
1234
1234
  );
1235
1235
  command.configureHelp({
1236
1236
  formatHelp: (cmd) => {
@@ -1341,6 +1341,58 @@ function createDbInitCommand() {
1341
1341
  throw errorMigrationPlanningFailed({ conflicts: plannerResult.conflicts });
1342
1342
  }
1343
1343
  const migrationPlan = plannerResult.plan;
1344
+ const existingMarker = await familyInstance.readMarker({ driver });
1345
+ if (existingMarker) {
1346
+ const markerMatchesDestination = existingMarker.coreHash === migrationPlan.destination.coreHash && (!migrationPlan.destination.profileHash || existingMarker.profileHash === migrationPlan.destination.profileHash);
1347
+ if (markerMatchesDestination) {
1348
+ const dbInitResult2 = {
1349
+ ok: true,
1350
+ mode: options.plan ? "plan" : "apply",
1351
+ plan: {
1352
+ targetId: migrationPlan.targetId,
1353
+ destination: migrationPlan.destination,
1354
+ operations: []
1355
+ },
1356
+ ...options.plan ? {} : {
1357
+ execution: { operationsPlanned: 0, operationsExecuted: 0 },
1358
+ marker: {
1359
+ coreHash: existingMarker.coreHash,
1360
+ profileHash: existingMarker.profileHash
1361
+ }
1362
+ },
1363
+ summary: "Database already at target contract state",
1364
+ timings: { total: Date.now() - startTime }
1365
+ };
1366
+ return dbInitResult2;
1367
+ }
1368
+ const coreHashMismatch = existingMarker.coreHash !== migrationPlan.destination.coreHash;
1369
+ const profileHashMismatch = migrationPlan.destination.profileHash && existingMarker.profileHash !== migrationPlan.destination.profileHash;
1370
+ const mismatchParts = [];
1371
+ if (coreHashMismatch) {
1372
+ mismatchParts.push(
1373
+ `coreHash (marker: ${existingMarker.coreHash}, destination: ${migrationPlan.destination.coreHash})`
1374
+ );
1375
+ }
1376
+ if (profileHashMismatch) {
1377
+ mismatchParts.push(
1378
+ `profileHash (marker: ${existingMarker.profileHash}, destination: ${migrationPlan.destination.profileHash})`
1379
+ );
1380
+ }
1381
+ throw errorRuntime(
1382
+ `Existing contract marker does not match plan destination. Mismatch in ${mismatchParts.join(" and ")}.`,
1383
+ {
1384
+ why: "Database has an existing contract marker that does not match the target contract",
1385
+ fix: "Use `prisma-next db migrate` to migrate from the existing contract, or drop the database and re-run `db init`",
1386
+ meta: {
1387
+ code: "MARKER_ORIGIN_MISMATCH",
1388
+ markerCoreHash: existingMarker.coreHash,
1389
+ destinationCoreHash: migrationPlan.destination.coreHash,
1390
+ ...existingMarker.profileHash ? { markerProfileHash: existingMarker.profileHash } : {},
1391
+ ...migrationPlan.destination.profileHash ? { destinationProfileHash: migrationPlan.destination.profileHash } : {}
1392
+ }
1393
+ }
1394
+ );
1395
+ }
1344
1396
  if (options.plan) {
1345
1397
  const dbInitResult2 = {
1346
1398
  ok: true,