Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 6 additions & 3 deletions packages/shared-db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"db:migrate": "pnpm exec drizzle-kit migrate",
"db:studio": "pnpm exec drizzle-kit studio",
"seed:dev": "bun ./scripts/seed-dev.ts",
"seed:remote": "bun ./scripts/seed-remote.ts"
"seed:remote": "bun ./scripts/seed-remote.ts",
"overwrite-vars:up": "bun run ./scripts/overwrite-vars.ts up",
"overwrite-vars:down": "bun run ./scripts/overwrite-vars.ts down"
},
"dependencies": {
"async-retry": "^1.3.3",
Expand All @@ -27,8 +29,9 @@
"dotenv": "^16.5.0",
"drizzle-kit": "^0.31.1",
"eslint": "^9.28.0",
"typescript": "^5.8.3",
"pg": "^8.15.6"
"pg": "^8.15.6",
"pg-connection-string": "^2.9.0",
"typescript": "^5.8.3"
},
"peerDependencies": {
"drizzle-orm": "^0.43.1",
Expand Down
87 changes: 87 additions & 0 deletions packages/shared-db/scripts/overwrite-vars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import * as dotenv from "dotenv";
import { sql } from "drizzle-orm";
import { drizzle, type NodePgDatabase } from "drizzle-orm/node-postgres";
import * as path from "path";
import { Client } from "pg";
import { feeds } from "../src/schema/feeds";

dotenv.config({ path: path.resolve(__dirname, "../.env") });

// Define your list of transformations
const replacements = [{ oldValue: "OLD", newValue: "{{NEW}}" }];

async function performUpOperation(db: NodePgDatabase) {
for (const { oldValue, newValue } of replacements) {
console.log(`Applying replacement: "${oldValue}" -> "${newValue}"`);
await db.execute(sql`
UPDATE ${feeds}
SET
config = REPLACE(config::text, ${oldValue}, ${newValue})::jsonb
WHERE
config::text LIKE ${`%${oldValue}%`};
`);
}
console.log("All JSONB string replacements applied.");
}

async function performDownOperation(db: NodePgDatabase) {
for (let i = replacements.length - 1; i >= 0; i--) {
const { oldValue, newValue } = replacements[i];
console.log(`Reverting replacement: "${newValue}" -> "${oldValue}"`);
await db.execute(sql`
UPDATE ${feeds}
SET
config = REPLACE(config::text, ${newValue}, ${oldValue})::jsonb
WHERE
config::text LIKE ${`%${newValue}%`};
`);
}
console.log("All JSONB string replacements reverted.");
}

async function main() {
const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
console.error("Error: DATABASE_URL environment variable is required.");
process.exit(1);
}

const operation = process.argv[2];
if (operation !== "up" && operation !== "down") {
console.error(
"Error: Please specify 'up' or 'down' as a command line argument.",
);
console.log(
"Usage: bun run packages/shared-db/scripts/overwrite-vars.ts <up|down>",
);
process.exit(1);
}

const dbClient = new Client({ connectionString: databaseUrl });

try {
console.log(`Connecting to remote database specified by DATABASE_URL...`);
await dbClient.connect();
const dbInstance = drizzle(dbClient);

if (operation === "up") {
console.log("Running UP operation...");
await performUpOperation(dbInstance);
} else {
// operation === 'down'
console.log("Running DOWN operation...");
await performDownOperation(dbInstance);
}
console.log(`Operation '${operation}' completed successfully.`);
} catch (error) {
console.error(`Error during '${operation}' operation:`, error);
process.exit(1);
} finally {
if (dbClient) {
console.log("Closing database connection.");
await dbClient.end();
}
}
}

main();
29 changes: 16 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading