Skip to content

Commit

Permalink
use parseArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-iohk committed Oct 30, 2024
1 parent d687abd commit f6012fe
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions tasks/search_tx_optimizations.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import pg from "pg";
import "@std/dotenv/load";
import { assertExists } from "@std/assert";
import { parseArgs } from "@std/cli";
import * as path from "@std/path";

const connectionString = Deno.env.get("DATABASE_URL");
assertExists(connectionString);

const args = Deno.args;
const args = parseArgs(Deno.args, {
flags: {
apply: { type: "boolean" },
drop: { type: "boolean" },
},
});

if (args.length !== 1 || (args[0] !== "--apply" && args[0] !== "--drop")) {
if (!args.apply && !args.drop) {
console.error("Allowed parameters: --apply | --drop");
Deno.exit(1);
}

const scriptPath = args[0] === "--apply"
? path.join(path.dirname(path.fromFileUrl(import.meta.url)), "../sql/apply_search_tx_optimizations.sql")
: path.join(path.dirname(path.fromFileUrl(import.meta.url)), "../sql/drop_search_tx_optimizations.sql");
const scriptPath = args.apply
? path.join(path.dirname(path.fromFileUrl(import.meta.url)), "../sql/migrations/apply_search_tx_optimizations.sql")
: path.join(path.dirname(path.fromFileUrl(import.meta.url)), "../sql/migrations/drop_search_tx_optimizations.sql");

const client = new pg.Client({ connectionString });
await client.connect();

try {
const sqlScript = await Deno.readTextFile(scriptPath);
console.log(`Executing ${args[0].slice(2)} script...`);
console.log(`Executing ${args.apply ? "apply" : "drop"} script...`);
await client.query(sqlScript);
console.log(`Successfully executed ${args[0].slice(2)} script.`);
console.log(`Successfully executed ${args.apply ? "apply" : "drop"} script.`);
} catch (error) {
console.error("Error executing SQL script:", error);
} finally {
Expand Down

0 comments on commit f6012fe

Please sign in to comment.