Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

1.2 KB
Raw Blame
import path from "node:path";
import { db } from "@cap/database";
import { DrizzleQueryError } from "drizzle-orm";
import { migrate } from "drizzle-orm/mysql2/migrator";

import { runOrgIdBackfill } from "./migrations/orgid_backfill.ts";
import { runSpaceMemberRoleBackfill } from "./migrations/space_member_role_backfill.ts";

async function runMigrate() {
	await migrate(db(), {
		migrationsFolder: path.join(process.cwd(), "/migrations"),
	});
}

function errorIsOrgIdMigration(e: unknown): e is DrizzleQueryError {
	return (
		e instanceof DrizzleQueryError &&
		e.query ===
			"ALTER TABLE `videos` MODIFY COLUMN `orgId` varchar(15) NOT NULL;"
	);
}

export async function migrateDb() {
	try {
		await runMigrate();
	} catch (e) {
		if (!errorIsOrgIdMigration(e)) throw e;

		console.log("non-null videos.orgId migration failed, running backfill");

		await runOrgIdBackfill();

		try {
			await runMigrate();
		} catch (retryError) {
			if (errorIsOrgIdMigration(retryError)) {
				throw new Error(
					"videos.orgId backfill failed, you will need to manually update the videos.orgId column before attempting to migrate again.",
				);
			}
			throw retryError;
		}
	}

	await runSpaceMemberRoleBackfill();
}