Skip to content

Commit 7b813c1

Browse files
authored
Merge pull request #180 from cuappdev/fix/migration-firebase-lazy-init
im sorry
2 parents 4a7cee5 + 9578945 commit 7b813c1

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

.github/workflows/migrate-neon.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@ jobs:
5555
echo "DATABASE_URL=$URL" >> "$GITHUB_ENV"
5656
5757
- name: Run migrations
58+
env:
59+
# Needed only if AuthorizationRefactor (or similar) still has to run.
60+
# Safe when unset if that migration is already recorded as applied.
61+
FIREBASE_SERVICE_ACCOUNT_JSON: ${{ secrets.FIREBASE_SERVICE_ACCOUNT_JSON }}
5862
run: npm run db:migrate

src/migrations/1740628691583-AuthorizationRefactor.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,45 @@ import {
55
} from "../utils/AuthorizationRefactor";
66
import * as admin from "firebase-admin";
77

8-
// Firebase Initialization
9-
const serviceAccountPath = process.env.FIREBASE_SERVICE_ACCOUNT_PATH!;
10-
const serviceAccount = require(serviceAccountPath);
8+
/**
9+
* Initialize Firebase only when this migration's `up` runs.
10+
* TypeORM loads every migration module on `migration:run`; top-level
11+
* `require(FIREBASE_SERVICE_ACCOUNT_PATH)` breaks CI when that env is unset.
12+
*/
13+
function ensureFirebaseApp(): void {
14+
if (admin.apps.length) return;
15+
16+
const json = process.env.FIREBASE_SERVICE_ACCOUNT_JSON?.trim();
17+
let serviceAccount: admin.ServiceAccount;
18+
19+
if (json) {
20+
try {
21+
serviceAccount = JSON.parse(json) as admin.ServiceAccount;
22+
} catch {
23+
const decoded = Buffer.from(json, "base64").toString("utf8");
24+
serviceAccount = JSON.parse(decoded) as admin.ServiceAccount;
25+
}
26+
} else {
27+
const serviceAccountPath = process.env.FIREBASE_SERVICE_ACCOUNT_PATH;
28+
if (!serviceAccountPath) {
29+
throw new Error(
30+
"AuthorizationRefactor migration requires FIREBASE_SERVICE_ACCOUNT_JSON " +
31+
"or FIREBASE_SERVICE_ACCOUNT_PATH when this migration still needs to run.",
32+
);
33+
}
34+
// eslint-disable-next-line @typescript-eslint/no-var-requires
35+
serviceAccount = require(serviceAccountPath) as admin.ServiceAccount;
36+
}
1137

12-
if (!admin.apps.length) {
1338
admin.initializeApp({
1439
credential: admin.credential.cert(serviceAccount),
1540
});
1641
}
1742

1843
export class AuthorizationRefactor1740628691583 implements MigrationInterface {
1944
public async up(queryRunner: QueryRunner): Promise<void> {
45+
ensureFirebaseApp();
46+
2047
// Add new column as nullable initially
2148
await queryRunner.query(
2249
`ALTER TABLE "User" ADD COLUMN "firebaseUid" VARCHAR`,

0 commit comments

Comments
 (0)