Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sync and migrate-content bugs #95

Merged
merged 6 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kontent-ai/data-ops",
"version": "2.1.1",
"version": "2.1.2",
"description": "",
"type": "module",
"scripts": {
Expand Down
20 changes: 7 additions & 13 deletions src/commands/environment/clean/clean.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import chalk from "chalk";

import { logError, LogOptions } from "../../../log.js";
import {
CleanEntityChoices,
Expand All @@ -8,7 +6,7 @@ import {
CleanEnvironmentParams,
} from "../../../modules/backupRestore/clean.js";
import { resolveIncludeExcludeCliParams } from "../../../modules/backupRestore/utils/includeExclude.js";
import { requestConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
import { checkConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
import { RegisterCommand } from "../../../types/yargs.js";
import { createClient } from "../../../utils/client.js";
import { simplifyErrors } from "../../../utils/error.js";
Expand Down Expand Up @@ -74,16 +72,12 @@ type CleanEnvironmentCliParams =
const cleanEnvironmentCli = async (
params: CleanEnvironmentCliParams,
): Promise<void> => {
const warningMessage = chalk.yellow(
`⚠ Running this operation may result in irreversible changes to the content in environment ${params.environmentId}.\n\nOK to proceed y/n? (suppress this message with -s parameter)\n`,
);

const confirmed = !params.skipWarning ? await requestConfirmation(warningMessage) : true;

if (!confirmed) {
logError(params, chalk.red("Operation aborted."));
process.exit(1);
}
await checkConfirmation({
message:
`⚠ Running this operation may result in irreversible changes to the content in environment ${params.environmentId}.\n\nOK to proceed y/n? (suppress this message with -s parameter)\n`,
skipConfirmation: params.skipWarning,
logOptions: params,
});

const client = createClient({
environmentId: params.environmentId,
Expand Down
15 changes: 12 additions & 3 deletions src/commands/migrateContent/run/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { match, P } from "ts-pattern";

import { logError, LogOptions } from "../../../log.js";
import {
migrateContentRunIntenal,
migrateContentRunInternal,
MigrateContentRunParams,
} from "../../../modules/migrateContent/migrateContentRun.js";
import { checkConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
import { RegisterCommand } from "../../../types/yargs.js";
import { simplifyErrors } from "../../../utils/error.js";
import { omit } from "../../../utils/object.js";
Expand Down Expand Up @@ -143,7 +144,15 @@ type MigrateContentRunCliParams =
const migrateContentRunCli = async (params: MigrateContentRunCliParams) => {
const resolvedParams = resolveParams(params);

migrateContentRunIntenal(resolvedParams, "migrate-content-run");
migrateContentRunInternal(resolvedParams, "migrate-content-run", async () => {
await checkConfirmation({
message:
`⚠ Running this operation may result in irreversible changes to the content in environment ${params.targetEnvironmentId}.
OK to proceed y/n? (suppress this message with --sw parameter)\n`,
skipConfirmation: params.skipConfirmation,
logOptions: params,
});
});
};

const resolveParams = (params: MigrateContentRunCliParams): MigrateContentRunParams => {
Expand Down Expand Up @@ -177,7 +186,7 @@ const resolveParams = (params: MigrateContentRunCliParams): MigrateContentRunPar
.otherwise(() => {
logError(
params,
"You need to provide exactly one from parameters: --items or --items with --depth, --filter, --byTypesCodenames, --last with --sourceDeliveryPeviewKey",
"You need to provide exactly one from parameters: --items or --items with --depth, --filter, --byTypesCodenames, --last with --sourceDeliveryPreviewKey",
);
process.exit(1);
});
Expand Down
2 changes: 1 addition & 1 deletion src/commands/migrateContent/snapshot/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const resolveParams = (params: MigrateContentSnapshotCliParams): MigrateContentS
.otherwise(() => {
logError(
params,
"You need to provide exactly one from parameters: --items or --items with --depth, --filter, --byTypesCodenames, --last with --sourceDeliveryPeviewKey",
"You need to provide exactly one from parameters: --items or --items with --depth, --filter, --byTypesCodenames, --last with --sourceDeliveryPreviewKey",
);
process.exit(1);
});
18 changes: 7 additions & 11 deletions src/commands/migrations/run/run.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import chalk from "chalk";
import { match, P } from "ts-pattern";

import { logError, logInfo, LogOptions } from "../../../log.js";
import { RunMigrationsParams, withMigrationsToRun } from "../../../modules/migrations/run.js";
import { WithErr } from "../../../modules/migrations/utils/errUtils.js";
import { executeMigrations } from "../../../modules/migrations/utils/migrationUtils.js";
import { parseRange } from "../../../modules/migrations/utils/rangeUtils.js";
import { requestConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
import { checkConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
import { RegisterCommand } from "../../../types/yargs.js";
import { createClient } from "../../../utils/client.js";
import { simplifyErrors } from "../../../utils/error.js";
Expand Down Expand Up @@ -153,15 +152,12 @@ const runMigrationsCli = async (params: RunMigrationsCliParams) => {
await withMigrationsToRun(resolvedParams.value, async migrations => {
const operation = resolvedParams.value.rollback ? "rollback" : "run";

const warningMessage = chalk.yellow(
`⚠ Running this operation may result in irreversible changes to your environment ${params.environmentId}.\nOK to proceed y/n? (suppress this message with --skipConfirmation parameter)\n`,
);

const confirmed = !params.skipConfirmation ? await requestConfirmation(warningMessage) : true;

if (!confirmed) {
process.exit(0);
}
await checkConfirmation({
message:
`⚠ Running this operation may result in irreversible changes to your environment ${params.environmentId}.\nOK to proceed y/n? (suppress this message with --skipConfirmation parameter)\n`,
skipConfirmation: params.skipConfirmation,
logOptions: params,
});

const migrationsStatus = await executeMigrations(migrations, client, {
operation,
Expand Down
21 changes: 8 additions & 13 deletions src/commands/sync/run/run.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import chalk from "chalk";
import { match, P } from "ts-pattern";

import { logError, logInfo, LogOptions } from "../../../log.js";
import { logError, LogOptions } from "../../../log.js";
import { syncEntityChoices, SyncEntityName } from "../../../modules/sync/constants/entities.js";
import { printDiff } from "../../../modules/sync/printDiff.js";
import { SyncEntities, syncRunInternal, SyncRunParams } from "../../../modules/sync/syncRun.js";
import { requestConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
import { checkConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
import { RegisterCommand } from "../../../types/yargs.js";
import { simplifyErrors } from "../../../utils/error.js";

Expand Down Expand Up @@ -91,18 +90,14 @@ const syncRunCli = async (params: SyncModelRunCliParams) => {
await syncRunInternal(resolvedParams, `sync-${commandName}`, async (diffModel) => {
printDiff(diffModel, new Set(params.entities), params);

const warningMessage = chalk.yellow(
`⚠ Running this operation may result in irreversible changes to the content in environment ${params.targetEnvironmentId}. Mentioned changes might include:
await checkConfirmation({
message:
`⚠ Running this operation may result in irreversible changes to the content in environment ${params.targetEnvironmentId}. Mentioned changes might include:
- Removing content due to element deletion
OK to proceed y/n? (suppress this message with --sw parameter)\n`,
);

const confirmed = !params.skipConfirmation ? await requestConfirmation(warningMessage) : true;

if (!confirmed) {
logInfo(params, "standard", chalk.red("Operation aborted."));
process.exit(1);
}
skipConfirmation: params.skipConfirmation,
logOptions: params,
});
});
} catch (e) {
logError(params, JSON.stringify(e, Object.getOwnPropertyNames(e)));
Expand Down
10 changes: 8 additions & 2 deletions src/modules/migrateContent/migrateContentRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ export type MigrateContentFilterParams = Readonly<
>;

export const migrateContentRun = async (params: MigrateContentRunParams) => {
await migrateContentRunIntenal(params, "migrate-content-run-API");
await migrateContentRunInternal(params, "migrate-content-run-API");
};

export const migrateContentRunIntenal = async (params: MigrateContentRunParams, commandName: string) => {
export const migrateContentRunInternal = async (
params: MigrateContentRunParams,
commandName: string,
withItemCodenames: (itemsCodenames: ReadonlyArray<string>) => Promise<void> = () => Promise.resolve(),
) => {
if ("filename" in params) {
const data = await extractAsync({ filename: params.filename });

Expand Down Expand Up @@ -85,6 +89,8 @@ export const migrateContentRunIntenal = async (params: MigrateContentRunParams,
}`,
);

await withItemCodenames(itemsCodenames);

await migrateAsync({
targetEnvironment: {
apiKey: params.targetApiKey,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/migrations/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const filterMigrationsToRun = (
logInfo(
params,
"standard",
`${operation === "run" ? "Running" : "Rollbacking"} ${migrationsToRun.length} migrations:\n${
`${operation === "run" ? "Running" : "Rolling back"} ${migrationsToRun.length} migrations:\n${
migrationsToRun.map(m => chalk.blue(m.name)).join("\n")
}\n`,
);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/sync/sync/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const updateContentTypesAndAddReferences = async (
) => {
const typesReplaceReferencesOps = typeOps.added.map(createUpdateReferencesOps);

if (!typesReplaceReferencesOps.length) {
if (!typesReplaceReferencesOps.length && !typeOps.updated.size) {
logInfo(logOptions, "standard", "No content types to update");
return;
}
Expand Down
21 changes: 20 additions & 1 deletion src/modules/sync/utils/consoleHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import readline from "node:readline";

export const requestConfirmation = async (message: string) => {
import chalk from "chalk";

import { logInfo, LogOptions } from "../../../log.js";

const requestConfirmation = async (message: string) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
Expand All @@ -13,3 +17,18 @@ export const requestConfirmation = async (message: string) => {
});
});
};

export const checkConfirmation = async (options: {
message: string;
skipConfirmation: boolean | undefined;
logOptions: LogOptions;
}) => {
const warningMessage = chalk.yellow(options.message);

const confirmed = !options.skipConfirmation ? await requestConfirmation(warningMessage) : true;

if (!confirmed) {
logInfo(options.logOptions, "standard", chalk.red("Operation aborted."));
process.exit(0);
}
};