neon 4.7.0 → 4.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +37 -59
  2. package/dist/_chunks/{auth_selection-ktL6uoEe.js → auth_selection-pitMFzDe.js} +1 -1
  3. package/dist/_chunks/{credential_io-B1JxWELN.js → credential_io-YeAxg9Xn.js} +2 -2
  4. package/dist/_chunks/{paths-g3DRqJiD.js → paths-DMq0Lt7a.js} +1 -1
  5. package/dist/_chunks/{profiles-D95AeZ5v.js → profiles-CvnFEQyd.js} +1 -1
  6. package/dist/analytics.js +2 -2
  7. package/dist/auth_context.js +1 -1
  8. package/dist/commands/auth.js +12 -9
  9. package/dist/commands/bootstrap.js +72 -43
  10. package/dist/commands/claim.js +1 -1
  11. package/dist/commands/init.js +80 -79
  12. package/dist/commands/link.js +78 -63
  13. package/dist/commands/mcp.js +1 -1
  14. package/dist/commands/plugins.js +4 -4
  15. package/dist/commands/profile.js +4 -4
  16. package/dist/commands/skills.js +1 -1
  17. package/dist/config.js +1 -1
  18. package/dist/credential_io.js +1 -1
  19. package/dist/index.js +1 -1
  20. package/dist/init/child.js +36 -0
  21. package/dist/init/plan.js +47 -0
  22. package/dist/init/tooling.js +43 -0
  23. package/dist/init/wizard.js +42 -0
  24. package/dist/mcp/targets.js +1 -1
  25. package/dist/plugins/plan.js +1 -1
  26. package/dist/plugins/targets.js +5 -1
  27. package/dist/retire_credential.js +1 -1
  28. package/dist/utils/middlewares.js +1 -1
  29. package/dist/utils/package_manager.js +10 -5
  30. package/package.json +13 -14
  31. package/dist/init/auth.js +0 -53
  32. package/dist/init/build_config.js +0 -4
  33. package/dist/init/detect_agent.js +0 -63
  34. package/dist/init/editors.js +0 -19
  35. package/dist/init/enrich_output.js +0 -61
  36. package/dist/init/extension.js +0 -154
  37. package/dist/init/inspect.js +0 -249
  38. package/dist/init/interactive.js +0 -529
  39. package/dist/init/neonctl.js +0 -139
  40. package/dist/init/orchestrate.js +0 -189
  41. package/dist/init/phases/auth.js +0 -195
  42. package/dist/init/phases/cleanup.js +0 -27
  43. package/dist/init/phases/db.js +0 -257
  44. package/dist/init/phases/getting_started.js +0 -218
  45. package/dist/init/phases/mcp.js +0 -219
  46. package/dist/init/phases/migrations.js +0 -250
  47. package/dist/init/phases/neon_auth.js +0 -116
  48. package/dist/init/phases/setup.js +0 -582
  49. package/dist/init/phases/skills.js +0 -86
  50. package/dist/init/phases/status.js +0 -82
  51. package/dist/init/profile_cli.js +0 -56
  52. package/dist/init/resolve_context.js +0 -105
  53. package/dist/init/route_command.js +0 -93
  54. package/dist/init/skills.js +0 -206
  55. package/dist/init/types.js +0 -1
  56. package/dist/init/vsix.js +0 -89
