@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
4 lines • 4.1 kB
JavaScript
import e from"../../../constants/constants.mjs";import t,{startLoggerBuffering as n,stopLoggerBuffering as r}from"../../logger/index.mjs";import i from"../../i18n/prepare-translations.mjs";import{destroyKVAdapter as a,getInitializedKVAdapter as o}from"../../kv/lifecycle.mjs";import s from"../../../utils/services/create-service-context.mjs";import c from"../logger.mjs";import l from"../../config/load-config-file.mjs";import{prepareExternalMigrations as u}from"../../db/load-external-migrations.mjs";import d from"../services/validate-env-vars.mjs";import{sql as f}from"kysely";import{confirm as p}from"@inquirer/prompts";import{Migrator as m}from"kysely/migration";const h=async h=>{let g,_,v,y,b,x,S=async()=>{_&&(await Promise.allSettled([x?.destroy(),a(g,{config:_,env:v,runtimeContext:y})]),x=void 0,g=void 0)};try{n();let a=c.startTimer(),C=h?.steps??1,w=h?.force??!1,T=await l({prepareRuntime:!0});_=T.config,v=T.env,y=T.runtimeContext,b=(await i({config:_,projectRoot:T.projectRoot})).translationStore,await d({envSchema:T.envSchema,env:T.env})||(await S(),await r(),process.exit(1)),c.info(`Checking rollback status`),await u(_,T.projectRoot),x=await _.db.connect(v);let E=new m({db:x.client,provider:{async getMigrations(){return T.config.db.migrations}},allowUnorderedMigrations:!0}),D=Object.keys(T.config.db.migrations),O=[];try{O=(await f`
SELECT name FROM kysely_migration
`.execute(x.client)).rows.map(e=>e.name)}catch{}let k=O.filter(e=>!D.includes(e));k.length>0&&(c.error(`Cannot rollback: previously executed migration(s) are no longer registered: ${k.join(`, `)}`),c.info(`If you removed a plugin or migration file, restore it so its migrations can be rolled back.`),await S(),await r(),process.exit(1));let A=(await E.getMigrations()).filter(e=>e.executedAt!==void 0).sort((e,t)=>{let n=e.executedAt?.getTime()??0,r=t.executedAt?.getTime()??0;return n===r?e.name.localeCompare(t.name):n-r});A.length===0&&(c.info(`No migrations to rollback`),await S(),await r(),process.exit(0));let j=_.db.protectedMigrations,M=[];for(let e=A.length-1;e>=0&&M.length<C;e--){let t=A[e];t&&(j.includes(t.name)&&(c.error(`Cannot rollback protected migration: "${t.name}"`),c.info(`Protected migrations are essential for the CMS to function and cannot be rolled back.`),c.info(`If you need to reset the database, use`,c.color.cyan(`migrate:reset`),`or`,c.color.cyan(`migrate:fresh`)),await S(),await r(),process.exit(1)),M.push(t.name))}if(c.info(`Found ${A.length} executed migration(s)`),c.warn(`Preparing to rollback ${M.length} migration(s)`),!w){let e;try{e=await p({message:`Are you sure you want to rollback ${M.length} migration(s)? This action cannot be undone.`,default:!1})}catch(e){throw e instanceof Error&&e.name===`ExitPromptError`&&(await S(),await r(),process.exit(0)),e}e||(c.info(`Rollback cancelled`),await S(),await r(),process.exit(0))}c.info(`Rolling back ${M.length} migration(s)...`);let N=0;for(let n=0;n<M.length;n++){let{error:n,results:i}=await E.migrateDown();if(i)for(let n of i)n.status===`Success`?(t.debug({message:`Rolled back "${n.migrationName}" successfully`,scope:e.logScopes.migrations}),N++):n.status===`Error`&&t.error({message:`Failed to rollback migration "${n.migrationName}"`,scope:e.logScopes.migrations});n&&(n instanceof Error?c.errorInstance(n,`Rollback failed`):c.error(`Rollback failed`,`Unknown error`),await S(),await r(),process.exit(1))}N===0?c.warn(`No migrations were rolled back`):c.success(`Rolled back ${N} migration(s)`,c.color.green(`successfully`)),c.info(`Clearing KV cache...`),g=await o(_,{env:v,runtimeContext:y}),await g.clear(s({config:_,database:x,translationStore:b,env:v,runtimeContext:y,kv:g})),await S();let P=a();c.log(c.createBadge(`LUCID CMS`),`Rollback completed`,c.color.green(`successfully`),`in`,c.color.green(c.formatMilliseconds(P)),{spaceAfter:!0,spaceBefore:!0}),await r(),process.exit(0)}catch(e){await S(),e instanceof Error?c.errorInstance(e,`Rollback failed`):c.error(`Rollback failed`,`Unknown error`),await r(),process.exit(1)}};export{h as default};
//# sourceMappingURL=migrate-rollback.mjs.map