Skip to content

Commit f596ee9

Browse files
committed
fix: Fix D1 deploy migrations with multiline CREATE TABLE.
1 parent 3307e6f commit f596ee9

2 files changed

Lines changed: 139 additions & 3 deletions

File tree

‎packages/void/src/runtime/migration-handler.ts‎

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ async function runD1Migrations(
114114
const start = Date.now();
115115
try {
116116
const statements = splitMigrationSql(m.sql);
117-
if (statements.length === 1 && statements[0] === m.sql) {
118-
await db.exec(m.sql);
117+
if (statements.length === 1) {
118+
if (isSingleSqlStatement(statements[0])) {
119+
await db.prepare(statements[0]).run();
120+
} else {
121+
await db.exec(m.sql);
122+
}
119123
} else {
120124
for (const statement of statements) {
121125
await db.prepare(statement).run();
@@ -189,3 +193,88 @@ function isStatementBreakpointLine(line: string): boolean {
189193
function hasInlineStatementBreakpoint(line: string): boolean {
190194
return /;\s*-->\s*statement-breakpoint\s*$/.test(line.trim());
191195
}
196+
197+
function isSingleSqlStatement(sql: string): boolean {
198+
let statements = 0;
199+
let inStatement = false;
200+
let index = 0;
201+
202+
while (index < sql.length) {
203+
const character = sql[index];
204+
const next = index + 1 < sql.length ? sql[index + 1] : '';
205+
206+
if (character === '-' && next === '-') {
207+
index += 2;
208+
while (index < sql.length && sql[index] !== '\n') {
209+
index++;
210+
}
211+
continue;
212+
}
213+
214+
if (character === '/' && next === '*') {
215+
index += 2;
216+
while (index < sql.length - 1 && !(sql[index] === '*' && sql[index + 1] === '/')) {
217+
index++;
218+
}
219+
index = Math.min(index + 2, sql.length);
220+
continue;
221+
}
222+
223+
if (character === "'") {
224+
inStatement = true;
225+
index++;
226+
while (index < sql.length) {
227+
if (sql[index] === "'" && sql[index + 1] === "'") {
228+
index += 2;
229+
continue;
230+
}
231+
if (sql[index] === "'") {
232+
index++;
233+
break;
234+
}
235+
index++;
236+
}
237+
continue;
238+
}
239+
240+
if (character === '"' || character === '`' || character === '[') {
241+
inStatement = true;
242+
const close = character === '[' ? ']' : character;
243+
index++;
244+
while (index < sql.length) {
245+
if (close !== ']' && sql[index] === close && sql[index + 1] === close) {
246+
index += 2;
247+
continue;
248+
}
249+
if (sql[index] === close) {
250+
index++;
251+
break;
252+
}
253+
index++;
254+
}
255+
continue;
256+
}
257+
258+
if (character === ';') {
259+
if (inStatement) {
260+
statements++;
261+
if (statements > 1) {
262+
return false;
263+
}
264+
inStatement = false;
265+
}
266+
index++;
267+
continue;
268+
}
269+
270+
if (character !== ' ' && character !== '\t' && character !== '\n' && character !== '\r') {
271+
inStatement = true;
272+
}
273+
index++;
274+
}
275+
276+
if (inStatement) {
277+
statements++;
278+
}
279+
return statements === 1;
280+
}

‎packages/void/test/unit/migration-binding-consistency.test.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ describe('createMigrationHandler with missing D1 binding', () => {
346346
describe('createMigrationHandler D1 execution', () => {
347347
const proxyToken = 'test-token-123';
348348

349-
function createMockD1() {
349+
function createMockD1(options?: { failExec?: (sql: string) => Error | undefined }) {
350350
const calls = {
351351
batch: 0,
352352
exec: [] as Array<string>,
@@ -361,6 +361,10 @@ describe('createMigrationHandler D1 execution', () => {
361361
},
362362
async exec(sql: string) {
363363
calls.exec.push(sql);
364+
const error = options?.failExec?.(sql);
365+
if (error) {
366+
throw error;
367+
}
364368
},
365369
prepare(sql: string) {
366370
let bound: Array<unknown> = [];
@@ -428,6 +432,49 @@ describe('createMigrationHandler D1 execution', () => {
428432
expect(calls.batch).toBe(0);
429433
});
430434

435+
it('applies scaffolded multiline single-statement D1 migrations without raw exec', async () => {
436+
const sql = [
437+
'CREATE TABLE `posts` (',
438+
'\t`id` integer PRIMARY KEY NOT NULL,',
439+
'\t`title` text NOT NULL',
440+
');',
441+
].join('\n');
442+
const { calls, db } = createMockD1({
443+
failExec: (executedSql) =>
444+
executedSql.includes('\n')
445+
? new Error('D1_EXEC_ERROR: Error in line 1: CREATE TABLE `posts` (: incomplete input')
446+
: undefined,
447+
});
448+
const handler = createMigrationHandler(
449+
{ __VOID_PROXY_TOKEN: proxyToken, DB: db },
450+
[{ name: '0000_generated.sql', sql }],
451+
'sqlite',
452+
);
453+
454+
const response = await handler(
455+
new Request('https://example.com/__void/migrate', {
456+
method: 'POST',
457+
headers: { 'x-void-internal': proxyToken },
458+
}),
459+
);
460+
const body = (await response.json()) as MigrationResponse;
461+
462+
expect(response.status).toBe(200);
463+
expect(body.ok).toBe(true);
464+
expect(body.migrations).toEqual([
465+
{ name: '0000_generated.sql', applied: true, durationMs: expect.any(Number) },
466+
]);
467+
expect(calls.exec).toEqual([]);
468+
expect(calls.run).toEqual([
469+
`CREATE TABLE IF NOT EXISTS _void_migrations (
470+
name TEXT PRIMARY KEY,
471+
applied_at TEXT NOT NULL
472+
)`,
473+
sql,
474+
'INSERT OR IGNORE INTO _void_migrations (name, applied_at) VALUES (?, ?)',
475+
]);
476+
});
477+
431478
it('applies Drizzle statement breakpoint chunks separately on D1', async () => {
432479
const sql = [
433480
'CREATE TABLE `account` (',

0 commit comments

Comments
 (0)