summaryrefslogtreecommitdiff
path: root/apps/web/src/services/storage/migrations/runner.ts
blob: cbc3739fd9f308cd5fd49d84fcaac8a54e29cb43 (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { IndexedDBAdapter } from "@/services/storage/indexeddb-adapter";
import type { StorageMigration } from "./base";
import { StorageVersionManager } from "./version-manager";

export interface StorageMigrationResult {
	fromVersion: number;
	toVersion: number;
	migrated: boolean;
}

export type StorageMigrationCallbacks = {
	onMigrationStart?: ({
		fromVersion,
		toVersion,
	}: {
		fromVersion: number;
		toVersion: number;
	}) => void;
	onMigrationComplete?: ({
		fromVersion,
		toVersion,
	}: {
		fromVersion: number;
		toVersion: number;
	}) => void;
};

type ProjectRecord = Record<string, unknown>;

export async function runStorageMigrations({
	migrations,
	versionManager = new StorageVersionManager(),
	callbacks,
}: {
	migrations: StorageMigration[];
	versionManager?: StorageVersionManager;
	callbacks?: StorageMigrationCallbacks;
}): Promise<StorageMigrationResult> {
	const versionRecord = await versionManager.getVersionRecord();
	const inferredVersion = versionRecord
		? null
		: await inferStorageVersionFromProjects();
	const fromVersion =
		versionRecord?.inProgress?.from ??
		versionRecord?.version ??
		inferredVersion ??
		0;

	if (!versionRecord) {
		await versionManager.setVersion({ version: fromVersion });
	}

	const orderedMigrations = [...migrations].sort((a, b) => a.from - b.from);
	let currentVersion = fromVersion;

	for (const migration of orderedMigrations) {
		if (migration.from !== currentVersion) {
			continue;
		}

		await versionManager.setInProgress({
			from: migration.from,
			to: migration.to,
		});
		callbacks?.onMigrationStart?.({
			fromVersion: migration.from,
			toVersion: migration.to,
		});
		await migration.run();
		currentVersion = migration.to;
		await versionManager.setVersion({ version: currentVersion });
		await versionManager.clearInProgress();
		callbacks?.onMigrationComplete?.({
			fromVersion: migration.from,
			toVersion: migration.to,
		});
	}

	return {
		fromVersion,
		toVersion: currentVersion,
		migrated: currentVersion !== fromVersion,
	};
}

async function inferStorageVersionFromProjects(): Promise<number> {
	const projectsAdapter = new IndexedDBAdapter<unknown>(
		"video-editor-projects",
		"projects",
		1,
	);
	const projects = await projectsAdapter.getAll();

	if (projects.length === 0) {
		return 0;
	}

	let lowestVersion = Number.POSITIVE_INFINITY;

	for (const project of projects) {
		const projectVersion = checkProjectVersion({ project });
		if (projectVersion < lowestVersion) {
			lowestVersion = projectVersion;
		}
	}

	if (lowestVersion === Number.POSITIVE_INFINITY) {
		return 0;
	}

	return lowestVersion;
}

function checkProjectVersion({ project }: { project: unknown }): number {
	if (!isRecord(project)) {
		return 0;
	}

	const versionValue = project.version;

	// v2 and up
	if (typeof versionValue === "number") {
		return versionValue;
	}

	// v1 (got scenes)
	const scenesValue = project.scenes;
	if (Array.isArray(scenesValue) && scenesValue.length > 0) {
		return 1;
	}

	// v0 (didn't have scenes)
	return 0;
}

function isRecord(value: unknown): value is ProjectRecord {
	return typeof value === "object" && value !== null;
}