UNPKG

@augment-vir/node

Version:

A collection of augments, helpers types, functions, and classes only for Node.js (backend) JavaScript environments.

98 lines (97 loc) 3.57 kB
import { check } from '@augment-vir/assert'; import { log, safeMatch, toEnsuredNumber } from '@augment-vir/common'; import terminate from 'terminate'; import { runShellCommand } from '../augments/terminal/shell.js'; import { PrismaMigrationNeededError, PrismaResetNeededError } from './prisma-errors.js'; import { runPrismaCommand, verifyOutput } from './run-prisma-command.js'; export async function applyPrismaMigrationsToProd(schemaFilePath, env = {}) { await runPrismaCommand({ command: 'migrate deploy' }, schemaFilePath, env); } var DbChangeRequired; (function (DbChangeRequired) { DbChangeRequired["MigrationNeeded"] = "migration-needed"; DbChangeRequired["ResetNeeded"] = "reset-needed"; })(DbChangeRequired || (DbChangeRequired = {})); export async function applyPrismaMigrationsToDev(schemaFilePath, env = {}) { const command = [ 'prisma', 'migrate', 'dev', `--schema='${schemaFilePath}'`, ].join(' '); log.faint(`> ${command}`); let dbRequirement = undefined; const result = await runShellCommand(command, { env: { ...process.env, ...env, }, stdoutCallback(stdout, childProcess) { if (stdout.includes('Enter a name for the new migration')) { if (childProcess.pid) { terminate(childProcess.pid); } dbRequirement = DbChangeRequired.MigrationNeeded; } else if (stdout.includes('We need to reset the SQLite database')) { if (childProcess.pid) { terminate(childProcess.pid); } dbRequirement = DbChangeRequired.ResetNeeded; } }, }); if (dbRequirement === DbChangeRequired.MigrationNeeded) { throw new PrismaMigrationNeededError(schemaFilePath); } else if (dbRequirement === DbChangeRequired.ResetNeeded) { throw new PrismaResetNeededError(schemaFilePath); } verifyOutput(schemaFilePath, result, false); } export async function getMigrationStatus(schemaFilePath, env = {}) { const output = await runPrismaCommand({ command: 'migrate status', ignoreExitCode: true, }, schemaFilePath, env); const listedMigrations = { totalMigrations: 0, unappliedMigrations: [], }; let foundNotAppliedMigrations = false; output.stdout.split('\n').some((rawLine) => { const line = rawLine.trim(); if (foundNotAppliedMigrations) { if (line) { listedMigrations.unappliedMigrations.push(line); } else { /** We're done parsing. */ return true; } } else if (line.endsWith('not yet been applied:')) { foundNotAppliedMigrations = true; } else { const [, countMatch,] = safeMatch(line, /^([\d,]+) migrations? found in/); if (countMatch) { listedMigrations.totalMigrations = toEnsuredNumber(countMatch); } } /** Still need to keep parsing. */ return false; }); return listedMigrations; } export async function createPrismaMigration({ migrationName, createOnly = false, }, schemaFilePath, env = {}) { const command = [ 'migrate', 'dev', `--name='${migrationName}'`, createOnly ? '--create-only' : '', ] .filter(check.isTruthy) .join(' '); await runPrismaCommand({ command }, schemaFilePath, env); }