{"version":3,"file":"policy-fSP-Qd0W.mjs","names":[],"sources":["../src/database/migrations/policy.ts"],"sourcesContent":["import type { Kysely } from \"kysely\";\n\nimport type { Database } from \"../types.js\";\nimport { getExactMigrationStatus, runMigrations } from \"./runner.js\";\n\nexport type RuntimeMigrationMode = \"auto\" | \"check\" | \"manual\";\n\nexport interface RuntimeMigrationConfig {\n\truntime: RuntimeMigrationMode;\n\tdev?: RuntimeMigrationMode;\n}\n\nfunction parseMigrationMode(value: unknown, source: string): RuntimeMigrationMode {\n\tif (value === \"auto\" || value === \"check\" || value === \"manual\") {\n\t\treturn value;\n\t}\n\tthrow new Error(\n\t\t`Invalid ${source} value ${JSON.stringify(value)}; expected \"auto\", \"check\", or \"manual\"`,\n\t);\n}\n\nexport function normalizeMigrationConfig(value: unknown): RuntimeMigrationConfig {\n\tif (value === undefined) return { runtime: \"auto\" };\n\tif (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n\t\tthrow new Error(\"Invalid migrations configuration; expected an object\");\n\t}\n\n\tconst runtimeValue = Reflect.get(value, \"runtime\");\n\tconst devValue = Reflect.get(value, \"dev\");\n\tconst runtime = parseMigrationMode(runtimeValue, \"migrations.runtime\");\n\tconst dev = devValue === undefined ? undefined : parseMigrationMode(devValue, \"migrations.dev\");\n\treturn dev === undefined ? { runtime } : { runtime, dev };\n}\n\nexport function resolveRuntimeMigrationMode(\n\tconfig: RuntimeMigrationConfig | undefined,\n\toptions: { dev: boolean; override?: unknown },\n): RuntimeMigrationMode {\n\tif (options.override !== undefined) {\n\t\treturn parseMigrationMode(options.override, \"EMDASH_MIGRATIONS_MODE\");\n\t}\n\tif (options.dev) return config?.dev ?? \"auto\";\n\treturn config?.runtime ?? \"auto\";\n}\n\nexport class PendingMigrationsError extends Error {\n\treadonly pending: string[];\n\n\tconstructor(pending: readonly string[]) {\n\t\tsuper(`Database has pending EmDash migrations: ${pending.join(\", \")}`);\n\t\tthis.name = \"PendingMigrationsError\";\n\t\tthis.pending = [...pending];\n\t}\n}\n\nexport async function enforceRuntimeMigrationPolicy(\n\tdb: Kysely<Database>,\n\tmode: RuntimeMigrationMode,\n): Promise<void> {\n\tif (mode === \"manual\") return;\n\tif (mode === \"auto\") {\n\t\tawait runMigrations(db);\n\t\treturn;\n\t}\n\n\tconst status = await getExactMigrationStatus(db);\n\tif (status.pending.length > 0) {\n\t\tthrow new PendingMigrationsError(status.pending);\n\t}\n}\n"],"mappings":";;;AAYA,SAAS,mBAAmB,OAAgB,QAAsC;AACjF,KAAI,UAAU,UAAU,UAAU,WAAW,UAAU,SACtD,QAAO;AAER,OAAM,IAAI,MACT,WAAW,OAAO,SAAS,KAAK,UAAU,MAAM,CAAC,yCACjD;;AAGF,SAAgB,yBAAyB,OAAwC;AAChF,KAAI,UAAU,OAAW,QAAO,EAAE,SAAS,QAAQ;AACnD,KAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,MAAM,QAAQ,MAAM,CACtE,OAAM,IAAI,MAAM,uDAAuD;CAGxE,MAAM,eAAe,QAAQ,IAAI,OAAO,UAAU;CAClD,MAAM,WAAW,QAAQ,IAAI,OAAO,MAAM;CAC1C,MAAM,UAAU,mBAAmB,cAAc,qBAAqB;CACtE,MAAM,MAAM,aAAa,SAAY,SAAY,mBAAmB,UAAU,iBAAiB;AAC/F,QAAO,QAAQ,SAAY,EAAE,SAAS,GAAG;EAAE;EAAS;EAAK;;AAG1D,SAAgB,4BACf,QACA,SACuB;AACvB,KAAI,QAAQ,aAAa,OACxB,QAAO,mBAAmB,QAAQ,UAAU,yBAAyB;AAEtE,KAAI,QAAQ,IAAK,QAAO,QAAQ,OAAO;AACvC,QAAO,QAAQ,WAAW;;AAG3B,IAAa,yBAAb,cAA4C,MAAM;CACjD,AAAS;CAET,YAAY,SAA4B;AACvC,QAAM,2CAA2C,QAAQ,KAAK,KAAK,GAAG;AACtE,OAAK,OAAO;AACZ,OAAK,UAAU,CAAC,GAAG,QAAQ;;;AAI7B,eAAsB,8BACrB,IACA,MACgB;AAChB,KAAI,SAAS,SAAU;AACvB,KAAI,SAAS,QAAQ;AACpB,QAAM,cAAc,GAAG;AACvB;;CAGD,MAAM,SAAS,MAAM,wBAAwB,GAAG;AAChD,KAAI,OAAO,QAAQ,SAAS,EAC3B,OAAM,IAAI,uBAAuB,OAAO,QAAQ"}