Skip to content

Commit

Permalink
Add option to specify order in addMigration
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriLojda committed Nov 4, 2024
1 parent 4a6776a commit c2dfc79
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
11 changes: 9 additions & 2 deletions src/commands/migrations/add/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ export const register: RegisterCommand = yargs =>
alias: "d",
describe: "Sets the current DateTime in the order property and prefixes the migration name with it.",
type: "boolean",
default: false,
conflicts: ["order"],
})
.option("order", {
alias: "o",
describe: "Sets the order property to the provided value.",
type: "number",
conflicts: ["timestamp"],
}),
handler: args => addMigrationCli(args).catch(simplifyErrors),
});
Expand All @@ -43,7 +49,8 @@ type AddMigrationCliParams =
& Readonly<{
name: string;
migrationsFolder: string | undefined;
timestamp: boolean;
timestamp: boolean | undefined;
order: number | undefined;
type: string;
}>
& LogOptions;
Expand Down
6 changes: 3 additions & 3 deletions src/modules/migrations/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export type AddMigrationParams = Readonly<
& {
name: string;
migrationsFolder?: string;
timestamp?: boolean;
type: "js" | "ts";
}
& ({ timestamp: true } | { timestamp?: false; order?: number })
& LogOptions
>;

Expand All @@ -41,8 +41,8 @@ export const addMigration = async (params: AddMigrationParams) => {
params.timestamp ? formatDateForFileName(currentDate) : undefined,
);
const migrationData = params.type === "ts"
? generateTypescriptMigration(params.timestamp ? currentDate : undefined)
: generateJavascriptMigration(params.timestamp ? currentDate : undefined);
? generateTypescriptMigration(params.timestamp ? currentDate : params.order)
: generateJavascriptMigration(params.timestamp ? currentDate : params.order);

const migrationPath = path.join(folderPath, migrationName);

Expand Down
11 changes: 7 additions & 4 deletions src/modules/migrations/utils/migrationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,31 @@ export const formatDateForFileName = (date: Date) =>

export const getMigrationName = (name: string, type: "js" | "ts", prefix?: string) => `${prefix ?? ""}${name}.${type}`;

export const generateTypescriptMigration = (orderDate?: Date): string =>
export const generateTypescriptMigration = (order: Date | number | undefined): string =>
`import { MigrationModule } from "@kontent-ai/data-ops";
const migration: MigrationModule = {
order: ${orderDate ? `new Date('${orderDate.toISOString()}')` : "1"},
order: ${order === undefined ? "1" : createOrderPropertyValue(order)},
run: async apiClient => {},
rollback: async apiClient => {},
};
export default migration;
`;

export const generateJavascriptMigration = (orderDate?: Date | null): string =>
export const generateJavascriptMigration = (order: Date | number | undefined): string =>
`const migration = {
order: ${orderDate ? `new Date('${orderDate.toISOString()}')` : "1"},
order: ${order === undefined ? "1" : createOrderPropertyValue(order)},
run: async apiClient => {},
rollback: asyncapiClient => {},
};
module.exports = migration;
`;

const createOrderPropertyValue = (order: Date | number) =>
typeof order === "number" ? order.toString() : `new Date('${order.toISOString()}')`;

export const filterMigrations = (
migrations: ReadonlyArray<Migration>,
params: RunMigrationFilterParams,
Expand Down

0 comments on commit c2dfc79

Please sign in to comment.