From 94657b8b6ee426fb515bd501a5ca614d9a6c5469 Mon Sep 17 00:00:00 2001 From: Neil Macneale V Date: Tue, 10 Sep 2024 14:21:34 -0700 Subject: [PATCH] Add fauna schema status --- src/commands/schema/status.ts | 37 ++++++++++++++++++++++++ test/integ/schema.test.ts | 53 +++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/commands/schema/status.ts diff --git a/src/commands/schema/status.ts b/src/commands/schema/status.ts new file mode 100644 index 00000000..8803c243 --- /dev/null +++ b/src/commands/schema/status.ts @@ -0,0 +1,37 @@ +import SchemaCommand from "../../lib/schema-command"; + +export default class StatusSchemaCommand extends SchemaCommand { + static flags = { + ...SchemaCommand.flags, + }; + + static description = "Print the staged schema status."; + static examples = ["$ fauna schema status"]; + + async run() { + try { + const { url, secret } = await this.fetchsetup(); + const res = await fetch( + new URL("/schema/1/staged/status?diff=true", url), + { + method: "GET", + headers: { AUTHORIZATION: `Bearer ${secret}` }, + // https://github.com/nodejs/node/issues/46221 + // https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1483 + // @ts-expect-error-next-line + duplex: "half", + } + ); + + const json = await res.json(); + if (json.error) { + this.error(json.error.message); + } + + this.log(json.diff); + } catch (err) { + console.log(err); + this.error(err); + } + } +} diff --git a/test/integ/schema.test.ts b/test/integ/schema.test.ts index a990675d..dc4943f9 100644 --- a/test/integ/schema.test.ts +++ b/test/integ/schema.test.ts @@ -43,3 +43,56 @@ it("fauna schema push --stage --force works", async () => { ) ).to.deep.equal(null); }); + +const status = async (secret: string) => { + const output = await shellOk("fauna schema status --dir .", secret); + + // Remove the "Connected to endpoint" line. + return output.split("\n").slice(1).join("\n"); +}; + +it.only("fauna schema status works", async () => { + const secret = await newDB(); + + await shellOk( + "fauna schema push --dir test/integ/schema/start --force", + secret + ); + + expect(await status(secret)).to.equal( + stripMargin( + `|Status: none + |` + ) + ); + + await shellOk( + "fauna schema push --dir test/integ/schema/staged_index --force --stage", + secret + ); + + expect(await status(secret)).to.equal( + stripMargin( + `|Status: ready + |The schema is ready to be committed. + | + |Staged changes: + |* Modifying collection \`User\` at 0:90/main.fsl: + | * Summary: + | + added: index \`byName\` (see diff) + | + | * Diff: + | collection User { + | name: String + | email: String + | + + | + index byName { + | + terms [.name] + | + } + | } + | + | + |` + ) + ); +});