-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1dad9f9
commit c0c544e
Showing
11 changed files
with
109 additions
and
141 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./parse-block.js"; | ||
export * from "./migrate-db.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* eslint-disable no-console */ | ||
import { Logger } from "drizzle-orm"; | ||
import { drizzle } from "drizzle-orm/node-postgres"; | ||
import { migrate } from "drizzle-orm/node-postgres/migrator"; | ||
import { backOff } from "exponential-backoff"; | ||
import pg, { PoolClient } from "pg"; | ||
|
||
class MyLogger implements Logger { | ||
logQuery(query: string): void { | ||
console.log("QUERY:", query); | ||
} | ||
} | ||
|
||
const TOTAL_DB_RESET = process.env.TOTAL_DB_RESET === "true"; | ||
|
||
const retries = 50; | ||
|
||
async function printTableList(client: PoolClient, ID: string): Promise<void> { | ||
const res = await client.query( | ||
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_name" | ||
); | ||
console.log(`=========== ${ID} ============`); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
res.rows.forEach((row) => console.log(row.table_name)); | ||
console.log("=============================="); | ||
} | ||
|
||
async function tryMigrate(pool: pg.Pool, db: ReturnType<typeof drizzle>) { | ||
const client = await pool.connect(); | ||
await printTableList(client, "BEFORE MIGRATION"); | ||
if (TOTAL_DB_RESET) await dropAllTables(client); | ||
|
||
await migrate(db, { migrationsFolder: "./migrations" }); | ||
await printTableList(client, "AFTER MIGRATION"); | ||
client.release(); | ||
console.log("👌 Client released"); | ||
} | ||
|
||
async function run( | ||
dbCredentials: pg.PoolConfig, | ||
db: ReturnType<typeof drizzle> | ||
) { | ||
console.log("🥸 Running migrations..."); | ||
console.log( | ||
`host: ${dbCredentials.host} port: ${dbCredentials.port} db: ${dbCredentials.database} user: ${dbCredentials.user}` | ||
); | ||
await backOff(() => tryMigrate(new pg.Pool(dbCredentials), db), { | ||
maxDelay: 10000, | ||
numOfAttempts: retries, | ||
retry: (e, attemptNumber: number) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
if (e.code === "ECONNREFUSED") { | ||
console.log(`Retrying attempt ${attemptNumber} of ${retries}...`); | ||
return true; | ||
} | ||
console.error(e); | ||
return false; | ||
}, | ||
}); | ||
console.log("🤩 Migrations complete!"); | ||
if (TOTAL_DB_RESET) console.log("🔥🔥🔥 Total DB reset!"); | ||
} | ||
|
||
async function dropAllTables(client: PoolClient) { | ||
console.log("🔥 Dropping all tables..."); | ||
const res1 = await client.query("DROP SCHEMA public CASCADE"); | ||
console.log(`💣 ${res1.command} ${res1.rowCount}`); | ||
const res2 = await client.query("CREATE SCHEMA public"); | ||
console.log(`💣 ${res2.command} ${res2.rowCount}`); | ||
const res3 = await client.query("DROP SCHEMA drizzle CASCADE"); | ||
console.log(`💣 ${res3.command} ${res3.rowCount}`); | ||
} | ||
|
||
export async function runMigrations(dbCredentials: pg.PoolConfig) { | ||
const pool = new pg.Pool(dbCredentials); | ||
const db = drizzle(pool, { logger: new MyLogger() }); | ||
try { | ||
await run(dbCredentials, db); | ||
await pool.end(); | ||
console.log("👋 Pool closed!"); | ||
} catch (e) { | ||
console.error(e); | ||
await pool.end(); | ||
console.log("👋👋 Pool closed!"); | ||
process.exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,6 @@ | ||
/* eslint-disable no-console, @typescript-eslint/no-unsafe-member-access */ | ||
|
||
import { Logger } from "drizzle-orm"; | ||
import { drizzle } from "drizzle-orm/node-postgres"; | ||
import { migrate } from "drizzle-orm/node-postgres/migrator"; | ||
import { backOff } from "exponential-backoff"; | ||
import pg, { PoolClient } from "pg"; | ||
/* eslint-disable no-console */ | ||
import { dbCredentials } from "../src/constants.js"; | ||
import { runMigrations } from "@chicmoz-pkg/backend-utils"; | ||
|
||
class MyLogger implements Logger { | ||
logQuery(query: string): void { | ||
console.log("QUERY:", query); | ||
} | ||
} | ||
|
||
const pool = new pg.Pool(dbCredentials); | ||
const db = drizzle(pool, { logger: new MyLogger() }); | ||
const retries = 50; | ||
|
||
async function printTableList(client: PoolClient, ID: string): Promise<void> { | ||
const res = await client.query( | ||
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_name" | ||
); | ||
console.log(`=========== ${ID} ============`); | ||
res.rows.forEach((row) => console.log(row.table_name)); | ||
console.log("=============================="); | ||
} | ||
|
||
async function tryMigrate() { | ||
const client = await pool.connect(); | ||
await printTableList(client, "BEFORE MIGRATION"); | ||
await migrate(db, { migrationsFolder: "./migrations" }); | ||
await printTableList(client, "AFTER MIGRATION"); | ||
client.release(); | ||
console.log("👌 Client released"); | ||
} | ||
|
||
async function runMigrations() { | ||
console.log("🥸 Running migrations..."); | ||
console.log( | ||
`host: ${dbCredentials.host} port: ${dbCredentials.port} db: ${dbCredentials.database} user: ${dbCredentials.user}` | ||
); | ||
await backOff(() => tryMigrate(), { | ||
maxDelay: 10000, | ||
numOfAttempts: retries, | ||
retry: (e, attemptNumber: number) => { | ||
if (e.code === "ECONNREFUSED") { | ||
console.log(`Retrying attempt ${attemptNumber} of ${retries}...`); | ||
return true; | ||
} | ||
console.error(e); | ||
return false; | ||
}, | ||
}); | ||
console.log("🤩 Migrations complete!"); | ||
await pool.end(); | ||
console.log("👋 Pool closed!"); | ||
} | ||
|
||
runMigrations().catch(async (e) => { | ||
console.error(e); | ||
await pool.end(); | ||
console.log("👋 Pool closed!"); | ||
process.exit(1); | ||
}); | ||
runMigrations(dbCredentials).catch(console.error); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,5 @@ | ||
/* eslint-disable no-console, @typescript-eslint/no-unsafe-member-access */ | ||
|
||
import { Logger } from "drizzle-orm"; | ||
import { drizzle } from "drizzle-orm/node-postgres"; | ||
import { migrate } from "drizzle-orm/node-postgres/migrator"; | ||
import { backOff } from "exponential-backoff"; | ||
import pg, { PoolClient } from "pg"; | ||
/* eslint-disable no-console */ | ||
import { dbCredentials } from "../src/environment.js"; | ||
import { runMigrations } from "@chicmoz-pkg/backend-utils"; | ||
|
||
class MyLogger implements Logger { | ||
logQuery(query: string): void { | ||
console.log("QUERY:", query); | ||
} | ||
} | ||
|
||
const pool = new pg.Pool(dbCredentials); | ||
const db = drizzle(pool, { logger: new MyLogger() }); | ||
const retries = 50; | ||
|
||
async function printTableList(client: PoolClient, ID: string): Promise<void> { | ||
const res = await client.query( | ||
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_name" | ||
); | ||
console.log(`=========== ${ID} ============`); | ||
res.rows.forEach((row) => console.log(row.table_name)); | ||
console.log("=============================="); | ||
} | ||
|
||
async function tryMigrate() { | ||
const client = await pool.connect(); | ||
await printTableList(client, "BEFORE MIGRATION"); | ||
await migrate(db, { migrationsFolder: "./migrations" }); | ||
await printTableList(client, "AFTER MIGRATION"); | ||
client.release(); | ||
console.log("👌 Client released"); | ||
} | ||
|
||
async function runMigrations() { | ||
console.log("🥸 Running migrations..."); | ||
console.log( | ||
`host: ${dbCredentials.host} port: ${dbCredentials.port} db: ${dbCredentials.database} user: ${dbCredentials.user}` | ||
); | ||
await backOff(() => tryMigrate(), { | ||
maxDelay: 10000, | ||
numOfAttempts: retries, | ||
retry: (e, attemptNumber: number) => { | ||
if (e.code === "ECONNREFUSED") { | ||
console.log(`Retrying attempt ${attemptNumber} of ${retries}...`); | ||
return true; | ||
} | ||
console.error(e); | ||
return false; | ||
}, | ||
}); | ||
console.log("🤩 Migrations complete!"); | ||
await pool.end(); | ||
console.log("👋 Pool closed!"); | ||
} | ||
|
||
runMigrations().catch(async (e) => { | ||
console.error(e); | ||
await pool.end(); | ||
console.log("👋 Pool closed!"); | ||
process.exit(1); | ||
}); | ||
runMigrations(dbCredentials).catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters