Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: experimental support for using embedded-postgres as dev DB #28318

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thin-elephants-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/cli': patch
---

The `package start` command now supports an experimental `EXPERIMENTAL_DEV_DB` env flag that can be set to enable the use of `embedded-postgres` as the database for local development, rather than SQLite. For this to work the desired version of the `embedded-postgres` package must be installed in your project, typically as a `devDependency`.
7 changes: 7 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"cross-spawn": "^7.0.3",
"css-loader": "^6.5.1",
"ctrlc-windows": "^2.1.0",
"embedded-postgres": "^17.2.0-beta.15",
"esbuild": "^0.24.0",
"esbuild-loader": "^4.0.0",
"eslint": "^8.6.0",
Expand Down Expand Up @@ -128,6 +129,7 @@
"p-limit": "^3.1.0",
"p-queue": "^6.6.2",
"pirates": "^4.0.6",
"portfinder": "^1.0.32",
"postcss": "^8.1.0",
"process": "^0.11.10",
"raw-loader": "^4.0.2",
Expand Down Expand Up @@ -194,6 +196,7 @@
"@types/yarnpkg__lockfile": "^1.1.4",
"@vitejs/plugin-react": "^4.3.1",
"del": "^8.0.0",
"embedded-postgres": "17.2.0-beta.15",
"msw": "^1.0.0",
"nodemon": "^3.0.1",
"vite": "^5.0.0",
Expand All @@ -206,6 +209,7 @@
"@rspack/dev-server": "^1.0.9",
"@rspack/plugin-react-refresh": "^1.0.0",
"@vitejs/plugin-react": "^4.0.4",
"embedded-postgres": "17.2.0-beta.15",
"vite": "^5.0.0",
"vite-plugin-html": "^3.2.0",
"vite-plugin-node-polyfills": "^0.22.0"
Expand All @@ -226,6 +230,9 @@
"@vitejs/plugin-react": {
"optional": true
},
"embedded-postgres": {
"optional": true
},
"vite": {
"optional": true
},
Expand Down
12 changes: 12 additions & 0 deletions packages/cli/src/lib/runner/runBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { fileURLToPath } from 'url';
import { isAbsolute as isAbsolutePath } from 'path';
import { paths } from '../paths';
import spawn from 'cross-spawn';
import { startEmbeddedDb } from './startEmbeddedDb';

