-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add schema push --stage #355
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { runCommand } from "@oclif/test"; | ||
import { fail } from "assert"; | ||
import { env } from "process"; | ||
|
||
export type ShellResult = { stdout: string; stderr: string; ok: boolean }; | ||
|
||
const TEST_PREFIX = "fauna_shell_integ_test_"; | ||
|
||
export const newDB = async (secret?: string): Promise<string> => { | ||
const name = TEST_PREFIX + Math.floor(Math.random() * 1000000000); | ||
|
||
return evalOk<string>( | ||
stripMargin( | ||
`|if (Database.byName('${name}').exists()) { | ||
| Database.byName('${name}').delete() | ||
|} | ||
|Database.create({ name: '${name}', typechecked: true }) | ||
|Key.create({ role: 'admin', database: '${name}' }).secret | ||
|` | ||
), | ||
secret | ||
); | ||
}; | ||
|
||
export const cleanupDBs = async (): Promise<void> => { | ||
const { url, secret } = endpoint(); | ||
|
||
const query = stripMargin( | ||
`|Database.all().forEach((db) => { | ||
| if (db.name.startsWith('${TEST_PREFIX}')) { | ||
| db.delete() | ||
| } | ||
|}) | ||
|` | ||
); | ||
|
||
const res = await fetch(new URL("/query/1", url), { | ||
method: "POST", | ||
headers: { AUTHORIZATION: `Bearer ${secret}` }, | ||
body: JSON.stringify({ query }), | ||
// @ts-expect-error-next-line | ||
duplex: "half", | ||
}); | ||
|
||
if (res.status !== 200) { | ||
fail(`Cleanup failed: ${await res.text()}`); | ||
} | ||
}; | ||
|
||
export const evalOk = async <T>(code: string, secret?: string): Promise<T> => { | ||
const res = JSON.parse( | ||
await shellOk(`fauna eval "${code}" --format json`, secret) | ||
); | ||
// FIXME: This should really fail `shellOk`, but error handling is hard. | ||
if (res?.error) { | ||
fail(`Eval failed: ${res.summary}`); | ||
} | ||
|
||
return res; | ||
}; | ||
|
||
export const shellOk = async ( | ||
cmd: string, | ||
secret?: string | ||
): Promise<string> => { | ||
const res = await shell(cmd, secret); | ||
if (!res.ok) { | ||
fail(`Command unexpectedly failed:\n${res.stderr}`); | ||
} | ||
|
||
return res.stdout; | ||
}; | ||
|
||
export const shellErr = async (cmd: string): Promise<string> => { | ||
const res = await shell(cmd); | ||
if (res.ok) { | ||
fail(`Command should not have exitted succesfully:\n${res.stdout}`); | ||
} | ||
|
||
return res.stderr; | ||
}; | ||
|
||
export const stripMargin = (str: string): string => { | ||
return str | ||
.split("\n") | ||
.map((line) => { | ||
const trimmed = line.trimStart(); | ||
if (trimmed.startsWith("|")) { | ||
return trimmed.slice(1); | ||
} else { | ||
return trimmed; | ||
} | ||
}) | ||
.join("\n"); | ||
}; | ||
|
||
export const shell = async ( | ||
cmd: string, | ||
secret?: string | ||
): Promise<ShellResult> => { | ||
const parts = cmd.split(" "); | ||
if (parts[0] !== "fauna") { | ||
fail("Command must start with fauna"); | ||
} | ||
|
||
const { url, secret: s } = endpoint(); | ||
|
||
const opts = [ | ||
parts.slice(1).join(" "), | ||
`--url ${url}`, | ||
`--secret ${secret ?? s}`, | ||
]; | ||
|
||
const out = await runCommand(opts); | ||
|
||
return { | ||
stdout: out.stdout, | ||
stderr: out.stderr + out.error?.message, | ||
ok: out.error === undefined, | ||
}; | ||
}; | ||
|
||
const endpoint = () => { | ||
return { | ||
url: `${env.FAUNA_SCHEME ?? "http"}://${env.FAUNA_DOMAIN ?? "127.0.0.1"}:${ | ||
env.FAUNA_PORT ?? 8443 | ||
}`, | ||
secret: env.FAUNA_SECRET ?? "secret", | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { expect } from "chai"; | ||
import { cleanupDBs, evalOk, newDB, shellOk, stripMargin } from "./base"; | ||
|
||
// FIXME: Once we get `nock` out of here, we need to revive this test. It works | ||
// fine locally, but it causes the entire test run to freeze in CI. | ||
describe.skip("fauna schema staged commands", () => { | ||
// Cleanup after ourselves. | ||
after(async function () { | ||
await cleanupDBs(); | ||
}); | ||
|
||
it("fauna schema push --stage --force works", async () => { | ||
const secret = await newDB(); | ||
|
||
await shellOk( | ||
"fauna schema push --dir test/integ/schema/start --force", | ||
secret | ||
); | ||
|
||
expect( | ||
await evalOk("Collection.all().map(.name).toArray()", secret) | ||
).to.deep.equal(["User"]); | ||
|
||
await shellOk( | ||
"fauna schema push --dir test/integ/schema/staged_index --force --stage", | ||
secret | ||
); | ||
|
||
// Index should be in the FQL definition. | ||
expect( | ||
await evalOk("Collection.byName('User')!.indexes.byName", secret) | ||
).to.deep.equal({ | ||
terms: [ | ||
{ | ||
field: ".name", | ||
mva: false, | ||
}, | ||
], | ||
queryable: true, | ||
status: "complete", | ||
}); | ||
|
||
// But, the index should not be visible on the companion object. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for comments with intent |
||
expect( | ||
await evalOk( | ||
stripMargin( | ||
`|let user: Any = User | ||
|user.byName` | ||
), | ||
secret | ||
) | ||
).to.deep.equal(null); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
collection User { | ||
name: String | ||
email: String | ||
|
||
index byName { | ||
terms [.name] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
collection User { | ||
name: String | ||
email: String | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we just drop the "force" param for validate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can pass a
version
instead, so we just requireforce
orversion
to be set on all the schema endpoints. we could probably drop it, it just keeps the validation simple in the core endpoint.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, just thinking it might make using the API itself simpler. I forget/lost track if we're actually passing version to validate at any point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to double check, I think we might be using it in the dashboard to check if a schema version is stale. If we're not using it there, I'll go ahead and remove it from the core endpoint.