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

Require a database or secret on schema commands #498

Merged
merged 6 commits into from
Dec 10, 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
2 changes: 2 additions & 0 deletions src/commands/schema/schema.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//@ts-check

import { validateDatabaseOrSecret } from "../../lib/command-helpers.mjs";
import abandonCommand from "./abandon.mjs";
import commitCommand from "./commit.mjs";
import diffCommand from "./diff.mjs";
Expand All @@ -25,6 +26,7 @@ function buildSchema(yargs) {
.command(pushCommand)
.command(pullCommand)
.command(statusCommand)
.check(validateDatabaseOrSecret)
.demandCommand();
}

Expand Down
24 changes: 14 additions & 10 deletions src/commands/schema/status.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "../../lib/command-helpers.mjs";
import { getSecret } from "../../lib/fauna-client.mjs";
import { reformatFSL } from "../../lib/schema.mjs";
import { localSchemaOptions } from "./schema.mjs";

async function doStatus(argv) {
const logger = container.resolve("logger");
Expand All @@ -32,6 +33,7 @@ async function doStatus(argv) {
staged: "true",
version: statusResponse.version,
});

const validationResponse = await makeFaunaRequest({
argv,
path: "/schema/1/validate",
Expand Down Expand Up @@ -64,16 +66,18 @@ async function doStatus(argv) {
}

function buildStatusCommand(yargs) {
return yargsWithCommonQueryOptions(yargs).example([
[
"$0 schema status --database us/example",
"Get the staged schema status for the 'us/example' database.",
],
[
"$0 schema status --secret my-secret",
"Get the staged schema status for the database scoped to a secret.",
],
]);
return yargsWithCommonQueryOptions(yargs)
.options(localSchemaOptions)
.example([
[
"$0 schema status --database us/example",
"Get the staged schema status for the 'us/example' database.",
],
[
"$0 schema status --secret my-secret",
"Get the staged schema status for the database scoped to a secret.",
],
]);
}

export default {
Expand Down
35 changes: 35 additions & 0 deletions test/schema/schema.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//@ts-check

import { expect } from "chai";
import chalk from "chalk";

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

describe("schema", function () {
let container, logger;
beforeEach(() => {
container = setupContainer();
logger = container.resolve("logger");
});

[
{ command: "schema status" },
{ command: "schema push" },
{ command: "schema abandon" },
{ command: "schema diff" },
{ command: "schema pull" },
{ command: "schema commit" },
].forEach(({ command }) => {
it(`requires a database or secret to call: ${command}`, async function () {
try {
await run(command, container);
} catch (e) {}

expect(logger.stderr).to.have.been.calledWith(
`${chalk.reset(await builtYargs.getHelp())}\n\n${chalk.red("No database or secret specified. Please use either --database, --secret, or --local to connect to your desired Fauna database.")}`,
);
expect(container.resolve("parseYargs")).to.have.been.calledOnce;
});
});
});
Loading