Skip to content

Commit

Permalink
switch us over to better-sqlite3
Browse files Browse the repository at this point in the history
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
  • Loading branch information
freben committed Mar 11, 2022
1 parent 3f21eb4 commit efc73db
Show file tree
Hide file tree
Showing 39 changed files with 121 additions and 302 deletions.
13 changes: 13 additions & 0 deletions .changeset/heavy-kangaroos-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@backstage/backend-common': patch
'@backstage/backend-tasks': patch
'@backstage/backend-test-utils': patch
'@backstage/plugin-auth-backend': patch
'@backstage/plugin-bazaar-backend': patch
'@backstage/plugin-catalog-backend': patch
'@backstage/plugin-code-coverage-backend': patch
'@backstage/plugin-config-schema': patch
'@backstage/plugin-scaffolder-backend': patch
---

Use `better-sqlite3` instead of `@vscode/sqlite3`
24 changes: 24 additions & 0 deletions .changeset/warm-impalas-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'@backstage/create-app': patch
---

The main repo has switched from `@vscode/sqlite3` to `better-sqlite3` as its preferred SQLite installation. This decision was triggered by a number of issues with the former that arose because it needs build infrastructure in place and functional in order to be installed. The main drawback of this is that the new package uses the database client ID `better-sqlite3` instead of the plain `sqlite3`.

If you want to perform the same switch in your own repository,

- Replace all of your `package.json` dependencies on `@vscode/sqlite3` with the latest version of `better-sqlite3` instead

```diff
"dependencies": {
- "@vscode/sqlite3": "^5.0.7",
+ "better-sqlite3": "^7.5.0",
```

- In your app-config and tests, wherever you supply `client: 'sqlite3'`, instead supply `client: 'better-sqlite3`

```diff
backend:
database:
- client: sqlite3
+ client: better-sqlite3
```
2 changes: 1 addition & 1 deletion app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ backend:
listen:
port: 7007
database:
client: sqlite3
client: better-sqlite3
connection: ':memory:'
cache:
store: memory
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ from the previous steps.
```diff
backend:
database:
- client: sqlite3
- client: better-sqlite3
- connection: ':memory:'
+ # config options: https://node-postgres.com/api/client
+ client: pg
Expand Down
16 changes: 8 additions & 8 deletions docs/tutorials/configuring-plugin-databases.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ Backstage's databases.
### Dependencies

Please ensure the appropriate database drivers are installed in your `backend`
package. If you intend to use both `postgres` and `sqlite3`, you can install
package. If you intend to use both PostgreSQL and SQLite, you can install
both of them.

```sh
cd packages/backend

# install pg if you need postgres
# install pg if you need PostgreSQL
yarn add pg

# install sqlite3 if you intend to set it as the client
yarn add sqlite3
# install SQLite 3 if you intend to set it as the client
yarn add better-sqlite3
```

From an operational perspective, you only need to install drivers for clients
Expand All @@ -66,14 +66,14 @@ configurations below.

### Minimal In-Memory Configuration

In the example below, we are using `sqlite3` in-memory databases for all
In the example below, we are using `better-sqlite3` in-memory databases for all
plugins. You may want to use this configuration for testing or other non-durable
use cases.

```yaml
backend:
database:
client: sqlite3
client: better-sqlite3
connection: ':memory:'
```

Expand Down Expand Up @@ -138,7 +138,7 @@ backend:
### PostgreSQL and SQLite 3

The example below uses PostgreSQL (`pg`) as the database client for all plugins
except the `auth` plugin which uses `sqlite3`. As the `auth` plugin's client
except the `auth` plugin which uses `better-sqlite3`. As the `auth` plugin's client
type is different from the base client type, the connection configuration for
`auth` is used verbatim without extending the base configuration for PostgreSQL.

Expand All @@ -149,7 +149,7 @@ backend:
connection: 'postgresql://foo:bar@some.example-pg-instance.tld:5432'
plugin:
auth:
client: sqlite3
client: better-sqlite3
connection: ':memory:'
```

Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/switching-sqlite-postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ configuration for the backend:
```diff
backend:
database:
- client: sqlite3
- client: better-sqlite3
- connection: ':memory:'
+ # config options: https://node-postgres.com/api/client
+ client: pg
Expand Down
4 changes: 2 additions & 2 deletions packages/backend-common/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions packages/backend-common/src/database/DatabaseManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -176,7 +176,7 @@ describe('DatabaseManager', () => {
new ConfigReader({
backend: {
database: {
client: 'sqlite3',
client: 'better-sqlite3',
connection: ':memory:',
},
},
Expand All @@ -198,7 +198,7 @@ describe('DatabaseManager', () => {
new ConfigReader({
backend: {
database: {
client: 'sqlite3',
client: 'better-sqlite3',
connection: 'some-file-path',
},
},
Expand All @@ -215,7 +215,7 @@ describe('DatabaseManager', () => {
new ConfigReader({
backend: {
database: {
client: 'sqlite3',
client: 'better-sqlite3',
connection: {
directory: 'sqlite-files',
},
Expand All @@ -239,7 +239,7 @@ describe('DatabaseManager', () => {
new ConfigReader({
backend: {
database: {
client: 'sqlite3',
client: 'better-sqlite3',
connection: {
directory: 'sqlite-files',
},
Expand Down Expand Up @@ -270,7 +270,7 @@ describe('DatabaseManager', () => {
new ConfigReader({
backend: {
database: {
client: 'sqlite3',
client: 'better-sqlite3',
connection: {
directory: 'sqlite-files',
},
Expand Down Expand Up @@ -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,
});
});
Expand All @@ -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',
Expand All @@ -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:');
});
Expand Down Expand Up @@ -465,7 +465,7 @@ describe('DatabaseManager', () => {
new ConfigReader({
backend: {
database: {
client: 'sqlite3',
client: 'better-sqlite3',
pluginDivisionMode: 'schema',
connection: {
host: 'localhost',
Expand All @@ -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,
});

Expand Down
24 changes: 22 additions & 2 deletions packages/backend-common/src/database/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('config', () => {
expect(
mergeDatabaseConfig(
{
client: 'sqlite3',
client: 'better-sqlite3',
connection: ':memory:',
useNullAsDefault: true,
},
Expand All @@ -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',
},
Expand Down
10 changes: 6 additions & 4 deletions packages/backend-common/src/database/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('database connection', () => {
expect(
createDatabaseClient(
new ConfigReader({
client: 'sqlite3',
client: 'better-sqlite3',
connection: ':memory:',
}),
),
Expand Down Expand Up @@ -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',
);
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -218,7 +220,7 @@ describe('database connection', () => {
return expect(
ensureSchemaExists(
new ConfigReader({
client: 'sqlite3',
client: 'better-sqlite3',
schema: 'catalog',
connection: ':memory:',
}),
Expand Down

0 comments on commit efc73db

Please sign in to comment.