add utils to migrations up and down

This commit is contained in:
Matt Hill
2023-05-05 10:45:21 -06:00
parent e32b768e5c
commit 8f69f3df9e
8 changed files with 54 additions and 51 deletions

View File

@@ -1,5 +1,3 @@
import { Effects, ExpectedExports, ExportedAction } from "../types"
import { ActionMetaData } from "../types"
import { once } from "../util/once" import { once } from "../util/once"
import { CreatedAction } from "./createAction" import { CreatedAction } from "./createAction"

View File

@@ -1,12 +1,13 @@
import { ManifestVersion } from "../../manifest/ManifestTypes" import { ManifestVersion } from "../../manifest/ManifestTypes"
import { Effects } from "../../types" import { Effects } from "../../types"
import { Utils } from "../../util"
export class Migration<Version extends ManifestVersion> { export class Migration<Version extends ManifestVersion, WD> {
constructor( constructor(
readonly options: { readonly options: {
version: Version version: Version
up: (opts: { effects: Effects }) => Promise<void> up: (opts: { effects: Effects; utils: Utils<WD> }) => Promise<void>
down: (opts: { effects: Effects }) => Promise<void> down: (opts: { effects: Effects; utils: Utils<WD> }) => Promise<void>
}, },
) {} ) {}
static of<Version extends ManifestVersion>(options: { static of<Version extends ManifestVersion>(options: {
@@ -17,11 +18,11 @@ export class Migration<Version extends ManifestVersion> {
return new Migration(options) return new Migration(options)
} }
async up(opts: { effects: Effects }) { async up(opts: { effects: Effects; utils: Utils<WD> }) {
this.up(opts) this.up(opts)
} }
async down(opts: { effects: Effects }) { async down(opts: { effects: Effects; utils: Utils<WD> }) {
this.down(opts) this.down(opts)
} }
} }

View File

@@ -1,69 +1,70 @@
import { setupActions } from "../../actions/setupActions"
import { EmVer } from "../../emverLite/mod" import { EmVer } from "../../emverLite/mod"
import { SDKManifest } from "../../manifest/ManifestTypes" import { SDKManifest } from "../../manifest/ManifestTypes"
import { ExpectedExports } from "../../types" import { ExpectedExports } from "../../types"
import { once } from "../../util/once" import { once } from "../../util/once"
import { Migration } from "./Migration" import { Migration } from "./Migration"
export class Migrations { export class Migrations<WD> {
private constructor( private constructor(
readonly manifest: SDKManifest, readonly manifest: SDKManifest,
readonly migrations: Array<Migration<any>>, readonly migrations: Array<Migration<any, WD>>,
) {} ) {}
private sortedMigrations = once(() => { private sortedMigrations = once(() => {
const migrationsAsVersions = (this.migrations as Array<Migration<any>>).map( const migrationsAsVersions = (
(x) => [EmVer.parse(x.options.version), x] as const, this.migrations as Array<Migration<any, WD>>
) ).map((x) => [EmVer.parse(x.options.version), x] as const)
migrationsAsVersions.sort((a, b) => a[0].compareForSort(b[0])) migrationsAsVersions.sort((a, b) => a[0].compareForSort(b[0]))
return migrationsAsVersions return migrationsAsVersions
}) })
private currentVersion = once(() => EmVer.parse(this.manifest.version)) private currentVersion = once(() => EmVer.parse(this.manifest.version))
static of<Migrations extends Array<Migration<any>>>( static of<Migrations extends Array<Migration<any, WD>>, WD>(
manifest: SDKManifest, manifest: SDKManifest,
...migrations: EnsureUniqueId<Migrations> ...migrations: EnsureUniqueId<Migrations, WD>
) { ) {
return new Migrations(manifest, migrations as Array<Migration<any>>) return new Migrations(manifest, migrations as Array<Migration<any, WD>>)
} }
async init({ async init({
effects, effects,
utils,
previousVersion, previousVersion,
}: Parameters<ExpectedExports.init>[0]) { }: Parameters<ExpectedExports.init<WD>>[0]) {
if (!!previousVersion) { if (!!previousVersion) {
const previousVersionEmVer = EmVer.parse(previousVersion) const previousVersionEmVer = EmVer.parse(previousVersion)
for (const [_, migration] of this.sortedMigrations() for (const [_, migration] of this.sortedMigrations()
.filter((x) => x[0].greaterThan(previousVersionEmVer)) .filter((x) => x[0].greaterThan(previousVersionEmVer))
.filter((x) => x[0].lessThanOrEqual(this.currentVersion()))) { .filter((x) => x[0].lessThanOrEqual(this.currentVersion()))) {
await migration.up({ effects }) await migration.up({ effects, utils })
} }
} }
} }
async uninit({ async uninit({
effects, effects,
utils,
nextVersion, nextVersion,
}: Parameters<ExpectedExports.uninit>[0]) { }: Parameters<ExpectedExports.uninit<WD>>[0]) {
if (!!nextVersion) { if (!!nextVersion) {
const nextVersionEmVer = EmVer.parse(nextVersion) const nextVersionEmVer = EmVer.parse(nextVersion)
const reversed = [...this.sortedMigrations()].reverse() const reversed = [...this.sortedMigrations()].reverse()
for (const [_, migration] of reversed for (const [_, migration] of reversed
.filter((x) => x[0].greaterThan(nextVersionEmVer)) .filter((x) => x[0].greaterThan(nextVersionEmVer))
.filter((x) => x[0].lessThanOrEqual(this.currentVersion()))) { .filter((x) => x[0].lessThanOrEqual(this.currentVersion()))) {
await migration.down({ effects }) await migration.down({ effects, utils })
} }
} }
} }
} }
export function setupMigrations<Migrations extends Array<Migration<any>>>( export function setupMigrations<
manifest: SDKManifest, Migrations extends Array<Migration<any, WD>>,
...migrations: EnsureUniqueId<Migrations> WD,
) { >(manifest: SDKManifest, ...migrations: EnsureUniqueId<Migrations, WD>) {
return Migrations.of(manifest, ...migrations) return Migrations.of(manifest, ...migrations)
} }
// prettier-ignore // prettier-ignore
export type EnsureUniqueId<A, B = A, ids = never> = export type EnsureUniqueId<A, WD, B = A, ids = never> =
B extends [] ? A : B extends [] ? A :
B extends [Migration<infer id>, ...infer Rest] ? ( B extends [Migration<infer id, WD>, ...infer Rest] ? (
id extends ids ? "One of the ids are not unique"[] : id extends ids ? "One of the ids are not unique"[] :
EnsureUniqueId<A, Rest, id | ids> EnsureUniqueId<A, Rest, id | ids>
) : "There exists a migration that is not a Migration"[] ) : "There exists a migration that is not a Migration"[]

View File

@@ -3,13 +3,13 @@ import { Migrations } from "./migrations/setupMigrations"
import { Install } from "./setupInstall" import { Install } from "./setupInstall"
import { Uninstall } from "./setupUninstall" import { Uninstall } from "./setupUninstall"
export function setupInit<WrapperData>( export function setupInit<WD>(
migrations: Migrations, migrations: Migrations<WD>,
install: Install<WrapperData>, install: Install<WD>,
uninstall: Uninstall<WrapperData>, uninstall: Uninstall<WD>,
): { ): {
init: ExpectedExports.init init: ExpectedExports.init<WD>
uninit: ExpectedExports.uninit uninit: ExpectedExports.uninit<WD>
} { } {
return { return {
init: async (opts) => { init: async (opts) => {

View File

@@ -1,24 +1,24 @@
import { Effects, ExpectedExports } from "../types" import { Effects, ExpectedExports } from "../types"
import { Utils, utils } from "../util" import { Utils, utils } from "../util"
export type InstallFn<WrapperData> = (opts: { export type InstallFn<WD> = (opts: {
effects: Effects effects: Effects
utils: Utils<WrapperData> utils: Utils<WD>
}) => Promise<void> }) => Promise<void>
export class Install<WrapperData> { export class Install<WD> {
private constructor(readonly fn: InstallFn<WrapperData>) {} private constructor(readonly fn: InstallFn<WD>) {}
static of<WrapperData>(fn: InstallFn<WrapperData>) { static of<WD>(fn: InstallFn<WD>) {
return new Install(fn) return new Install(fn)
} }
async init({ async init({
effects, effects,
previousVersion, previousVersion,
}: Parameters<ExpectedExports.init>[0]) { }: Parameters<ExpectedExports.init<WD>>[0]) {
if (!previousVersion) await this.fn({ effects, utils: utils(effects) }) if (!previousVersion) await this.fn({ effects, utils: utils(effects) })
} }
} }
export function setupInstall<WrapperData>(fn: InstallFn<WrapperData>) { export function setupInstall<WD>(fn: InstallFn<WD>) {
return Install.of(fn) return Install.of(fn)
} }

View File

@@ -1,24 +1,24 @@
import { Effects, ExpectedExports } from "../types" import { Effects, ExpectedExports } from "../types"
import { Utils, utils } from "../util" import { Utils, utils } from "../util"
export type UninstallFn<WrapperData> = (opts: { export type UninstallFn<WD> = (opts: {
effects: Effects effects: Effects
utils: Utils<WrapperData> utils: Utils<WD>
}) => Promise<void> }) => Promise<void>
export class Uninstall<WrapperData> { export class Uninstall<WD> {
private constructor(readonly fn: UninstallFn<WrapperData>) {} private constructor(readonly fn: UninstallFn<WD>) {}
static of<WrapperData>(fn: UninstallFn<WrapperData>) { static of<WD>(fn: UninstallFn<WD>) {
return new Uninstall(fn) return new Uninstall(fn)
} }
async uninit({ async uninit({
effects, effects,
nextVersion, nextVersion,
}: Parameters<ExpectedExports.uninit>[0]) { }: Parameters<ExpectedExports.uninit<WD>>[0]) {
if (!nextVersion) await this.fn({ effects, utils: utils(effects) }) if (!nextVersion) await this.fn({ effects, utils: utils(effects) })
} }
} }
export function setupUninstall<WrapperData>(fn: UninstallFn<WrapperData>) { export function setupUninstall<WD>(fn: UninstallFn<WD>) {
return Uninstall.of(fn) return Uninstall.of(fn)
} }

View File

@@ -1,6 +1,7 @@
export * as configTypes from "./config/configTypes" export * as configTypes from "./config/configTypes"
import { InputSpec } from "./config/configTypes" import { InputSpec } from "./config/configTypes"
import { DependenciesReceipt } from "./config/setupConfig" import { DependenciesReceipt } from "./config/setupConfig"
import { Utils } from "./util"
export type ExportedAction = (options: { export type ExportedAction = (options: {
effects: Effects effects: Effects
@@ -66,15 +67,17 @@ export namespace ExpectedExports {
* Every time a package completes an install, this function is called before the main. * Every time a package completes an install, this function is called before the main.
* Can be used to do migration like things. * Can be used to do migration like things.
*/ */
export type init = (options: { export type init<WD> = (options: {
effects: Effects effects: Effects
utils: Utils<WD>
previousVersion: null | string previousVersion: null | string
}) => Promise<unknown> }) => Promise<unknown>
/** This will be ran during any time a package is uninstalled, for example during a update /** This will be ran during any time a package is uninstalled, for example during a update
* this will be called. * this will be called.
*/ */
export type uninit = (options: { export type uninit<WD> = (options: {
effects: Effects effects: Effects
utils: Utils<WD>
nextVersion: null | string nextVersion: null | string
}) => Promise<unknown> }) => Promise<unknown>

View File

@@ -101,14 +101,14 @@ export const utils = <WrapperData = never, WrapperOverWrite = { const: never }>(
generator: DefaultString generator: DefaultString
}) => { }) => {
if (value) { if (value) {
await effects.vaultSet({ key, value }) await effects.vault.set({ key, value })
return value return value
} }
if (await effects.vaultList().then((x) => x.includes(key))) { if (await effects.vault.list().then((x) => x.includes(key))) {
return null return null
} }
const newValue = getDefaultString(generator) const newValue = getDefaultString(generator)
await effects.vaultSet({ key, value: newValue }) await effects.vault.set({ key, value: newValue })
return newValue return newValue
}, },
getSystemSmtp: () => getSystemSmtp: () =>