Do some groundwork for supporting the better-sqlite3 driver
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
---
|
||||
'@backstage/backend-common': patch
|
||||
'@backstage/backend-tasks': patch
|
||||
'@backstage/plugin-app-backend': patch
|
||||
'@backstage/plugin-auth-backend': patch
|
||||
'@backstage/plugin-bazaar-backend': patch
|
||||
'@backstage/plugin-catalog-backend': patch
|
||||
'@backstage/plugin-scaffolder-backend': patch
|
||||
---
|
||||
|
||||
Do some groundwork for supporting the `better-sqlite3` driver, to maybe eventually replace `@vscode/sqlite3` (#9912)
|
||||
@@ -33,6 +33,7 @@ type DatabaseClient = 'pg' | 'sqlite3' | 'mysql' | 'mysql2' | string;
|
||||
*/
|
||||
const ConnectorMapping: Record<DatabaseClient, DatabaseConnector> = {
|
||||
pg: pgConnector,
|
||||
'better-sqlite3': sqlite3Connector,
|
||||
sqlite3: sqlite3Connector,
|
||||
mysql: mysqlConnector,
|
||||
mysql2: mysqlConnector,
|
||||
|
||||
@@ -13,12 +13,11 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import path from 'path';
|
||||
|
||||
import { ensureDirSync } from 'fs-extra';
|
||||
import knexFactory, { Knex } from 'knex';
|
||||
|
||||
import { Config } from '@backstage/config';
|
||||
import { ensureDirSync } from 'fs-extra';
|
||||
import knexFactory, { Knex } from 'knex';
|
||||
import path from 'path';
|
||||
import { mergeDatabaseConfig } from '../config';
|
||||
import { DatabaseConnector } from '../types';
|
||||
|
||||
|
||||
@@ -229,10 +229,9 @@ export class TaskWorker {
|
||||
// leaning on the database as a central clock source
|
||||
const dbNull = this.knex.raw('null');
|
||||
const dt = Duration.fromISO(recurringAtMostEveryDuration).as('seconds');
|
||||
const nextRun =
|
||||
this.knex.client.config.client === 'sqlite3'
|
||||
? this.knex.raw('datetime(next_run_start_at, ?)', [`+${dt} seconds`])
|
||||
: this.knex.raw(`next_run_start_at + interval '${dt} seconds'`);
|
||||
const nextRun = this.knex.client.config.client.includes('sqlite3')
|
||||
? this.knex.raw('datetime(next_run_start_at, ?)', [`+${dt} seconds`])
|
||||
: this.knex.raw(`next_run_start_at + interval '${dt} seconds'`);
|
||||
|
||||
const rows = await this.knex<DbTasksRow>(DB_TASKS_TABLE)
|
||||
.where('id', '=', this.taskId)
|
||||
|
||||
@@ -40,7 +40,7 @@ export function nowPlus(duration: Duration | undefined, knex: Knex) {
|
||||
if (!seconds) {
|
||||
return knex.fn.now();
|
||||
}
|
||||
return knex.client.config.client === 'sqlite3'
|
||||
return knex.client.config.client.includes('sqlite3')
|
||||
? knex.raw(`datetime('now', ?)`, [`${seconds} seconds`])
|
||||
: knex.raw(`now() + interval '${seconds} seconds'`);
|
||||
}
|
||||
|
||||
@@ -140,10 +140,9 @@ describe('StaticAssetsStore', () => {
|
||||
const updated = await database('static_assets_cache')
|
||||
.where({ path: 'old' })
|
||||
.update({
|
||||
last_modified_at:
|
||||
database.client.config.client === 'sqlite3'
|
||||
? database.raw(`datetime('now', '-3600 seconds')`)
|
||||
: database.raw(`now() + interval '-3600 seconds'`),
|
||||
last_modified_at: database.client.config.client.includes('sqlite3')
|
||||
? database.raw(`datetime('now', '-3600 seconds')`)
|
||||
: database.raw(`now() + interval '-3600 seconds'`),
|
||||
});
|
||||
expect(updated).toBe(1);
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ export class StaticAssetsStore implements StaticAssetProvider {
|
||||
.where(
|
||||
'last_modified_at',
|
||||
'<=',
|
||||
this.#db.client.config.client === 'sqlite3'
|
||||
this.#db.client.config.client.includes('sqlite3')
|
||||
? this.#db.raw(`datetime('now', ?)`, [`-${maxAgeSeconds} seconds`])
|
||||
: this.#db.raw(`now() + interval '${-maxAgeSeconds} seconds'`),
|
||||
)
|
||||
|
||||
@@ -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('signing_keys', table => {
|
||||
table
|
||||
.timestamp('created_at', { useTz: true, precision: 0 })
|
||||
@@ -38,7 +38,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('signing_keys', table => {
|
||||
table
|
||||
.timestamp('created_at', { useTz: false, precision: 0 })
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
exports.up = async function up(knex) {
|
||||
if (knex.client.config.client === 'sqlite3') {
|
||||
if (knex.client.config.client.includes('sqlite3')) {
|
||||
await knex.schema.dropTable('metadata');
|
||||
await knex.schema.createTable('metadata', table => {
|
||||
table.increments('id').comment('Automatically generated unique ID');
|
||||
@@ -93,7 +93,7 @@ exports.up = async function up(knex) {
|
||||
};
|
||||
|
||||
exports.down = async function down(knex) {
|
||||
if (knex.client.config.client === 'sqlite3') {
|
||||
if (knex.client.config.client.includes('sqlite3')) {
|
||||
await knex.schema.dropTable('metadata');
|
||||
await knex.schema.createTable('metadata', table => {
|
||||
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')) {
|
||||
// sqlite doesn't support dropPrimary so we recreate it properly instead
|
||||
await knex.schema.dropTable('entities_relations');
|
||||
await knex.schema.createTable('entities_relations', table => {
|
||||
@@ -58,7 +58,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.dropTable('entities_relations');
|
||||
await knex.schema.createTable('entities_relations', table => {
|
||||
table.comment('All relations between entities in the catalog');
|
||||
|
||||
@@ -403,10 +403,9 @@ export class DefaultProcessingDatabase implements ProcessingDatabase {
|
||||
items.map(i => i.entity_ref),
|
||||
)
|
||||
.update({
|
||||
next_update_at:
|
||||
tx.client.config.client === 'sqlite3'
|
||||
? tx.raw(`datetime('now', ?)`, [`${interval} seconds`])
|
||||
: tx.raw(`now() + interval '${interval} seconds'`),
|
||||
next_update_at: tx.client.config.client.includes('sqlite3')
|
||||
? tx.raw(`datetime('now', ?)`, [`${interval} seconds`])
|
||||
: tx.raw(`now() + interval '${interval} seconds'`),
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
@@ -182,7 +182,7 @@ export class DatabaseTaskStore implements TaskStore {
|
||||
.andWhere(
|
||||
'last_heartbeat_at',
|
||||
'<=',
|
||||
this.db.client.config.client === 'sqlite3'
|
||||
this.db.client.config.client.includes('sqlite3')
|
||||
? this.db.raw(`datetime('now', ?)`, [`-${timeoutS} seconds`])
|
||||
: this.db.raw(`dateadd('second', ?, ?)`, [
|
||||
`-${timeoutS}`,
|
||||
|
||||
Reference in New Issue
Block a user