workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

๐Ÿ› BUG: Unable to apply D1 migration: syntax error

Open MrSuttonmann opened this issue 1 year ago โ€ข 4 comments
trafficstars

Which Cloudflare product(s) does this pertain to?

D1, Wrangler core

What version(s) of the tool(s) are you using?

3.48.0

What version of Node are you using?

20.12.1

What operating system and version are you using?

Windows 11

Describe the Bug

Observed behavior

When applying a migration locally, the migration fails with the following error:

X [ERROR] near "INSERT": syntax error at offset 370

Expected behavior

The migration should apply correctly.

Steps to reproduce

  • Create a migration (npx wrangler d1 migrations create db-name init)
  • Add the following SQL into the migration:
    CREATE TABLE Users (
        UserId INTEGER PRIMARY KEY,
        FullName TEXT NOT NULL,
        EmailAddress TEXT NOT NULL UNIQUE,
        DateCreated DATE NOT NULL,
        DateUpdated DATE
    )
    
  • Run npx wrangler d1 migrations apply db-name

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

--- 2024-04-09T09:10:28.946Z debug
๐Ÿชต  Writing logs to "C:\Users\REDACTED\AppData\Roaming\xdg.config\.wrangler\logs\wrangler-2024-04-09_09-10-28_769.log"
---

--- 2024-04-09T09:10:28.946Z debug
Failed to load .env file ".env": Error: ENOENT: no such file or directory, open 'G:\REDACTED\REDACTED\.env'
    at Object.openSync (node:fs:582:18)
    at Object.readFileSync (node:fs:461:35)
    at tryLoadDotEnv (G:\REDACTED\REDACTED\node_modules\wrangler\wrangler-dist\cli.js:124895:72)
    at loadDotEnv (G:\REDACTED\REDACTED\node_modules\wrangler\wrangler-dist\cli.js:124904:12)
    at G:\REDACTED\REDACTED\node_modules\wrangler\wrangler-dist\cli.js:166830:20
    at G:\REDACTED\REDACTED\node_modules\wrangler\wrangler-dist\cli.js:132321:16
    at maybeAsyncResult (G:\REDACTED\REDACTED\node_modules\wrangler\wrangler-dist\cli.js:130542:44)
    at G:\REDACTED\REDACTED\node_modules\wrangler\wrangler-dist\cli.js:132320:14
    at G:\REDACTED\REDACTED\node_modules\wrangler\wrangler-dist\cli.js:130529:22
    at Array.reduce (<anonymous>) {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'G:\\REDACTED\\REDACTED\\.env'
}
---

--- 2024-04-09T09:10:28.957Z log
 โ›…๏ธ wrangler 3.48.0
[38;2;255;136;0m-------------------[39m
---

--- 2024-04-09T09:10:28.959Z log
๐ŸŒ€ Mapping SQL input into an array of statements
---

--- 2024-04-09T09:10:28.959Z log
๐ŸŒ€ Executing on local database REDACTED (REDACTED) from .wrangler\state\v3\d1:
---

--- 2024-04-09T09:10:28.959Z log
๐ŸŒ€ To execute on your remote database, add a --remote flag to your wrangler command.
---

--- 2024-04-09T09:10:29.217Z log
๐ŸŒ€ Mapping SQL input into an array of statements
---

--- 2024-04-09T09:10:29.218Z log
๐ŸŒ€ Executing on local database REDACTED (REDACTED) from .wrangler\state\v3\d1:
---

--- 2024-04-09T09:10:29.218Z log
๐ŸŒ€ To execute on your remote database, add a --remote flag to your wrangler command.
---

--- 2024-04-09T09:10:29.446Z log
Migrations to be applied:
[1mโ”Œ[22m[1mโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[22m[1mโ”[22m
[1mโ”‚[22m[1m[34m name          [22m[39m[1mโ”‚[22m
[1mโ”œ[22m[1mโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[22m[1mโ”ค[22m
[1mโ”‚[22m 0001_init.sql [1mโ”‚[22m
[1mโ””[22m[1mโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[22m[1mโ”˜[22m
---

--- 2024-04-09T09:10:30.685Z debug
Retrieving cached values for account from node_modules\.cache\wrangler
---

--- 2024-04-09T09:10:30.685Z debug
-- START CF API REQUEST: GET https://api.cloudflare.com/client/v4/accounts/<REDACTED>/d1/database/<REDACTED>
---

--- 2024-04-09T09:10:30.685Z debug
HEADERS: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
---

--- 2024-04-09T09:10:30.685Z debug
INIT: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
---

--- 2024-04-09T09:10:30.685Z debug
-- END CF API REQUEST
---

--- 2024-04-09T09:10:32.913Z debug
-- START CF API RESPONSE: OK 200
---

--- 2024-04-09T09:10:32.913Z debug
HEADERS: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
---

--- 2024-04-09T09:10:32.913Z debug
RESPONSE: omitted; set WRANGLER_LOG_SANITIZE=false to include sanitized data
---

--- 2024-04-09T09:10:32.913Z debug
-- END CF API RESPONSE
---

--- 2024-04-09T09:10:32.913Z log
๐ŸŒ€ Mapping SQL input into an array of statements
---

--- 2024-04-09T09:10:32.913Z log
๐ŸŒ€ Executing on local database REDACTED (REDACTED) from .wrangler\state\v3\d1:
---

--- 2024-04-09T09:10:32.914Z log
๐ŸŒ€ To execute on your remote database, add a --remote flag to your wrangler command.
---

--- 2024-04-09T09:10:33.170Z log
[1mโ”Œ[22m[1mโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[22m[1mโ”ฌ[22m[1mโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[22m[1mโ”[22m
[1mโ”‚[22m[1m[34m name          [22m[39m[1mโ”‚[22m[1m[34m status [22m[39m[1mโ”‚[22m
[1mโ”œ[22m[1mโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[22m[1mโ”ผ[22m[1mโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[22m[1mโ”ค[22m
[1mโ”‚[22m 0001_init.sql [1mโ”‚[22m โŒ       [1mโ”‚[22m
[1mโ””[22m[1mโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[22m[1mโ”ด[22m[1mโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€[22m[1mโ”˜[22m

โŒ Migration 0001_init.sql failed with the following errors:
---

MrSuttonmann avatar Apr 09 '24 09:04 MrSuttonmann

Additional note, the migration successfully applies if a ; is added to the end of the migration SQL.

MrSuttonmann avatar Apr 09 '24 09:04 MrSuttonmann

I think this might be a genuine error messageโ€”I suspect the ; is required. @rozenmd could you confirm?

penalosa avatar Jul 09 '24 08:07 penalosa

We had a fix for requiring the ; in a PR that was merged and unmerged, I can't seem to find it though

rozenmd avatar Jul 15 '24 15:07 rozenmd

I got the same error with "wrangler": "3.57.1", but "wrangler": "3.80.5" fixed it.

gentamura avatar Oct 16 '24 12:10 gentamura

I got the same error with โ›…๏ธ wrangler 3.99.0, adding the ; fixed it.

Svenlaa avatar Dec 27 '24 11:12 Svenlaa