WiseLibs / better-sqlite3

The fastest and simplest library for SQLite3 in Node.js.
MIT License
5.48k stars 396 forks source link

Missing error code: UNKNOWN_SQLITE_ERROR_257 #1271

Open Meztlicoatl opened 2 weeks ago

Meztlicoatl commented 2 weeks ago

I'm using a SQLite extension, particularly ICU support: I loaded it in the database creation and did normal things. Then, I tried to fill up that database with a separate script and connection, running a different ts-node instance. I forgot to update this script to also load this particular extension with the required connection, so I guess that was the cause. Also, the error code description matches my guess: https://www.sqlite.org/rescode.html#error_missing_collseq

The error stack:

/path/to/project/backend/node_modules/better-sqlite3/lib/methods/wrappers.js:5
        return this[cppdb].prepare(sql, this, false);
                    ^
SqliteError: no such collation sequence: SPANISH
    at Database.prepare (/path/to/project/backend/node_modules/better-sqlite3/lib/methods/wrappers.js:5:21)
    at Object.<anonymous> (/path/to/project/backend/source/database/development-setup.ts:9:12)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Module.m._compile (/path/to/project/backend/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
    at Object.require.extensions.<computed> [as .ts] (/path/to/project/backend/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1317:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1127:12)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24) {
  code: 'UNKNOWN_SQLITE_ERROR_257'
}

And the other main script where I loaded the extension:

const connection = new Database(serverRootPath('database/website.sqlite'))
connection.pragma('journal_mode = WAL')
const sqlite_version = (connection.prepare('SELECT sqlite_version() as version').get() as any).version
connection.loadExtension(assetPath(`database/libSQLiteICU-${sqlite_version}.so`))
connection.prepare('SELECT icu_load_collation(\'es_AR\', \'SPANISH\')').run()
// ...
connection.close()
Prinzhorn commented 2 weeks ago

Do you have a question or just wanted to share this? As you said, you tried to access the database using a different process that didn't have the necessary extension to work with the database file.

Meztlicoatl commented 2 weeks ago

I posted this issue as recommended here in the API docs: image

Prinzhorn commented 2 weeks ago

Sorry, I did not realize that. It was not clear from your message, you should re-open the issue then as it has not been resolved.

I think the error needs to be added to https://github.com/WiseLibs/better-sqlite3/blob/f01e0e42da4b8ba509961e772132e69ef5aa11c0/src/util/constants.lzz#L116 if you're interested in creating a PR.

Meztlicoatl commented 2 weeks ago

Sorry for not being clear. First time doing this :D. Fixed as requested and PR done #1273.