Skip to content

An open-source, faster Devin alternative

TypeScript

fix: handle legacy projects without databases (#760)

Mohamed Habib3dba64b9cf3b
diff --git a/cloudflare-workers/api-edge/src/managed_agents.test.ts b/cloudflare-workers/api-edge/src/managed_agents.test.ts
index f0b9a5c5..85b8d6d9 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.test.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.test.ts
@@ -885,6 +885,53 @@ describe("managed agents proxy", () => {
);
});
+ it("preserves the typed state for a legacy project without a database", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () =>
+ Response.json(
+ {
+ error: {
+ code: "database_not_provisioned",
+ message:
+ "Redeploy this project to development to provision its database",
+ },
+ },
+ { status: 409 },
+ ),
+ ),
+ );
+
+ const response = await proxyManagedAgents(
+ new Request(
+ "https://app.opencomputer.dev/api/managed-agents/projects/prj_test/database/query",
+ {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({
+ environment: "development",
+ sql: "SELECT 1",
+ parameters: [],
+ }),
+ },
+ ),
+ {
+ OC_MANAGED_AGENTS_SECRET: "test-secret",
+ MANAGED_AGENTS_API_URL: "https://managedagents.test",
+ },
+ { orgID: "org_test", userID: "user_test" },
+ "/api/managed-agents",
+ );
+
+ expect(response.status).toBe(409);
+ await expect(response.json()).resolves.toEqual({
+ error: {
+ code: "database_not_provisioned",
+ message: "Redeploy this project to provision its database.",
+ },
+ });
+ });
+
it("exposes public template provenance on a project overview", async () => {
vi.stubGlobal(
"fetch",
diff --git a/cloudflare-workers/api-edge/src/managed_agents.ts b/cloudflare-workers/api-edge/src/managed_agents.ts
index 2527fa3d..e6d5fb9c 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.ts
@@ -167,6 +167,8 @@ async function publicErrorResponse(upstream: Response): Promise<Response> {
if (backendCode === "invalid_model_selection") {
message =
backendMessage || "The deployment selects an unavailable model.";
+ } else if (backendCode === "database_not_provisioned") {
+ message = "Redeploy this project to provision its database.";
} else if (backendCode === "destination_verification_failed") {
if (
backendMessage === "Invite the Slack app to this conversation first"
diff --git a/web/src/managed-agents/Database.test.ts b/web/src/managed-agents/Database.test.ts
index a459eedf..0cfec172 100644
--- a/web/src/managed-agents/Database.test.ts
+++ b/web/src/managed-agents/Database.test.ts
@@ -1,5 +1,9 @@
import { describe, expect, it } from 'vitest'
-import { quoteDatabaseIdentifier } from './Database'
+import { ApiError } from '@/api/client'
+import {
+ isUnprovisionedDatabaseError,
+ quoteDatabaseIdentifier,
+} from './Database'
describe('quoteDatabaseIdentifier', () => {
it('quotes table names', () => {
@@ -10,3 +14,21 @@ describe('quoteDatabaseIdentifier', () => {
expect(quoteDatabaseIdentifier('odd"name')).toBe('"odd""name"')
})
})
+
+describe('isUnprovisionedDatabaseError', () => {
+ it('recognizes the typed legacy deployment state', () => {
+ expect(
+ isUnprovisionedDatabaseError(
+ new ApiError('Conflict', 409, 'database_not_provisioned'),
+ ),
+ ).toBe(true)
+ })
+
+ it('leaves real query failures visible as errors', () => {
+ expect(
+ isUnprovisionedDatabaseError(
+ new ApiError('Query failed', 400, 'database_query_failed'),
+ ),
+ ).toBe(false)
+ })
+})
diff --git a/web/src/managed-agents/Database.tsx b/web/src/managed-agents/Database.tsx
index ef0ef02e..1f99ca6a 100644
--- a/web/src/managed-agents/Database.tsx
+++ b/web/src/managed-agents/Database.tsx
@@ -22,6 +22,7 @@ import {
TableRow,
} from '@/components/ui/table'
import { cn } from '@/lib/utils'
+import { ApiError } from '@/api/client'
import {
queryManagedProjectDatabase,
type ManagedDatabaseResult,
@@ -47,6 +48,10 @@ export function quoteDatabaseIdentifier(value: string) {
return `"${value.replace(/"/g, '""')}"`
}
+export function isUnprovisionedDatabaseError(error: unknown) {
+ return error instanceof ApiError && error.type === 'database_not_provisioned'
+}
+
function displayValue(value: ManagedDatabaseValue) {
if (value === null) return <span className="text-muted-foreground">NULL</span>
const text = String(value)
@@ -174,6 +179,18 @@ export function ManagedProjectDatabase({
)
}
+ if (tables.isError && isUnprovisionedDatabaseError(tables.error)) {
+ return (
+ <Panel>
+ <EmptyState
+ icon={Database}
+ title={`No ${environment} database yet`}
+ description={`Redeploy this project to ${environment} to provision its database and apply migrations.`}
+ />
+ </Panel>
+ )
+ }
+
return (
<div className="space-y-5">
<Panel>