tursodatabase / tursodatabase/libsql

ALTER COLUMN breaks if it contains a new line at the end

Open
#1,517 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
17.2k
Forks
531
Avg merge
1h 12m
Merged PRs (30d)
1

Description

Steps to reproduce

// Clean, :memory: DB using @libsql/client@^0.6.0

 await client.execute(`
        CREATE TABLE IF NOT EXISTS \`users\` (
            \`id\` text PRIMARY KEY NOT NULL
    );
      `);

  await client.execute(
    `
  CREATE TABLE IF NOT EXISTS \`test\` (
          \`id\` text PRIMARY KEY NOT NULL,
          \`user_id\` text
  );
  `,
  );

  await client.execute(
    `
    ALTER TABLE \`test\` ALTER COLUMN \`user_id\` TO \`user_id\` TEXT NOT NULL REFERENCES users(id);
    `,
  );

The last statement will throw this error:

Serialized Error: { code: 'SQLITE_ERROR', rawCode: 1 }
Caused by: SqliteError: CREATE TABLE `test` (
          `id` text PRIMARY KEY NOT NULL,
          `user_id` TEXT NOT NULL REFERENCES users(id);
    )
 ❯ convertError node_modules/.pnpm/libsql@0.3.18/node_modules/libsql/index.js:48:12
 ❯ Statement.run node_modules/.pnpm/libsql@0.3.18/node_modules/libsql/index.js:295:13
 ❯ executeStmt node_modules/.pnpm/@libsql+client@0.6.0/node_modules/@libsql/client/lib-esm/sqlite3.js:215:34
 ❯ Sqlite3Client.execute node_modules/.pnpm/@libsql+client@0.6.0/node_modules/@libsql/client/lib-esm/sqlite3.js:61:16

But it works if you remove the last newline:

 await client.execute(
    `
    ALTER TABLE \`test\` ALTER COLUMN \`user_id\` TO \`user_id\` TEXT NOT NULL REFERENCES users(id);`,
  );

My guess is that if you add the new line the query gets incorrectly parsed, keeping the semicolon when re-creating the table (as shown in the error)

Took me quite a bit to figure it out! So hopefully it's quick and easy fix 🙏

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the @libsql/client@^0.6.0 TypeScript example and the trailing-newline ALTER TABLE statement. Trace the SQLite/libSQL ALTER COLUMN table-recreation path that produces the CREATE TABLE text shown in the error. Done means the same statement succeeds without retaining the semicolon in the recreated table definition.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, sqlite, typescript
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.