Skip to content

Commit 20e5e76

Browse files
committed
Adding a schema diff tool that will automatically suggest to generate a
zero downtime migration
1 parent 08380ce commit 20e5e76

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/tools/definitions.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
listOrganizationsInputSchema,
2424
listSharedProjectsInputSchema,
2525
resetFromParentInputSchema,
26+
compareDatabaseSchemaInputSchema,
2627
} from './toolsSchema.js';
2728

2829
export const NEON_TOOLS = [
@@ -608,4 +609,66 @@ export const NEON_TOOLS = [
608609
description: 'Lists compute endpoints for a project or specific branch',
609610
inputSchema: listBranchComputesInputSchema,
610611
},
612+
{
613+
name: 'compare_database_schema' as const,
614+
description: `
615+
<use_case>
616+
Use this tool to compare the schema of a database between two branches.
617+
The output of the tool is a JSON object with one field: \`diff\`.
618+
At this field you will find a difference between two schemas.
619+
620+
You MUST BE READY to generate a zero-downtime migration from the diff and apply it to the parent branch.
621+
</use_case>
622+
623+
<important_notes>
624+
To generate schema diff, you MUST SPECIFY the \`database_name\`.
625+
If it's not specified, try to use the default database name: \`${NEON_DEFAULT_DATABASE_NAME}\`.
626+
627+
You MUST TAKE INTO ACCOUNT the Postgres version. The Postgres version is the same for both branches.
628+
You MUST ASK user consent before running each generated SQL query.
629+
You SHOULD USE \`run_sql\` tool to run each generated SQL query.
630+
Generated queries change the schema of the parent branch and MIGHT BE dangerous to execute.
631+
</important_notes>
632+
633+
<next_steps>
634+
After executing this tool, you MUST follow these steps:
635+
1. Review the schema diff and suggest to generate a zero-downtime migration.
636+
2. Follow these instructions to respond to the client:
637+
638+
<response_instructions>
639+
<instructions>
640+
Provide a brief information about the changes:
641+
* Tables
642+
* Views
643+
* Indexes
644+
* Ownership
645+
* Constraints
646+
* Triggers
647+
* Policies
648+
* Extensions
649+
* Schemas
650+
* Sequences
651+
* Tablespaces
652+
* Users
653+
* Roles
654+
* Privileges
655+
</instructions>
656+
</response_instructions>
657+
</next_steps>
658+
659+
This tool:
660+
1. Generates a diff between two branches.
661+
2. Generates a SQL migration from the diff.
662+
3. Suggest to generate zero-downtime migration.
663+
664+
Workflow:
665+
1. User asks you to generate a diff between two branches.
666+
2. You suggest to generate a SQL migration from the diff.
667+
3. You ensure that the generated migration is a zero-downtime migration,
668+
otherwise you should warn the user about it.
669+
4. You ensure that your suggested migration is also matching the Postgres version.
670+
5. You use \`run_sql\` tool to run each generated SQL query and ask the user consent before running it.
671+
`,
672+
inputSchema: compareDatabaseSchemaInputSchema,
673+
},
611674
];

src/tools/tools.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
EndpointType,
55
ListProjectsParams,
66
ListSharedProjectsParams,
7+
GetProjectBranchSchemaComparisonParams,
78
Organization,
89
ProjectCreateRequest,
910
} from '@neondatabase/api-client';
@@ -1212,6 +1213,14 @@ async function handleListSharedProjects(
12121213
return response.data.projects;
12131214
}
12141215

1216+
async function handleCompareDatabaseSchema(
1217+
params: GetProjectBranchSchemaComparisonParams,
1218+
neonClient: Api<unknown>,
1219+
) {
1220+
const response = await neonClient.getProjectBranchSchemaComparison(params);
1221+
return response.data;
1222+
}
1223+
12151224
export const NEON_HANDLERS = {
12161225
list_projects: async ({ params }, neonClient, extra) => {
12171226
const organization = await getOrgByOrgIdOrDefault(
@@ -1774,4 +1783,18 @@ export const NEON_HANDLERS = {
17741783
],
17751784
};
17761785
},
1786+
1787+
compare_database_schema: async ({ params }, neonClient) => {
1788+
const result = await handleCompareDatabaseSchema(
1789+
{
1790+
projectId: params.projectId,
1791+
branchId: params.branchId,
1792+
db_name: params.databaseName,
1793+
},
1794+
neonClient
1795+
);
1796+
return {
1797+
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
1798+
};
1799+
},
17771800
} satisfies ToolHandlers;

src/tools/toolsSchema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,9 @@ export const resetFromParentInputSchema = z.object({
334334
'Optional name to preserve the current state under a new branch before resetting',
335335
),
336336
});
337+
338+
export const compareDatabaseSchemaInputSchema = z.object({
339+
projectId: z.string().describe('The ID of the project'),
340+
branchId: z.string().describe('The ID of the branch'),
341+
databaseName: z.string().describe(DATABASE_NAME_DESCRIPTION),
342+
});

0 commit comments

Comments
 (0)