@@ -1,218 +0,0 @@
1
- import { DO_NOT_SUBSTITUTE_HINT, MISSING_BINARY_HINT, formatExecCommand, formatInstallCommand, resolvePackageManager } from "../../utils/package_manager.js";
2
- import { neonctlCmd } from "../neonctl.js";
3
- import { SKILL_REFERENCE_URLS, ensureSkillsUpToDate } from "../skills.js";
4
- //#region src/init/phases/getting_started.ts
5
- /**
6
- * Initiates the "Get started with Neon" workflow.
7
- *
8
- * Steps are concrete and executable — each has a CLI command to run
9
- * or a specific file operation. The agent should attempt each step
10
- * in order and actually perform the action using the Neon CLI.
11
- */
12
- async function handleGettingStartedPhase(options) {
13
- if (options.agent) await ensureSkillsUpToDate(options.agent);
14
- const steps = [];
15
- const installPm = resolvePackageManager(options.cwd);
16
- if (!options.hasConnectionString) {
17
- if (options.preview) steps.push({
18
- id: "select_org",
19
- description: [
20
- "List the user's Neon organizations using the CLI command below.",
21
- "If only one org exists, use it automatically.",
22
- "If multiple orgs exist, ask the user which one to use.",
23
- "Remember the selected org ID for the next steps."
24
- ].join(" "),
25
- command: `${neonctlCmd()} orgs list --output json`
26
- }, {
27
- id: "select_or_create_project",
28
- description: [
29
- "List existing Neon projects in the selected organization using the CLI command below (replace <org-id> with the selected org ID).",
30
- "IMPORTANT: Neon features (Functions, Object Storage, and AI Gateway) are currently in beta and only available in the AWS us-east-2 region (more regions coming shortly). Projects must have region_id 'aws-us-east-2' and be created on or after 2026-06-15.",
31
- "Filter the project list to ONLY show projects where region_id is 'aws-us-east-2' AND created_at is on or after '2026-06-15'.",
32
- "If eligible projects exist, present them alongside a 'Create new project' option.",
33
- "If no eligible projects exist, tell the user and proceed directly to creating a new one.",
34
- "IMPORTANT: Always include --org-id when creating a project to avoid interactive prompts."
35
- ].join(" "),
36
- command: `${neonctlCmd()} projects list --org-id <org-id> --output json`
37
- }, {
38
- id: "create_project_if_needed",
39
- description: [
40
- "If the user chose to create a new project, create it in the AWS us-east-2 region using the CLI command below (replace <org-id> and <project-name>).",
41
- "Ask the user for a project name (suggest the current directory name).",
42
- "If the user chose an existing eligible project, skip this step."
43
- ].join(" "),
44
- command: `${neonctlCmd()} projects create --name <project-name> --org-id <org-id> --region-id aws-us-east-2 --output json`
45
- });
46
- else steps.push({
47
- id: "select_org",
48
- description: [
49
- "List the user's Neon organizations using the CLI command below.",
50
- "If only one org exists, use it automatically.",
51
- "If multiple orgs exist, ask the user which one to use.",
52
- "Remember the selected org ID for the next steps."
53
- ].join(" "),
54
- command: `${neonctlCmd()} orgs list --output json`
55
- }, {
56
- id: "select_or_create_project",
57
- description: [
58
- "List existing Neon projects in the selected organization using the CLI command below (replace <org-id> with the selected org ID).",
59
- "Ask the user whether they want to use an existing project or create a new one.",
60
- "If creating new, ask the user for a project name (suggest the current directory name).",
61
- "IMPORTANT: Always include --org-id when creating a project to avoid interactive prompts."
62
- ].join(" "),
63
- command: `${neonctlCmd()} projects list --org-id <org-id> --output json`
64
- }, {
65
- id: "create_project_if_needed",
66
- description: ["If the user chose to create a new project, create it using the CLI command below (replace <org-id> and <project-name>).", "If the user chose an existing project, skip this step."].join(" "),
67
- command: `${neonctlCmd()} projects create --name <project-name> --org-id <org-id> --output json`
68
- });
69
- steps.push({
70
- id: "create_neon_context",
71
- description: [
72
- "Update the .neon context file in the project root with the selected org and project IDs.",
73
- "IMPORTANT: If a .neon file already exists, you MUST read it first, then merge the new orgId and projectId into the existing content. Do NOT overwrite the file — other fields (like _init, branch, etc.) must be preserved.",
74
- "If no .neon file exists, create one.",
75
- "The file is JSON. Add/update only the orgId and projectId fields: {\"orgId\": \"<org-id>\", \"projectId\": \"<project-id>\", ...existing fields}.",
76
- "This file is safe to commit — it contains no secrets."
77
- ].join(" ")
78
- });
79
- steps.push({
80
- id: "install_dependencies",
81
- description: [
82
- "Check if node_modules exists in the project root. If not, install the project's dependencies.",
83
- DO_NOT_SUBSTITUTE_HINT,
84
- "This must be done before `neon env pull` because the project's Neon config file may import packages that need to be installed first."
85
- ].join(" "),
86
- command: formatInstallCommand(installPm)
87
- });
88
- steps.push({
89
- id: "pull_env",
90
- description: [
91
- "Now that the .neon context file is in place and dependencies are installed, run `neon env pull` to populate the project's environment variables.",
92
- "This automatically writes the database connection string (and any other Neon-managed env vars) to the correct env file.",
93
- "It reads the .neon context file to determine the project, and writes to the appropriate env file for the project.",
94
- "Ensure the target env file is listed in .gitignore."
95
- ].join(" "),
96
- command: `${neonctlCmd()} env pull`
97
- });
98
- if (options.orm === "prisma") steps.push({
99
- id: "install_driver",
100
- description: [
101
- "Install the @neondatabase/serverless driver adapter for Prisma.",
102
- "This enables Prisma to use Neon's serverless driver for edge/serverless deployments.",
103
- DO_NOT_SUBSTITUTE_HINT
104
- ].join(" "),
105
- command: formatInstallCommand(installPm, ["@neondatabase/serverless", "@prisma/adapter-neon"])
106
- });
107
- else if (options.orm === "drizzle" || options.orm === "drizzle-orm") steps.push({
108
- id: "install_driver",
109
- description: `Install the Neon serverless driver for Drizzle. ${DO_NOT_SUBSTITUTE_HINT}`,
110
- command: formatInstallCommand(installPm, ["@neondatabase/serverless"])
111
- });
112
- else if (!options.orm || options.orm === "none") steps.push({
113
- id: "install_driver",
114
- description: `Install the Neon serverless driver for direct database access. ${DO_NOT_SUBSTITUTE_HINT}`,
115
- command: formatInstallCommand(installPm, ["@neondatabase/serverless"])
116
- });
117
- }
118
- if (options.migrationTool && options.migrationTool !== "none") {
119
- const tool = options.migrationTool.toLowerCase();
120
- const migrationDir = options.migrationDir;
121
- const hasMigrationDir = migrationDir && migrationDir !== "none";
122
- if (tool === "drizzle") {
123
- const migrate = formatExecCommand(installPm, "drizzle-kit", ["migrate"]);
124
- const generate = formatExecCommand(installPm, "drizzle-kit", ["generate"]);
125
- steps.push({
126
- id: "run_migrations",
127
- description: [
128
- hasMigrationDir ? `Check if the ${migrationDir} directory contains .sql migration files.` : "Check if a drizzle migrations directory exists with .sql files.",
129
- `If .sql files exist, apply them with \`${migrate}\`.`,
130
- `If the directory is empty or missing but a drizzle schema file exists (e.g. src/db/schema.ts, drizzle/schema.ts), run \`${generate}\` first to create migrations, then \`${migrate}\` to apply them.`,
131
- "If neither schema nor migrations exist, skip this step.",
132
- MISSING_BINARY_HINT
133
- ].join(" "),
134
- command: migrate
135
- });
136
- } else if (tool === "prisma") {
137
- const deploy = formatExecCommand(installPm, "prisma", ["migrate", "deploy"]);
138
- steps.push({
139
- id: "run_migrations",
140
- description: [
141
- hasMigrationDir ? `Check if the ${migrationDir} directory contains migration folders.` : "Check if prisma/migrations contains migration folders.",
142
- `If migrations exist, apply them with \`${deploy}\`.`,
143
- `If the migrations directory is empty or missing but prisma/schema.prisma has models defined, run \`${formatExecCommand(installPm, "prisma", [
144
- "migrate",
145
- "dev",
146
- "--name",
147
- "init"
148
- ])}\` to create and apply the initial migration.`,
149
- "If no models are defined, skip this step.",
150
- MISSING_BINARY_HINT
151
- ].join(" "),
152
- command: deploy
153
- });
154
- } else if (tool === "knex") steps.push({
155
- id: "run_migrations",
156
- description: `Apply existing knex migrations to the Neon database. ${MISSING_BINARY_HINT}`,
157
- command: formatExecCommand(installPm, "knex", ["migrate:latest"])
158
- });
159
- } else if (options.preview) steps.push({
160
- id: "run_migrations",
161
- description: [
162
- "Check the scaffolded project for a migration tool and schema.",
163
- "Look for: drizzle.config.ts/js (Drizzle), prisma/schema.prisma (Prisma), or knexfile.ts/js (Knex).",
164
- `If Drizzle is found: check if a drizzle migrations directory exists with .sql files. If .sql files exist, run \`${formatExecCommand(installPm, "drizzle-kit", ["migrate"])}\`. If the directory is empty or missing but a schema file exists, run \`${formatExecCommand(installPm, "drizzle-kit", ["generate"])}\` first, then \`${formatExecCommand(installPm, "drizzle-kit", ["migrate"])}\`.`,
165
- `If Prisma is found: check if prisma/migrations contains migration folders. If yes, run \`${formatExecCommand(installPm, "prisma", ["migrate", "deploy"])}\`. If not but models exist, run \`${formatExecCommand(installPm, "prisma", [
166
- "migrate",
167
- "dev",
168
- "--name",
169
- "init"
170
- ])}\`.`,
171
- "If no migration tool is found, skip this step.",
172
- MISSING_BINARY_HINT
173
- ].join(" ")
174
- });
175
- steps.push({
176
- id: "verify_connection",
177
- description: [
178
- "Verify the database connection works by running a SQL query against the Neon database.",
179
- "Write and run a short script that connects using DATABASE_URL from the project's env file and executes `SELECT 1` (or queries a table from the migration if migrations were run).",
180
- "Do NOT use the Neon CLI or MCP tools for this — use a direct database connection to verify end-to-end connectivity."
181
- ].join(" ")
182
- });
183
- return {
184
- phase: "setup",
185
- status: "getting_started",
186
- nextAction: {
187
- type: "agent_action",
188
- prerequisite: SKILL_REFERENCE_URLS.gettingStarted,
189
- steps,
190
- onComplete: buildOnComplete(options)
191
- }
192
- };
193
- }
194
- function buildOnComplete(options) {
195
- const agentArgs = options.agent ? ["--agent", options.agent] : [];
196
- const features = options.features ?? [];
197
- const hasFeatureRequirements = features.length > 0;
198
- if (hasFeatureRequirements && !features.includes("auth")) return {
199
- type: "run_neon_init",
200
- args: [
201
- "finalize",
202
- "--json",
203
- ...agentArgs
204
- ]
205
- };
206
- const authSetup = hasFeatureRequirements && features.includes("auth") ? ["--setup"] : [];
207
- return {
208
- type: "run_neon_init",
209
- args: [
210
- "neon-auth",
211
- "--json",
212
- ...agentArgs,
213
- ...authSetup
214
- ]
215
- };
216
- }
217
- //#endregion
218
- export { handleGettingStartedPhase };
@@ -1,219 +0,0 @@
1
- import { installNeonMcpServer } from "../../mcp/install.js";
2
- import { agentSupportsHttpMcp, agentSupportsProjectMcp, getAgentDisplayName, resolveAddMcpAgentId, tryResolveAddMcpAgentId } from "../../mcp/agents.js";
3
- import { getSkillsAgentName } from "../agents.js";
4
- import { isAuthenticated } from "../auth.js";
5
- //#region src/init/phases/mcp.ts
6
- function skillsFollowUp(agent) {
7
- if (agent && !getSkillsAgentName(agent)) return agent ? [
8
- "--agent",
9
- agent,
10
- "--json"
11
- ] : ["--json"];
12
- return [
13
- "skills",
14
- "--json",
15
- ...agent ? ["--agent", agent] : [],
16
- "--install"
17
- ];
18
- }
19
- async function handleMcpPhase(options) {
20
- const agentArgs = options.agent ? [
21
- "--agent",
22
- options.agent,
23
- "--json"
24
- ] : ["--json"];
25
- if (options.status) return {
26
- phase: "tooling",
27
- status: "status",
28
- nextAction: {
29
- type: "agent_check",
30
- checks: [{
31
- id: "mcp_server",
32
- description: "Check if the Neon MCP server is already configured in your MCP server list",
33
- lookFor: ["An MCP server entry named 'Neon' or with URL containing 'mcp.neon.tech'"]
34
- }],
35
- reportBack: {
36
- type: "run_neon_init",
37
- args: [
38
- "mcp",
39
- "--json",
40
- ...options.agent ? ["--agent", options.agent] : [],
41
- "--mcp-configured",
42
- "<true|false>"
43
- ]
44
- }
45
- }
46
- };
47
- if (options.install) {
48
- if (!await isAuthenticated()) return {
49
- phase: "tooling",
50
- status: "auth_required",
51
- nextAction: {
52
- type: "run_neon_init",
53
- args: ["auth", "--json"]
54
- }
55
- };
56
- const scope = options.scope ?? "global";
57
- const mcpAgentId = resolveAddMcpAgentId(options.agent ?? "claude-code");
58
- const installed = installNeonMcpServer({
59
- agent: mcpAgentId,
60
- scope,
61
- cwd: process.cwd()
62
- });
63
- if (!installed.ok) {
64
- if (installed.unsupported) {
65
- if (scope === "project" && !agentSupportsProjectMcp(mcpAgentId) && agentSupportsHttpMcp(mcpAgentId)) return {
66
- phase: "tooling",
67
- status: "unsupported",
68
- error: installed.error,
69
- nextAction: {
70
- type: "ask_user",
71
- question: `${getAgentDisplayName(mcpAgentId)} does not support project-level MCP. Install the Neon MCP server globally instead?`,
72
- options: [{
73
- value: "global",
74
- label: "Install globally"
75
- }, {
76
- value: "skip",
77
- label: "Skip MCP install"
78
- }],
79
- responseMapping: {
80
- global: { args: [
81
- "mcp",
82
- "--json",
83
- ...options.agent ? ["--agent", options.agent] : [],
84
- "--install"
85
- ] },
86
- skip: { args: skillsFollowUp(options.agent) }
87
- }
88
- }
89
- };
90
- return {
91
- phase: "tooling",
92
- status: "unsupported",
93
- error: installed.error,
94
- nextAction: {
95
- type: "run_neon_init",
96
- args: skillsFollowUp(options.agent)
97
- }
98
- };
99
- }
100
- return {
101
- phase: "tooling",
102
- status: "failed",
103
- error: installed.error,
104
- nextAction: {
105
- type: "ask_user",
106
- question: "Failed to install the Neon MCP server automatically. Would you like to try again or configure it manually?",
107
- options: [{
108
- value: "retry",
109
- label: "Try again"
110
- }, {
111
- value: "manual",
112
- label: "I'll configure it manually"
113
- }],
114
- responseMapping: {
115
- retry: { args: [
116
- "mcp",
117
- "--json",
118
- ...options.agent ? ["--agent", options.agent] : [],
119
- "--install",
120
- ...scope === "project" ? ["--scope", "project"] : []
121
- ] },
122
- manual: { args: agentArgs }
123
- }
124
- }
125
- };
126
- }
127
- const isCursor = mcpAgentId === "cursor" || options.agent?.toLowerCase() === "cursor";
128
- const isClaudeCode = mcpAgentId === "claude-code" || options.agent?.toLowerCase() === "claude-code";
129
- let enableNote = "";
130
- if (isCursor && scope === "project") enableNote = " Cursor disables project-level MCP servers by default — open Cursor Settings > MCP and toggle the \"Neon\" server on.";
131
- else if (isClaudeCode) enableNote = " Claude Code requires approval for newly added MCP servers. When prompted, approve the \"Neon\" server to enable it.";
132
- return {
133
- phase: "tooling",
134
- status: "installed",
135
- path: installed.path,
136
- nextAction: {
137
- type: "run_neon_init",
138
- args: skillsFollowUp(options.agent)
139
- },
140
- message: `Installed Neon MCP server (${scope} scope) for ${mcpAgentId}.${enableNote}`
141
- };
142
- }
143
- if (options.mcpConfigured === true) return {
144
- phase: "tooling",
145
- status: "mcp_configured",
146
- nextAction: {
147
- type: "run_neon_init",
148
- args: skillsFollowUp(options.agent)
149
- }
150
- };
151
- if (options.mcpConfigured === false) return {
152
- phase: "tooling",
153
- status: "install_needed",
154
- nextAction: {
155
- type: "ask_user",
156
- question: "The Neon MCP server is not yet configured. Would you like to install it?",
157
- options: [
158
- {
159
- value: "defaults",
160
- label: "Yes, install with default settings"
161
- },
162
- ...(() => {
163
- const known = options.agent ? tryResolveAddMcpAgentId(options.agent) : void 0;
164
- if (known && !agentSupportsProjectMcp(known)) return [];
165
- return [{
166
- value: "project_scope",
167
- label: "Yes, install for this project only"
168
- }];
169
- })(),
170
- {
171
- value: "skip",
172
- label: "Skip for now"
173
- }
174
- ],
175
- context: "The Neon MCP server gives your AI assistant direct access to Neon database operations like creating projects, running queries, and managing branches." + (options.agent?.toLowerCase() === "cursor" || options.editor?.toLowerCase() === "cursor" ? " Note: Cursor disables project-level MCP servers by default. If you choose project scope, you will need to manually enable the Neon server in Cursor Settings > MCP. Global scope is recommended for Cursor." : ""),
176
- responseMapping: {
177
- defaults: { args: [
178
- "mcp",
179
- "--json",
180
- ...options.agent ? ["--agent", options.agent] : [],
181
- "--install"
182
- ] },
183
- project_scope: { args: [
184
- "mcp",
185
- "--json",
186
- ...options.agent ? ["--agent", options.agent] : [],
187
- "--install",
188
- "--scope",
189
- "project"
190
- ] },
191
- skip: { args: skillsFollowUp(options.agent) }
192
- }
193
- }
194
- };
195
- return {
196
- phase: "tooling",
197
- status: "detection_needed",
198
- nextAction: {
199
- type: "agent_check",
200
- checks: [{
201
- id: "mcp_server",
202
- description: "Check if the Neon MCP server is already configured in your MCP server list",
203
- lookFor: ["An MCP server entry named 'Neon' or with URL containing 'mcp.neon.tech'"]
204
- }],
205
- reportBack: {
206
- type: "run_neon_init",
207
- args: [
208
- "mcp",
209
- "--json",
210
- ...options.agent ? ["--agent", options.agent] : [],
211
- "--mcp-configured",
212
- "<true|false>"
213
- ]
214
- }
215
- }
216
- };
217
- }
218
- //#endregion
219
- export { handleMcpPhase };
@@ -1,250 +0,0 @@
1
- import { DO_NOT_SUBSTITUTE_HINT, MISSING_BINARY_HINT, formatExecCommand, formatInstallCommand, resolvePackageManager } from "../../utils/package_manager.js";
2
- import { ensureSkillsUpToDate } from "../skills.js";
3
- //#region src/init/phases/migrations.ts
4
- async function handleMigrationsPhase(options) {
5
- if (options.agent) await ensureSkillsUpToDate(options.agent);
6
- const agentArgs = options.agent ? [
7
- "--agent",
8
- options.agent,
9
- "--json"
10
- ] : ["--json"];
11
- const pm = resolvePackageManager(options.cwd);
12
- if (options.scaffold) {
13
- if (options.scaffold === "prisma") return {
14
- phase: "migrations",
15
- status: "scaffolding",
16
- tool: "prisma",
17
- nextAction: {
18
- type: "agent_action",
19
- steps: [
20
- {
21
- id: "install_prisma",
22
- description: `Install Prisma as a dev dependency. ${DO_NOT_SUBSTITUTE_HINT}`,
23
- command: formatInstallCommand(pm, ["prisma"], { dev: true })
24
- },
25
- {
26
- id: "init_prisma",
27
- description: `Initialize Prisma with PostgreSQL provider. ${MISSING_BINARY_HINT}`,
28
- command: formatExecCommand(pm, "prisma", [
29
- "init",
30
- "--datasource-provider",
31
- "postgresql"
32
- ])
33
- },
34
- {
35
- id: "configure_env",
36
- description: "Ensure DATABASE_URL in .env points to your Neon database. The prisma init command may have created a placeholder — replace it with the real connection string if needed."
37
- },
38
- {
39
- id: "create_schema",
40
- description: "Help the user define their initial schema in prisma/schema.prisma based on their application needs."
41
- },
42
- {
43
- id: "run_migration",
44
- description: `Create and apply the initial migration. ${MISSING_BINARY_HINT}`,
45
- command: formatExecCommand(pm, "prisma", [
46
- "migrate",
47
- "dev",
48
- "--name",
49
- "init"
50
- ])
51
- }
52
- ],
53
- onComplete: {
54
- type: "complete",
55
- message: `Prisma is set up with your Neon database. You can now define models in schema.prisma and run migrations with \`${formatExecCommand(pm, "prisma", ["migrate", "dev"])}\`.`
56
- }
57
- }
58
- };
59
- return {
60
- phase: "migrations",
61
- status: "scaffolding",
62
- tool: "drizzle",
63
- nextAction: {
64
- type: "agent_action",
65
- steps: [
66
- {
67
- id: "install_drizzle",
68
- description: `Install Drizzle ORM, drizzle-kit, and the Neon serverless driver. ${DO_NOT_SUBSTITUTE_HINT}`,
69
- command: [formatInstallCommand(pm, ["drizzle-orm", "@neondatabase/serverless"]), formatInstallCommand(pm, ["drizzle-kit"], { dev: true })].join(" && ")
70
- },
71
- {
72
- id: "create_config",
73
- description: "Create drizzle.config.ts at the project root. Set the dialect to 'postgresql' and dbCredentials.url to process.env.DATABASE_URL."
74
- },
75
- {
76
- id: "create_schema",
77
- description: "Create a schema file (e.g. src/db/schema.ts) and help the user define their initial tables using Drizzle's table builder."
78
- },
79
- {
80
- id: "run_migration",
81
- description: `Generate and apply the initial migration. ${MISSING_BINARY_HINT}`,
82
- command: drizzleGenerateAndMigrate(pm)
83
- }
84
- ],
85
- onComplete: {
86
- type: "complete",
87
- message: `Drizzle ORM is set up with your Neon database. Define tables in your schema file and run migrations with \`${drizzleGenerateAndMigrate(pm)}\`.`
88
- }
89
- }
90
- };
91
- }
92
- if (options.apply && options.tool) {
93
- const applySteps = getMigrationApplySteps(options.tool, pm);
94
- return {
95
- phase: "migrations",
96
- status: "applying",
97
- tool: options.tool,
98
- nextAction: {
99
- type: "agent_action",
100
- steps: applySteps,
101
- onComplete: {
102
- type: "complete",
103
- message: "Database migrations applied successfully."
104
- }
105
- }
106
- };
107
- }
108
- if (options.tool && options.tool !== "none") return {
109
- phase: "migrations",
110
- status: "found",
111
- detected: {
112
- tool: options.tool,
113
- migrationDir: options.migrationDir ?? null
114
- },
115
- nextAction: {
116
- type: "ask_user",
117
- question: `Found ${options.tool} migrations${options.migrationDir ? ` in ${options.migrationDir}` : ""}. Would you like to apply them to your Neon database?`,
118
- options: [{
119
- value: "apply",
120
- label: "Yes, apply migrations"
121
- }, {
122
- value: "skip",
123
- label: "Skip for now"
124
- }],
125
- responseMapping: {
126
- apply: { args: [
127
- "migrations",
128
- "--json",
129
- "--apply",
130
- "--tool",
131
- options.tool
132
- ] },
133
- skip: { args: [...agentArgs, "--skip-migrations"] }
134
- }
135
- }
136
- };
137
- if (options.tool === "none") return {
138
- phase: "migrations",
139
- status: "none_found",
140
- nextAction: {
141
- type: "ask_user",
142
- question: "No existing database migrations were found. Would you like to set up a migration framework?",
143
- options: [
144
- {
145
- value: "prisma",
146
- label: "Prisma (recommended for TypeScript)"
147
- },
148
- {
149
- value: "drizzle",
150
- label: "Drizzle ORM"
151
- },
152
- {
153
- value: "skip",
154
- label: "Skip - I'll handle migrations myself"
155
- }
156
- ],
157
- context: "A migration framework helps you version-control your database schema changes.",
158
- responseMapping: {
159
- prisma: { args: [
160
- "migrations",
161
- "--json",
162
- "--scaffold",
163
- "prisma"
164
- ] },
165
- drizzle: { args: [
166
- "migrations",
167
- "--json",
168
- "--scaffold",
169
- "drizzle"
170
- ] },
171
- skip: { args: [...agentArgs, "--skip-migrations"] }
172
- }
173
- }
174
- };
175
- return {
176
- phase: "migrations",
177
- status: "detection_needed",
178
- nextAction: {
179
- type: "agent_check",
180
- checks: [{
181
- id: "existing_migrations",
182
- description: "Check if the project has existing database migrations",
183
- lookFor: [
184
- "prisma/migrations/ directory or schema.prisma file",
185
- "drizzle/ directory or drizzle.config.ts",
186
- "migrations/ or db/migrations/ directory",
187
- "knex migration files (knexfile.js/ts)"
188
- ]
189
- }],
190
- reportBack: {
191
- type: "run_neon_init",
192
- args: [
193
- "migrations",
194
- "--json",
195
- "--tool",
196
- "<prisma|drizzle|knex|none>",
197
- "--migration-dir",
198
- "<path|none>"
199
- ]
200
- }
201
- }
202
- };
203
- }
204
- /** `drizzle-kit generate` then `migrate`, both through the project's runner. */
205
- const drizzleGenerateAndMigrate = (pm) => [formatExecCommand(pm, "drizzle-kit", ["generate"]), formatExecCommand(pm, "drizzle-kit", ["migrate"])].join(" && ");
206
- function getMigrationApplySteps(tool, pm) {
207
- switch (tool) {
208
- case "prisma": return [
209
- {
210
- id: "ensure_env",
211
- description: "Verify DATABASE_URL is set in .env"
212
- },
213
- {
214
- id: "apply",
215
- description: `Apply migrations to the Neon database. ${MISSING_BINARY_HINT}`,
216
- command: formatExecCommand(pm, "prisma", ["migrate", "deploy"])
217
- },
218
- {
219
- id: "generate",
220
- description: "Generate the Prisma client.",
221
- command: formatExecCommand(pm, "prisma", ["generate"])
222
- }
223
- ];
224
- case "drizzle": return [{
225
- id: "ensure_env",
226
- description: "Verify DATABASE_URL is set in .env"
227
- }, {
228
- id: "apply",
229
- description: `Apply migrations to the Neon database. ${MISSING_BINARY_HINT}`,
230
- command: formatExecCommand(pm, "drizzle-kit", ["migrate"])
231
- }];
232
- case "knex": return [{
233
- id: "ensure_env",
234
- description: "Verify DATABASE_URL is set in .env"
235
- }, {
236
- id: "apply",
237
- description: `Apply migrations to the Neon database. ${MISSING_BINARY_HINT}`,
238
- command: formatExecCommand(pm, "knex", ["migrate:latest"])
239
- }];
240
- default: return [{
241
- id: "ensure_env",
242
- description: "Verify DATABASE_URL is set in .env"
243
- }, {
244
- id: "apply",
245
- description: `Apply ${tool} migrations to the Neon database using the appropriate command for your migration tool.`
246
- }];
247
- }
248
- }
249
- //#endregion
250
- export { handleMigrationsPhase };