tursodatabase / libsql-js

A better-sqlite3 compatible API for libSQL that supports Bun, Deno, and Node

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Disk image is malformed on `sync()` with fresh database

penberg opened this issue · comments

With the following knex dialect that runs db.sync() when a connection is opened:

const BaseClient = require("knex/lib/dialects/better-sqlite3");

class Client_Libsql extends BaseClient {
    static dialect = "libsql";

    _driver() {
        return require("libsql");
    }

    async acquireRawConnection() {
      const options = this.connectionSettings.options || {};

      console.log("Connecting with Turso")

      const db = new this.driver(this.connectionSettings.filename, options);
      db.sync();

      return db;
    }
}

Object.assign(Client_Libsql.prototype, {
    dialect: "libsql",
    driverName: "libsql",
});

module.exports = Client_Libsql;

An user reports that they get the following error when no database exists:

/tmp/testing/node_modules/libsql/index.js:90
    databaseSyncSync.call(this.db);
                     ^

Error: replication error: Injector error: SQLite error: database disk image is malformed
    at Database.sync (/tmp/testing/node_modules/libsql/index.js:90:22)
    at Client_Libsql.acquireRawConnection (/tmp/testing/src/db/turso-knex-client.js:17:10)
    at create (/tmp/testing/node_modules/knex/lib/client.js:262:39) {
  code: ''
}

But if they run the app again after the failure, it starts working fine.

@LucioFranco which commit fixed this?

tursodatabase/libsql#1217 this PR fixed this issue