Skip to content

Commit

Permalink
fix(cli): pass initial flag to the task
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Jan 15, 2024
1 parent c0fd703 commit 9b1b155
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-houses-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@datatruck/cli": patch
---

Fix initial restore
8 changes: 5 additions & 3 deletions packages/cli/src/actions/RestoreAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ export class RestoreAction<TRequired extends boolean = true> {
);
const repo = await createAndInitRepo(repoConfig, this.options.verbose);

if (this.options.initial) pkg = { ...pkg, restorePath: pkg.path };

let snapshotPath = pkg.restorePath ?? pkg.path;

await data.gc.cleanupIfFail(async () => {
Expand Down Expand Up @@ -263,14 +261,18 @@ export class RestoreAction<TRequired extends boolean = true> {
},
exitOnError: false,
run: async (listTask) => {
const pkg = resolvePackage(
let pkg = resolvePackage(
findPackageOrFail(this.config, snapshot.packageName),
{
snapshotId: options.snapshotId,
snapshotDate: snapshot.date,
action: "restore",
},
);

if (this.options.initial)
pkg = { ...pkg, restorePath: pkg.path };

const gc = new GargabeCollector();
const task = pkg.task ? createTask(pkg.task) : undefined;
const restore = await this.restore({
Expand Down
36 changes: 34 additions & 2 deletions packages/cli/test/datatruck.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { scriptTaskCode } from "../src/tasks/ScriptTask";
import { createCommands } from "../src/utils/datatruck/command";
import { existsFile } from "../src/utils/fs";
import { existsFile, readTextFile } from "../src/utils/fs";
import { parseStringList } from "../src/utils/string";
import { mkTmpDir } from "../src/utils/temp";
import { runBackups, runRestores } from "./expect";
import { fileChanges } from "./fileChanges";
import {
Expand All @@ -10,7 +12,7 @@ import {
FileChanges,
testRepositoryTypes,
} from "./util";
import { readFile, rm, writeFile } from "fs/promises";
import { readFile, rm, rmdir, writeFile } from "fs/promises";
import { join } from "path";
import { describe, expect, it } from "vitest";

Expand Down Expand Up @@ -279,6 +281,36 @@ describe(
expect(snapshot.repositoryName).toBe(repo.name);
expect(snapshot.repositoryType).toBe(repo.type);
});

it.each(repositoryTypes)("initial restore of %s", async (type) => {
const repo = await makeRepositoryConfig(type);
const fileChanger = await createFileChanger();
const restorePath = await mkTmpDir("restorePath");
const config = await makeConfig({
repositories: [repo],
packages: [
{
name: "main/files",
path: fileChanger.path,
restorePath,
repositoryNames: [type],
},
],
});

const restoredFile = `${restorePath}/file1`;
const dtt = createCommands({ config });
const [{ id }] = await runBackups(config, fileChanger, [
{ file1: "abc" },
]);

await dtt.restore({ id });
await expect(readTextFile(restoredFile)).resolves.toBe("abc");
await expect(dtt.restore({ id, initial: true })).rejects.toThrowError();
await rm(fileChanger.path, { recursive: true });
await dtt.restore({ id, initial: true });
await expect(readTextFile(restoredFile)).resolves.toBe("abc");
});
},
{
timeout: 300_000,
Expand Down

0 comments on commit 9b1b155

Please sign in to comment.