From 47cf6de393e16c41c3e7e64e867ad4689ecad6f9 Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Mon, 18 Jul 2022 11:57:02 -0600 Subject: [PATCH] add from and to arguments --- emver-lite/mod.ts | 17 ++++++++++---- migrations.ts | 57 +++++++++++++++++++++++------------------------ 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/emver-lite/mod.ts b/emver-lite/mod.ts index 0d19260..11510b3 100644 --- a/emver-lite/mod.ts +++ b/emver-lite/mod.ts @@ -1,3 +1,5 @@ +import { matches } from "../dependencies.ts"; + const starSub = /((\d+\.)*\d+)\.\*/; function incrementLastNumber(list: number[]) { @@ -123,15 +125,22 @@ export class EmVer { public lessThan(other: EmVer): boolean { return !this.greaterThanOrEqual(other); } - public compare(other: EmVer): number { + public compare(other: EmVer) { if (this.equals(other)) { - return 0; + return "equal" as const; } else if (this.greaterThan(other)) { - return 1; + return "greater" as const; } else { - return -1; + return "less" as const; } } + public compareForSort(other: EmVer) { + return matches.matches(this.compare(other)) + .when("equal", () => 0 as const) + .when("greater", () => 1 as const) + .when("less", () => -1 as const) + .unwrap(); + } } /** diff --git a/migrations.ts b/migrations.ts index c00609a..c7f516c 100644 --- a/migrations.ts +++ b/migrations.ts @@ -1,5 +1,6 @@ import { types as T } from "./mod.ts"; import { EmVer } from "./emver-lite/mod.ts"; +import { matches } from "./dependencies.ts"; export type MigrationFn = (effects: T.Effects) => Promise; @@ -16,43 +17,41 @@ export function fromMapping( migrations: MigrationMapping, currentVersion: string, ): T.ExpectedExports.migration { - return async (effects: T.Effects, version: string) => { + const directionShape = matches.literals("from", "to"); + return async ( + effects: T.Effects, + version: string, + direction?: unknown, + ) => { + if (!directionShape.test(direction)) { + return { error: 'Must specify arg "from" or "to".' }; + } + let configured = true; const current = EmVer.parse(currentVersion); - const previous = EmVer.parse(version); + const other = EmVer.parse(version); - let migrationsToRun: MigrationFn[]; - switch (previous.compare(current)) { - case 0: - migrationsToRun = []; - break; - case 1: // ups - migrationsToRun = Object.entries(migrations).map( - ([version, migration]) => ({ + const migrationsToRun = matches.matches(direction) + .when("from", () => + Object.entries(migrations) + .map(([version, migration]) => ({ version: EmVer.parse(version), migration, - }), - ).filter(({ version }) => version.greaterThan(previous)).sort((a, b) => - a.version.compare(b.version) - ).map(({ migration }) => migration.up); - break; - case -1: // downs - migrationsToRun = Object.entries(migrations).map( - ([version, migration]) => ({ + })).filter(({ version }) => + version.greaterThan(other) && version.lessThanOrEqual(current) + ).sort((a, b) => a.version.compareForSort(b.version)) + .map(({ migration }) => migration.up)) + .when("to", () => + Object.entries(migrations) + .map(([version, migration]) => ({ version: EmVer.parse(version), migration, - }), - ).filter(({ version }) => version.lessThanOrEqual(previous)).sort(( - a, - b, - ) => b.version.compare(a.version)).map(({ migration }) => - migration.down - ); - break; - default: - return { error: "unreachable" }; - } + })).filter(({ version }) => + version.lessThanOrEqual(other) && version.greaterThan(current) + ).sort((a, b) => b.version.compareForSort(a.version)) + .map(({ migration }) => migration.down)) + .unwrap(); for (const migration of migrationsToRun) { configured = (await migration(effects)).configured && configured;