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

[FE-6127] Delete Database #437

Merged
merged 5 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 2 additions & 3 deletions src/commands/database/create.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//@ts-check

import { FaunaError, fql } from "fauna";

import { container } from "../../cli.mjs";
import { commonQueryOptions } from "../../lib/command-helpers.mjs";
import { throwForV10Error } from "../../lib/fauna.mjs";
import { commonQueryOptions } from "../../lib/command-helpers.mjs";

async function createDatabase(argv) {
const logger = container.resolve("logger");
Expand All @@ -21,7 +20,7 @@ async function createDatabase(argv) {
priority: ${argv.priority ?? null},
})`,
});
logger.stdout(`Database ${argv.name} created`);
logger.stdout(`Database '${argv.name}' was successfully created.`);
} catch (e) {
if (e instanceof FaunaError) {
throwForV10Error(e, {
Expand Down
6 changes: 4 additions & 2 deletions src/commands/database/database.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//@ts-check

import createCommand from "./create.mjs";
import listCommand from "./list.mjs";
import createCommand from "./create.mjs";
import deleteCommand from "./delete.mjs";

function buildDatabase(yargs) {
return yargs
Expand All @@ -12,8 +13,9 @@ function buildDatabase(yargs) {
default: "default",
},
})
.command(createCommand)
.command(listCommand)
.command(createCommand)
.command(deleteCommand)
.demandCommand()
.version(false)
.help("help", "show help");
Expand Down
49 changes: 49 additions & 0 deletions src/commands/database/delete.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//@ts-check

import { FaunaError, fql } from "fauna";
import { container } from "../../cli.mjs";
import { throwForV10Error } from "../../lib/fauna.mjs";
import { commonQueryOptions } from "../../lib/command-helpers.mjs";

async function deleteDatabase(argv) {
const logger = container.resolve("logger");
const runV10Query = container.resolve("runV10Query");

try {
await runV10Query({
url: argv.url,
secret: argv.secret,
query: fql`Database.byName(${argv.name}).delete()`,
});
logger.stdout(`Database '${argv.name}' was successfully deleted.`);
} catch (e) {
if (e instanceof FaunaError) {
throwForV10Error(e, {
onDocumentNotFound: () =>
`Not found: Database '${argv.name}' not found. Please check the database name and try again.`,
});
}
throw e;
}
}

function buildDeleteCommand(yargs) {
return yargs
.options({
name: {
type: "string",
description: "the name of the database to delete",
},
...commonQueryOptions,
})
.demandOption("name")
henryfauna marked this conversation as resolved.
Show resolved Hide resolved
.version(false)
.help("help", "show help");
}

export default {
command: "delete",
description: "Deletes a database",
builder: buildDeleteCommand,
handler: deleteDatabase,
};
5 changes: 4 additions & 1 deletion src/lib/fauna.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const runV10Query = async ({
* @param {(e: ServiceError) => string} [handlers.onLimitExceeded] - Handler for rate/resource limit errors
* @param {(e: ServiceError) => string} [handlers.onTimeOut] - Handler for timeout errors
* @param {(e: ServiceError) => string} [handlers.onInternalError] - Handler for internal server errors
* @param {(e: ServiceError) => string} [handlers.onDocumentNotFound] - Handler for document not found errors
* @param {(e: ClientError) => string} [handlers.onClientError] - Handler for general client errors
* @param {(e: ClientClosedError) => string} [handlers.onClientClosedError] - Handler for closed client errors
* @param {(e: NetworkError) => string} [handlers.onNetworkError] - Handler for network-related errors
Expand All @@ -121,7 +122,7 @@ export const throwForV10Error = (e, handlers = {}) => {
case "unauthorized":
throw new Error(
handlers.onUnauthorized?.(e) ??
"Authentication failed: Please either log in using 'fauna login' or provide a valid database secret with '--secret'",
"Authentication failed: Please either log in using 'fauna login' or provide a valid database secret with '--secret'.",
);
case "forbidden":
throw new Error(handlers.onForbidden?.(e) ?? e.message);
Expand All @@ -133,6 +134,8 @@ export const throwForV10Error = (e, handlers = {}) => {
throw new Error(handlers.onTimeOut?.(e) ?? e.message);
case "internal_error":
throw new Error(handlers.onInternalError?.(e) ?? e.message);
case "document_not_found":
throw new Error(handlers.onDocumentNotFound?.(e) ?? e.message);
default:
throw e;
}
Expand Down
9 changes: 4 additions & 5 deletions test/database/create.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//@ts-check

import * as awilix from "awilix";
import { expect } from "chai";
import sinon from "sinon";
import chalk from "chalk";
import { expect } from "chai";
import * as awilix from "awilix";
import { fql, ServiceError } from "fauna";
import sinon from "sinon";

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

Expand Down Expand Up @@ -84,7 +83,7 @@ describe("database create", () => {
error: { code: "unauthorized", message: "whatever" },
}),
expectedMessage:
"Authentication failed: Please either log in using 'fauna login' or provide a valid database secret with '--secret'",
"Authentication failed: Please either log in using 'fauna login' or provide a valid database secret with '--secret'.",
},
].forEach(({ error, expectedMessage }) => {
it(`handles ${error.code} errors when calling fauna`, async () => {
Expand Down
89 changes: 89 additions & 0 deletions test/database/delete.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//@ts-check

import chalk from "chalk";
import sinon from "sinon";
import { expect } from "chai";
import * as awilix from "awilix";
import { fql, ServiceError } from "fauna";
import { builtYargs, run } from "../../src/cli.mjs";
import { setupTestContainer as setupContainer } from "../../src/config/setup-test-container.mjs";

describe("database delete", () => {
let container, logger, runV10Query;

beforeEach(() => {
// reset the container before each test
container = setupContainer();
logger = container.resolve("logger");
runV10Query = container.resolve("runV10Query");
});

[
{ missing: "name", command: "database delete --secret 'secret'" },
{ missing: "secret", command: "database delete --name 'name'" },
].forEach(({ missing, command }) => {
it(`requires a ${missing}`, async () => {
try {
await run(command, container);
} catch (e) {}

const message = `${chalk.reset(await builtYargs.getHelp())}\n\n${chalk.red(
`Missing required argument: ${missing}`,
)}`;
expect(logger.stderr).to.have.been.calledWith(message);
expect(container.resolve("parseYargs")).to.have.been.calledOnce;
});
});

[
{
args: "--name 'testdb' --secret 'secret'",
expected: { name: "testdb", secret: "secret" },
},
].forEach(({ args, expected }) => {
describe("calls fauna with the user specified arguments", () => {
it(`${args}`, async () => {
await run(`database create ${args}`, container);
expect(runV10Query).to.have.been.calledOnceWith({
url: sinon.match.string,
secret: expected.secret,
query: fql`Database.byName(${expected.name}).delete()`,
});
});
});
});

[
{
error: new ServiceError({
error: { code: "unauthorized", message: "whatever" },
}),
expectedMessage:
"Authentication failed: Please either log in using 'fauna login' or provide a valid database secret with '--secret'.",
},
{
error: new ServiceError({
error: { code: "document_not_found", message: "whatever" },
}),
expectedMessage:
"Not found: Database 'testdb' not found. Please check the database name and try again.",
},
].forEach(({ error, expectedMessage }) => {
it(`handles ${error.code} errors when calling fauna`, async () => {
const runV10QueryStub = sinon.stub().rejects(error);
container.register({
runV10Query: awilix.asValue(runV10QueryStub),
});

try {
await run(
`database delete --name 'testdb' --secret 'secret'`,
container,
);
} catch (e) {}

const message = `${chalk.reset(await builtYargs.getHelp())}\n\n${chalk.red(expectedMessage)}`;
expect(logger.stderr).to.have.been.calledWith(message);
});
});
});