mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-26 02:11:56 +00:00
add from and to arguments
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user