|
| 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(); |
0 commit comments