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

Schema commands update #403

Merged
merged 3 commits 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
17 changes: 7 additions & 10 deletions src/commands/schema/abandon.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ async function doAbandon(argv) {
const logger = container.resolve("logger");
const confirm = container.resolve("confirm");

if (argv.force) {
if (!argv.input) {
const params = new URLSearchParams({
force: "true", // Just abandon, don't pass a schema version through.
});

await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: `/schema/1/staged/abandon?${params}`,
secret: argv.secret,
method: "POST",
});
logger.stdout("Schema has been abandoned");
Expand All @@ -26,9 +25,8 @@ async function doAbandon(argv) {
if (argv.color) params.set("color", "ansi");

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

Expand All @@ -46,9 +44,8 @@ async function doAbandon(argv) {
const params = new URLSearchParams({ version: response.version });

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

Expand All @@ -62,10 +59,10 @@ async function doAbandon(argv) {
function buildAbandonCommand(yargs) {
return yargs
.options({
force: {
description: "Push the change without a diff or schema version check",
input: {
description: "Prompt for user input (e.g., confirmations)",
default: true,
type: "boolean",
default: false,
},
...commonQueryOptions,
})
Expand Down
17 changes: 7 additions & 10 deletions src/commands/schema/commit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ async function doCommit(argv) {
const logger = container.resolve("logger");
const confirm = container.resolve("confirm");

if (argv.force) {
if (!argv.input) {
const params = new URLSearchParams({
force: "true", // Just commit, don't pass a schema version through.
});

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

Expand All @@ -27,9 +26,8 @@ async function doCommit(argv) {
if (argv.color) params.set("color", "ansi");

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

Expand All @@ -50,9 +48,8 @@ async function doCommit(argv) {
const params = new URLSearchParams({ version: response.version });

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

Expand All @@ -66,10 +63,10 @@ async function doCommit(argv) {
function buildCommitCommand(yargs) {
return yargs
.options({
force: {
description: "Push the change without a diff or schema version check",
input: {
description: "Prompt for user input (e.g., confirmations)",
default: true,
type: "boolean",
default: false,
},
...commonQueryOptions,
})
Expand Down
113 changes: 94 additions & 19 deletions src/commands/schema/diff.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,122 @@ import { container } from "../../cli.mjs";
import { commonQueryOptions } from "../../lib/command-helpers.mjs";
import { reformatFSL } from "../../lib/schema.mjs";

/**
* @returns string[]
*/
function parseTarget(argv) {
if (!argv.active && !argv.staged) {
return ["staged", "local"];
}

if (argv.active && argv.staged) {
throw new Error("Cannot specify both --active and --staged");
}

if (argv.active) {
return ["active", "local"];
} else if (argv.staged) {
return ["active", "staged"];
} else {
throw new Error("Invalid target. Expected: active or staged");
}
}

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({ force: "true" });
const params = new URLSearchParams({});
if (argv.color) params.set("color", "ansi");
params.set("staged", argv.staged);

const response = await makeFaunaRequest({
baseUrl: argv.url,
path: new URL(`/schema/1/validate?${params}`, argv.url).href,
secret: argv.secret,
body: files,
method: "POST",
if (target === "staged") params.set("diff", diffKind);

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

const bold = argv.color ? chalk.bold : (str) => str;
const description = argv.staged ? "remote, staged" : "remote, active";
logger.stdout(
`Differences between the ${bold("local")} schema and the ${bold(
description,
)} schema:`,
);
logger.stdout(response.diff ? response.diff : "No schema differences");
if (target === "staged") {
logger.stdout(
`Differences from the ${chalk.bold("remote, active")} schema to the ${chalk.bold("remote, staged")} schema:`,
);
if (status === "none") {
logger.stdout("There is no staged schema present.");
} else {
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,
body: files,
method: "POST",
});

if (status === "none") {
logger.stdout(
`Differences from the ${chalk.bold("remote")} schema to the ${chalk.bold("local")} schema:`,
);
} else if (source === "active") {
logger.stdout(
`Differences from the ${chalk.bold("remote, active")} schema to the ${chalk.bold("local")} schema:`,
);
} else {
logger.stdout(
`Differences from the ${chalk.bold("remote, staged")} schema to the ${chalk.bold("local")} schema:`,
);
}
logger.stdout(diff ? diff : "No schema differences.");
}
}

function buildDiffCommand(yargs) {
return yargs
.options({
staged: {
description:
"Compare the local schema to the staged schema instead of the active schema.",
"Show the diff between the active and staged schema, instead of the local schema.",
default: false,
type: "boolean",
},
text: {
description: "Display the text diff instead of the semantic diff.",
default: false,
type: "boolean",
},
active: {
description:
"Show the diff against the active schema instead of the staged schema.",
default: false,
type: "boolean",
},
...commonQueryOptions,
})
.example([["$0 schema diff"], ["$0 schema diff --dir schemas/myschema"]])
.example([
["$0 schema diff"],
["$0 schema diff --dir schemas/myschema"],
["$0 schema diff --staged"],
["$0 schema diff --active --text"],
])
.version(false)
.help("help", "show help");
}
Expand Down
40 changes: 19 additions & 21 deletions src/commands/schema/pull.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@

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");
const gatherFSL = container.resolve("gatherFSL");
const confirm = container.resolve("confirm");
const getSchemaFiles = container.resolve("getSchemaFiles");
const getStagedSchemaStatus = container.resolve("getStagedSchemaStatus");

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

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

// if there's a staged schema, require the --staged flag.
// getting unstaged FSL while staged FSL exists is not yet
// 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.staged) {
if (statusResponse.status !== "none" && argv.active) {
throw new Error(
"There is a staged schema change. Use --staged to pull it.",
"There is a staged schema change. Remove the --active flag to pull it.",
);
} else if (statusResponse.status === "none" && argv.staged) {
} else if (statusResponse.status === "none" && !argv.active) {
throw new Error("There are no staged schema changes to pull.");
}

Expand Down Expand Up @@ -85,10 +86,7 @@ async function doPull(argv) {
const getAllSchemaFileContents = container.resolve(
"getAllSchemaFileContents",
);
const contents = await getAllSchemaFileContents(filenames, {
secret: argv.secret,
baseUrl: argv.url,
});
const contents = await getAllSchemaFileContents(filenames, argv);

// don't start writing or deleting files until we've successfully fetched all
// the remote schema files
Expand Down Expand Up @@ -117,16 +115,16 @@ function buildPullCommand(yargs) {
type: "boolean",
default: false,
},
staged: {
description: "Pulls staged schema instead of the active schema",
active: {
description: "Pulls the active schema instead of the staged schema",
type: "boolean",
default: false,
},
...commonQueryOptions,
})
.example([
["$0 schema pull"],
["$0 schema pull --staged"],
["$0 schema pull --active"],
["$0 schema pull --delete"],
])
.version(false)
Expand Down
Loading