Skip to content

Commit 63756a2

Browse files
authored
adds overwrite script (#170)
1 parent c53a8d7 commit 63756a2

File tree

3 files changed

+109
-16
lines changed

3 files changed

+109
-16
lines changed

packages/shared-db/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"db:migrate": "pnpm exec drizzle-kit migrate",
1111
"db:studio": "pnpm exec drizzle-kit studio",
1212
"seed:dev": "bun ./scripts/seed-dev.ts",
13-
"seed:remote": "bun ./scripts/seed-remote.ts"
13+
"seed:remote": "bun ./scripts/seed-remote.ts",
14+
"overwrite-vars:up": "bun run ./scripts/overwrite-vars.ts up",
15+
"overwrite-vars:down": "bun run ./scripts/overwrite-vars.ts down"
1416
},
1517
"dependencies": {
1618
"async-retry": "^1.3.3",
@@ -27,8 +29,9 @@
2729
"dotenv": "^16.5.0",
2830
"drizzle-kit": "^0.31.1",
2931
"eslint": "^9.28.0",
30-
"typescript": "^5.8.3",
31-
"pg": "^8.15.6"
32+
"pg": "^8.15.6",
33+
"pg-connection-string": "^2.9.0",
34+
"typescript": "^5.8.3"
3235
},
3336
"peerDependencies": {
3437
"drizzle-orm": "^0.43.1",
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import * as dotenv from "dotenv";
2+
import { sql } from "drizzle-orm";
3+
import { drizzle, type NodePgDatabase } from "drizzle-orm/node-postgres";
4+
import * as path from "path";
5+
import { Client } from "pg";
6+
import { feeds } from "../src/schema/feeds";
7+
8+
dotenv.config({ path: path.resolve(__dirname, "../.env") });
9+
10+
// Define your list of transformations
11+
const replacements = [{ oldValue: "OLD", newValue: "{{NEW}}" }];
12+
13+
async function performUpOperation(db: NodePgDatabase) {
14+
for (const { oldValue, newValue } of replacements) {
15+
console.log(`Applying replacement: "${oldValue}" -> "${newValue}"`);
16+
await db.execute(sql`
17+
UPDATE ${feeds}
18+
SET
19+
config = REPLACE(config::text, ${oldValue}, ${newValue})::jsonb
20+
WHERE
21+
config::text LIKE ${`%${oldValue}%`};
22+
`);
23+
}
24+
console.log("All JSONB string replacements applied.");
25+
}
26+
27+
async function performDownOperation(db: NodePgDatabase) {
28+
for (let i = replacements.length - 1; i >= 0; i--) {
29+
const { oldValue, newValue } = replacements[i];
30+
console.log(`Reverting replacement: "${newValue}" -> "${oldValue}"`);
31+
await db.execute(sql`
32+
UPDATE ${feeds}
33+
SET
34+
config = REPLACE(config::text, ${newValue}, ${oldValue})::jsonb
35+
WHERE
36+
config::text LIKE ${`%${newValue}%`};
37+
`);
38+
}
39+
console.log("All JSONB string replacements reverted.");
40+
}
41+
42+
async function main() {
43+
const databaseUrl = process.env.DATABASE_URL;
44+
if (!databaseUrl) {
45+
console.error("Error: DATABASE_URL environment variable is required.");
46+
process.exit(1);
47+
}
48+
49+
const operation = process.argv[2];
50+
if (operation !== "up" && operation !== "down") {
51+
console.error(
52+
"Error: Please specify 'up' or 'down' as a command line argument.",
53+
);
54+
console.log(
55+
"Usage: bun run packages/shared-db/scripts/overwrite-vars.ts <up|down>",
56+
);
57+
process.exit(1);
58+
}
59+
60+
const dbClient = new Client({ connectionString: databaseUrl });
61+
62+
try {
63+
console.log(`Connecting to remote database specified by DATABASE_URL...`);
64+
await dbClient.connect();
65+
const dbInstance = drizzle(dbClient);
66+
67+
if (operation === "up") {
68+
console.log("Running UP operation...");
69+
await performUpOperation(dbInstance);
70+
} else {
71+
// operation === 'down'
72+
console.log("Running DOWN operation...");
73+
await performDownOperation(dbInstance);
74+
}
75+
console.log(`Operation '${operation}' completed successfully.`);
76+
} catch (error) {
77+
console.error(`Error during '${operation}' operation:`, error);
78+
process.exit(1);
79+
} finally {
80+
if (dbClient) {
81+
console.log("Closing database connection.");
82+
await dbClient.end();
83+
}
84+
}
85+
}
86+
87+
main();

pnpm-lock.yaml

Lines changed: 16 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)