create-nextblock 0.10.7 → 0.10.9

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.
@@ -0,0 +1,209 @@
1
+ // Milestone 4 — environment-aware, zero-config build-time schema migration hook.
2
+ //
3
+ // Runs BEFORE `next build` (wired in apps/nextblock/project.json for nx/Vercel and in
4
+ // the app's `prebuild` script for standalone/Docker `npm run build`). It applies any
5
+ // pending, forward-only migrations to the tenant's Postgres so the deployed app and its
6
+ // schema move together — additively, transactionally, and tracked exactly like the
7
+ // Supabase CLI in supabase_migrations.schema_migrations.
8
+ //
9
+ // Gating (no manual config required):
10
+ // * On Vercel: run only when VERCEL_ENV === 'production'. Preview/development builds
11
+ // are skipped so feature previews never touch live data structures.
12
+ // * Off Vercel (npm create / local / Docker): run only when NEXTBLOCK_BUILD_MIGRATE === '1'
13
+ // (the /setup wizard and the create/docker setup scripts write this into .env).
14
+ //
15
+ // Safety: this NEVER fails the build. A missing flag, missing POSTGRES_URL, or an
16
+ // unreachable database logs a warning and exits 0 so static packaging always proceeds.
17
+ //
18
+ // The SQL constants + apply loop are intentionally kept in lockstep with
19
+ // apps/nextblock/lib/setup/schema-apply.ts (the /setup wizard's applier). This file is
20
+ // plain ESM with no app/server-only imports so it loads safely in any build context.
21
+
22
+ import { existsSync } from 'node:fs';
23
+ import { readdir, readFile } from 'node:fs/promises';
24
+ import path from 'node:path';
25
+
26
+ const log = (msg) => console.log(`[build-migrate] ${msg}`);
27
+
28
+ /** Best-effort .env.local / .env load (dotenv is a dependency; tolerate its absence). */
29
+ async function loadEnv() {
30
+ try {
31
+ const dotenv = await import('dotenv');
32
+ for (const file of ['.env.local', '.env']) {
33
+ const p = path.join(process.cwd(), file);
34
+ if (existsSync(p)) dotenv.config({ path: p, override: false, quiet: true });
35
+ }
36
+ } catch {
37
+ /* dotenv unavailable — env may already be injected by the platform */
38
+ }
39
+ }
40
+
41
+ /** Decide whether the hook should run, with a human-readable reason for the log. */
42
+ function evaluateGate() {
43
+ const vercelEnv = process.env.VERCEL_ENV;
44
+ if (vercelEnv) {
45
+ return vercelEnv === 'production'
46
+ ? { run: true, reason: 'VERCEL_ENV=production' }
47
+ : { run: false, reason: `VERCEL_ENV=${vercelEnv} — preview/development build, skipping` };
48
+ }
49
+ if (process.env.NEXTBLOCK_BUILD_MIGRATE === '1') {
50
+ return { run: true, reason: 'NEXTBLOCK_BUILD_MIGRATE=1' };
51
+ }
52
+ return { run: false, reason: 'NEXTBLOCK_BUILD_MIGRATE is not set — skipping' };
53
+ }
54
+
55
+ /**
56
+ * Locate the migrations dir: monorepo keeps them at libs/db/src/supabase/migrations
57
+ * (found via the nearest nx.json); a standalone create-nextblock project materializes
58
+ * them at <projectRoot>/supabase/migrations. Mirrors schema-apply.ts.
59
+ */
60
+ function resolveMigrationsDir() {
61
+ let dir = process.cwd();
62
+ for (let i = 0; i < 8; i++) {
63
+ if (existsSync(path.join(dir, 'nx.json'))) {
64
+ const monorepo = path.join(dir, 'libs', 'db', 'src', 'supabase', 'migrations');
65
+ if (existsSync(monorepo)) return monorepo;
66
+ break;
67
+ }
68
+ const parent = path.dirname(dir);
69
+ if (parent === dir) break;
70
+ dir = parent;
71
+ }
72
+ dir = process.cwd();
73
+ for (let i = 0; i < 8; i++) {
74
+ const standalone = path.join(dir, 'supabase', 'migrations');
75
+ if (existsSync(standalone)) return standalone;
76
+ const parent = path.dirname(dir);
77
+ if (parent === dir) break;
78
+ dir = parent;
79
+ }
80
+ return null;
81
+ }
82
+
83
+ // --- SQL kept in sync with apps/nextblock/lib/setup/schema-apply.ts ---------------
84
+ const TRACKING_DDL =
85
+ 'create schema if not exists supabase_migrations;' +
86
+ 'create table if not exists supabase_migrations.schema_migrations ' +
87
+ '(version text primary key, name text, statements text[]);';
88
+
89
+ const GRANTS_SQL =
90
+ 'grant usage on schema public to anon, authenticated, service_role;' +
91
+ 'grant select on all tables in schema public to anon;' +
92
+ 'grant all on all tables in schema public to authenticated, service_role;' +
93
+ 'grant all on all sequences in schema public to anon, authenticated, service_role;' +
94
+ 'grant execute on all functions in schema public to anon, authenticated, service_role;';
95
+
96
+ function recordSql(version, file) {
97
+ return (
98
+ `insert into supabase_migrations.schema_migrations (version, name) ` +
99
+ `values ('${version}', '${file.replace(/'/g, "''")}') on conflict (version) do nothing;`
100
+ );
101
+ }
102
+
103
+ /** Wrap a migration file + its version record in one transaction (all-or-nothing). */
104
+ function transactionalMigration(version, file, sqlText) {
105
+ return `begin;\n${sqlText}\n;\n${recordSql(version, file)}\ncommit;`;
106
+ }
107
+
108
+ /** Apply pending migrations over a direct Postgres connection. Never throws. */
109
+ async function applyViaPostgres(dbUrl, files, readSql) {
110
+ let postgres;
111
+ try {
112
+ ({ default: postgres } = await import('postgres'));
113
+ } catch {
114
+ return { ok: false, applied: 0, error: 'the "postgres" package is not installed' };
115
+ }
116
+
117
+ const db = postgres(dbUrl, { ssl: 'require', onnotice: () => undefined, max: 1 });
118
+ let applied = 0;
119
+ try {
120
+ await db.unsafe(TRACKING_DDL);
121
+ const rows = await db`select version from supabase_migrations.schema_migrations`;
122
+ const done = new Set(rows.map((r) => r.version));
123
+
124
+ // Self-heal stale history: recorded versions but a core table missing means the
125
+ // schema was wiped without clearing history (separate schema) — re-apply all.
126
+ if (done.size > 0) {
127
+ const sentinel = await db`select to_regclass('public.site_settings')::text as t`;
128
+ if (!sentinel[0]?.t) {
129
+ await db`delete from supabase_migrations.schema_migrations`;
130
+ done.clear();
131
+ }
132
+ }
133
+
134
+ for (const file of files) {
135
+ const version = file.split('_')[0];
136
+ if (done.has(version)) continue;
137
+ const sqlText = await readSql(file);
138
+ await db.unsafe(transactionalMigration(version, file, sqlText));
139
+ applied += 1;
140
+ }
141
+
142
+ await db.unsafe(GRANTS_SQL);
143
+ await db.unsafe("notify pgrst, 'reload schema';");
144
+ return { ok: true, applied };
145
+ } catch (caught) {
146
+ const message = caught instanceof Error ? caught.message : 'unknown error applying migrations';
147
+ const unreachable = /ENOTFOUND|ENOENT|EAI_AGAIN|getaddrinfo|ECONNREFUSED|ETIMEDOUT/i.test(message);
148
+ return {
149
+ ok: false,
150
+ applied,
151
+ error: unreachable
152
+ ? `could not reach the database host (${message}); use the Session pooler connection string for IPv4 build networks`
153
+ : message,
154
+ };
155
+ } finally {
156
+ try {
157
+ await db.end({ timeout: 5 });
158
+ } catch {
159
+ /* closing should never mask the result */
160
+ }
161
+ }
162
+ }
163
+
164
+ async function main() {
165
+ await loadEnv();
166
+
167
+ const gate = evaluateGate();
168
+ if (!gate.run) {
169
+ log(`Skipping build-time migrations (${gate.reason}).`);
170
+ return;
171
+ }
172
+ log(`Build-time migrations enabled (${gate.reason}).`);
173
+
174
+ const dbUrl = process.env.POSTGRES_URL || process.env.DATABASE_URL;
175
+ if (!dbUrl) {
176
+ log(
177
+ 'No POSTGRES_URL/DATABASE_URL is set — skipping. Apply the schema with the /setup wizard or "npm run db:migrate".',
178
+ );
179
+ return;
180
+ }
181
+
182
+ const dir = resolveMigrationsDir();
183
+ if (!dir) {
184
+ log('Could not locate the migrations directory — skipping.');
185
+ return;
186
+ }
187
+
188
+ const files = (await readdir(dir)).filter((name) => /^\d+_.*\.sql$/.test(name)).sort();
189
+ const result = await applyViaPostgres(dbUrl, files, (file) =>
190
+ readFile(path.join(dir, file), 'utf8'),
191
+ );
192
+
193
+ if (result.ok) {
194
+ log(result.applied > 0 ? `Applied ${result.applied} pending migration(s).` : 'Schema already up to date.');
195
+ } else {
196
+ log(`WARNING: could not apply migrations: ${result.error}.`);
197
+ log('Continuing the build — apply later with "npm run db:migrate".');
198
+ }
199
+ }
200
+
201
+ // Guarantee a clean exit code regardless of outcome: infrastructure issues must never
202
+ // break web packaging.
203
+ main()
204
+ .catch((err) => {
205
+ log(`WARNING: ${err instanceof Error ? err.message : String(err)}.`);
206
+ })
207
+ .finally(() => {
208
+ process.exit(0);
209
+ });