libsql-js icon indicating copy to clipboard operation
libsql-js copied to clipboard

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

Open penberg opened this issue 1 year ago • 0 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.

penberg avatar Feb 27 '24 09:02 penberg