Skip to content

Commit

Permalink
Db reset command (#730)
Browse files Browse the repository at this point in the history
Drop the sys database
  • Loading branch information
manojdbos authored Jan 22, 2025
1 parent e76dc26 commit 6482ddc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/dbos-runtime/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { cancelWorkflow, getWorkflow, listWorkflows, reattemptWorkflow } from ".
import { GetWorkflowsInput, StatusString } from "..";
import { exit } from "node:process";
import { runCommand } from "./commands";
import { reset} from "./reset";

const program = new Command();

Expand Down Expand Up @@ -118,6 +119,17 @@ program
.description("Perform a database migration")
.action(async () => { await runAndLog(migrate); });

program
.command('reset')
.description("reset the system database")
.option('-y, --yes', 'Skip confirmation prompt', false)
.action(async (options: {yes: boolean}) => {
const logger = new GlobalLogger();
const _ = parseConfigFile(); // Validate config file
const configFile = loadConfigFile(dbosConfigFilePath);
await reset(configFile, logger, options.yes);
});

program
.command('rollback')
.action(async () => { await runAndLog(rollbackMigration); });
Expand Down
51 changes: 51 additions & 0 deletions src/dbos-runtime/reset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { GlobalLogger } from "../telemetry/logs";
import { ConfigFile, constructPoolConfig } from "./config";
import { PoolConfig, Client } from "pg";
import { confirm } from '@inquirer/prompts';

export async function reset(configFile: ConfigFile, logger: GlobalLogger, cnf: boolean) {

if (cnf) {
const userConfirmed = await confirm({
message:
'This command resets your DBOS system database, deleting metadata about past workflows and steps. Are you sure you want to proceed?',
default: false, // Default value for confirmation
});

if (!userConfirmed) {
console.log('Operation cancelled.');
process.exit(0); // Exit the process if the user cancels
}
}

const userPoolConfig: PoolConfig = constructPoolConfig(configFile)

const sysDbName = configFile.database.sys_db_name ?? `${userPoolConfig.database}_dbos_sys`;

logger.info(`Resetting ${sysDbName} if it exists`);

const pgClient = new Client({
user: userPoolConfig.user,
host: userPoolConfig.host,
database: "postgres", // Connect to the default PostgreSQL database
password: userPoolConfig.password,
port: userPoolConfig.port,
});

await pgClient.connect();

console.log("Terminating connections to system database");


await pgClient.query(`SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = $1
AND pid <> pg_backend_pid()`, [sysDbName]);


await pgClient.query(`DROP DATABASE IF EXISTS ${sysDbName};`);

await pgClient.end();

return 0;
}

0 comments on commit 6482ddc

Please sign in to comment.