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

Dbschema migr -- command side #204

Merged
merged 10 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 2 additions & 0 deletions examples/hello/dbos-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ database:
system_database: 'hello_systemdb'
connectionTimeoutMillis: 3000
user_dbclient: 'knex'
migrate: ['migrate:latest']
rollback: ['migrate:rollback']
version: "1.0"
8 changes: 7 additions & 1 deletion src/cloud-cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { Command } from 'commander';
import { login } from "./login";
import { registerUser } from "./register";
import { createUserDb, getUserDb, deleteUserDb } from "./userdb";
import { createUserDb, getUserDb, deleteUserDb, migrate } from "./userdb";
import { credentialsExist } from "./utils";

const program = new Command();
Expand Down Expand Up @@ -174,6 +174,12 @@ userdb
await deleteUserDb(host, port, dbname, options.sync)
}))

userdb
.command('migrate')
.action((() => {
migrate()
}))

program.parse(process.argv);

// If no arguments provided, display help by default
Expand Down
57 changes: 56 additions & 1 deletion src/cloud-cli/userdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import axios from "axios";
import { createGlobalLogger } from "../telemetry/logs";
import { getCloudCredentials } from "./utils";
import { sleep } from "../utils"
import { ConfigFile, loadConfigFile, dbosConfigFilePath } from "../dbos-runtime/config";
import { execSync } from "child_process";

export async function createUserDb(host: string, port: string, dbName: string, adminName: string, adminPassword: string, sync: boolean) {
const logger = createGlobalLogger();
Expand Down Expand Up @@ -42,7 +44,7 @@ export async function createUserDb(host: string, port: string, dbName: string, a
logger.info("Saving db state to cloud db");
await axios.put(`http://${host}:${port}/${userCredentials.userName}/databases/userdb/info`,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
{"Name": dbName,"Status": status, "HostName": dbhostname, "Port": dbport},
{"DBName": dbName,"Status": status, "HostName": dbhostname, "Port": dbport},
{
headers: {
"Content-Type": "application/json",
Expand Down Expand Up @@ -133,6 +135,59 @@ export async function getUserDb(host: string, port: string, dbName: string) {
}
}

export function migrate() {
const logger = createGlobalLogger();

// read the yaml file
const configFile: ConfigFile | undefined = loadConfigFile(dbosConfigFilePath);
if (!configFile) {
logger.error(`failed to parse ${dbosConfigFilePath}`);
return;
}

let create_db = configFile.database.create_db;
if (create_db == undefined) {
create_db = false;
}

const userdbname = configFile.database.user_database

if (create_db) {
logger.info(`Creating database ${userdbname}`);
let cmd = `createdb -h ${configFile.database.hostname} -p ${configFile.database.port} ${userdbname} -U ${configFile.database.username} -w ${configFile.database.password} -e`;
logger.info(cmd);
try {
execSync(cmd);
} catch(e) {
logger.error("Database already exists or could not be created.");
}
}

let dbType = configFile.database.user_dbclient;
if (dbType == undefined) {
dbType = 'knex';
}

const migratecommands = configFile.database.migrate;
const rollbackcommands = configFile.database.rollback;

try {
migratecommands?.forEach((cmd) => {
const command = "npx "+ dbType + " " + cmd;
logger.info("Executing " + command);
execSync(command);
})
} catch(e) {
logger.error("Error running migration");
rollbackcommands?.forEach((cmd) => {
const command = "npx " + dbType + " " + cmd;
logger.info("Executing " + command);
execSync(command);
})

}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a use case for users wanting to trigger a rollback manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can agree with that. Let us what @kraftp thinks. In we do that should add a npx db-cloud userdb rollback

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we decided to separate migrate from deploy and provide a manual migrate command? In that case, yeah, we need manual rollback as well.


// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any
async function getDb(host: string, port: string, dbName: string) : Promise<any> {

Expand Down
3 changes: 3 additions & 0 deletions src/dbos-runtime/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export interface ConfigFile {
observability_database: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
user_dbclient?: UserDatabaseName;
create_db: boolean;
migrate?: string[];
rollback?: string[];
};
telemetry?: TelemetryConfig;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
Loading