-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
executable file
·86 lines (74 loc) · 2.55 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import * as admin from "firebase-admin";
import { getFirestore } from "firebase-admin/firestore";
import * as fs from "fs/promises";
import * as path from "path";
import { MigrationFile, RunMigrationOptions } from "./types";
import {
ensureValidMigrationFiles,
getSortedMigrationFilesByVersion,
getVersionFromMigrationFile,
logger,
MIGRATIONS_COLLECTION_NAME,
registerTsCompiler,
} from "./utils";
admin.initializeApp();
export async function runMigrations({
migrations,
databaseId,
tsconfig,
migrationsCollectionName = MIGRATIONS_COLLECTION_NAME,
verbose,
}: RunMigrationOptions) {
if (verbose) {
logger.level = "debug";
}
await registerTsCompiler(tsconfig);
let migrationsPath = migrations;
if (!path.isAbsolute(migrations)) {
migrationsPath = path.join(process.cwd(), migrations);
}
logger.debug(`Migrations path: ${migrationsPath}`);
const files = await fs.readdir(migrationsPath);
ensureValidMigrationFiles(files);
const sortedFilesByVersion = getSortedMigrationFilesByVersion(files);
if (sortedFilesByVersion.length === 0) {
logger.info("No migrations to run");
return;
}
const firestore = databaseId ? getFirestore(databaseId) : getFirestore();
const migrationsCollection = firestore.collection(migrationsCollectionName);
const versions = await migrationsCollection.listDocuments();
let currentVersion = null;
if (versions.length > 0) {
versions.sort((a, b) => {
return a.id.localeCompare(b.id);
});
const latestMigration = versions[versions.length - 1];
currentVersion = latestMigration.id;
}
logger.info(`Current Firestore Version: ${currentVersion}`);
for (const file of sortedFilesByVersion) {
const migrationVersion = getVersionFromMigrationFile(file);
if (currentVersion && migrationVersion <= currentVersion) {
logger.debug(
`Skipping migration because it has already been run: ${file}`
);
continue;
}
const migrationFilePath = `${migrationsPath}/${file}`;
logger.info(`Running migration: ${migrationFilePath}`);
const migrationFile: MigrationFile = await import(migrationFilePath);
if (!migrationFile.default && !migrationFile.migrate) {
throw new Error(
`Invalid migration file: ${file}. No default or migrate export found`
);
}
await (migrationFile.default ?? migrationFile.migrate)({ firestore });
await migrationsCollection.doc(migrationVersion).set({
timestamp: new Date().toISOString(),
caller: process.env.USER,
version: migrationVersion,
});
}
}
export * from "./types";