@wwchen/stack 0.10.1 → 0.11.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.
- package/README.md +42 -25
- package/blocks/AGENTS.md +8 -20
- package/blocks/CLAUDE.md +9 -27
- package/blocks/env.example +1 -7
- package/blocks/gitignore +0 -6
- package/dist/cli/builtin.d.ts +4 -3
- package/dist/cli/builtin.js +31 -36
- package/dist/cli/check.d.ts +5 -0
- package/dist/cli/check.js +39 -0
- package/dist/cli/context.d.ts +1 -18
- package/dist/cli/context.js +0 -22
- package/dist/cli/discover.d.ts +15 -0
- package/dist/cli/discover.js +156 -0
- package/dist/cli/generate.d.ts +6 -0
- package/dist/cli/generate.js +94 -0
- package/dist/cli/host.d.ts +1 -1
- package/dist/cli/host.js +90 -72
- package/dist/cli/owned.d.ts +33 -0
- package/dist/cli/owned.js +151 -0
- package/dist/cli/{sync-blocks.d.ts → sync.d.ts} +6 -2
- package/dist/cli/{sync-blocks.js → sync.js} +46 -18
- package/dist/configs/vitest.d.ts +3 -1
- package/dist/configs/vitest.js +8 -6
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/next/config.d.ts +2 -0
- package/dist/next/config.js +10 -6
- package/dist/next/health.d.ts +1 -1
- package/dist/next/health.js +1 -1
- package/dist/stack/contracts.d.ts +65 -15
- package/dist/stack/contracts.js +30 -6
- package/files/.claude/rules/stack-ui.md +24 -0
- package/files/next.config.ts +6 -0
- package/files/src/app/api/health/route.ts +9 -0
- package/package.json +6 -37
- package/dist/cli/db.d.ts +0 -4
- package/dist/cli/db.js +0 -43
- package/dist/cli/dev.d.ts +0 -4
- package/dist/cli/dev.js +0 -70
- package/dist/cli/neon.d.ts +0 -79
- package/dist/cli/neon.js +0 -394
- package/dist/cli/run.d.ts +0 -2
- package/dist/cli/run.js +0 -29
- package/dist/db/branches.d.ts +0 -11
- package/dist/db/branches.js +0 -19
- package/dist/db/doctor.d.ts +0 -9
- package/dist/db/doctor.js +0 -120
- package/dist/db/env.d.ts +0 -14
- package/dist/db/env.js +0 -15
- package/dist/db/explaining-pool.d.ts +0 -11
- package/dist/db/explaining-pool.js +0 -55
- package/dist/db/health.d.ts +0 -10
- package/dist/db/health.js +0 -26
- package/dist/db/index.d.ts +0 -21
- package/dist/db/index.js +0 -32
- package/dist/db/migration-status.d.ts +0 -5
- package/dist/db/migration-status.js +0 -38
- package/dist/db/schema-changes.d.ts +0 -13
- package/dist/db/schema-changes.js +0 -99
- package/dist/db/ssl.d.ts +0 -4
- package/dist/db/ssl.js +0 -17
- package/dist/db/target.d.ts +0 -19
- package/dist/db/target.js +0 -66
- package/dist/db/testing/index.d.ts +0 -23
- package/dist/db/testing/index.js +0 -67
- package/dist/db/testing/isolate.d.ts +0 -1
- package/dist/db/testing/isolate.js +0 -3
- package/dist/db/testing/pglite.d.ts +0 -20
- package/dist/db/testing/pglite.js +0 -79
- package/dist/env/testing.d.ts +0 -6
- package/dist/env/testing.js +0 -13
package/dist/cli/neon.js
DELETED
|
@@ -1,394 +0,0 @@
|
|
|
1
|
-
// Your Neon branches, driven through the Neon CLI rather than a `.env` file. `stack dev-setup` makes your dev branch, `stack with-neon` hands its connection to every local command, and `stack neon seed` / `reset-preview` keep the two shared branches. Needs the Neon CLI signed in (`neon auth`) or NEON_API_KEY, and NEON_PROJECT_ID in the environment.
|
|
2
|
-
//
|
|
3
|
-
// Runs before any database is set, so it reads its own `deps.env`, never the app's validated env.
|
|
4
|
-
//
|
|
5
|
-
// The branch tree: production -> `seed` (the app's migrations and seed rows, no production data) -> `dev/<user>` for each developer and one shared `preview`. Nothing local or preview is ever cut from production, so production's data never leaves it. Production is the project's default branch, whatever it is named (Neon calls a new project's `main`): `defaultBranch`.
|
|
6
|
-
import { endpointOf } from "../db/index.js";
|
|
7
|
-
import { VERCEL_TARGET } from "../stack/contracts.js";
|
|
8
|
-
import { linkedProject, listEnvs } from "../vercel/index.js";
|
|
9
|
-
import { CliError, importApp, loadAppEnv } from "./context.js";
|
|
10
|
-
export const SEED = "seed";
|
|
11
|
-
export const PREVIEW = "preview";
|
|
12
|
-
/** Where the new seed is built; it takes the name `seed` only once it is clean. */
|
|
13
|
-
export const SEED_NEXT = "seed-next";
|
|
14
|
-
export function projectId(env) {
|
|
15
|
-
const id = env.NEON_PROJECT_ID?.trim();
|
|
16
|
-
if (!id) {
|
|
17
|
-
throw new CliError("NEON_PROJECT_ID is not set. Export your Neon project's id (Neon console -> Project settings).");
|
|
18
|
-
}
|
|
19
|
-
return id;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Refuses a dev branch name that is not a dev branch: production (whatever this project calls its default branch, and the name `production` itself), the shared `seed` and `preview`, and `seed-next` (the seed being built, which may briefly hold production's data). One check for every command that uses your dev branch: with-neon, dev-setup, and a package's command through `databaseEnv`.
|
|
23
|
-
*/
|
|
24
|
-
export function assertDevBranchName(name, production) {
|
|
25
|
-
const yours = "set NEON_DEV_BRANCH to your own branch (default dev/<os user>)";
|
|
26
|
-
if (name === production.name || name === "production") {
|
|
27
|
-
throw new CliError(`${name} is production, not a dev branch: production commands use ENV_FILE=<production env file>; ${yours}.`);
|
|
28
|
-
}
|
|
29
|
-
if (name === SEED || name === SEED_NEXT || name === PREVIEW) {
|
|
30
|
-
throw new CliError(`${name} is a shared branch, not a dev branch; ${yours}.`);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
/** `dev/<os user>` unless NEON_DEV_BRANCH names another. */
|
|
34
|
-
export const devBranchName = (cli) => cli.env.NEON_DEV_BRANCH?.trim() || `dev/${cli.username}`;
|
|
35
|
-
/** Runs `neon <args> --project-id <id>`; stdout trimmed, a failure is the CLI's own message plus the exit code. */
|
|
36
|
-
async function neon(cli, args) {
|
|
37
|
-
const { stdout, code } = await cli.exec("neon", [...args, "--project-id", projectId(cli.env)]);
|
|
38
|
-
return { stdout: stdout.trim(), code };
|
|
39
|
-
}
|
|
40
|
-
async function neonOk(cli, args) {
|
|
41
|
-
const { stdout, code } = await neon(cli, args);
|
|
42
|
-
if (code !== 0)
|
|
43
|
-
throw new CliError(`neon ${args.slice(0, 2).join(" ")} failed (exit ${code}).`);
|
|
44
|
-
return stdout;
|
|
45
|
-
}
|
|
46
|
-
async function neonJson(cli, args) {
|
|
47
|
-
return JSON.parse(await neonOk(cli, [...args, "--output", "json"]));
|
|
48
|
-
}
|
|
49
|
-
const listBranches = (cli) => neonJson(cli, ["branches", "list"]);
|
|
50
|
-
export async function findBranch(cli, name) {
|
|
51
|
-
return (await listBranches(cli)).find((b) => b.name === name);
|
|
52
|
-
}
|
|
53
|
-
/** Production: the project's default branch (Neon's `default` flag), not a branch name. */
|
|
54
|
-
export function defaultBranch(branches) {
|
|
55
|
-
const production = branches.find((b) => b.default);
|
|
56
|
-
if (!production) {
|
|
57
|
-
throw new CliError("the Neon project has no default branch, so there is no production to tell apart; set one in the Neon console.");
|
|
58
|
-
}
|
|
59
|
-
return production;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* The variables local work needs: `branch`'s URLs, and the host of production (the default branch; `production` when the caller already has it, else listed).
|
|
63
|
-
*/
|
|
64
|
-
export async function branchEnv(cli, branch, production) {
|
|
65
|
-
const productionId = (production ?? defaultBranch(await listBranches(cli))).id;
|
|
66
|
-
const [pooled, direct, productionUrl] = await Promise.all([
|
|
67
|
-
neonOk(cli, ["connection-string", branch, "--pooled"]),
|
|
68
|
-
neonOk(cli, ["connection-string", branch]),
|
|
69
|
-
neonOk(cli, ["connection-string", productionId]),
|
|
70
|
-
]);
|
|
71
|
-
return {
|
|
72
|
-
DATABASE_URL: pooled,
|
|
73
|
-
DATABASE_URL_UNPOOLED: direct,
|
|
74
|
-
// Keeps assertDevDatabase armed: it refuses this host and allows the dev branch.
|
|
75
|
-
PRODUCTION_DATABASE_HOST: new URL(productionUrl).hostname,
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
/** Cuts `name` from `parent`; undefined when the Neon CLI refuses. */
|
|
79
|
-
async function createBranch(cli, name, parent, extra = []) {
|
|
80
|
-
const created = await neon(cli, [
|
|
81
|
-
"branches",
|
|
82
|
-
"create",
|
|
83
|
-
"--name",
|
|
84
|
-
name,
|
|
85
|
-
"--parent",
|
|
86
|
-
parent,
|
|
87
|
-
...extra,
|
|
88
|
-
"--output",
|
|
89
|
-
"json",
|
|
90
|
-
]);
|
|
91
|
-
if (created.code !== 0)
|
|
92
|
-
return undefined;
|
|
93
|
-
return JSON.parse(created.stdout).branch;
|
|
94
|
-
}
|
|
95
|
-
/** Deletes `name` if it exists, then cuts it afresh from `parent`; the new branch. */
|
|
96
|
-
async function recreate(cli, name, parent) {
|
|
97
|
-
const existing = await findBranch(cli, name);
|
|
98
|
-
if (existing)
|
|
99
|
-
await neonOk(cli, ["branches", "delete", existing.id]);
|
|
100
|
-
return createBranch(cli, name, parent);
|
|
101
|
-
}
|
|
102
|
-
/** The migrations, run against whatever database `env` names, through the direct endpoint. */
|
|
103
|
-
export async function migrate(cli, env) {
|
|
104
|
-
return (await cli.exec("pnpm", ["exec", "drizzle-kit", "migrate"], { env, inherit: true })).code;
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* `stack with-neon <command> [args]` — runs a local command against your Neon dev branch with no `.env`: the connection strings and production's host come from the Neon CLI on every run.
|
|
108
|
-
*
|
|
109
|
-
* Steps aside, and runs the command untouched, when something else already says where the database is: DATABASE_URL in the environment (Vercel, an e2e run, a local Postgres), ENV_FILE (a deliberate production command). A `.env` never decides: what this sets wins over it, in Next and in `loadEnvFile` alike.
|
|
110
|
-
*/
|
|
111
|
-
export async function withNeon(cli, [command, ...args]) {
|
|
112
|
-
if (!command) {
|
|
113
|
-
cli.error("usage: stack with-neon <command> [args]");
|
|
114
|
-
return 2;
|
|
115
|
-
}
|
|
116
|
-
const env = { ...cli.env, ...(await devBranchEnv(cli)) };
|
|
117
|
-
return (await cli.exec(command, args, { env, inherit: true })).code;
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Your dev branch's `branchEnv`, as `with-neon` hands it to a command; nothing (`{}`) when DATABASE_URL or ENV_FILE already says where the database is. Refuses the default branch.
|
|
121
|
-
*/
|
|
122
|
-
export async function devBranchEnv(cli) {
|
|
123
|
-
if (cli.env.DATABASE_URL || cli.env.ENV_FILE)
|
|
124
|
-
return {};
|
|
125
|
-
const name = devBranchName(cli);
|
|
126
|
-
const branches = await listBranches(cli);
|
|
127
|
-
const production = defaultBranch(branches);
|
|
128
|
-
assertDevBranchName(name, production);
|
|
129
|
-
const branch = branches.find((b) => b.name === name);
|
|
130
|
-
if (!branch) {
|
|
131
|
-
throw new CliError(`no branch ${name} yet; run \`stack dev-setup\` once.`);
|
|
132
|
-
}
|
|
133
|
-
const env = await branchEnv(cli, branch.id, production);
|
|
134
|
-
cli.error(`[neon] ${name}`);
|
|
135
|
-
return env;
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* The shared `preview` branch's `branchEnv`, for a package's command run with `--preview` (`databaseEnv({ preview: true })`): previews hold only `db/seed.ts`'s rows, so this is how a developer changes what one holds. `--preview` names the database, so DATABASE_URL or ENV_FILE set as well is refused rather than overridden, as is a `preview` that is the default branch or answers with production's endpoint.
|
|
139
|
-
*/
|
|
140
|
-
export async function previewBranchEnv(cli) {
|
|
141
|
-
if (cli.env.DATABASE_URL || cli.env.ENV_FILE) {
|
|
142
|
-
throw new CliError("--preview names the database itself; unset DATABASE_URL and ENV_FILE.");
|
|
143
|
-
}
|
|
144
|
-
const branches = await listBranches(cli);
|
|
145
|
-
const production = defaultBranch(branches);
|
|
146
|
-
const branch = branches.find((b) => b.name === PREVIEW);
|
|
147
|
-
if (!branch) {
|
|
148
|
-
throw new CliError(`no ${PREVIEW} branch yet; run \`stack neon reset-preview\` first.`);
|
|
149
|
-
}
|
|
150
|
-
const env = await branchEnv(cli, branch.id, production);
|
|
151
|
-
const productionEndpoint = endpointOf(env.PRODUCTION_DATABASE_HOST);
|
|
152
|
-
if (branch.default ||
|
|
153
|
-
endpointOf(env.DATABASE_URL) === productionEndpoint ||
|
|
154
|
-
endpointOf(env.DATABASE_URL_UNPOOLED) === productionEndpoint) {
|
|
155
|
-
throw new CliError(`${PREVIEW} is production's database; refusing to change it.`);
|
|
156
|
-
}
|
|
157
|
-
cli.error(`[neon] ${PREVIEW}`);
|
|
158
|
-
return env;
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* `stack dev-setup` — once per developer: makes your Neon dev branch from `seed` (never from production) if it does not exist yet (`dev/<os user>`, or NEON_DEV_BRANCH) and migrates it. Safe to re-run. There is no `.env` to write: `stack with-neon` reads the branch from the Neon CLI on every command.
|
|
162
|
-
*/
|
|
163
|
-
export async function devSetup(cli) {
|
|
164
|
-
const name = devBranchName(cli);
|
|
165
|
-
const branches = await listBranches(cli);
|
|
166
|
-
const production = defaultBranch(branches);
|
|
167
|
-
assertDevBranchName(name, production);
|
|
168
|
-
let branch = branches.find((b) => b.name === name);
|
|
169
|
-
if (branch) {
|
|
170
|
-
cli.log(`[dev-setup] using ${name} (${branch.id})`);
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
if (!branches.some((b) => b.name === SEED)) {
|
|
174
|
-
throw new CliError(`no \`${SEED}\` branch to cut ${name} from; run \`stack neon seed\` first.`);
|
|
175
|
-
}
|
|
176
|
-
branch = (await recreate(cli, name, SEED)) ?? undefined;
|
|
177
|
-
if (!branch)
|
|
178
|
-
throw new CliError(`could not create ${name} from ${SEED}.`);
|
|
179
|
-
cli.log(`[dev-setup] created ${name} (${branch.id}) from ${SEED}`);
|
|
180
|
-
}
|
|
181
|
-
const status = await migrate(cli, {
|
|
182
|
-
...cli.env,
|
|
183
|
-
...(await branchEnv(cli, branch.id, production)),
|
|
184
|
-
});
|
|
185
|
-
if (status === 0)
|
|
186
|
-
cli.log("[dev-setup] done — `stack with-neon pnpm dev` serves the app");
|
|
187
|
-
return status;
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Schemas Neon's own services install, which hold functions and settings rather than anyone's rows: `auth` is the pg_session_jwt extension the Data API installs (Neon: "do not install this extension manually"), `pgrst` is the Data API's PostgREST configuration. Left in place so the branch keeps working like any other. Neon Auth's `neon_auth` is *not* here: it holds users.
|
|
191
|
-
*/
|
|
192
|
-
export const NEON_SCHEMAS = ["auth", "pgrst"];
|
|
193
|
-
const isKept = (schema) => schema === "information_schema" || schema.startsWith("pg_") || NEON_SCHEMAS.includes(schema);
|
|
194
|
-
const quoteIdent = (name) => `"${name.replaceAll('"', '""')}"`;
|
|
195
|
-
const quoteLiteral = (value) => `'${value.replaceAll("'", "''")}'`;
|
|
196
|
-
/**
|
|
197
|
-
* Empties a database down to an empty `public`: drops every schema but Postgres's own (`pg_catalog`, `pg_toast*`, `information_schema`, any `pg_*`) and Neon's service schemas, with everything in them — tables, rows, drizzle's journal, Neon Auth's users. Then the migrations run from nothing, as on a new database. A schema-only branch needs this as much as a full copy: it keeps drizzle's journal *table* but not its rows, so migrate would re-run 0000 into tables that already exist. Large objects belong to the database rather than to a schema, so no drop removes them: every one is unlinked too.
|
|
198
|
-
*
|
|
199
|
-
* Neon's schemas are kept by name, so first it checks they hold what Neon puts there: functions and settings, no rows. A table, partitioned table, materialized view, sequence or foreign table in one (an app's own `auth.accounts`, say) would carry production's rows into the seed, so it refuses before dropping anything.
|
|
200
|
-
*/
|
|
201
|
-
export async function emptyDatabase(sql) {
|
|
202
|
-
const held = await sql("SELECT n.nspname, c.relname FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace " +
|
|
203
|
-
`WHERE n.nspname IN (${NEON_SCHEMAS.map(quoteLiteral).join(", ")}) ` +
|
|
204
|
-
"AND c.relkind IN ('r', 'p', 'm', 'S', 'f') ORDER BY 1, 2");
|
|
205
|
-
if (held.length > 0) {
|
|
206
|
-
const names = held.map((r) => `${String(r.nspname)}.${String(r.relname)}`);
|
|
207
|
-
throw new CliError(`${names.join(", ")} ${names.length === 1 ? "is a relation" : "are relations"} in a schema kept as Neon's own (${NEON_SCHEMAS.join(", ")}), ` +
|
|
208
|
-
"so it could carry production's rows into the seed. Refusing; move the app's tables out of those schemas.");
|
|
209
|
-
}
|
|
210
|
-
const rows = await sql("SELECT nspname FROM pg_namespace");
|
|
211
|
-
const dropped = rows.map((r) => String(r.nspname)).filter((name) => !isKept(name));
|
|
212
|
-
if (dropped.length > 0)
|
|
213
|
-
await sql(`DROP SCHEMA ${dropped.map(quoteIdent).join(", ")} CASCADE`);
|
|
214
|
-
await sql("SELECT lo_unlink(oid) FROM pg_largeobject_metadata");
|
|
215
|
-
await sql("CREATE SCHEMA public");
|
|
216
|
-
await sql("GRANT USAGE ON SCHEMA public TO PUBLIC");
|
|
217
|
-
return dropped;
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* `stack neon seed` — (re)creates the `seed` branch: exactly the app's migrations plus its seed rows, and none of production's data. Cuts it from production (the project's default branch) as Neon's `--schema-only` branch (a normal branch where that is unsupported), empties it of every non-system schema either way (`emptyDatabase`), migrates it from nothing and runs the app's `db/seed.ts`: `export default async function seed(db)`.
|
|
221
|
-
*
|
|
222
|
-
* The new seed is built as `seed-next` and takes the name `seed` only once it is clean, so a failure part-way never leaves a branch holding production's data under the name every dev and preview branch is cut from; `seed-next` is deleted on any failure. Neon will not delete a branch that has children, so a seed with dev/preview branches on it is refused up front: remove them yourself (this never deletes them).
|
|
223
|
-
*/
|
|
224
|
-
export async function neonSeed(cli) {
|
|
225
|
-
projectId(cli.env);
|
|
226
|
-
const branches = await listBranches(cli);
|
|
227
|
-
const production = defaultBranch(branches);
|
|
228
|
-
const old = branches.find((b) => b.name === SEED);
|
|
229
|
-
const children = old ? branches.filter((b) => b.parent_id === old.id) : [];
|
|
230
|
-
if (children.length > 0) {
|
|
231
|
-
const names = children.map((b) => b.name);
|
|
232
|
-
throw new CliError(`${SEED} has child branches (${names.join(", ")}), and Neon will not delete a branch that has children. ` +
|
|
233
|
-
`Delete them first (${names.map((n) => `\`neon branches delete ${n}\``).join(", ")}), ` +
|
|
234
|
-
`then re-run; re-cut ${PREVIEW} with \`stack neon reset-preview\` and your dev branch with \`stack dev-setup\` afterwards.`);
|
|
235
|
-
}
|
|
236
|
-
const stale = branches.find((b) => b.name === SEED_NEXT);
|
|
237
|
-
if (stale)
|
|
238
|
-
await neonOk(cli, ["branches", "delete", stale.id]);
|
|
239
|
-
let next = await createBranch(cli, SEED_NEXT, production.name, ["--schema-only"]);
|
|
240
|
-
const schemaOnly = next !== undefined;
|
|
241
|
-
next ??= await createBranch(cli, SEED_NEXT, production.name);
|
|
242
|
-
if (!next)
|
|
243
|
-
throw new CliError(`could not create the ${SEED} branch from ${production.name}.`);
|
|
244
|
-
try {
|
|
245
|
-
cli.log(schemaOnly
|
|
246
|
-
? `[seed] built ${SEED_NEXT} (schema only)`
|
|
247
|
-
: `[seed] built ${SEED_NEXT}; --schema-only is unsupported here, so it is a full copy`);
|
|
248
|
-
const env = await branchEnv(cli, next.id, production);
|
|
249
|
-
const direct = env.DATABASE_URL_UNPOOLED;
|
|
250
|
-
// The destructive step below must be pointed at the new branch, never at production.
|
|
251
|
-
if (endpointOf(direct) === endpointOf(env.PRODUCTION_DATABASE_HOST)) {
|
|
252
|
-
throw new CliError(`${SEED_NEXT} resolves to production's endpoint (${new URL(direct).hostname}); refusing to touch it.`);
|
|
253
|
-
}
|
|
254
|
-
const dropped = await emptyDatabase((statement) => cli.sql(direct, statement));
|
|
255
|
-
cli.log(`[seed] emptied ${SEED_NEXT} (dropped ${dropped.join(", ") || "nothing"})`);
|
|
256
|
-
// The app's modules read process.env: the branch's connection wins over its `.env`.
|
|
257
|
-
Object.assign(cli.env, env);
|
|
258
|
-
loadAppEnv(cli);
|
|
259
|
-
const status = await migrate(cli, cli.env);
|
|
260
|
-
if (status !== 0) {
|
|
261
|
-
await deleteOrWarn(cli, next);
|
|
262
|
-
return status;
|
|
263
|
-
}
|
|
264
|
-
await runAppSeed(cli);
|
|
265
|
-
}
|
|
266
|
-
catch (error) {
|
|
267
|
-
await deleteOrWarn(cli, next);
|
|
268
|
-
throw error;
|
|
269
|
-
}
|
|
270
|
-
// Clean: now it replaces the old seed. From here `seed-next` holds only the fixtures, so a failure leaves it in place and says how to finish.
|
|
271
|
-
const rename = `\`neon branches rename ${SEED_NEXT} ${SEED}\``;
|
|
272
|
-
if (old) {
|
|
273
|
-
const deleted = await neon(cli, ["branches", "delete", old.id]);
|
|
274
|
-
if (deleted.code !== 0) {
|
|
275
|
-
throw new CliError(`could not delete the old ${SEED} (exit ${deleted.code}); it is unchanged. ${SEED_NEXT} is built and clean: ` +
|
|
276
|
-
`finish by hand with \`neon branches delete ${SEED}\`, then ${rename}.`);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
const renamed = await neon(cli, ["branches", "rename", next.id, SEED]);
|
|
280
|
-
if (renamed.code !== 0) {
|
|
281
|
-
throw new CliError(`could not rename ${SEED_NEXT} to ${SEED} (exit ${renamed.code})${old ? `; the old ${SEED} is already deleted` : ""}. ` +
|
|
282
|
-
`${SEED_NEXT} is built and clean: finish by hand with ${rename}.`);
|
|
283
|
-
}
|
|
284
|
-
cli.log(`[seed] ${SEED} is ready`);
|
|
285
|
-
return 0;
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Runs the app's `db/seed.ts` (`export default async function seed(db)`) on the app's own `db` (`src/lib/db.ts`), wherever `cli.env` points it, then closes that client's pool: on PGlite it holds the one connection the server allows.
|
|
289
|
-
*/
|
|
290
|
-
export async function runAppSeed(cli) {
|
|
291
|
-
const { db } = await importApp(cli, "src/lib/db.ts");
|
|
292
|
-
const seedFn = (await importApp(cli, "db/seed.ts")).default;
|
|
293
|
-
if (!db || typeof seedFn !== "function") {
|
|
294
|
-
throw new CliError("src/lib/db.ts must export `db` and db/seed.ts a default `seed(db)` function.");
|
|
295
|
-
}
|
|
296
|
-
try {
|
|
297
|
-
await seedFn(db);
|
|
298
|
-
}
|
|
299
|
-
finally {
|
|
300
|
-
const client = db.$client;
|
|
301
|
-
if (typeof client?.end === "function")
|
|
302
|
-
await client.end();
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Deletes a half-built `seed-next`; the failure being reported comes first, so this only warns. But loudly: until it is emptied, a full-copy `seed-next` holds production's data.
|
|
307
|
-
*/
|
|
308
|
-
async function deleteOrWarn(cli, branch) {
|
|
309
|
-
const code = await neon(cli, ["branches", "delete", branch.id]).then((result) => result.code, () => -1);
|
|
310
|
-
if (code !== 0) {
|
|
311
|
-
cli.error(`[seed] DELETE ${branch.name} BY HAND — it may hold production data. ` +
|
|
312
|
-
`Cleanup failed (exit ${code}); run \`neon branches delete ${branch.name}\` now.`);
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* Refuses `--set-vercel` when a variable it would replace is one Vercel variable for Preview and another environment. Reads metadata only, never a value.
|
|
317
|
-
*/
|
|
318
|
-
async function refuseSharedPreviewVariables(cli) {
|
|
319
|
-
const { envs } = await listEnvs(cli, linkedProject(cli));
|
|
320
|
-
for (const { name } of PREVIEW_VARIABLES) {
|
|
321
|
-
for (const v of envs) {
|
|
322
|
-
if (v.key !== name || !v.target.includes(VERCEL_TARGET.preview))
|
|
323
|
-
continue;
|
|
324
|
-
const others = v.target.filter((t) => t !== VERCEL_TARGET.preview);
|
|
325
|
-
if (others.length > 0) {
|
|
326
|
-
throw new CliError(`${name} is one Vercel variable for Preview and ${others.join(", ")}; --set-vercel would change it there too. In Vercel, take Preview off it (Settings, Environment Variables), then run this again. Nothing was changed.`);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
/** The Preview variables `--set-vercel` writes, and which of the branch's URLs each holds. */
|
|
332
|
-
export const PREVIEW_VARIABLES = [
|
|
333
|
-
{ name: "DATABASE_URL", pooled: true },
|
|
334
|
-
{ name: "DATABASE_URL_UNPOOLED", pooled: false },
|
|
335
|
-
];
|
|
336
|
-
/**
|
|
337
|
-
* `stack neon reset-preview [--set-vercel]` — (re)creates the one shared `preview` branch from `seed`. A build cannot change the DATABASE_URL a preview deployment uses at runtime, so previews share this branch, and `stack vercel-build` migrates it on each preview build; run this again to start it clean.
|
|
338
|
-
*
|
|
339
|
-
* Without `--set-vercel` it prints the pooled URL, to set by hand as Preview's DATABASE_URL. With it, it writes the pooled URL as DATABASE_URL and the direct one as DATABASE_URL_UNPOOLED into the linked Vercel project, Preview only, as sensitive variables replacing any there, and prints neither. Each value is read from the Neon CLI in full before `vercel env add` starts, then handed over on its stdin: never in its arguments, where `ps` would show it, and never piped from a command still running, which `vercel env add` would read as empty.
|
|
340
|
-
*/
|
|
341
|
-
export async function resetPreview(cli, args = []) {
|
|
342
|
-
const setVercel = args.includes("--set-vercel");
|
|
343
|
-
// Before anything changes: `vercel env add --environment preview --force` on a variable that Production (or Development) shares with Preview could rewrite it for them too, pointing production at the preview branch. Only a Preview-only variable may be replaced.
|
|
344
|
-
if (setVercel)
|
|
345
|
-
await refuseSharedPreviewVariables(cli);
|
|
346
|
-
if (!(await findBranch(cli, SEED))) {
|
|
347
|
-
throw new CliError(`no \`${SEED}\` branch to cut ${PREVIEW} from; run \`stack neon seed\` first.`);
|
|
348
|
-
}
|
|
349
|
-
const preview = await recreate(cli, PREVIEW, SEED);
|
|
350
|
-
if (!preview)
|
|
351
|
-
throw new CliError(`could not create ${PREVIEW} from ${SEED}.`);
|
|
352
|
-
if (!setVercel) {
|
|
353
|
-
cli.error(`[preview] created ${PREVIEW} from ${SEED}; set this as Preview DATABASE_URL on Vercel:`);
|
|
354
|
-
cli.log(await neonOk(cli, ["connection-string", preview.id, "--pooled"]));
|
|
355
|
-
return 0;
|
|
356
|
-
}
|
|
357
|
-
cli.error(`[preview] created ${PREVIEW} from ${SEED}`);
|
|
358
|
-
const production = defaultBranch(await listBranches(cli));
|
|
359
|
-
const productionUrl = await neonOk(cli, ["connection-string", production.id]);
|
|
360
|
-
const values = [];
|
|
361
|
-
for (const { name, pooled } of PREVIEW_VARIABLES) {
|
|
362
|
-
const value = await neonOk(cli, [
|
|
363
|
-
"connection-string",
|
|
364
|
-
preview.id,
|
|
365
|
-
...(pooled ? ["--pooled"] : []),
|
|
366
|
-
]);
|
|
367
|
-
if (!value)
|
|
368
|
-
throw new CliError(`neon connection-string returned nothing for ${PREVIEW}.`);
|
|
369
|
-
// The same backstop `vercel-build` applies: Preview must never be handed production.
|
|
370
|
-
if (endpointOf(value) === endpointOf(productionUrl)) {
|
|
371
|
-
throw new CliError(`${PREVIEW}'s ${pooled ? "pooled" : "direct"} URL resolves to production's endpoint; refusing to set it on Vercel.`);
|
|
372
|
-
}
|
|
373
|
-
values.push({ name, value });
|
|
374
|
-
}
|
|
375
|
-
for (const { name, value } of values) {
|
|
376
|
-
const { code } = await cli.exec("vercel", [
|
|
377
|
-
"env",
|
|
378
|
-
"add",
|
|
379
|
-
name,
|
|
380
|
-
"--environment",
|
|
381
|
-
VERCEL_TARGET.preview,
|
|
382
|
-
"--sensitive",
|
|
383
|
-
"--force",
|
|
384
|
-
"--yes",
|
|
385
|
-
], { input: value });
|
|
386
|
-
if (code !== 0) {
|
|
387
|
-
throw new CliError(`vercel env add ${name} failed (exit ${code}). Is the Vercel CLI signed in (\`vercel login\`) ` +
|
|
388
|
-
"and this directory linked (`vercel link`)? Re-run with --set-vercel once it is.");
|
|
389
|
-
}
|
|
390
|
-
cli.error(`[preview] set ${name} on Vercel (Preview, sensitive)`);
|
|
391
|
-
}
|
|
392
|
-
cli.log(`[preview] Vercel's Preview DATABASE_URL and DATABASE_URL_UNPOOLED now point at ${PREVIEW}; redeploy a preview to use them.`);
|
|
393
|
-
return 0;
|
|
394
|
-
}
|
package/dist/cli/run.d.ts
DELETED
package/dist/cli/run.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
// `stack run <script> [args...]` — runs one of the app's own scripts (`scripts/backfill.ts`, say) with this Node, type stripping and all, and the environment every other command gives the app: your Neon dev branch unless DATABASE_URL or ENV_FILE says otherwise, then the app's `.env` (or the file ENV_FILE names) for whatever is still unset. ENV_FILE is also the deliberate production opt-in, exactly as for `databaseEnv` (`@wwchen/stack/db/branches`). The arguments and the exit code pass through.
|
|
2
|
-
import { resolve } from "node:path";
|
|
3
|
-
import { assertDevDatabase } from "../db/index.js";
|
|
4
|
-
import { loadAppEnv } from "./context.js";
|
|
5
|
-
import { devBranchEnv } from "./neon.js";
|
|
6
|
-
export async function runScript(cli, [script, ...args]) {
|
|
7
|
-
if (!script) {
|
|
8
|
-
cli.error("usage: stack run <script> [args...]");
|
|
9
|
-
return 2;
|
|
10
|
-
}
|
|
11
|
-
Object.assign(cli.env, await devBranchEnv(cli));
|
|
12
|
-
loadAppEnv(cli);
|
|
13
|
-
// The same refusal the app's own db.ts makes, applied before the script starts: production only with ENV_FILE.
|
|
14
|
-
for (const url of [cli.env.DATABASE_URL, cli.env.DATABASE_URL_UNPOOLED]) {
|
|
15
|
-
if (!url?.trim())
|
|
16
|
-
continue;
|
|
17
|
-
assertDevDatabase(url, {
|
|
18
|
-
nodeEnv: cli.env.NODE_ENV,
|
|
19
|
-
allowRemote: cli.env.ALLOW_REMOTE_DATABASE,
|
|
20
|
-
envFile: cli.env.ENV_FILE,
|
|
21
|
-
productionHost: cli.env.PRODUCTION_DATABASE_HOST,
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
const { code } = await cli.exec(cli.execPath, [resolve(cli.cwd, script), ...args], {
|
|
25
|
-
env: cli.env,
|
|
26
|
-
inherit: true,
|
|
27
|
-
});
|
|
28
|
-
return code;
|
|
29
|
-
}
|
package/dist/db/branches.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { loadAppEnv } from "../cli/context.ts";
|
|
2
|
-
import type { CommandContext } from "../stack/contracts.ts";
|
|
3
|
-
/** Loads the app's `.env`, or the file `ENV_FILE` names (the deliberate production opt-in), into `ctx.env`, filling in only what is still unset: what every `stack` command that runs the app's code does after `databaseEnv`. */
|
|
4
|
-
export { loadAppEnv };
|
|
5
|
-
/**
|
|
6
|
-
* The variables that point a command at its database, to assign onto `ctx.env` before the app's modules load: your Neon dev branch's URLs and production's host (`dev/<username>`, or NEON_DEV_BRANCH), or nothing when DATABASE_URL or ENV_FILE already names a database. With `preview`, the shared `preview` branch's, refusing when DATABASE_URL or ENV_FILE is also set, and refusing a `preview` that is production's database. Every refusal throws with the reason; the Neon CLI runs through `ctx.exec`, and `[neon] <branch>` goes to `ctx.error`.
|
|
7
|
-
*/
|
|
8
|
-
export declare function databaseEnv(ctx: Pick<CommandContext, "cwd" | "env" | "exec" | "log" | "error">, opts: {
|
|
9
|
-
preview: boolean;
|
|
10
|
-
username?: string;
|
|
11
|
-
}): Promise<Record<string, string>>;
|
package/dist/db/branches.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
// `@wwchen/stack/db/branches`: the Neon branch a package's command works on, for a command that reaches the app's database. Its own export, never re-exported from `@wwchen/stack/db`: the app's runtime bundles that one, and this module loads the CLI's Neon helpers.
|
|
2
|
-
import { loadAppEnv, resolveDeps } from "../cli/context.js";
|
|
3
|
-
import { devBranchEnv, previewBranchEnv } from "../cli/neon.js";
|
|
4
|
-
/** Loads the app's `.env`, or the file `ENV_FILE` names (the deliberate production opt-in), into `ctx.env`, filling in only what is still unset: what every `stack` command that runs the app's code does after `databaseEnv`. */
|
|
5
|
-
export { loadAppEnv };
|
|
6
|
-
/**
|
|
7
|
-
* The variables that point a command at its database, to assign onto `ctx.env` before the app's modules load: your Neon dev branch's URLs and production's host (`dev/<username>`, or NEON_DEV_BRANCH), or nothing when DATABASE_URL or ENV_FILE already names a database. With `preview`, the shared `preview` branch's, refusing when DATABASE_URL or ENV_FILE is also set, and refusing a `preview` that is production's database. Every refusal throws with the reason; the Neon CLI runs through `ctx.exec`, and `[neon] <branch>` goes to `ctx.error`.
|
|
8
|
-
*/
|
|
9
|
-
export async function databaseEnv(ctx, opts) {
|
|
10
|
-
const cli = resolveDeps({
|
|
11
|
-
cwd: ctx.cwd,
|
|
12
|
-
env: ctx.env,
|
|
13
|
-
exec: ctx.exec,
|
|
14
|
-
log: ctx.log,
|
|
15
|
-
error: ctx.error,
|
|
16
|
-
...(opts.username === undefined ? {} : { username: opts.username }),
|
|
17
|
-
});
|
|
18
|
-
return opts.preview ? previewBranchEnv(cli) : devBranchEnv(cli);
|
|
19
|
-
}
|
package/dist/db/doctor.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { type BuildStep, type DoctorFacts, type Finding } from "../stack/contracts.ts";
|
|
2
|
-
/** Every variable the stack's env reads a database connection from. */
|
|
3
|
-
export declare const DATABASE_URL_VARIABLES: readonly ["DATABASE_URL", "DATABASE_URL_UNPOOLED"];
|
|
4
|
-
/** What the Neon Vercel integration writes on every variable it creates. */
|
|
5
|
-
export declare const NEON_INTEGRATION_COMMENT = "The Neon Postgres integration sets this environment variable automatically";
|
|
6
|
-
/** Migrates a Preview or Production deployment's database before `next build`; a refusal or a failed migration throws, which fails the build. */
|
|
7
|
-
export declare const migrateStep: BuildStep;
|
|
8
|
-
/** The database's and the core's variables, on metadata alone. */
|
|
9
|
-
export declare function databaseChecks(h: DoctorFacts): Promise<Finding[]>;
|
package/dist/db/doctor.js
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
// The database's share of `stack vercel-build` and `stack doctor` (a composed hosting package's commands): the migrate build step, and the checks of the database variables, the Neon Vercel integration and the core's own secrets. The built-in package contributes them; never re-exported from `db/index.ts`, because it reaches the CLI's `migrate`.
|
|
2
|
-
//
|
|
3
|
-
// The build step, Neon's documented pattern: the build applies the migrations, so the deployment never goes live on a schema it was not built for.
|
|
4
|
-
// - Preview: migrates the one shared `preview` branch (see `stack neon reset-preview`), which lacks this PR's new migrations. Refuses if any of its database URLs turns out to be production.
|
|
5
|
-
// - Production: migrates production's database. A failed migration fails the build, so the new code never goes live on a schema it was not built for. The old deployment keeps serving while this runs, which is why every migration must be expand-then-contract.
|
|
6
|
-
// - Anything else (a local run): does nothing.
|
|
7
|
-
import { CliError } from "../cli/context.js";
|
|
8
|
-
import { migrate } from "../cli/neon.js";
|
|
9
|
-
import { SEVERITY, VERCEL_TARGET, } from "../stack/contracts.js";
|
|
10
|
-
import { previewOwnSecretsCheck } from "../vercel/index.js";
|
|
11
|
-
import { endpointOf } from "./index.js";
|
|
12
|
-
const DATABASE_URL = "DATABASE_URL";
|
|
13
|
-
const DATABASE_URL_UNPOOLED = "DATABASE_URL_UNPOOLED";
|
|
14
|
-
const PRODUCTION_DATABASE_HOST = "PRODUCTION_DATABASE_HOST";
|
|
15
|
-
/** Every variable the stack's env reads a database connection from. */
|
|
16
|
-
export const DATABASE_URL_VARIABLES = [DATABASE_URL, DATABASE_URL_UNPOOLED];
|
|
17
|
-
/** What the Neon Vercel integration writes on every variable it creates. */
|
|
18
|
-
export const NEON_INTEGRATION_COMMENT = "The Neon Postgres integration sets this environment variable automatically";
|
|
19
|
-
/** The core's secrets a preview must hold its own value of, never production's (runbook §3). */
|
|
20
|
-
const PREVIEW_OWN_SECRETS = [
|
|
21
|
-
"TOKEN_ENCRYPTION_KEY",
|
|
22
|
-
"CRON_SECRET",
|
|
23
|
-
];
|
|
24
|
-
const hostOf = (url) => {
|
|
25
|
-
try {
|
|
26
|
-
return new URL(url).hostname;
|
|
27
|
-
}
|
|
28
|
-
catch {
|
|
29
|
-
return "(unparseable URL)";
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* A deployment that migrates needs a database URL. drizzle.config.ts migrates through the direct (unpooled) endpoint when it is set, bypassing PgBouncer; without it, through the pooler.
|
|
34
|
-
*/
|
|
35
|
-
function requireDatabaseUrl(ctx, environment) {
|
|
36
|
-
if (!ctx.env[DATABASE_URL_UNPOOLED] && !ctx.env[DATABASE_URL]) {
|
|
37
|
-
// Not the Neon integration: its preview branches are cut from production, with its data.
|
|
38
|
-
const where = environment === "Preview"
|
|
39
|
-
? "the `preview` branch's URL from `stack neon reset-preview`, scoped to Preview"
|
|
40
|
-
: "production's URL, scoped to Production";
|
|
41
|
-
throw new CliError(`${environment} deployment with no ${DATABASE_URL}: set it in Vercel (${where}).`);
|
|
42
|
-
}
|
|
43
|
-
if (!ctx.env[DATABASE_URL_UNPOOLED]) {
|
|
44
|
-
ctx.error(`[build] ${environment}: no ${DATABASE_URL_UNPOOLED}; migrating through the pooler.`);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
/** Migrates a Preview or Production deployment's database before `next build`; a refusal or a failed migration throws, which fails the build. */
|
|
48
|
-
export const migrateStep = {
|
|
49
|
-
name: "migrate",
|
|
50
|
-
async run(ctx) {
|
|
51
|
-
const environment = ctx.env.VERCEL_ENV;
|
|
52
|
-
if (environment !== VERCEL_TARGET.preview && environment !== VERCEL_TARGET.production)
|
|
53
|
-
return;
|
|
54
|
-
requireDatabaseUrl(ctx, environment === VERCEL_TARGET.preview ? "Preview" : "Production");
|
|
55
|
-
if (environment === VERCEL_TARGET.preview) {
|
|
56
|
-
// A backstop for a mis-scoped variable: a preview must never migrate or serve production. Every database URL is checked, not just the one migrate uses: the running preview reads DATABASE_URL, and Vercel previews run with NODE_ENV=production, so assertDevDatabase does not catch it at runtime. Pooled and direct hosts of one endpoint compare equal (endpointOf), whichever form each variable holds. Not optional: without production's host there is nothing to check against, and the host is not a secret, so it is set for every environment.
|
|
57
|
-
const productionHost = ctx.env[PRODUCTION_DATABASE_HOST] ?? "";
|
|
58
|
-
if (productionHost.trim() === "") {
|
|
59
|
-
throw new CliError(`${PRODUCTION_DATABASE_HOST} is not set on Preview, so this build cannot tell the ` +
|
|
60
|
-
"preview database from production. Set it in Vercel for all environments " +
|
|
61
|
-
"(production's direct host, not a secret); see the runbook.");
|
|
62
|
-
}
|
|
63
|
-
for (const name of DATABASE_URL_VARIABLES) {
|
|
64
|
-
const value = ctx.env[name];
|
|
65
|
-
if (value && endpointOf(value) === endpointOf(productionHost)) {
|
|
66
|
-
throw new CliError(`This preview's ${name} (${hostOf(value)}) is production (${productionHost.trim()}). ` +
|
|
67
|
-
"Refusing to migrate or build; scope it to the `preview` branch (see the runbook).");
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
ctx.log(`[build] ${environment}: migrating before the build.`);
|
|
72
|
-
// The database is remote by design; the guard's opt-in says this command means it.
|
|
73
|
-
const status = await migrate(ctx, { ...ctx.env, ALLOW_REMOTE_DATABASE: "1" });
|
|
74
|
-
if (status !== 0)
|
|
75
|
-
throw new CliError(`drizzle-kit migrate failed (exit ${status}).`);
|
|
76
|
-
},
|
|
77
|
-
};
|
|
78
|
-
/** Variables named `key` that apply to every deployment of `target`: no single git branch. */
|
|
79
|
-
const covering = (h, key, target) => h.envs.filter((v) => v.key === key && v.target.includes(target) && !v.gitBranch);
|
|
80
|
-
const fromNeonIntegration = (v) => (v.configurationId !== undefined && v.gitBranch !== undefined) ||
|
|
81
|
-
(v.comment?.includes(NEON_INTEGRATION_COMMENT) === true &&
|
|
82
|
-
v.target.includes(VERCEL_TARGET.preview));
|
|
83
|
-
/** The database's and the core's variables, on metadata alone. */
|
|
84
|
-
export async function databaseChecks(h) {
|
|
85
|
-
const findings = [];
|
|
86
|
-
const add = (severity, message) => findings.push({ severity, message });
|
|
87
|
-
const integration = h.envs.filter(fromNeonIntegration);
|
|
88
|
-
if (integration.length > 0) {
|
|
89
|
-
const names = [
|
|
90
|
-
...new Set(integration.map((v) => (v.gitBranch ? `${v.key} (${v.gitBranch})` : v.key))),
|
|
91
|
-
];
|
|
92
|
-
add(SEVERITY.error, `The Neon Vercel integration is creating per-preview database variables: ${names.join(", ")}. ` +
|
|
93
|
-
"It cuts each preview's branch from production, copying production's data into previews. " +
|
|
94
|
-
"Turn off its preview branching (Vercel, Integrations, Neon), remove these variables, and " +
|
|
95
|
-
"point Preview at the shared `preview` branch: `stack neon reset-preview --set-vercel`.");
|
|
96
|
-
}
|
|
97
|
-
findings.push(...(await previewOwnSecretsCheck(PREVIEW_OWN_SECRETS)(h)));
|
|
98
|
-
for (const target of Object.values(VERCEL_TARGET)) {
|
|
99
|
-
if (covering(h, PRODUCTION_DATABASE_HOST, target).length === 0) {
|
|
100
|
-
add(target === VERCEL_TARGET.development ? SEVERITY.warning : SEVERITY.error, `${PRODUCTION_DATABASE_HOST} is not set for ${target}: set production's direct host (not a ` +
|
|
101
|
-
"secret) in every environment, so builds and local tooling can refuse production." +
|
|
102
|
-
(target === VERCEL_TARGET.preview
|
|
103
|
-
? " `stack vercel-build` refuses a preview build without it."
|
|
104
|
-
: ""));
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
if (covering(h, DATABASE_URL_UNPOOLED, VERCEL_TARGET.production).length === 0) {
|
|
108
|
-
add(SEVERITY.warning, `${DATABASE_URL_UNPOOLED} is not set for Production, so migrations run through the pooler. ` +
|
|
109
|
-
"Set it to production's direct (non -pooler) URL.");
|
|
110
|
-
}
|
|
111
|
-
if (covering(h, DATABASE_URL, VERCEL_TARGET.preview).length === 0) {
|
|
112
|
-
add(SEVERITY.error, `Preview has no ${DATABASE_URL}, so every preview build fails. Run \`stack neon reset-preview ` +
|
|
113
|
-
"--set-vercel` to point Preview at the shared `preview` branch.");
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
add(SEVERITY.info, `Whether Preview's ${DATABASE_URL} is production's database can only be told from its value, ` +
|
|
117
|
-
"which doctor never reads; `stack vercel-build` refuses that at build time.");
|
|
118
|
-
}
|
|
119
|
-
return findings;
|
|
120
|
-
}
|
package/dist/db/env.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
/** The database's variables, an env fragment for `defineEnv({ fragments })`. */
|
|
3
|
-
export declare const dbEnv: {
|
|
4
|
-
readonly name: "dbEnv";
|
|
5
|
-
readonly schema: {
|
|
6
|
-
readonly DATABASE_URL: z.ZodString;
|
|
7
|
-
readonly DATABASE_URL_UNPOOLED: z.ZodOptional<z.ZodString>;
|
|
8
|
-
readonly PRODUCTION_DATABASE_HOST: z.ZodOptional<z.ZodString>;
|
|
9
|
-
};
|
|
10
|
-
readonly productionRequired: readonly ["DATABASE_URL"];
|
|
11
|
-
readonly buildPlaceholders: {
|
|
12
|
-
readonly DATABASE_URL: "postgres://unused:unused@127.0.0.1:1/unused";
|
|
13
|
-
};
|
|
14
|
-
};
|
package/dist/db/env.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
// Loaded by the app's `env.ts`, so it imports nothing but zod: never `pg` or the database client.
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
/** The database's variables, an env fragment for `defineEnv({ fragments })`. */
|
|
4
|
-
export const dbEnv = {
|
|
5
|
-
name: "dbEnv",
|
|
6
|
-
schema: {
|
|
7
|
-
DATABASE_URL: z.string().min(1),
|
|
8
|
-
DATABASE_URL_UNPOOLED: z.string().optional(),
|
|
9
|
-
// Production's database host, so local tooling can refuse it.
|
|
10
|
-
PRODUCTION_DATABASE_HOST: z.string().optional(),
|
|
11
|
-
},
|
|
12
|
-
productionRequired: ["DATABASE_URL"],
|
|
13
|
-
// Stands in for a connection string during a build, which never opens a connection.
|
|
14
|
-
buildPlaceholders: { DATABASE_URL: "postgres://unused:unused@127.0.0.1:1/unused" },
|
|
15
|
-
};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Pool, type PoolClient } from "pg";
|
|
2
|
-
/**
|
|
3
|
-
* A `pg` Pool for PGlite (a local PGlite dev database, and the PGlite test database) — never built in production.
|
|
4
|
-
*
|
|
5
|
-
* PGlite over pglite-socket serves exactly one connection: a second client is refused, not queued, and after any SQL error the server drops that connection and briefly resets the next one while it tears the old one down. So a refused or reset checkout is retried for a few seconds — the other holder (a script, the dev server, the socket being torn down) lets go within milliseconds — and then fails with a hint that names the likely cause. `pool.query` checks out through `connect(callback)`, so both call shapes go through one path.
|
|
6
|
-
*/
|
|
7
|
-
export declare const PGLITE_HINT: string;
|
|
8
|
-
export declare class ExplainingPool extends Pool {
|
|
9
|
-
connect(): Promise<PoolClient>;
|
|
10
|
-
connect(callback: Parameters<Pool["connect"]>[0] & object): void;
|
|
11
|
-
}
|