mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-31 04:33:40 +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+)\.\*/;
|
const starSub = /((\d+\.)*\d+)\.\*/;
|
||||||
|
|
||||||
function incrementLastNumber(list: number[]) {
|
function incrementLastNumber(list: number[]) {
|
||||||
@@ -123,15 +125,22 @@ export class EmVer {
|
|||||||
public lessThan(other: EmVer): boolean {
|
public lessThan(other: EmVer): boolean {
|
||||||
return !this.greaterThanOrEqual(other);
|
return !this.greaterThanOrEqual(other);
|
||||||
}
|
}
|
||||||
public compare(other: EmVer): number {
|
public compare(other: EmVer) {
|
||||||
if (this.equals(other)) {
|
if (this.equals(other)) {
|
||||||
return 0;
|
return "equal" as const;
|
||||||
} else if (this.greaterThan(other)) {
|
} else if (this.greaterThan(other)) {
|
||||||
return 1;
|
return "greater" as const;
|
||||||
} else {
|
} 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 { types as T } from "./mod.ts";
|
||||||
import { EmVer } from "./emver-lite/mod.ts";
|
import { EmVer } from "./emver-lite/mod.ts";
|
||||||
|
import { matches } from "./dependencies.ts";
|
||||||
|
|
||||||
export type MigrationFn = (effects: T.Effects) => Promise<T.MigrationRes>;
|
export type MigrationFn = (effects: T.Effects) => Promise<T.MigrationRes>;
|
||||||
|
|
||||||
@@ -16,43 +17,41 @@ export function fromMapping(
|
|||||||
migrations: MigrationMapping,
|
migrations: MigrationMapping,
|
||||||
currentVersion: string,
|
currentVersion: string,
|
||||||
): T.ExpectedExports.migration {
|
): 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;
|
let configured = true;
|
||||||
|
|
||||||
const current = EmVer.parse(currentVersion);
|
const current = EmVer.parse(currentVersion);
|
||||||
const previous = EmVer.parse(version);
|
const other = EmVer.parse(version);
|
||||||
|
|
||||||
let migrationsToRun: MigrationFn[];
|
const migrationsToRun = matches.matches(direction)
|
||||||
switch (previous.compare(current)) {
|
.when("from", () =>
|
||||||
case 0:
|
Object.entries(migrations)
|
||||||
migrationsToRun = [];
|
.map(([version, migration]) => ({
|
||||||
break;
|
|
||||||
case 1: // ups
|
|
||||||
migrationsToRun = Object.entries(migrations).map(
|
|
||||||
([version, migration]) => ({
|
|
||||||
version: EmVer.parse(version),
|
version: EmVer.parse(version),
|
||||||
migration,
|
migration,
|
||||||
}),
|
})).filter(({ version }) =>
|
||||||
).filter(({ version }) => version.greaterThan(previous)).sort((a, b) =>
|
version.greaterThan(other) && version.lessThanOrEqual(current)
|
||||||
a.version.compare(b.version)
|
).sort((a, b) => a.version.compareForSort(b.version))
|
||||||
).map(({ migration }) => migration.up);
|
.map(({ migration }) => migration.up))
|
||||||
break;
|
.when("to", () =>
|
||||||
case -1: // downs
|
Object.entries(migrations)
|
||||||
migrationsToRun = Object.entries(migrations).map(
|
.map(([version, migration]) => ({
|
||||||
([version, migration]) => ({
|
|
||||||
version: EmVer.parse(version),
|
version: EmVer.parse(version),
|
||||||
migration,
|
migration,
|
||||||
}),
|
})).filter(({ version }) =>
|
||||||
).filter(({ version }) => version.lessThanOrEqual(previous)).sort((
|
version.lessThanOrEqual(other) && version.greaterThan(current)
|
||||||
a,
|
).sort((a, b) => b.version.compareForSort(a.version))
|
||||||
b,
|
.map(({ migration }) => migration.down))
|
||||||
) => b.version.compare(a.version)).map(({ migration }) =>
|
.unwrap();
|
||||||
migration.down
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return { error: "unreachable" };
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const migration of migrationsToRun) {
|
for (const migration of migrationsToRun) {
|
||||||
configured = (await migration(effects)).configured && configured;
|
configured = (await migration(effects)).configured && configured;
|
||||||
|
|||||||
Reference in New Issue
Block a user