import * as plugins from '../ts/plugins.js'; import { getDbSync } from '../ts/skr.database.js'; export const SKR_MIGRATION_COLLECTION = 'SkrMigration'; /** * What the ledger unification (v1 shapes → hashed journal) changed. The counts * are the writes of the run that completed the step; writes an interrupted * earlier run committed are not repeated and not counted again, and * `verifiedChains` covers them. */ export interface ILedgerUnificationReport { kind: 'ledger-unification'; /** legacy journal entries whose lines gained exact cents, side and Gegenkonto */ journalEntriesBackfilled: number; /** legacy transactions that gained cent mirrors */ transactionsBackfilled: number; /** largest float-to-cent rounding drift met, in cents */ maxDriftCents: number; /** legacy journal entries moved into the hashed journal */ legacyEntriesUpgraded: number; /** standalone legacy transactions synthesized into two-line journal entries */ transactionsSynthesized: number; /** * standalone legacy transactions whose synthesized entry an interrupted * 6.x `migrateToV2()` had written without marking the transaction: the entry * was found identical to the one this step derives and the transaction marked */ synthesizedEntriesAdopted: number; /** the hash chains the unchained entries were appended to, with the entries this run chained */ chains: Array<{ skrType: string; fiscalYear: number; chainedEntries: number; headHash: string; }>; /** * every chain this step appended to or that holds imported legacy entries, * verified end to end (gapless, every hash and link, counter and head) * after the step's writes */ verifiedChains: Array<{ skrType: string; fiscalYear: number; entries: number; headHash: string; }>; } /** What the storage topology step changed. */ export interface IStorageTopologyReport { kind: 'storage-topology'; /** collections whose legacy `smartdata_text_index` was dropped */ textIndexesDropped: string[]; /** the partial unique index over the journal's sequence numbers exists */ sequenceIndexEnsured: true; } export type TSkrMigrationReport = ILedgerUnificationReport | IStorageTopologyReport; /** An applied migration step as skr reports it (Verfahrensdokumentation artifact). */ export interface ISkrMigrationRecord { id: string; description: string; startedAt: Date; appliedAt: Date; report: TSkrMigrationReport; } /** * One document per migration step: the lease while a step runs, the record * and its report once it has been applied. */ @plugins.nosqldb.Collection(() => getDbSync(), { collectionName: SKR_MIGRATION_COLLECTION }) export class SkrMigrationRecord extends plugins.nosqldb.SmartDataDbDoc< SkrMigrationRecord, SkrMigrationRecord > { @plugins.nosqldb.unI() public id!: string; @plugins.nosqldb.svDb() public description!: string; @plugins.nosqldb.svDb() public status!: 'running' | 'applied'; /** the process holding the lease while the step runs */ @plugins.nosqldb.svDb() public owner!: string; /** epoch milliseconds; a holder that stopped renewing loses the lease after it */ @plugins.nosqldb.svDb() public leaseUntil!: number; @plugins.nosqldb.svDb() public startedAt!: Date; @plugins.nosqldb.svDb() public appliedAt!: Date | null; @plugins.nosqldb.svDb() public report!: TSkrMigrationReport | null; } /** The applied steps, oldest first. */ export const listAppliedMigrations = async (): Promise => { const records = await SkrMigrationRecord.getInstances({ status: 'applied' }); return records .filter( (record): record is SkrMigrationRecord & { appliedAt: Date; report: TSkrMigrationReport } => record.appliedAt !== null && record.report !== null, ) .map((record) => ({ id: record.id, description: record.description, startedAt: record.startedAt, appliedAt: record.appliedAt, report: record.report, })) .sort((a, b) => a.appliedAt.getTime() - b.appliedAt.getTime()); };