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

clean up schema commands and tests, fix complexity lint warns #411

Merged
merged 1 commit into from
Oct 29, 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
25 changes: 15 additions & 10 deletions src/commands/eval.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export async function performQuery(client, fqlQuery, outputFile, flags) {
}
}

async function doEval(argv) {
function validateArgv(argv) {
const noSourceSet =
!argv.stdin && argv.query === undefined && argv.file === undefined;
if (noSourceSet) {
Expand All @@ -175,6 +175,20 @@ async function doEval(argv) {
);
}

// used to use ensureDbScopeClient
if (argv.dbname) throw new Error("Not currently supported!");

// used to use runQueries/wrapQueries
if (argv.stdin) throw new Error("Not currently supported!");

// used to use runQueries/wrapQueries
if (argv.file) throw new Error("Not currently supported!");
}

async function doEval(argv) {
validateArgv(argv);

// TODO: this used to be a scoped db client...
// const client = argv.dbname
// ? (
// await ensureDbScopeClient({
Expand All @@ -185,15 +199,6 @@ async function doEval(argv) {
// ).client
// : container.resolve("getSimpleClient")(argv);

// used to use ensureDbScopeClient
if (argv.dbname) throw new Error("Not currently supported!");

// used to use runQueries/wrapQueries
if (argv.stdin) throw new Error("Not currently supported!");

// used to use runQueries/wrapQueries
if (argv.file) throw new Error("Not currently supported!");

const client = await container.resolve("getSimpleClient")(argv);

const readQuery = argv.stdin || argv.file !== undefined;
Expand Down
10 changes: 6 additions & 4 deletions src/commands/schema/abandon.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ async function doAbandon(argv) {

await makeFaunaRequest({
argv,
path: `/schema/1/staged/abandon?${params}`,
path: "/schema/1/staged/abandon",
params,
method: "POST",
});
logger.stdout("Schema has been abandoned");
} else {
// Show status to confirm.
const params = new URLSearchParams({ diff: "true" });
if (argv.color) params.set("color", "ansi");

const response = await makeFaunaRequest({
argv,
path: `/schema/1/staged/status?${params}`,
path: "/schema/1/staged/status",
params,
method: "GET",
});

Expand All @@ -45,7 +46,8 @@ async function doAbandon(argv) {

await makeFaunaRequest({
argv,
path: `/schema/1/staged/abandon?${params}`,
path: "/schema/1/staged/abandon",
params,
method: "POST",
});

Expand Down
10 changes: 6 additions & 4 deletions src/commands/schema/commit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ async function doCommit(argv) {

await makeFaunaRequest({
argv,
path: `/schema/1/staged/commit?${params}`,
path: "/schema/1/staged/commit",
params,
method: "POST",
});

logger.stdout("Schema has been committed");
} else {
// Show status to confirm.
const params = new URLSearchParams({ diff: "true" });
if (argv.color) params.set("color", "ansi");

const response = await makeFaunaRequest({
argv,
path: `/schema/1/staged/status?${params}`,
path: "/schema/1/staged/status",
params,
method: "GET",
});

Expand All @@ -49,7 +50,8 @@ async function doCommit(argv) {

await makeFaunaRequest({
argv,
path: `/schema/1/staged/commit?${params}`,
path: "/schema/1/staged/commit",
params,
method: "POST",
});

Expand Down
46 changes: 28 additions & 18 deletions src/commands/schema/diff.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,45 @@ function parseTarget(argv) {
}
}

function buildStatusParams(argv) {
const params = new URLSearchParams({});
const [, target] = parseTarget(argv);
const diffKind = argv.text ? "textual" : "semantic";

if (target === "staged") params.set("diff", diffKind);

return params;
}

function buildValidateParams(argv, version) {
const [source] = parseTarget(argv);
const diffKind = argv.text ? "textual" : "semantic";
const params = new URLSearchParams({
diff: diffKind,
staged: String(source === "staged"),
});
if (version) {
params.set("version", version);
} else {
params.set("force", "true");
}

return params;
}

async function doDiff(argv) {
const [source, target] = parseTarget(argv);
const diffKind = argv.text ? "textual" : "semantic";

const gatherFSL = container.resolve("gatherFSL");
const logger = container.resolve("logger");
const makeFaunaRequest = container.resolve("makeFaunaRequest");

const files = reformatFSL(await gatherFSL(argv.dir));

const params = new URLSearchParams({});
if (argv.color) params.set("color", "ansi");
if (target === "staged") params.set("diff", diffKind);

const { version, status, diff } = await makeFaunaRequest({
argv,
path: "/schema/1/staged/status",
params,
params: buildStatusParams(argv),
method: "GET",
});

Expand All @@ -58,21 +79,10 @@ async function doDiff(argv) {
logger.stdout(diff ? diff : "No schema differences.");
}
} else {
const params = new URLSearchParams({
diff: diffKind,
staged: String(source === "staged"),
});
if (argv.color) params.set("color", "ansi");
if (version) {
params.set("version", version);
} else {
params.set("force", "true");
}

const { diff } = await makeFaunaRequest({
argv,
path: "/schema/1/validate",
params,
params: buildValidateParams(argv, version),
body: files,
method: "POST",
});
Expand Down
84 changes: 49 additions & 35 deletions src/commands/schema/pull.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,8 @@ import { container } from "../../cli.mjs";
import { commonQueryOptions } from "../../lib/command-helpers.mjs";
import { makeFaunaRequest } from "../../lib/db.mjs";

async function doPull(argv) {
const logger = container.resolve("logger");
async function determineFileState(argv, filenames) {
const gatherFSL = container.resolve("gatherFSL");
const confirm = container.resolve("confirm");

// fetch the list of remote FSL files
const filesResponse = await makeFaunaRequest({
argv,
path: "/schema/1/files",
method: "GET",
});

// check if there's a staged schema
const statusResponse = await makeFaunaRequest({
argv,
path: "/schema/1/staged/status",
params: new URLSearchParams({ version: filesResponse.version }),
method: "GET",
});

// if there's a staged schema, cannot use the --active flag.
// getting active FSL while staged FSL exists is not yet
// implemented at the service level.
if (statusResponse.status !== "none" && argv.active) {
throw new Error(
"There is a staged schema change. Remove the --active flag to pull it.",
);
} else if (statusResponse.status === "none" && !argv.active) {
throw new Error("There are no staged schema changes to pull.");
}

// sort for consistent order (it's nice for tests)
const filenames = filesResponse.files
.map((file) => file.filename)
.filter((name) => name.endsWith(".fsl"))
.sort();

// Gather local .fsl files to overwrite or delete.
const existing = (await gatherFSL(argv.dir)).map((file) => file.name);
Expand All @@ -63,6 +29,11 @@ async function doPull(argv) {
}
deletes.sort();

return { adds, deletes, existing, overwrites };
}

function logDiff({ argv, adds, overwrites, deletes }) {
const logger = container.resolve("logger");
logger.stdout("Pull will make the following changes:");
if (argv.delete) {
for (const deleteme of deletes) {
Expand All @@ -75,6 +46,49 @@ async function doPull(argv) {
for (const overwrite of overwrites) {
logger.stdout(`overwrite: ${overwrite}`);
}
}

async function doPull(argv) {
const logger = container.resolve("logger");
const confirm = container.resolve("confirm");

// fetch the list of remote FSL files
const filesResponse = await makeFaunaRequest({
argv,
path: "/schema/1/files",
method: "GET",
});

// sort for consistent order (it's nice for tests)
const filenames = filesResponse.files
.map((file) => file.filename)
.filter((name) => name.endsWith(".fsl"))
.sort();

// check if there's a staged schema
const statusResponse = await makeFaunaRequest({
argv,
path: "/schema/1/staged/status",
params: new URLSearchParams({ version: filesResponse.version }),
method: "GET",
});

// if there's a staged schema, cannot use the --active flag.
// getting active FSL while staged FSL exists is not yet
// implemented at the service level.
if (statusResponse.status !== "none" && argv.active) {
throw new Error(
"There is a staged schema change. Remove the --active flag to pull it.",
);
} else if (statusResponse.status === "none" && !argv.active) {
throw new Error("There are no staged schema changes to pull.");
}

const { adds, deletes, overwrites } = await determineFileState(
argv,
filenames,
);
logDiff({ argv, adds, deletes, overwrites });

const confirmed = await confirm({
message: "Accept the changes?",
Expand Down
1 change: 0 additions & 1 deletion src/commands/schema/push.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ async function doPush(argv) {
force: "true",
staged: argv.active ? "false" : "true",
});
if (argv.color) params.set("color", "ansi");

const response = await makeFaunaRequest({
argv,
Expand Down
2 changes: 0 additions & 2 deletions src/commands/schema/status.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ async function doStatus(argv) {
const makeFaunaRequest = container.resolve("makeFaunaRequest");

let params = new URLSearchParams({ diff: "summary" });
if (argv.color) params.set("color", "ansi");
const gatherFSL = container.resolve("gatherFSL");
const fsl = reformatFSL(await gatherFSL(argv.dir));

Expand All @@ -27,7 +26,6 @@ async function doStatus(argv) {
staged: "true",
version: statusResponse.version,
});
if (argv.color) params.set("color", "ansi");
const validationResponse = await makeFaunaRequest({
argv,
path: "/schema/1/validate",
Expand Down
15 changes: 10 additions & 5 deletions src/lib/db.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

import { container } from "../cli.mjs";

function buildParamsString({ argv, params, path }) {
const routesWithColor = ["/schema/1/staged/status", "/schema/1/validate"];
if (params && argv.color && routesWithColor.includes(path))
params.set("color", "ansi");
if (params && params.sort) params.sort();
const paramsString = params && params.size ? `?${params.toString()}` : "";
return paramsString;
}

/**
* @typedef {('GET'|'HEAD'|'OPTIONS'|'PATCH'|'PUT'|'POST'|'DELETE'|'PATCH')} method
*/
Expand All @@ -28,11 +37,7 @@ export async function makeFaunaRequest({
shouldThrow = true,
}) {
const fetch = container.resolve("fetch");
const routesWithColor = ["/schema/1/staged/status", "/schema/1/validate"];
if (params && argv.color && routesWithColor.includes(path))
params.set("color", "ansi");
if (params && params.sort) params.sort();
const paramsString = params && params.size ? `?${params.toString()}` : "";
const paramsString = buildParamsString({ argv, params, path });
let fullUrl;

try {
Expand Down
12 changes: 6 additions & 6 deletions test/schema/abandon.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tryToCatch from "try-to-catch";

import { builtYargs, run } from "../../src/cli.mjs";
import { setupTestContainer as setupContainer } from "../../src/config/setup-test-container.mjs";
import { commonFetchParams, f } from "../helpers.mjs";
import { buildUrl, commonFetchParams, f } from "../helpers.mjs";

describe("schema abandon", function () {
let diff =
Expand Down Expand Up @@ -36,7 +36,7 @@ describe("schema abandon", function () {

expect(fetch).to.have.been.calledOnce;
expect(fetch).to.have.been.calledWith(
"https://db.fauna.com/schema/1/staged/abandon?force=true",
buildUrl("/schema/1/staged/abandon", { force: "true" }),
{ ...commonFetchParams, method: "POST" },
);
expect(logger.stdout).to.have.been.calledWith("Schema has been abandoned");
Expand All @@ -63,11 +63,11 @@ describe("schema abandon", function () {
await run(`schema abandon --secret "secret"`, container);

expect(fetch).to.have.been.calledWith(
"https://db.fauna.com/schema/1/staged/status?diff=true&color=ansi",
buildUrl("/schema/1/staged/status", { diff: "true", color: "ansi" }),
{ ...commonFetchParams, method: "GET" },
);
expect(fetch).to.have.been.calledWith(
"https://db.fauna.com/schema/1/staged/abandon?version=1728677726190000",
buildUrl("/schema/1/staged/abandon", { version: "1728677726190000" }),
{ ...commonFetchParams, method: "POST" },
);
expect(logger.stdout).to.have.been.calledWith("Schema has been abandoned");
Expand All @@ -93,7 +93,7 @@ describe("schema abandon", function () {
expect(logger.stderr).to.have.been.calledWith(message);

expect(fetch).to.have.been.calledWith(
"https://db.fauna.com/schema/1/staged/status?diff=true&color=ansi",
buildUrl("/schema/1/staged/status", { diff: "true", color: "ansi" }),
{ ...commonFetchParams, method: "GET" },
);
});
Expand All @@ -117,7 +117,7 @@ describe("schema abandon", function () {

expect(fetch).to.have.been.calledOnce;
expect(fetch).to.have.been.calledWith(
"https://db.fauna.com/schema/1/staged/status?diff=true&color=ansi",
buildUrl("/schema/1/staged/status", { diff: "true", color: "ansi" }),
{ ...commonFetchParams, method: "GET" },
);
expect(logger.stdout).to.have.been.calledWith("Abandon cancelled");
Expand Down
Loading
Loading