Skip to content

Commit

Permalink
Resolve plugins path properly in migration commands
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriLojda committed Oct 30, 2024
1 parent ac25622 commit 2ce9dd4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
27 changes: 14 additions & 13 deletions src/modules/migrations/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from "path";

import { logInfo, LogOptions } from "../../log.js";
import { createClient } from "../../utils/client.js";
import { apply } from "../../utils/function.js";
import { AnyOnePropertyOf } from "../../utils/types.js";
import { Migration, MigrationOrder } from "./models/migration.js";
import { MigrationOperation, MigrationStatus, SaveStatus, Status, StatusPlugin } from "./models/status.js";
Expand Down Expand Up @@ -75,16 +76,17 @@ export const withMigrationsToRun = async (
) => {
const operation = params.rollback ? "rollback" : "run";

const absoluteDirectoryPath = path.resolve(params.migrationsFolder);
const absoluteStatusPath = apply(p => path.resolve(p), params.statusPlugins);

const { readStatus, saveStatus } = handleErr(
await loadStatusFunctions(params.statusPlugins, params.migrationsFolder),
await loadStatusFunctions(absoluteStatusPath, absoluteDirectoryPath),
params,
);

const status = handleErr(await loadStatus(readStatus), params);
const environmentStatus = status[params.environmentId] ?? [];

const absoluteDirectoryPath = path.resolve(params.migrationsFolder);

const migrations = handleErr(await loadMigrationFiles(absoluteDirectoryPath), params);
const migrationsToRun = filterMigrationsToRun(migrations, environmentStatus, operation, params);

Expand All @@ -100,16 +102,15 @@ export const withMigrationsToRun = async (
const loadStatusFunctions = async (
pluginsPath: string | undefined,
migrationsFolder: string,
): Promise<WithErr<StatusPlugin>> => {
const absoluteDirectoryPath = path.resolve(migrationsFolder);

return pluginsPath ? await loadStatusPlugin(pluginsPath) : Promise.resolve({
value: {
readStatus: createDefaultReadStatus(absoluteDirectoryPath),
saveStatus: createDefaultWriteStatus(absoluteDirectoryPath),
},
});
};
): Promise<WithErr<StatusPlugin>> =>
pluginsPath
? await loadStatusPlugin(pluginsPath)
: Promise.resolve({
value: {
readStatus: createDefaultReadStatus(migrationsFolder),
saveStatus: createDefaultWriteStatus(migrationsFolder),
},
});

const filterMigrationsToRun = (
migrations: ReadonlyArray<Migration>,
Expand Down
12 changes: 6 additions & 6 deletions src/modules/migrations/utils/statusUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from "fs";
import * as path from "path";
import * as fs from "node:fs";
import * as path from "node:path";

import {
MigrationOperation,
Expand Down Expand Up @@ -38,12 +38,12 @@ export const createDefaultWriteStatus = (folderPath: string) => async (data: Sta
fs.writeFileSync(statusPath, JSON.stringify(data, null, 2), { flag: "w" });
};

export const loadStatusPlugin = async (path: string): Promise<WithErr<StatusPlugin>> => {
if (!fs.existsSync(path)) {
return { err: `Provided plugins path ${path} does not exists` };
export const loadStatusPlugin = async (pluginsPath: string): Promise<WithErr<StatusPlugin>> => {
if (!fs.existsSync(pluginsPath)) {
return { err: `Provided plugins path ${pluginsPath} does not exists` };
}

const pluginModule = await import(path);
const pluginModule = await import(pluginsPath);

return { value: statusPluginSchema.parse(pluginModule) };
};
Expand Down

0 comments on commit 2ce9dd4

Please sign in to comment.