more prep work for better-sqlite3

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2022-03-08 12:02:02 +01:00
parent 257af4c80e
commit dc104b3f4a
17 changed files with 193 additions and 22 deletions
+1
View File
@@ -1,6 +1,7 @@
---
'@backstage/backend-common': patch
'@backstage/backend-tasks': patch
'@backstage/backend-test-utils': patch
'@backstage/plugin-app-backend': patch
'@backstage/plugin-auth-backend': patch
'@backstage/plugin-bazaar-backend': patch
+1
View File
@@ -106,6 +106,7 @@
"@types/unzipper": "^0.10.3",
"@types/webpack-env": "^1.15.2",
"aws-sdk-mock": "^5.2.1",
"better-sqlite3": "^7.5.0",
"http-errors": "^2.0.0",
"jest": "^26.0.1",
"mock-fs": "^5.1.0",
@@ -119,7 +119,7 @@ export class DatabaseManager {
private getDatabaseName(pluginId: string): string | undefined {
const connection = this.getConnectionConfig(pluginId);
if (this.getClientType(pluginId).client === 'sqlite3') {
if (this.getClientType(pluginId).client.includes('sqlite3')) {
const sqliteFilename: string | undefined = (
connection as Knex.Sqlite3ConnectionConfig
).filename;
@@ -224,7 +224,7 @@ export class DatabaseManager {
);
if (
client === 'sqlite3' &&
client.includes('sqlite3') &&
'filename' in baseConnection &&
baseConnection.filename !== ':memory:'
) {
@@ -23,7 +23,13 @@ import { DatabaseConnector } from './types';
import { mysqlConnector, pgConnector, sqlite3Connector } from './connectors';
type DatabaseClient = 'pg' | 'sqlite3' | 'mysql' | 'mysql2' | string;
type DatabaseClient =
| 'pg'
| 'better-sqlite3'
| 'sqlite3'
| 'mysql'
| 'mysql2'
| string;
/**
* Mapping of client type to supported database connectors
@@ -101,3 +101,84 @@ describe('sqlite3', () => {
});
});
});
describe('better-sqlite3', () => {
const createConfig = (connection: any) =>
new ConfigReader({ client: 'better-sqlite3', connection });
describe('buildSqliteDatabaseConfig', () => {
it('builds an in-memory connection', () => {
expect(buildSqliteDatabaseConfig(createConfig(':memory:'))).toEqual({
client: 'better-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: 'better-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: 'better-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: 'better-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: 'better-sqlite3',
connection: {
filename: path.join('path', 'to', 'foo'),
},
useNullAsDefault: true,
});
});
});
describe('createSqliteDatabaseClient', () => {
it('creates an in memory knex instance', () => {
expect(
createSqliteDatabaseClient(
createConfig({
client: 'better-sqlite3',
connection: ':memory:',
}),
),
).toBeTruthy();
});
});
});
@@ -182,6 +182,7 @@ export class TestDatabases {
return this.initPostgres(properties);
case 'mysql2':
return this.initMysql(properties);
case 'better-sqlite3':
case 'sqlite3':
return this.initSqlite(properties);
default:
@@ -240,13 +241,13 @@ export class TestDatabases {
}
private async initSqlite(
_properties: TestDatabaseProperties,
properties: TestDatabaseProperties,
): Promise<Instance> {
const databaseManager = DatabaseManager.fromConfig(
new ConfigReader({
backend: {
database: {
client: 'sqlite3',
client: properties.driver,
connection: ':memory:',
},
},
@@ -40,6 +40,7 @@ export type Instance = {
databaseManager: DatabaseManager;
connections: Array<Knex>;
};
export const allDatabases: Record<TestDatabaseId, TestDatabaseProperties> =
Object.freeze({
POSTGRES_13: {
@@ -21,7 +21,7 @@
*/
exports.up = async function up(knex) {
// SQLite does not support FK and PK
if (knex.client.config.client !== 'sqlite3') {
if (!knex.client.config.client.includes('sqlite3')) {
await knex.schema.alterTable('entities_search', table => {
table.dropForeign(['entity_id']);
});
@@ -130,7 +130,7 @@ exports.up = async function up(knex) {
*/
exports.down = async function down(knex) {
// SQLite does not support FK and PK
if (knex.client.config.client !== 'sqlite3') {
if (!knex.client.config.client.includes('sqlite3')) {
await knex.schema.alterTable('entities_search', table => {
table.dropForeign(['entity_id']);
});
@@ -21,7 +21,7 @@
*/
exports.up = async function up(knex) {
// Sqlite does not support alter column.
if (knex.client.config.client !== 'sqlite3') {
if (!knex.client.config.client.includes('sqlite3')) {
await knex.schema.alterTable('entities_search', table => {
table.text('value').nullable().alter({ alterType: true });
});
@@ -33,7 +33,7 @@ exports.up = async function up(knex) {
*/
exports.down = async function down(knex) {
// Sqlite does not support alter column.
if (knex.client.config.client !== 'sqlite3') {
if (!knex.client.config.client.includes('sqlite3')) {
await knex.schema.alterTable('entities_search', table => {
table.string('value').nullable().alter({ alterType: true });
});
@@ -31,7 +31,7 @@ exports.up = async function up(knex) {
});
// SQLite does not support alter column
if (knex.client.config.client !== 'sqlite3') {
if (!knex.client.config.client.includes('sqlite3')) {
await knex.schema.alterTable('entities', table => {
table.text('full_name').notNullable().alter({ alterNullable: true });
});
@@ -41,7 +41,7 @@ exports.up = async function up(knex) {
});
// SQLite does not support ALTER COLUMN.
if (knex.client.config.client !== 'sqlite3') {
if (!knex.client.config.client.includes('sqlite3')) {
await knex.schema.alterTable('entities', table => {
table.text('data').notNullable().alter({ alterNullable: true });
});
@@ -20,7 +20,7 @@
* @param {import('knex').Knex} knex
*/
exports.up = async function up(knex) {
if (knex.client.config.client !== 'sqlite3') {
if (!knex.client.config.client.includes('sqlite3')) {
await knex.schema.alterTable('entities_relations', table => {
table.index('originating_entity_id', 'originating_entity_id_idx');
});
@@ -34,7 +34,7 @@ exports.up = async function up(knex) {
* @param {import('knex').Knex} knex
*/
exports.down = async function down(knex) {
if (knex.client.config.client !== 'sqlite3') {
if (!knex.client.config.client.includes('sqlite3')) {
await knex.schema.alterTable('entities_relations', table => {
table.dropIndex([], 'originating_entity_id_idx');
});
@@ -20,7 +20,7 @@
* @param {import('knex').Knex} knex
*/
exports.up = async function up(knex) {
if (knex.client.config.client !== 'sqlite3') {
if (!knex.client.config.client.includes('sqlite3')) {
// We actually just want to widen columns, but can't do that while a
// view is dependent on them - so we just reconstruct it exactly as it was
await knex.schema
@@ -49,7 +49,7 @@ exports.up = async function up(knex) {
* @param {import('knex').Knex} knex
*/
exports.down = async function down(knex) {
if (knex.client.config.client !== 'sqlite3') {
if (!knex.client.config.client.includes('sqlite3')) {
await knex.schema
.raw('DROP VIEW location_update_log_latest;')
.alterTable('location_update_log', table => {
@@ -20,7 +20,7 @@
* @param {import('knex').Knex} knex
*/
exports.up = async function up(knex) {
if (knex.client.config.client !== 'sqlite3') {
if (!knex.client.config.client.includes('sqlite3')) {
await knex.schema.alterTable('entities', table => {
table.index('location_id', 'entity_location_id_idx');
});
@@ -34,7 +34,7 @@ exports.up = async function up(knex) {
* @param {import('knex').Knex} knex
*/
exports.down = async function down(knex) {
if (knex.client.config.client !== 'sqlite3') {
if (!knex.client.config.client.includes('sqlite3')) {
await knex.schema.alterTable('entities', table => {
table.dropIndex([], 'entity_location_id_idx');
});
@@ -588,7 +588,7 @@ export class DefaultProcessingDatabase implements ProcessingDatabase {
// We have to do this because the only way to detect if there was a conflict with
// SQLite is to catch the error, while Postgres needs to ignore the conflict to not
// break the ongoing transaction.
if (tx.client.config.client !== 'sqlite3') {
if (!tx.client.config.client.includes('sqlite3')) {
query = query.onConflict('entity_ref').ignore() as any; // type here does not match runtime
}
@@ -75,7 +75,7 @@ exports.up = async function up(knex) {
* @param {import('knex').Knex} knex
*/
exports.down = async function down(knex) {
if (knex.client.config.client !== 'sqlite3') {
if (!knex.client.config.client.includes('sqlite3')) {
await knex.schema.alterTable('task_events', table => {
table.dropIndex([], 'task_events_task_id_idx');
});
+83 -3
View File
@@ -8108,6 +8108,14 @@ better-path-resolve@1.0.0:
dependencies:
is-windows "^1.0.0"
better-sqlite3@^7.5.0:
version "7.5.0"
resolved "https://registry.npmjs.org/better-sqlite3/-/better-sqlite3-7.5.0.tgz#2a91cb616453f002096743b0e5b66a7021cd1c63"
integrity sha512-6FdG9DoytYGDhLW7VWW1vxjEz7xHkqK6LnaUQYA8d6GHNgZhu9PFX2xwKEEnSBRoT1J4PjTUPeg217ShxNmuPg==
dependencies:
bindings "^1.5.0"
prebuild-install "^7.0.0"
bfj@^7.0.2:
version "7.0.2"
resolved "https://registry.npmjs.org/bfj/-/bfj-7.0.2.tgz#1988ce76f3add9ac2913fd8ba47aad9e651bfbb2"
@@ -8168,6 +8176,13 @@ binaryextensions@^4.15.0, binaryextensions@^4.16.0:
resolved "https://registry.npmjs.org/binaryextensions/-/binaryextensions-4.18.0.tgz#22aeada2d14de062c60e8ca59a504a5636a76ceb"
integrity sha512-PQu3Kyv9dM4FnwB7XGj1+HucW+ShvJzJqjuw1JkKVs1mWdwOKVcRjOi+pV9X52A0tNvrPCsPkbFFQb+wE1EAXw==
bindings@^1.5.0:
version "1.5.0"
resolved "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df"
integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==
dependencies:
file-uri-to-path "1.0.0"
bintrees@1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.org/bintrees/-/bintrees-1.0.1.tgz#0e655c9b9c2435eaab68bf4027226d2b55a34524"
@@ -10760,6 +10775,11 @@ detect-libc@^1.0.3:
resolved "https://registry.npmjs.org/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b"
integrity sha1-+hN8S9aY7fVc1c0CrFWfkaTEups=
detect-libc@^2.0.0:
version "2.0.1"
resolved "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.1.tgz#e1897aa88fa6ad197862937fbc0441ef352ee0cd"
integrity sha512-463v3ZeIrcWtdgIg6vI6XUncguvr2TnGl4SzDXinkt9mSLpBJKXT3mW6xT3VQdDN11+WVs29pgvivTc4Lp8v+w==
detect-newline@^3.0.0:
version "3.1.0"
resolved "https://registry.npmjs.org/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651"
@@ -12124,6 +12144,11 @@ expand-brackets@^2.1.4:
snapdragon "^0.8.1"
to-regex "^3.0.1"
expand-template@^2.0.3:
version "2.0.3"
resolved "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz#6e14b3fcee0f3a6340ecb57d2e8918692052a47c"
integrity sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==
expect@^26.6.2:
version "26.6.2"
resolved "https://registry.npmjs.org/expect/-/expect-26.6.2.tgz#c6b996bf26bf3fe18b67b2d0f51fc981ba934417"
@@ -12481,6 +12506,11 @@ file-type@^9.0.0:
resolved "https://registry.npmjs.org/file-type/-/file-type-9.0.0.tgz#a68d5ad07f486414dfb2c8866f73161946714a18"
integrity sha512-Qe/5NJrgIOlwijpq3B7BEpzPFcgzggOTagZmkXQY4LA6bsXKTUstK7Wp12lEJ/mLKTpvIZxmIuRcLYWT6ov9lw==
file-uri-to-path@1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd"
integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==
filelist@^1.0.1:
version "1.0.2"
resolved "https://registry.npmjs.org/filelist/-/filelist-1.0.2.tgz#80202f21462d4d1c2e214119b1807c1bc0380e5b"
@@ -13143,6 +13173,11 @@ gitconfiglocal@^1.0.0:
dependencies:
ini "^1.3.2"
github-from-package@0.0.0:
version "0.0.0"
resolved "https://registry.npmjs.org/github-from-package/-/github-from-package-0.0.0.tgz#97fb5d96bfde8973313f20e8288ef9a167fa64ce"
integrity sha1-l/tdlr/eiXMxPyDoKI75oWf6ZM4=
glob-parent@^5.1.0, glob-parent@^5.1.1, glob-parent@~5.1.2:
version "5.1.2"
resolved "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4"
@@ -17986,6 +18021,11 @@ mkdirp-classic@^0.5.2:
resolved "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.2.tgz#54c441ce4c96cd7790e10b41a87aa51068ecab2b"
integrity sha512-ejdnDQcR75gwknmMw/tx02AuRs8jCtqFoFqDZMjiNxsu85sRIJVXDKHuLYvUUPRBUtV2FpSZa9bL1BUa3BdR2g==
mkdirp-classic@^0.5.3:
version "0.5.3"
resolved "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz#fa10c9115cc6d8865be221ba47ee9bed78601113"
integrity sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==
mkdirp-infer-owner@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/mkdirp-infer-owner/-/mkdirp-infer-owner-2.0.0.tgz#55d3b368e7d89065c38f32fd38e638f0ab61d316"
@@ -18227,6 +18267,11 @@ nanomatch@^1.2.9:
snapdragon "^0.8.1"
to-regex "^3.0.1"
napi-build-utils@^1.0.1:
version "1.0.2"
resolved "https://registry.npmjs.org/napi-build-utils/-/napi-build-utils-1.0.2.tgz#b1fddc0b2c46e380a0b7a76f984dd47c41a13806"
integrity sha512-ONmRUqK7zj7DWX0D9ADe03wbwOBZxNAfF20PlGfCWQcD3+/MakShIHrMqx9YwPTfxDdF1zLeL+RGZiR9kGMLdg==
natural-compare@^1.4.0:
version "1.4.0"
resolved "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
@@ -18281,6 +18326,13 @@ no-case@^3.0.4:
lower-case "^2.0.2"
tslib "^2.0.3"
node-abi@^3.3.0:
version "3.8.0"
resolved "https://registry.npmjs.org/node-abi/-/node-abi-3.8.0.tgz#679957dc8e7aa47b0a02589dbfde4f77b29ccb32"
integrity sha512-tzua9qWWi7iW4I42vUPKM+SfaF0vQSLAm4yO5J83mSwB7GeoWrDKC/K+8YCnYNwqP5duwazbw2X9l4m8SC2cUw==
dependencies:
semver "^7.3.5"
node-abort-controller@^3.0.1:
version "3.0.1"
resolved "https://registry.npmjs.org/node-abort-controller/-/node-abort-controller-3.0.1.tgz#f91fa50b1dee3f909afabb7e261b1e1d6b0cb74e"
@@ -18659,7 +18711,7 @@ npm-run-path@^4.0.0, npm-run-path@^4.0.1:
dependencies:
path-key "^3.0.0"
npmlog@^4.1.2:
npmlog@^4.0.1, npmlog@^4.1.2:
version "4.1.2"
resolved "https://registry.npmjs.org/npmlog/-/npmlog-4.1.2.tgz#08a7f2a8bf734604779a9efa4ad5cc717abb954b"
integrity sha512-2uUqazuKlTaSI/dC8AzicUck7+IrEaOnN/e0jd3Xtt1KcGpwx30v50mL7oPyr/h9bL3E4aZccVwpwP+5W9Vjkg==
@@ -20233,6 +20285,25 @@ postgres-interval@^1.1.0:
dependencies:
xtend "^4.0.0"
prebuild-install@^7.0.0:
version "7.0.1"
resolved "https://registry.npmjs.org/prebuild-install/-/prebuild-install-7.0.1.tgz#c10075727c318efe72412f333e0ef625beaf3870"
integrity sha512-QBSab31WqkyxpnMWQxubYAHR5S9B2+r81ucocew34Fkl98FhvKIF50jIJnNOBmAZfyNV7vE5T6gd3hTVWgY6tg==
dependencies:
detect-libc "^2.0.0"
expand-template "^2.0.3"
github-from-package "0.0.0"
minimist "^1.2.3"
mkdirp-classic "^0.5.3"
napi-build-utils "^1.0.1"
node-abi "^3.3.0"
npmlog "^4.0.1"
pump "^3.0.0"
rc "^1.2.7"
simple-get "^4.0.0"
tar-fs "^2.0.0"
tunnel-agent "^0.6.0"
precond@0.2:
version "0.2.3"
resolved "https://registry.npmjs.org/precond/-/precond-0.2.3.tgz#aa9591bcaa24923f1e0f4849d240f47efc1075ac"
@@ -20738,7 +20809,7 @@ rc-util@^5.16.1:
react-is "^16.12.0"
shallowequal "^1.1.0"
rc@^1.2.8:
rc@^1.2.7, rc@^1.2.8:
version "1.2.8"
resolved "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed"
integrity sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==
@@ -22517,6 +22588,15 @@ simple-get@^3.0.2, simple-get@^3.0.3:
once "^1.3.1"
simple-concat "^1.0.0"
simple-get@^4.0.0:
version "4.0.1"
resolved "https://registry.npmjs.org/simple-get/-/simple-get-4.0.1.tgz#4a39db549287c979d352112fa03fd99fd6bc3543"
integrity sha512-brv7p5WgH0jmQJr1ZDDfKDOSeWWg+OVypG99A/5vYGPqJ6pxiaHLy8nxtFjBA7oMa01ebA9gfh1uMCFqOuXxvA==
dependencies:
decompress-response "^6.0.0"
once "^1.3.1"
simple-concat "^1.0.0"
simple-swizzle@^0.2.2:
version "0.2.2"
resolved "https://registry.npmjs.org/simple-swizzle/-/simple-swizzle-0.2.2.tgz#a4da6b635ffcccca33f70d17cb92592de95e557a"
@@ -23642,7 +23722,7 @@ tapable@^2.0.0, tapable@^2.1.1, tapable@^2.2.0, tapable@^2.2.1:
resolved "https://registry.npmjs.org/tapable/-/tapable-2.2.1.tgz#1967a73ef4060a82f12ab96af86d52fdb76eeca0"
integrity sha512-GNzQvQTOIP6RyTfE2Qxb8ZVlNmw0n88vp1szwWRimP02mnTsx3Wtn5qRdqY9w2XduFNUgvOwhNnQsjwCp+kqaQ==
tar-fs@2.1.1, tar-fs@^2.1.1:
tar-fs@2.1.1, tar-fs@^2.0.0, tar-fs@^2.1.1:
version "2.1.1"
resolved "https://registry.npmjs.org/tar-fs/-/tar-fs-2.1.1.tgz#489a15ab85f1f0befabb370b7de4f9eb5cbe8784"
integrity sha512-V0r2Y9scmbDRLCNex/+hYzvp/zyYjvFbHPNgVTKfQvVrb6guiE/fxP+XblDNR011utopbkex2nM4dHNV6GDsng==