const loaderArgs = [
'--enable-source-maps',
Expand Down Expand Up @@ -54,6 +55,16 @@ export async function runBackend(options: RunBackendOptions) {
const server = new IpcServer();
ServerDataStore.bind(server);

const extraEnv: Record<string, string> = {};

if (process.env.EXPERIMENTAL_DEV_DB) {
const db = await startEmbeddedDb();
extraEnv.APP_CONFIG_backend_database = JSON.stringify({
client: 'pg',
connection: db.connection,
});
}

let exiting = false;
let firstStart = true;
let child: ChildProcess | undefined;
Expand Down Expand Up @@ -121,6 +132,7 @@ export async function runBackend(options: RunBackendOptions) {
stdio: ['ignore', 'inherit', 'inherit', 'ipc'],
env: {
...process.env,
...extraEnv,
BACKSTAGE_CLI_LINKED_WORKSPACE: options.linkedWorkspace,
BACKSTAGE_CLI_CHANNEL: '1',
ESBK_TSCONFIG_PATH: paths.resolveTargetRoot('tsconfig.json'),
Expand Down
67 changes: 67 additions & 0 deletions packages/cli/src/lib/runner/startEmbeddedDb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import os from 'node:os';
import fs from 'fs-extra';
import { resolve as resolvePath } from 'path';
import { getPortPromise } from 'portfinder';

export async function startEmbeddedDb() {
const { default: EmbeddedPostgres } = await import('embedded-postgres').catch(
error => {
throw new Error(
`Failed to load peer dependency 'embedded-postgres' for generating SQL reports. ` +
`It must be installed as an explicit dependency in your project. Caused by; ${error}`,
);
},
);

const host = 'localhost';
const user = 'postgres';
const password = 'password';
const port = await getPortPromise();
const tmpDir = await fs.mkdtemp(
resolvePath(os.tmpdir(), 'backstage-dev-db-'),
);
const pg = new EmbeddedPostgres({
databaseDir: tmpDir,
user,
password,
port,
persistent: false,
onError(_messageOrError) {},
onLog(_message) {},
});

// Create the cluster config files
await pg.initialise();

// Start the server
await pg.start();

return {
connection: {
host,
user,
password,
port,
},
async close() {
await pg.stop();
await fs.rmdir(tmpDir, { recursive: true, maxRetries: 3 });
},
};
}
5 changes: 5 additions & 0 deletions packages/cli/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,8 @@ declare module 'webpack-node-externals' {
}

declare module '@esbuild-kit/cjs-loader' {}

// It's missing a types entry point, but has types in dist
declare module 'embedded-postgres' {
export { default } from 'embedded-postgres/dist/index';
}
98 changes: 97 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3987,6 +3987,7 @@ __metadata:
css-loader: ^6.5.1
ctrlc-windows: ^2.1.0
del: ^8.0.0
embedded-postgres: 17.2.0-beta.15
esbuild: ^0.24.0
esbuild-loader: ^4.0.0
eslint: ^8.6.0
Expand Down Expand Up @@ -4027,6 +4028,7 @@ __metadata:
p-limit: ^3.1.0
p-queue: ^6.6.2
pirates: ^4.0.6
portfinder: ^1.0.32
postcss: ^8.1.0
process: ^0.11.10
raw-loader: ^4.0.2
Expand Down Expand Up @@ -4063,6 +4065,7 @@ __metadata:
"@rspack/dev-server": ^1.0.9
"@rspack/plugin-react-refresh": ^1.0.0
"@vitejs/plugin-react": ^4.0.4
embedded-postgres: 17.2.0-beta.15
vite: ^5.0.0
vite-plugin-html: ^3.2.0
vite-plugin-node-polyfills: ^0.22.0
Expand All @@ -4077,6 +4080,8 @@ __metadata:
optional: true
"@vitejs/plugin-react":
optional: true
embedded-postgres:
optional: true
vite:
optional: true
vite-plugin-html:
Expand Down Expand Up @@ -8990,6 +8995,62 @@ __metadata:
languageName: node
linkType: hard

"@embedded-postgres/darwin-arm64@npm:^17.2.0-beta.15":
version: 17.2.0-beta.15
resolution: "@embedded-postgres/darwin-arm64@npm:17.2.0-beta.15"
conditions: os=darwin & cpu=arm64
languageName: node
linkType: hard

"@embedded-postgres/darwin-x64@npm:^17.2.0-beta.15":
version: 17.2.0-beta.15
resolution: "@embedded-postgres/darwin-x64@npm:17.2.0-beta.15"
conditions: os=darwin & cpu=x64
languageName: node
linkType: hard

"@embedded-postgres/linux-arm64@npm:^17.2.0-beta.15":
version: 17.2.0-beta.15
resolution: "@embedded-postgres/linux-arm64@npm:17.2.0-beta.15"
conditions: os=linux & cpu=arm64
languageName: node
linkType: hard

"@embedded-postgres/linux-arm@npm:^17.2.0-beta.15":
version: 17.2.0-beta.15
resolution: "@embedded-postgres/linux-arm@npm:17.2.0-beta.15"
conditions: os=linux & cpu=arm
languageName: node
linkType: hard

"@embedded-postgres/linux-ia32@npm:^17.2.0-beta.15":
version: 17.2.0-beta.15
resolution: "@embedded-postgres/linux-ia32@npm:17.2.0-beta.15"
conditions: os=linux & cpu=ia32
languageName: node
linkType: hard

"@embedded-postgres/linux-ppc64@npm:^17.2.0-beta.15":
version: 17.2.0-beta.15
resolution: "@embedded-postgres/linux-ppc64@npm:17.2.0-beta.15"
conditions: os=linux & cpu=ppc64
languageName: node
linkType: hard

"@embedded-postgres/linux-x64@npm:^17.2.0-beta.15":
version: 17.2.0-beta.15
resolution: "@embedded-postgres/linux-x64@npm:17.2.0-beta.15"
conditions: os=linux & cpu=x64
languageName: node
linkType: hard

"@embedded-postgres/windows-x64@npm:^17.2.0-beta.15":
version: 17.2.0-beta.15
resolution: "@embedded-postgres/windows-x64@npm:17.2.0-beta.15"
conditions: os=win32 & cpu=x64
languageName: node
linkType: hard

"@emotion/babel-plugin@npm:^11.13.5":
version: 11.13.5
resolution: "@emotion/babel-plugin@npm:11.13.5"
Expand Down Expand Up @@ -27179,6 +27240,41 @@ __metadata:
languageName: node
linkType: hard

"embedded-postgres@npm:17.2.0-beta.15":
version: 17.2.0-beta.15
resolution: "embedded-postgres@npm:17.2.0-beta.15"
dependencies:
"@embedded-postgres/darwin-arm64": ^17.2.0-beta.15
"@embedded-postgres/darwin-x64": ^17.2.0-beta.15
"@embedded-postgres/linux-arm": ^17.2.0-beta.15
"@embedded-postgres/linux-arm64": ^17.2.0-beta.15
"@embedded-postgres/linux-ia32": ^17.2.0-beta.15
"@embedded-postgres/linux-ppc64": ^17.2.0-beta.15
"@embedded-postgres/linux-x64": ^17.2.0-beta.15
"@embedded-postgres/windows-x64": ^17.2.0-beta.15
async-exit-hook: ^2.0.1
pg: ^8.7.3
dependenciesMeta:
"@embedded-postgres/darwin-arm64":
optional: true
"@embedded-postgres/darwin-x64":
optional: true
"@embedded-postgres/linux-arm":
optional: true
"@embedded-postgres/linux-arm64":
optional: true
"@embedded-postgres/linux-ia32":
optional: true
"@embedded-postgres/linux-ppc64":
optional: true
"@embedded-postgres/linux-x64":
optional: true
"@embedded-postgres/windows-x64":
optional: true
checksum: 53b261693dcc83cea6cc2589794dbc69bcf4cae508f2021d4686d7fb08e686e93db97392ebd28dfd2bb0126060b54fa609487d9929a4fee19da6d66dc568863f
languageName: node
linkType: hard

"emittery@npm:^0.13.1":
version: 0.13.1
resolution: "emittery@npm:0.13.1"
Expand Down Expand Up @@ -38938,7 +39034,7 @@ __metadata:
languageName: node
linkType: hard

"pg@npm:^8.11.3, pg@npm:^8.9.0":
"pg@npm:^8.11.3, pg@npm:^8.7.3, pg@npm:^8.9.0":
version: 8.13.1
resolution: "pg@npm:8.13.1"
dependencies:
Expand Down
Loading