Skip to content

Commit

Permalink
feat: add sh and url db commands
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterPtato committed Mar 14, 2024
1 parent d19a304 commit 70d54f7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
63 changes: 62 additions & 1 deletion src/cli/commands/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ import { migrateDev } from "../../migrate/dev.ts";
import { migrateStatus } from "../../migrate/status.ts";
import { migrateDeploy } from "../../migrate/deploy.ts";
import { migrateReset } from "../../migrate/reset.ts";
import { UserError } from "../../error/mod.ts";
import { CommandError, UserError } from "../../error/mod.ts";
import { Project } from "../../project/mod.ts";
import { verbose } from "../../term/status.ts";
import { getDatabaseUrl } from "../../utils/db.ts";

export const POSTGRES_IMAGE = "postgres:16.2-alpine3.19";
// Unique container name for this runtime so we can run multiple instances in
// parallel
export const POSTGRES_CONTAINER_NAME = `opengb-postgres-${Deno.pid}`;

export const dbCommand = new Command<GlobalOpts>()
.description("Database commands");
Expand Down Expand Up @@ -61,6 +68,60 @@ dbCommand
await migrateDeploy(project, modules);
});

dbCommand
.command("sh")
.arguments("<module>")
.action(async (opts, moduleName: string) => {
// Validate terminal
if (!Deno.stdin.isTerminal()) {
throw new UserError("Cannot run this command without a terminal.", {
suggest:
"This is likely because you're running from a non-interactive shell, such as a CI environment. Run this command in a terminal that supports TTY.",
});
}

const project = await initProject(opts);
const module = resolveModules(project, [moduleName])[0];

if (!module.db) throw new UserError(`Module does not have a database configured: ${name}`);

const dbUrl = getDatabaseUrl(module.db.name).toString();

// Start the container
verbose("Starting container", `${POSTGRES_CONTAINER_NAME} (${POSTGRES_IMAGE})`);
await new Deno.Command("docker", {
args: [
"run",
"-it",
"--rm",
`--name=${POSTGRES_CONTAINER_NAME}`,
"--add-host=host.docker.internal:host-gateway",
POSTGRES_IMAGE,
// ===
"psql",
dbUrl,
],
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
}).output();
});

dbCommand
.command("url")
.arguments("<module>")
.action(async (opts, moduleName: string) => {
const project = await initProject(opts);
const module = resolveModules(project, [moduleName])[0];

if (!module.db) throw new UserError(`Module does not have a database configured: ${name}`);

const dbUrl = getDatabaseUrl(module.db.name).toString();

await Deno.stdout.write(new TextEncoder().encode(dbUrl));
console.error("");
});

function resolveModules(project: Project, moduleNames: string[]) {
if (moduleNames.length > 0) {
return moduleNames.map((name) => {
Expand Down
2 changes: 1 addition & 1 deletion src/migrate/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export async function runPrismaCommand(
if (opts.interactive && !Deno.stdin.isTerminal()) {
throw new UserError("Cannot run this command without a terminal.", {
suggest:
"This is likely because you're running from a non-interactive shell, such as a CI environment., Run this command in a terminal that supports TTY.",
"This is likely because you're running from a non-interactive shell, such as a CI environment. Run this command in a terminal that supports TTY.",
});
}

Expand Down

0 comments on commit 70d54f7

Please sign in to comment.