Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(review): tell an absent policy from a failed refresh

rebelice041972463d6c
diff --git a/frontend/src/routes/project/ProjectSQLReviewPage.test.tsx b/frontend/src/routes/project/ProjectSQLReviewPage.test.tsx
index 0846798b78..d7b29d7fa8 100644
--- a/frontend/src/routes/project/ProjectSQLReviewPage.test.tsx
+++ b/frontend/src/routes/project/ProjectSQLReviewPage.test.tsx
@@ -60,7 +60,8 @@ vi.mock("@/components/RouterLink", () => ({
}));
// Mirrors the real store: a fetch of a missing policy caches one with no
-// payload, and a delete drops the cached entry.
+// payload and resolves to null, a failed read resolves to undefined and
+// leaves the cache as it was, and a delete drops the cached entry.
vi.mock("@/stores/app", async () => {
const { create: createStore } = await import("zustand");
const { create: createMessage } = await import("@bufbuild/protobuf");
@@ -73,6 +74,10 @@ vi.mock("@/stores/app", async () => {
getPolicyByParentAndType: (params: {
parentPath: string;
}) => StorePolicy | undefined;
+ fetchPolicyByParentAndType: (params: {
+ parentPath: string;
+ refresh?: boolean;
+ }) => Promise<StorePolicy | null | undefined>;
getOrFetchPolicyByParentAndType: (params: {
parentPath: string;
refresh?: boolean;
@@ -85,7 +90,7 @@ vi.mock("@/stores/app", async () => {
}>()((set, get) => ({
policies: {},
getPolicyByParentAndType: ({ parentPath }) => get().policies[parentPath],
- getOrFetchPolicyByParentAndType: async (params) => {
+ fetchPolicyByParentAndType: async (params) => {
mocks.fetchPolicy(params);
if (mocks.unreadable.has(params.parentPath)) return undefined;
if (!get().policies[params.parentPath]) {
@@ -97,9 +102,12 @@ vi.mock("@/stores/app", async () => {
}),
},
}));
+ return null;
}
return get().policies[params.parentPath];
},
+ getOrFetchPolicyByParentAndType: async (params) =>
+ (await get().fetchPolicyByParentAndType(params)) ?? undefined,
upsertPolicy: async (params) => {
await mocks.upsertPolicy(params);
const saved = {
@@ -400,7 +408,12 @@ describe("ProjectSQLReviewPage", () => {
).toBeInTheDocument();
});
- test("moving to another project drops the draft", async () => {
+ test("moving to another project drops the draft and the remembered rules", async () => {
+ seedPolicies({
+ [WORKSPACE]: workspacePolicy(),
+ // Customized with SYNTAX off.
+ "projects/q": reviewRulePolicy("projects/q", []),
+ });
const { rerender } = render(<ProjectSQLReviewPage projectId="p" />);
const customize = await screen.findByRole("switch", {
name: "sql-review.standard-rules.customize.self",
@@ -416,8 +429,37 @@ describe("ProjectSQLReviewPage", () => {
expect(mocks.fetchPolicy).toHaveBeenCalledWith(
expect.objectContaining({ parentPath: "projects/q" })
);
- expect(customizeOther).toHaveAttribute("aria-checked", "false");
+ expect(customizeOther).toHaveAttribute("aria-checked", "true");
expect(updateButton()).not.toBeInTheDocument();
+
+ // Switching SYNTAX on starts from every rule, not from what the first
+ // project had on.
+ fireEvent.click(ruleSwitch("syntax"));
+ expect(ruleSwitch("disallow-truncate")).toHaveAttribute(
+ "aria-checked",
+ "true"
+ );
+ });
+
+ test("a retry that fails again keeps the error, whatever the cache holds", async () => {
+ mocks.unreadable.add(PROJECT);
+ render(<ProjectSQLReviewPage projectId="p" />);
+ await screen.findByRole("alert");
+
+ // The workspace policy read fine and is cached; now only its read fails.
+ mocks.unreadable.clear();
+ mocks.unreadable.add(WORKSPACE);
+ fireEvent.click(
+ screen.getByRole("button", { name: "sql-review.standard-rules.retry" })
+ );
+
+ expect(await screen.findByRole("alert")).toBeInTheDocument();
+ expect(screen.queryAllByRole("switch")).toHaveLength(0);
+ expect(mocks.fetchPolicy).toHaveBeenLastCalledWith({
+ parentPath: WORKSPACE,
+ policyType: PolicyType.REVIEW_RULE,
+ refresh: true,
+ });
});
test("a role held only on the project loads without the workspace policy", async () => {
diff --git a/frontend/src/routes/project/ProjectSQLReviewPage.tsx b/frontend/src/routes/project/ProjectSQLReviewPage.tsx
index 369e1d01ae..1c979fa23a 100644
--- a/frontend/src/routes/project/ProjectSQLReviewPage.tsx
+++ b/frontend/src/routes/project/ProjectSQLReviewPage.tsx
@@ -100,15 +100,13 @@ export function useProjectStandardRules(
}));
void Promise.all(
finds.map((find) =>
- store.getOrFetchPolicyByParentAndType({ ...find, refresh: attempt > 0 })
+ store.fetchPolicyByParentAndType({ ...find, refresh: attempt > 0 })
)
- ).then(() => {
+ ).then((policies) => {
if (!active) return;
- // A read that failed leaves nothing in the cache, where a project
- // without its own policy leaves a policy with no payload.
- if (
- finds.some((find) => store.getPolicyByParentAndType(find) === undefined)
- ) {
+ // A read that failed may leave an earlier policy in the cache, so the
+ // results decide, not the cache.
+ if (policies.some((policy) => policy === undefined)) {
setReadFailed(true);
return;
}
@@ -281,6 +279,9 @@ export function ProjectSQLReviewPage({ projectId }: { projectId: string }) {
/>
{standardRules.rules ? (
<StandardRuleSwitches
+ // The rules remembered while SYNTAX is off belong to one
+ // project.
+ key={projectName}
rules={standardRules.rules}
onChange={standardRules.setRules}
disabled={!canSave || standardRules.saving}
diff --git a/frontend/src/routes/workspace/SQLReviewStandardRulesSection.test.tsx b/frontend/src/routes/workspace/SQLReviewStandardRulesSection.test.tsx
index c124f21237..94f47c425f 100644
--- a/frontend/src/routes/workspace/SQLReviewStandardRulesSection.test.tsx
+++ b/frontend/src/routes/workspace/SQLReviewStandardRulesSection.test.tsx
@@ -44,10 +44,10 @@ vi.mock("@/stores/app", async () => {
getPolicyByParentAndType: (params: {
parentPath: string;
}) => StorePolicy | undefined;
- getOrFetchPolicyByParentAndType: (params: {
+ fetchPolicyByParentAndType: (params: {
parentPath: string;
refresh?: boolean;
- }) => Promise<StorePolicy | undefined>;
+ }) => Promise<StorePolicy | null | undefined>;
listPolicies: (params: {
parentPath: string;
showDeleted?: boolean;
@@ -59,8 +59,8 @@ vi.mock("@/stores/app", async () => {
}>()((set, get) => ({
policies: {},
getPolicyByParentAndType: ({ parentPath }) => get().policies[parentPath],
- // A failed read caches nothing, as the real store does.
- getOrFetchPolicyByParentAndType: async (params) => {
+ // A read of a policy that is not seeded fails.
+ fetchPolicyByParentAndType: async (params) => {
mocks.fetchPolicy(params);
return get().policies[params.parentPath];
},
diff --git a/frontend/src/routes/workspace/SQLReviewStandardRulesSection.tsx b/frontend/src/routes/workspace/SQLReviewStandardRulesSection.tsx
index 3015f7f505..f9b49102a7 100644
--- a/frontend/src/routes/workspace/SQLReviewStandardRulesSection.tsx
+++ b/frontend/src/routes/workspace/SQLReviewStandardRulesSection.tsx
@@ -69,13 +69,10 @@ export function useWorkspaceStandardRules(
parentPath: workspace,
policyType: PolicyType.REVIEW_RULE,
};
- // A read that failed leaves nothing in the cache.
void store
- .getOrFetchPolicyByParentAndType({ ...find, refresh: attempt > 0 })
- .then(() => {
- if (active && store.getPolicyByParentAndType(find) === undefined) {
- setReadFailed(true);
- }
+ .fetchPolicyByParentAndType({ ...find, refresh: attempt > 0 })
+ .then((policy) => {
+ if (active && policy === undefined) setReadFailed(true);
});
if (canList) {
void store
@@ -162,6 +159,7 @@ export function SQLReviewStandardRulesSection({
standardRules: WorkspaceStandardRules;
}) {
const { t } = useTranslation();
+ const workspace = useWorkspaceResourceName();
return (
<section className="flex flex-col gap-y-6 pt-8">
@@ -190,6 +188,8 @@ export function SQLReviewStandardRulesSection({
)}
{standardRules.rules && (
<StandardRuleSwitches
+ // The rules remembered while SYNTAX is off belong to one workspace.
+ key={workspace}
rules={standardRules.rules}
onChange={standardRules.setRules}
disabled={!standardRules.canEdit || standardRules.saving}
diff --git a/frontend/src/stores/app/policy.test.ts b/frontend/src/stores/app/policy.test.ts
index 3ad05022eb..b36a361bd6 100644
--- a/frontend/src/stores/app/policy.test.ts
+++ b/frontend/src/stores/app/policy.test.ts
@@ -1,5 +1,6 @@
// @vitest-environment node
import { create } from "@bufbuild/protobuf";
+import { Code, ConnectError } from "@connectrpc/connect";
import { beforeEach, describe, expect, test, vi } from "vitest";
import {
type ListPoliciesRequest,
@@ -14,12 +15,14 @@ import { ReviewRuleType } from "@/types/proto-es/v1/review_rule_pb";
import { createPolicySlice } from "./policy";
const mocks = vi.hoisted(() => ({
+ getPolicy: vi.fn(),
listPolicies: vi.fn(),
updatePolicy: vi.fn(),
}));
vi.mock("@/api", () => ({
orgPolicyServiceClientConnect: {
+ getPolicy: mocks.getPolicy,
listPolicies: mocks.listPolicies,
updatePolicy: mocks.updatePolicy,
},
@@ -85,6 +88,47 @@ describe("policy store", () => {
).toEqual(expect.objectContaining({ rules: [ReviewRuleType.SYNTAX] }));
});
+ test("fetches a policy, and tells an absent one from a failed read", async () => {
+ const store = createStore();
+ const find = {
+ parentPath: "projects/p",
+ policyType: PolicyType.REVIEW_RULE,
+ refresh: true,
+ };
+ const row = create(PolicySchema, {
+ name: "projects/p/policies/review_rule",
+ type: PolicyType.REVIEW_RULE,
+ enforce: true,
+ });
+
+ mocks.getPolicy.mockResolvedValueOnce(row);
+ await expect(store.fetchPolicyByParentAndType(find)).resolves.toBe(row);
+ expect(store.getPolicyByParentAndType(find)).toBe(row);
+
+ // A failed read leaves the last policy read in the cache.
+ mocks.getPolicy.mockRejectedValueOnce(
+ new ConnectError("gone", Code.Unavailable)
+ );
+ await expect(
+ store.fetchPolicyByParentAndType(find)
+ ).resolves.toBeUndefined();
+ expect(store.getPolicyByParentAndType(find)).toBe(row);
+
+ // An absent policy caches a stand-in without a payload.
+ mocks.getPolicy.mockRejectedValueOnce(
+ new ConnectError("missing", Code.NotFound)
+ );
+ await expect(store.fetchPolicyByParentAndType(find)).resolves.toBeNull();
+ expect(store.getPolicyByParentAndType(find)?.policy.case).toBeUndefined();
+
+ mocks.getPolicy.mockRejectedValueOnce(
+ new ConnectError("missing", Code.NotFound)
+ );
+ await expect(
+ store.getOrFetchPolicyByParentAndType(find)
+ ).resolves.toBeUndefined();
+ });
+
test("lists the rows of a parent, including ones switched off", async () => {
const row = create(PolicySchema, {
name: "workspaces/ws/policies/review_rule",
diff --git a/frontend/src/stores/app/policy.ts b/frontend/src/stores/app/policy.ts
index f949e71d56..f8213970b6 100644
--- a/frontend/src/stores/app/policy.ts
+++ b/frontend/src/stores/app/policy.ts
@@ -87,14 +87,14 @@ const policyResourceName = (parent: string, policyType: PolicyType) =>
/**
* `policyMapByName` cache + sync/async getters keyed by resource name.
*/
-export const createPolicySlice: AppSliceCreator<PolicySlice> = (set, get) => ({
- policyMapByName: {},
- policyRequests: {},
-
- getPolicyByName: (name) =>
- get().policyMapByName[replacePolicyTypeNameToLowerCase(name)],
-
- getOrFetchPolicyByName: async (name, refresh = false) => {
+export const createPolicySlice: AppSliceCreator<PolicySlice> = (set, get) => {
+ // Resolves to the policy; to null when the resource has none, caching a
+ // stand-in so repeated lookups do not hammer the backend; or to undefined
+ // when the read failed, leaving the cache as it was.
+ const fetchPolicy = async (
+ name: string,
+ refresh: boolean
+ ): Promise<Policy | null | undefined> => {
const key = replacePolicyTypeNameToLowerCase(name);
const cached = get().policyMapByName[key];
if (cached && !refresh) return cached;
@@ -118,12 +118,12 @@ export const createPolicySlice: AppSliceCreator<PolicySlice> = (set, get) => ({
});
return policy;
})
- .catch((error): undefined => {
+ .catch((error): null | undefined => {
+ const notFound =
+ error instanceof ConnectError && error.code === Code.NotFound;
set((state) => {
const { [key]: _, ...policyRequests } = state.policyRequests;
- // Cache an empty policy on NotFound so repeated lookups don't
- // hammer the backend.
- if (error instanceof ConnectError && error.code === Code.NotFound) {
+ if (notFound) {
return {
policyMapByName: {
...state.policyMapByName,
@@ -134,79 +134,97 @@ export const createPolicySlice: AppSliceCreator<PolicySlice> = (set, get) => ({
}
return { policyRequests };
});
- return undefined;
+ return notFound ? null : undefined;
});
set((state) => ({
policyRequests: { ...state.policyRequests, [key]: request },
}));
return request;
- },
-
- getPolicyByParentAndType: ({ parentPath, policyType }) =>
- get().policyMapByName[policyResourceName(parentPath, policyType)],
-
- getOrFetchPolicyByParentAndType: ({ parentPath, policyType, refresh }) =>
- get().getOrFetchPolicyByName(
- policyResourceName(parentPath, policyType),
- refresh
- ),
-
- getQueryDataPolicyByParent: (parent) => {
- const policy = get().getPolicyByParentAndType({
- parentPath: parent,
- policyType: PolicyType.DATA_QUERY,
- });
- return policy?.policy?.case === "queryDataPolicy"
- ? policy.policy.value
- : EMPTY_QUERY_DATA_POLICY;
- },
-
- listPolicies: async ({ parentPath, policyType, showDeleted = false }) => {
- const { policies } = await orgPolicyServiceClientConnect.listPolicies(
- createProto(ListPoliciesRequestSchema, {
- parent: parentPath,
- policyType,
- showDeleted,
- }),
- { contextValues: createContextValues().set(silentContextKey, true) }
- );
- return policies;
- },
-
- upsertPolicy: async ({ parentPath, policy }) => {
- if (!policy.type) {
- throw new Error("policy type is required");
- }
- const name = policyResourceName(parentPath, policy.type);
- const response = await orgPolicyServiceClientConnect.updatePolicy(
- createProto(UpdatePolicyRequestSchema, {
- policy: createProto(PolicySchema, {
- name,
- inheritFromParent: policy.inheritFromParent ?? false,
- type: policy.type,
- resourceType:
- policy.resourceType ?? PolicyResourceType.RESOURCE_TYPE_UNSPECIFIED,
- enforce: policy.enforce ?? false,
- policy: policy.policy,
+ };
+
+ return {
+ policyMapByName: {},
+ policyRequests: {},
+
+ getPolicyByName: (name) =>
+ get().policyMapByName[replacePolicyTypeNameToLowerCase(name)],
+
+ getOrFetchPolicyByName: async (name, refresh = false) =>
+ (await fetchPolicy(name, refresh)) ?? undefined,
+
+ getPolicyByParentAndType: ({ parentPath, policyType }) =>
+ get().policyMapByName[policyResourceName(parentPath, policyType)],
+
+ getOrFetchPolicyByParentAndType: ({ parentPath, policyType, refresh }) =>
+ get().getOrFetchPolicyByName(
+ policyResourceName(parentPath, policyType),
+ refresh
+ ),
+
+ fetchPolicyByParentAndType: ({ parentPath, policyType, refresh = false }) =>
+ fetchPolicy(policyResourceName(parentPath, policyType), refresh),
+
+ getQueryDataPolicyByParent: (parent) => {
+ const policy = get().getPolicyByParentAndType({
+ parentPath: parent,
+ policyType: PolicyType.DATA_QUERY,
+ });
+ return policy?.policy?.case === "queryDataPolicy"
+ ? policy.policy.value
+ : EMPTY_QUERY_DATA_POLICY;
+ },
+
+ listPolicies: async ({ parentPath, policyType, showDeleted = false }) => {
+ const { policies } = await orgPolicyServiceClientConnect.listPolicies(
+ createProto(ListPoliciesRequestSchema, {
+ parent: parentPath,
+ policyType,
+ showDeleted,
}),
- updateMask: { paths: getUpdateMaskFromPolicyType(policy.type) },
- allowMissing: true,
- })
- );
- set((state) => ({
- policyMapByName: { ...state.policyMapByName, [response.name]: response },
- }));
- return response;
- },
+ { contextValues: createContextValues().set(silentContextKey, true) }
+ );
+ return policies;
+ },
- deletePolicy: async (name) => {
- await orgPolicyServiceClientConnect.deletePolicy(
- createProto(DeletePolicyRequestSchema, { name })
- );
- const key = replacePolicyTypeNameToLowerCase(name);
- set((state) => {
- const { [key]: _removed, ...policyMapByName } = state.policyMapByName;
- return { policyMapByName };
- });
- },
-});
+ upsertPolicy: async ({ parentPath, policy }) => {
+ if (!policy.type) {
+ throw new Error("policy type is required");
+ }
+ const name = policyResourceName(parentPath, policy.type);
+ const response = await orgPolicyServiceClientConnect.updatePolicy(
+ createProto(UpdatePolicyRequestSchema, {
+ policy: createProto(PolicySchema, {
+ name,
+ inheritFromParent: policy.inheritFromParent ?? false,
+ type: policy.type,
+ resourceType:
+ policy.resourceType ??
+ PolicyResourceType.RESOURCE_TYPE_UNSPECIFIED,
+ enforce: policy.enforce ?? false,
+ policy: policy.policy,
+ }),
+ updateMask: { paths: getUpdateMaskFromPolicyType(policy.type) },
+ allowMissing: true,
+ })
+ );
+ set((state) => ({
+ policyMapByName: {
+ ...state.policyMapByName,
+ [response.name]: response,
+ },
+ }));
+ return response;
+ },
+
+ deletePolicy: async (name) => {
+ await orgPolicyServiceClientConnect.deletePolicy(
+ createProto(DeletePolicyRequestSchema, { name })
+ );
+ const key = replacePolicyTypeNameToLowerCase(name);
+ set((state) => {
+ const { [key]: _removed, ...policyMapByName } = state.policyMapByName;
+ return { policyMapByName };
+ });
+ },
+ };
+};
diff --git a/frontend/src/stores/app/types.ts b/frontend/src/stores/app/types.ts
index 89a65d641e..fde8da057a 100644
--- a/frontend/src/stores/app/types.ts
+++ b/frontend/src/stores/app/types.ts
@@ -968,7 +968,7 @@ export type PreferencesSlice = {
// fallback when the policy isn't cached.
export type PolicySlice = {
policyMapByName: Record<string, Policy>;
- policyRequests: Record<string, Promise<Policy | undefined>>;
+ policyRequests: Record<string, Promise<Policy | null | undefined>>;
getPolicyByName: (name: string) => Policy | undefined;
getOrFetchPolicyByName: (
name: string,
@@ -983,6 +983,13 @@ export type PolicySlice = {
policyType: PolicyType;
refresh?: boolean;
}) => Promise<Policy | undefined>;
+ // Resolves to null when the resource has no policy and to undefined when
+ // the read failed, which leaves any earlier policy in the cache.
+ fetchPolicyByParentAndType: (params: {
+ parentPath: string;
+ policyType: PolicyType;
+ refresh?: boolean;
+ }) => Promise<Policy | null | undefined>;
getQueryDataPolicyByParent: (parent: string) => QueryDataPolicy;
listPolicies: (params: {
parentPath: string;