switch us over to better-sqlite3

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2022-03-11 14:01:35 +01:00
parent 3f21eb4d13
commit efc73db10c
39 changed files with 121 additions and 302 deletions
+2 -2
View File
@@ -70,7 +70,7 @@ export interface Config {
/** Database connection configuration, select base database type using the `client` field */
database: {
/** Default database client to use */
client: 'sqlite3' | 'pg';
client: 'better-sqlite3' | 'sqlite3' | 'pg';
/**
* Base database connection string or Knex object
* @secret
@@ -106,7 +106,7 @@ export interface Config {
plugin?: {
[pluginId: string]: {
/** Database client override */
client?: 'sqlite3' | 'pg';
client?: 'better-sqlite3' | 'sqlite3' | 'pg';
/**
* Database connection string or Knex object override
* @secret
@@ -97,13 +97,13 @@ describe('DatabaseManager', () => {
},
},
differentclient: {
client: 'sqlite3',
client: 'better-sqlite3',
connection: {
filename: 'plugin_with_different_client',
},
},
differentclientconnstring: {
client: 'sqlite3',
client: 'better-sqlite3',
connection: ':memory:',
},
stringoverride: {
@@ -176,7 +176,7 @@ describe('DatabaseManager', () => {
new ConfigReader({
backend: {
database: {
client: 'sqlite3',
client: 'better-sqlite3',
connection: ':memory:',
},
},
@@ -198,7 +198,7 @@ describe('DatabaseManager', () => {
new ConfigReader({
backend: {
database: {
client: 'sqlite3',
client: 'better-sqlite3',
connection: 'some-file-path',
},
},
@@ -215,7 +215,7 @@ describe('DatabaseManager', () => {
new ConfigReader({
backend: {
database: {
client: 'sqlite3',
client: 'better-sqlite3',
connection: {
directory: 'sqlite-files',
},
@@ -239,7 +239,7 @@ describe('DatabaseManager', () => {
new ConfigReader({
backend: {
database: {
client: 'sqlite3',
client: 'better-sqlite3',
connection: {
directory: 'sqlite-files',
},
@@ -270,7 +270,7 @@ describe('DatabaseManager', () => {
new ConfigReader({
backend: {
database: {
client: 'sqlite3',
client: 'better-sqlite3',
connection: {
directory: 'sqlite-files',
},
@@ -349,7 +349,7 @@ describe('DatabaseManager', () => {
// plugin connection should be used as base config, client is different
expect(baseConfig.get()).toMatchObject({
client: 'sqlite3',
client: 'better-sqlite3',
connection: config.backend.database.plugin[pluginId].connection,
});
});
@@ -361,10 +361,10 @@ describe('DatabaseManager', () => {
const mockCalls = mocked(createDatabaseClient).mock.calls.splice(-1);
const [baseConfig, overrides] = mockCalls[0];
// plugin client should be sqlite3
expect(baseConfig.get().client).toEqual('sqlite3');
// plugin client should be better-sqlite3
expect(baseConfig.get().client).toEqual('better-sqlite3');
// sqlite3 uses 'filename' instead of 'database'
// SQLite uses 'filename' instead of 'database'
expect(overrides).toHaveProperty(
'connection.filename',
'plugin_with_different_client',
@@ -378,7 +378,7 @@ describe('DatabaseManager', () => {
const mockCalls = mocked(createDatabaseClient).mock.calls.splice(-1);
const [baseConfig, overrides] = mockCalls[0];
expect(baseConfig.get().client).toEqual('sqlite3');
expect(baseConfig.get().client).toEqual('better-sqlite3');
expect(overrides).toHaveProperty('connection.filename', ':memory:');
});
@@ -465,7 +465,7 @@ describe('DatabaseManager', () => {
new ConfigReader({
backend: {
database: {
client: 'sqlite3',
client: 'better-sqlite3',
pluginDivisionMode: 'schema',
connection: {
host: 'localhost',
@@ -484,7 +484,7 @@ describe('DatabaseManager', () => {
const [baseConfig, overrides] = mockCalls[0];
expect(baseConfig.get()).toMatchObject({
client: 'sqlite3',
client: 'better-sqlite3',
connection: config.backend.database.connection,
});
@@ -101,7 +101,7 @@ describe('config', () => {
expect(
mergeDatabaseConfig(
{
client: 'sqlite3',
client: 'better-sqlite3',
connection: ':memory:',
useNullAsDefault: true,
},
@@ -112,7 +112,27 @@ describe('config', () => {
},
),
).toEqual({
client: 'sqlite3',
client: 'better-sqlite3',
connection: {
filename: '/path/to/file',
},
useNullAsDefault: true,
});
expect(
mergeDatabaseConfig(
{
client: 'better-sqlite3',
connection: ':memory:',
useNullAsDefault: true,
},
{
connection: {
filename: '/path/to/file',
},
},
),
).toEqual({
client: 'better-sqlite3',
connection: {
filename: '/path/to/file',
},
@@ -59,7 +59,7 @@ describe('database connection', () => {
expect(
createDatabaseClient(
new ConfigReader({
client: 'sqlite3',
client: 'better-sqlite3',
connection: ':memory:',
}),
),
@@ -133,7 +133,7 @@ describe('database connection', () => {
});
it('returns Knex config for sqlite', () => {
expect(createNameOverride('sqlite3', 'testsqlite')).toHaveProperty(
expect(createNameOverride('better-sqlite3', 'testsqlite')).toHaveProperty(
'connection.filename',
'testsqlite',
);
@@ -178,7 +178,9 @@ describe('database connection', () => {
});
it('throws error for sqlite', () => {
expect(createSchemaOverride('sqlite3', 'testsqlite')).toBeUndefined();
expect(
createSchemaOverride('better-sqlite3', 'testsqlite'),
).toBeUndefined();
});
it('returns Knex config for mysql', () => {
@@ -218,7 +220,7 @@ describe('database connection', () => {
return expect(
ensureSchemaExists(
new ConfigReader({
client: 'sqlite3',
client: 'better-sqlite3',
schema: 'catalog',
connection: ':memory:',
}),
@@ -21,87 +21,6 @@ import {
createSqliteDatabaseClient,
} from './sqlite3';
describe('sqlite3', () => {
const createConfig = (connection: any) =>
new ConfigReader({ client: 'sqlite3', connection });
describe('buildSqliteDatabaseConfig', () => {
it('builds an in-memory connection', () => {
expect(buildSqliteDatabaseConfig(createConfig(':memory:'))).toEqual({
client: 'sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true,
});
});
it('builds an in-memory connection by override with filename', () => {
expect(
buildSqliteDatabaseConfig(
createConfig(path.join('path', 'to', 'foo')),
{ connection: ':memory:' },
),
).toEqual({
client: 'sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true,
});
});
it('builds a persistent connection, normalize config with filename', () => {
expect(
buildSqliteDatabaseConfig(createConfig(path.join('path', 'to', 'foo'))),
).toEqual({
client: 'sqlite3',
connection: { filename: path.join('path', 'to', 'foo') },
useNullAsDefault: true,
});
});
it('builds a persistent connection', () => {
expect(
buildSqliteDatabaseConfig(
createConfig({
filename: path.join('path', 'to', 'foo'),
}),
),
).toEqual({
client: 'sqlite3',
connection: {
filename: path.join('path', 'to', 'foo'),
},
useNullAsDefault: true,
});
});
it('replaces the connection with an override', () => {
expect(
buildSqliteDatabaseConfig(createConfig(':memory:'), {
connection: { filename: path.join('path', 'to', 'foo') },
}),
).toEqual({
client: 'sqlite3',
connection: {
filename: path.join('path', 'to', 'foo'),
},
useNullAsDefault: true,
});
});
});
describe('createSqliteDatabaseClient', () => {
it('creates an in memory knex instance', () => {
expect(
createSqliteDatabaseClient(
createConfig({
client: 'sqlite3',
connection: ':memory:',
}),
),
).toBeTruthy();
});
});
});
describe('better-sqlite3', () => {
const createConfig = (connection: any) =>
new ConfigReader({ client: 'better-sqlite3', connection });
@@ -171,14 +90,7 @@ describe('better-sqlite3', () => {
describe('createSqliteDatabaseClient', () => {
it('creates an in memory knex instance', () => {
expect(
createSqliteDatabaseClient(
createConfig({
client: 'better-sqlite3',
connection: ':memory:',
}),
),
).toBeTruthy();
expect(createSqliteDatabaseClient(createConfig(':memory:'))).toBeTruthy();
});
});
});
@@ -28,6 +28,7 @@ export function isDatabaseConflictError(e: unknown) {
return (
typeof message === 'string' &&
(/SQLITE_CONSTRAINT(?:_UNIQUE)?: UNIQUE/.test(message) ||
/UNIQUE constraint failed:/.test(message) ||
/unique constraint/.test(message))
);
}
@@ -56,7 +56,7 @@ export class PluginTaskSchedulerJanitor {
// SQLite currently (Oct 1 2021) returns a number for returning()
// statements, effectively ignoring them and instead returning the outcome
// of the delete() - and knex also emits a warning about that fact, which
// is why we avoid that entirely for the sqlite3 driver.
// is why we avoid that entirely for the sqlite3 family of drivers.
// https://github.com/knex/knex/issues/4370
// https://github.com/mapbox/node-sqlite3/issues/1453
-47
View File
@@ -1,47 +0,0 @@
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
version: v1.22.1
# ignores vulnerabilities until expiry date; change duration by modifying expiry date
ignore:
SNYK-JS-TAR-1579155:
- 'sqlite3 > node-gyp > tar':
reason: >-
The only usage is via node-gyp; there is no unpacking of untrusted tar
files
expires: 2022-11-11T14:30:05.581Z
created: 2021-11-11T14:30:05.582Z
SNYK-JS-TAR-1579152:
- 'sqlite3 > node-gyp > tar':
reason: >-
The only usage is via node-gyp; there is no unpacking of untrusted tar
files
expires: 2022-11-11T14:30:05.581Z
created: 2021-11-11T14:30:05.582Z
SNYK-JS-TAR-1579147:
- 'sqlite3 > node-gyp > tar':
reason: >-
The only usage is via node-gyp; there is no unpacking of untrusted tar
files
expires: 2022-11-11T14:30:05.581Z
created: 2021-11-11T14:30:05.582Z
SNYK-JS-TAR-1536758:
- 'sqlite3 > node-gyp > tar':
reason: >-
The only usage is via node-gyp; there is no unpacking of untrusted tar
files
expires: 2022-11-11T14:30:05.581Z
created: 2021-11-11T14:30:05.582Z
SNYK-JS-TAR-1536531:
- 'sqlite3 > node-gyp > tar':
reason: >-
The only usage is via node-gyp; there is no unpacking of untrusted tar
files
expires: 2022-11-11T14:30:05.581Z
created: 2021-11-11T14:30:05.582Z
SNYK-JS-TAR-1536528:
- 'sqlite3 > node-gyp > tar':
reason: >-
The only usage is via node-gyp; there is no unpacking of untrusted tar
files
expires: 2022-11-11T14:30:05.581Z
created: 2021-11-11T14:30:05.582Z
patch: {}
+1 -1
View File
@@ -37,7 +37,7 @@
"@backstage/backend-common": "^0.13.0",
"@backstage/cli": "^0.15.2",
"@backstage/config": "^0.1.15",
"@vscode/sqlite3": "^5.0.7",
"better-sqlite3": "^7.5.0",
"knex": "^1.0.2",
"msw": "^0.35.0",
"mysql2": "^2.2.5",
@@ -66,6 +66,6 @@ export const allDatabases: Record<TestDatabaseId, TestDatabaseProperties> =
},
SQLITE_3: {
name: 'SQLite 3.x',
driver: 'sqlite3',
driver: 'better-sqlite3',
},
});
-47
View File
@@ -1,47 +0,0 @@
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
version: v1.22.1
# ignores vulnerabilities until expiry date; change duration by modifying expiry date
ignore:
SNYK-JS-TAR-1579155:
- 'sqlite3 > node-gyp > tar':
reason: >-
The only usage is via node-gyp; there is no unpacking of untrusted tar
files
expires: 2022-11-11T14:30:05.581Z
created: 2021-11-11T14:30:05.582Z
SNYK-JS-TAR-1579152:
- 'sqlite3 > node-gyp > tar':
reason: >-
The only usage is via node-gyp; there is no unpacking of untrusted tar
files
expires: 2022-11-11T14:30:05.581Z
created: 2021-11-11T14:30:05.582Z
SNYK-JS-TAR-1579147:
- 'sqlite3 > node-gyp > tar':
reason: >-
The only usage is via node-gyp; there is no unpacking of untrusted tar
files
expires: 2022-11-11T14:30:05.581Z
created: 2021-11-11T14:30:05.582Z
SNYK-JS-TAR-1536758:
- 'sqlite3 > node-gyp > tar':
reason: >-
The only usage is via node-gyp; there is no unpacking of untrusted tar
files
expires: 2022-11-11T14:30:05.581Z
created: 2021-11-11T14:30:05.582Z
SNYK-JS-TAR-1536531:
- 'sqlite3 > node-gyp > tar':
reason: >-
The only usage is via node-gyp; there is no unpacking of untrusted tar
files
expires: 2022-11-11T14:30:05.581Z
created: 2021-11-11T14:30:05.582Z
SNYK-JS-TAR-1536528:
- 'sqlite3 > node-gyp > tar':
reason: >-
The only usage is via node-gyp; there is no unpacking of untrusted tar
files
expires: 2022-11-11T14:30:05.581Z
created: 2021-11-11T14:30:05.582Z
patch: {}
+1 -1
View File
@@ -16,7 +16,7 @@
module.exports = {
development: {
client: 'sqlite3',
client: 'better-sqlite3',
connection: {
filename: './dev.sqlite3',
},
+1 -1
View File
@@ -62,7 +62,7 @@
"@backstage/plugin-todo-backend": "^0.1.26",
"@gitbeaker/node": "^35.1.0",
"@octokit/rest": "^18.5.3",
"@vscode/sqlite3": "^5.0.7",
"better-sqlite3": "^7.5.0",
"azure-devops-node-api": "^11.0.1",
"dockerode": "^3.3.1",
"example-app": "link:../app",
@@ -25,7 +25,7 @@ backend:
credentials: true
{{#if dbTypeSqlite}}
database:
client: sqlite3
client: better-sqlite3
connection: ':memory:'
{{/if}}
{{#if dbTypePG}}
@@ -46,7 +46,7 @@
"pg": "^8.3.0",
{{/if}}
{{#if dbTypeSqlite}}
"@vscode/sqlite3": "^5.0.7",
"better-sqlite3": "^7.5.0",
{{/if}}
"winston": "^3.2.1"
},