Skip to content

Commit

Permalink
Create App Database if Does Not Exist (#737)
Browse files Browse the repository at this point in the history
This was removed in
#697. Re-enabling it
and adding tests.
  • Loading branch information
kraftp authored Jan 28, 2025
1 parent c7888ea commit 411e808
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/dbos-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
DrizzleUserDatabase,
UserDatabaseClient,
pgNodeIsKeyConflictError,
createDBIfDoesNotExist,
} from './user_database';
import { MethodRegistrationBase, getRegisteredOperations, getOrCreateClassRegistration, MethodRegistration, getRegisteredMethodClassName, getRegisteredMethodName, getConfiguredInstance, ConfiguredInstance, getAllRegisteredClasses } from './decorators';
import { SpanStatusCode } from '@opentelemetry/api';
Expand Down Expand Up @@ -378,6 +379,7 @@ export class DBOSExecutor implements DBOSExecutorContext {
this.logger.debug(`Loaded ${length} ORM entities`);
}

await createDBIfDoesNotExist(this.config.poolConfig, this.logger);
this.configureDbClient();

if (!this.userDatabase) {
Expand Down
38 changes: 24 additions & 14 deletions tests/dbos-runtime/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Client } from "pg";
import { generateDBOSTestConfig, setUpDBOSTestDb } from "../helpers";
import { HealthUrl } from "../../src/httpServer/server";

async function waitForMessageTest(command: ChildProcess, port: string, adminPort?: string) {
async function waitForMessageTest(command: ChildProcess, port: string, adminPort?: string, checkResponse: boolean = true) {
const stdout = command.stdout as unknown as Writable;
const stdin = command.stdin as unknown as Writable;
const stderr = command.stderr as unknown as Writable;
Expand Down Expand Up @@ -36,8 +36,10 @@ async function waitForMessageTest(command: ChildProcess, port: string, adminPort
// Axios will throw an exception if the return status is 500
// Trying and catching is the only way to debug issues in this test
try {
const response = await axios.get(`http://127.0.0.1:${port}/greeting/dbos`);
expect(response.status).toBe(200);
if (checkResponse) {
const response = await axios.get(`http://127.0.0.1:${port}/greeting/dbos`);
expect(response.status).toBe(200);
}
const healthRes = await axios.get(`http://127.0.0.1:${adminPort}${HealthUrl}`);
expect(healthRes.status).toBe(200);
} catch (error) {
Expand All @@ -53,7 +55,7 @@ async function waitForMessageTest(command: ChildProcess, port: string, adminPort
}
}

async function dropHelloSystemDB() {
async function dropTemplateDatabases() {
const config = generateDBOSTestConfig();
config.poolConfig.database = "hello";
await setUpDBOSTestDb(config);
Expand All @@ -62,7 +64,7 @@ async function dropHelloSystemDB() {
port: config.poolConfig.port,
host: config.poolConfig.host,
password: config.poolConfig.password,
database: "hello",
database: "postgres",
});
await pgSystemClient.connect();
await pgSystemClient.query(`DROP DATABASE IF EXISTS dbos_typeorm_dbos_sys;`);
Expand All @@ -76,7 +78,7 @@ async function dropHelloSystemDB() {
await pgSystemClient.end();
}

function configureHelloExample() {
function configureTemplate() {
execSync("npm i");
execSync("npm run build");
if (process.env.PGPASSWORD === undefined) {
Expand All @@ -87,9 +89,9 @@ function configureHelloExample() {

describe("runtime-tests-knex", () => {
beforeAll(async () => {
await dropHelloSystemDB();
await dropTemplateDatabases();
process.chdir("packages/create/templates/dbos-knex");
configureHelloExample();
configureTemplate();
});

afterAll(() => {
Expand All @@ -107,13 +109,21 @@ describe("runtime-tests-knex", () => {
});
await waitForMessageTest(command, "3000");
});

test("test hello-knex creates database if does not exist", async () => {
await dropTemplateDatabases();
const command = spawn("node", ["dist/main.js"], {
env: process.env,
});
await waitForMessageTest(command, "3000", "3001", false);
});
});

describe("runtime-tests-typeorm", () => {
beforeAll(async () => {
await dropHelloSystemDB();
await dropTemplateDatabases();
process.chdir("packages/create/templates/dbos-typeorm");
configureHelloExample();
configureTemplate();
});

afterAll(() => {
Expand All @@ -135,9 +145,9 @@ describe("runtime-tests-typeorm", () => {

describe("runtime-tests-prisma", () => {
beforeAll(async () => {
await dropHelloSystemDB();
await dropTemplateDatabases();
process.chdir("packages/create/templates/dbos-prisma");
configureHelloExample();
configureTemplate();
});

afterAll(() => {
Expand All @@ -159,9 +169,9 @@ describe("runtime-tests-prisma", () => {

describe("runtime-tests-drizzle", () => {
beforeAll(async () => {
await dropHelloSystemDB();
await dropTemplateDatabases();
process.chdir("packages/create/templates/dbos-drizzle");
configureHelloExample();
configureTemplate();
});

afterAll(() => {
Expand Down

0 comments on commit 411e808

Please sign in to comment.