Skip to content

Commit

Permalink
fix: migrations should now use the same pg client as the rest of the …
Browse files Browse the repository at this point in the history
…application
  • Loading branch information
Tethik committed Nov 18, 2024
1 parent ba362dc commit 68b3764
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
8 changes: 6 additions & 2 deletions api/jest.globalSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { loadConfig } from "@gram/core/dist/config/index.js";
import { registerConfiguration } from "@gram/core/dist/config/configMap.js";
import { migrate } from "@gram/core/dist/data/Migration.js";
import { testConfig } from "./dist/test-util/testConfig.js";
import { DataAccessLayer } from "@gram/core/dist/data/dal.js";
import { createPostgresPool } from "@gram/core/dist/data/postgres.js";

export default async function setup() {
registerConfiguration("test", testConfig);
loadConfig();
await migrate();
}
const pool = await createPostgresPool();
const dal = new DataAccessLayer(pool);
await migrate(dal);
}
2 changes: 1 addition & 1 deletion api/src/util/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function initSentry() {
}

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
const { nodeProfilingIntegration } = require("@sentry/profiling-node");

Sentry.init({
release: `gram@${version}`,
Expand Down
6 changes: 5 additions & 1 deletion core/jest.globalSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { loadConfig } from "./dist/config/index.js";
import { registerConfiguration } from "./dist/config/configMap.js";
import { migrate } from "./dist/data/Migration.js";
import { testConfig } from "./dist/test-util/testConfig.js";
import { DataAccessLayer } from "./dist/data/dal.js";
import { createPostgresPool } from "./dist/data/postgres.js";

export default async function setup() {
registerConfiguration("test", testConfig);
loadConfig();
await migrate();
const pool = await createPostgresPool();
const dal = new DataAccessLayer(pool);
await migrate(dal);
}
2 changes: 1 addition & 1 deletion core/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function bootstrap(): Promise<DataAccessLayer> {
const dal = new DataAccessLayer(pool);
const bt = new Bootstrapper(dal);

await migrate();
await migrate(dal);

// Set https proxy for outgoing requests
if (config.httpsProxy) {
Expand Down
33 changes: 22 additions & 11 deletions core/src/data/Migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,39 @@ import {
} from "postgres-migrations";
import { config } from "../config/index.js";
import { getDatabaseName } from "./postgres.js";
import { DataAccessLayer } from "./dal.js";
import { runCreateQuery } from "postgres-migrations/dist/create.js";
import { Pool } from "pg";

const log = log4js.getLogger("Migration");

export class Migration {
constructor(public folderPath: string, public pluginSuffix: string) {}

async migrate() {
async ensureDbExists(pool: Pool, databaseName: string) {
const result = await pool.query(
"SELECT 1 FROM pg_database WHERE datname=$1",
[databaseName]
);
if (result.rowCount !== 1) {
await runCreateQuery(databaseName, (msg: string) => log.debug(msg))(pool);
}
}

async migrate(dal: DataAccessLayer) {
const host = await config.postgres.host.getValue();
const databaseName = await getDatabaseName(this.pluginSuffix);
const pool = this.pluginSuffix
? await dal.pluginPool(this.pluginSuffix)
: dal.pool._pool;

log.info(
`Starting migration for ${process.env.NODE_ENV} (${host} - ${databaseName})`
);

const migrationConfig: MigrateDBConfig = {
database: databaseName,
port: parseInt((await config.postgres.port.getValue()) || "5432"),
host: (await config.postgres.host.getValue()) as string,
user: (await config.postgres.user.getValue()) as string,
password: (await config.postgres.password.getValue()) as string,
ensureDatabaseExists: true,
};
await this.ensureDbExists(pool, databaseName);

const migrationConfig: MigrateDBConfig = { client: pool };

const migs = await postgresMigrate(migrationConfig, this.folderPath);
migs.forEach((mig) => {
Expand All @@ -47,10 +58,10 @@ export const coreMigration = new Migration(
""
);

export async function migrate() {
export async function migrate(dal: DataAccessLayer) {
// Perform migrations
const migrations = [coreMigration, ...(config.additionalMigrations || [])];
for (const mig of migrations) {
await mig.migrate();
await mig.migrate(dal);
}
}

0 comments on commit 68b3764

Please sign in to comment.