add from and to arguments

This commit is contained in:
Aiden McClelland
2022-07-18 11:57:02 -06:00
parent 028ade0b59
commit 47cf6de393
2 changed files with 41 additions and 33 deletions

View File

@@ -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();
}
}
/**

View File

@@ -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<T.MigrationRes>;
@@ -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;