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
|
import {
IndexedDBAdapter,
deleteDatabase,
} from "@/services/storage/indexeddb-adapter";
import type { StorageMigration } from "./base";
import type { ProjectRecord } from "./transformers/types";
import { getProjectId, isRecord } from "./transformers/utils";
export interface StorageMigrationResult {
migratedCount: number;
}
export interface MigrationProgress {
isMigrating: boolean;
fromVersion: number | null;
toVersion: number | null;
projectName: string | null;
}
let hasCleanedUpMetaDb = false;
export async function runStorageMigrations({
migrations,
onProgress,
}: {
migrations: StorageMigration[];
onProgress?: (progress: MigrationProgress) => void;
}): Promise<StorageMigrationResult> {
// One-time cleanup: delete the old global version database
if (!hasCleanedUpMetaDb) {
try {
await deleteDatabase({ dbName: "video-editor-meta" });
} catch {
// Ignore errors - DB might not exist
}
hasCleanedUpMetaDb = true;
}
const projectsAdapter = new IndexedDBAdapter<ProjectRecord>(
"video-editor-projects",
"projects",
1,
);
const projects = await projectsAdapter.getAll();
const orderedMigrations = [...migrations].sort((a, b) => a.from - b.from);
let migratedCount = 0;
for (const project of projects) {
if (typeof project !== "object" || project === null) {
continue;
}
let projectRecord = project as ProjectRecord;
let currentVersion = getProjectVersion({ project: projectRecord });
const targetVersion = orderedMigrations.at(-1)?.to ?? currentVersion;
if (currentVersion >= targetVersion) {
continue;
}
const projectName = getProjectName({ project: projectRecord });
onProgress?.({
isMigrating: true,
fromVersion: currentVersion,
toVersion: targetVersion,
projectName,
});
for (const migration of orderedMigrations) {
if (migration.from !== currentVersion) {
continue;
}
const result = await migration.transform(projectRecord);
if (result.skipped) {
break;
}
const projectId = getProjectId({ project: result.project });
if (!projectId) {
break;
}
await projectsAdapter.set(projectId, result.project);
migratedCount++;
currentVersion = migration.to;
projectRecord = result.project;
}
}
onProgress?.({
isMigrating: false,
fromVersion: null,
toVersion: null,
projectName: null,
});
return { migratedCount };
}
function getProjectVersion({ project }: { project: ProjectRecord }): number {
const versionValue = project.version;
// v2 and up - has explicit version field
if (typeof versionValue === "number") {
return versionValue;
}
// v1 - has scenes array
const scenesValue = project.scenes;
if (Array.isArray(scenesValue) && scenesValue.length > 0) {
return 1;
}
// v0 - no scenes
return 0;
}
function getProjectName({ project }: { project: ProjectRecord }): string | null {
const metadata = project.metadata;
if (isRecord(metadata) && typeof metadata.name === "string") {
return metadata.name;
}
// v0 had name directly on project
if (typeof project.name === "string") {
return project.name;
}
return null;
}
|