0

I recently changed some code I have using SQLite via better-sqlite3 to check if a table exists before running certain import queries.

The code and tests all run fine. But the tests don't pass on our build machines or on another developers machine. The tests are using an in-memory database, the problem hasn't occurred on any non-memory databases though it has only been ran on a half-dozen machines. There are tables in the database and code is able to read/write to them.

It turns out that the query I was using to check what tables I can import from was return an empty result. In fact, if I just query anything at all from sql_master in these cases I get an empty result.

  // db is a database from better-sqlite3
    private async getAllTables() {
        var query = connection.db.prepare(`SELECT name, type FROM sqlite_master`).raw();
        return query.all();
    }

    var allTables = await this.getAllTables();
    console.log(`all things in sql_master: ${await allTables.join(", ")}.`);

The console has the following logged on the problematic matchines:

All things in sql_master: .

Locally it prints a list of all the things in sql_master as you'd expected. All the other queries my test code is doing is working as expected (reading and writing to tables besides sql_master).

The database was created like:

db = new Connection(":memory:", { memory: true, fileMustExist: true });
Frank Schwieterman
  • 24,142
  • 15
  • 92
  • 130
  • When an in-memory database is created it will not have sql_master rows unless or until an object (table) is created. – DinoCoderSaurus Apr 02 '19 at 16:45
  • Ok. I'm creating tables though, and the code is reading/writing from them so I knoew they are there. Thank you, I'll update the question. – Frank Schwieterman Apr 03 '19 at 01:31
  • One scenario that would explain this behaviour: a new database connection is instantiated between the table creates and the sqlite_master SELECT. Perhaps one (or a combo) of these issues on the better-sqlite3 repo is in play [#87](https://github.com/JoshuaWise/better-sqlite3/issues/87),[#149](https://github.com/JoshuaWise/better-sqlite3/issues/149), [#208](https://github.com/JoshuaWise/better-sqlite3/issues/208) – DinoCoderSaurus Apr 03 '19 at 12:29

0 Answers0