Skip to content

Commit 85a7404

Browse files
committed
✨ 增加按钮可以重试数据迁移 #409
1 parent f1ee723 commit 85a7404

2 files changed

Lines changed: 216 additions & 188 deletions

File tree

src/app/migrate.ts

Lines changed: 192 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,197 @@ import { Subscribe, SubscribeDAO } from "./repo/subscribe";
55
import { Value, ValueDAO } from "./repo/value";
66
import { Permission, PermissionDAO } from "./repo/permission";
77

8+
// 迁移数据到chrome.storage
9+
export function migrateToChromeStorage() {
10+
// 默认使用的事务,这里加个延时,用db.open()打开数据库后,再执行
11+
setTimeout(async () => {
12+
try {
13+
// 迁移脚本
14+
const scripts = await db.table("scripts").toArray();
15+
const scriptDAO = new ScriptDAO();
16+
const scriptCodeDAO = new ScriptCodeDAO();
17+
console.log("开始迁移脚本数据", scripts.length);
18+
await Promise.all(
19+
scripts.map(async (script: ScriptAndCode) => {
20+
const {
21+
uuid,
22+
name,
23+
namespace,
24+
author,
25+
originDomain,
26+
subscribeUrl,
27+
type,
28+
sort,
29+
status,
30+
runStatus,
31+
metadata,
32+
createtime,
33+
checktime,
34+
code,
35+
checkUpdateUrl,
36+
downloadUrl,
37+
selfMetadata,
38+
config,
39+
error,
40+
updatetime,
41+
lastruntime,
42+
nextruntime,
43+
} = script;
44+
const s = await scriptDAO.save({
45+
uuid,
46+
name,
47+
namespace,
48+
author,
49+
originDomain,
50+
origin,
51+
checkUpdateUrl,
52+
downloadUrl,
53+
metadata,
54+
selfMetadata,
55+
subscribeUrl,
56+
config,
57+
type,
58+
status,
59+
sort,
60+
runStatus,
61+
error,
62+
createtime,
63+
updatetime,
64+
checktime,
65+
lastruntime,
66+
nextruntime,
67+
});
68+
return scriptCodeDAO
69+
.save({
70+
uuid: s.uuid,
71+
code,
72+
})
73+
.catch((e) => {
74+
console.log("脚本代码迁移失败", e);
75+
return Promise.reject(e);
76+
});
77+
})
78+
);
79+
// 迁移订阅
80+
const subscribe = await db.table("subscribe").toArray();
81+
const subscribeDAO = new SubscribeDAO();
82+
if (subscribe.length) {
83+
await Promise.all(
84+
subscribe.map((s: Subscribe) => {
85+
const { url, name, code, author, scripts, metadata, status, createtime, updatetime, checktime } = s;
86+
return subscribeDAO.save({
87+
url,
88+
name,
89+
code,
90+
author,
91+
scripts,
92+
metadata,
93+
status,
94+
createtime,
95+
updatetime,
96+
checktime,
97+
});
98+
})
99+
);
100+
}
101+
console.log("订阅数据迁移完成", subscribe.length);
102+
// 迁移value
103+
interface MV2Value {
104+
id: number;
105+
scriptId: number;
106+
storageName?: string;
107+
key: string;
108+
value: any;
109+
createtime: number;
110+
updatetime: number;
111+
}
112+
const values = await db.table("value").toArray();
113+
const valueDAO = new ValueDAO();
114+
const valueMap = new Map<string, Value>();
115+
await Promise.all(
116+
values.map((v: MV2Value) => {
117+
const { scriptId, storageName, key, value, createtime } = v;
118+
return db
119+
.table("scripts")
120+
.where("id")
121+
.equals(scriptId)
122+
.first((script: Script) => {
123+
if (script) {
124+
let data: { [key: string]: any } = {};
125+
if (!valueMap.has(script.uuid)) {
126+
valueMap.set(script.uuid, {
127+
uuid: script.uuid,
128+
storageName: getStorageName(script),
129+
data: data,
130+
createtime,
131+
updatetime: 0,
132+
});
133+
} else {
134+
data = valueMap.get(script.uuid)!.data;
135+
}
136+
data[key] = value;
137+
}
138+
});
139+
})
140+
);
141+
// 保存到数据库
142+
await Promise.all(
143+
Array.from(valueMap.keys()).map((uuid) => {
144+
const { storageName, data, createtime } = valueMap.get(uuid)!;
145+
return valueDAO.save(storageName!, {
146+
uuid,
147+
storageName,
148+
data,
149+
createtime,
150+
updatetime: 0,
151+
});
152+
})
153+
);
154+
console.log("脚本value数据迁移完成", values.length);
155+
// 迁移permission
156+
const permissions = await db.table("permission").toArray();
157+
const permissionDAO = new PermissionDAO();
158+
await Promise.all(
159+
permissions.map((p: Permission & { scriptId: number }) => {
160+
const { scriptId, permission, permissionValue, createtime, updatetime, allow } = p;
161+
return db
162+
.table("scripts")
163+
.where("id")
164+
.equals(scriptId)
165+
.first((script: Script) => {
166+
if (script) {
167+
return permissionDAO.save({
168+
uuid: script.uuid,
169+
permission,
170+
permissionValue,
171+
createtime,
172+
updatetime,
173+
allow,
174+
});
175+
}
176+
});
177+
})
178+
);
179+
console.log("脚本permission数据迁移完成", permissions.length);
180+
// 打开页面,告知数据储存+升级至了mv3,重启一次扩展
181+
setTimeout(async () => {
182+
const scripts = await scriptDAO.all();
183+
console.log("脚本数据迁移完成", scripts.length);
184+
if (scripts.length > 0) {
185+
chrome.tabs.create({
186+
url: "https://docs.scriptcat.org/docs/change/v0.17/",
187+
});
188+
setTimeout(() => {
189+
chrome.runtime.reload();
190+
}, 1000);
191+
}
192+
}, 2000);
193+
} catch (e) {
194+
console.error("脚本数据迁移失败", e);
195+
}
196+
}, 200);
197+
}
198+
8199
// 0.10.0重构,重命名字段,统一使用小峰驼
9200
function renameField() {
10201
db.version(16)
@@ -38,193 +229,7 @@ function renameField() {
38229
});
39230
// 将脚本数据迁移到chrome.storage
40231
db.version(18).upgrade(() => {
41-
// 默认使用的事务,这里加个延时,用db.open()打开数据库后,再执行
42-
setTimeout(async () => {
43-
try {
44-
// 迁移脚本
45-
const scripts = await db.table("scripts").toArray();
46-
const scriptDAO = new ScriptDAO();
47-
const scriptCodeDAO = new ScriptCodeDAO();
48-
console.log("开始迁移脚本数据", scripts.length);
49-
await Promise.all(
50-
scripts.map(async (script: ScriptAndCode) => {
51-
const {
52-
uuid,
53-
name,
54-
namespace,
55-
author,
56-
originDomain,
57-
subscribeUrl,
58-
type,
59-
sort,
60-
status,
61-
runStatus,
62-
metadata,
63-
createtime,
64-
checktime,
65-
code,
66-
checkUpdateUrl,
67-
downloadUrl,
68-
selfMetadata,
69-
config,
70-
error,
71-
updatetime,
72-
lastruntime,
73-
nextruntime,
74-
} = script;
75-
const s = await scriptDAO.save({
76-
uuid,
77-
name,
78-
namespace,
79-
author,
80-
originDomain,
81-
origin,
82-
checkUpdateUrl,
83-
downloadUrl,
84-
metadata,
85-
selfMetadata,
86-
subscribeUrl,
87-
config,
88-
type,
89-
status,
90-
sort,
91-
runStatus,
92-
error,
93-
createtime,
94-
updatetime,
95-
checktime,
96-
lastruntime,
97-
nextruntime,
98-
});
99-
return scriptCodeDAO
100-
.save({
101-
uuid: s.uuid,
102-
code,
103-
})
104-
.catch((e) => {
105-
console.log("脚本代码迁移失败", e);
106-
return Promise.reject(e);
107-
});
108-
})
109-
);
110-
// 迁移订阅
111-
const subscribe = await db.table("subscribe").toArray();
112-
const subscribeDAO = new SubscribeDAO();
113-
if (subscribe.length) {
114-
await Promise.all(
115-
subscribe.map((s: Subscribe) => {
116-
const { url, name, code, author, scripts, metadata, status, createtime, updatetime, checktime } = s;
117-
return subscribeDAO.save({
118-
url,
119-
name,
120-
code,
121-
author,
122-
scripts,
123-
metadata,
124-
status,
125-
createtime,
126-
updatetime,
127-
checktime,
128-
});
129-
})
130-
);
131-
}
132-
console.log("订阅数据迁移完成", subscribe.length);
133-
// 迁移value
134-
interface MV2Value {
135-
id: number;
136-
scriptId: number;
137-
storageName?: string;
138-
key: string;
139-
value: any;
140-
createtime: number;
141-
updatetime: number;
142-
}
143-
const values = await db.table("value").toArray();
144-
const valueDAO = new ValueDAO();
145-
const valueMap = new Map<string, Value>();
146-
await Promise.all(
147-
values.map((v: MV2Value) => {
148-
const { scriptId, storageName, key, value, createtime } = v;
149-
return db
150-
.table("scripts")
151-
.where("id")
152-
.equals(scriptId)
153-
.first((script: Script) => {
154-
if (script) {
155-
let data: { [key: string]: any } = {};
156-
if (!valueMap.has(script.uuid)) {
157-
valueMap.set(script.uuid, {
158-
uuid: script.uuid,
159-
storageName: getStorageName(script),
160-
data: data,
161-
createtime,
162-
updatetime: 0,
163-
});
164-
} else {
165-
data = valueMap.get(script.uuid)!.data;
166-
}
167-
data[key] = value;
168-
}
169-
});
170-
})
171-
);
172-
// 保存到数据库
173-
await Promise.all(
174-
Array.from(valueMap.keys()).map((uuid) => {
175-
const { storageName, data, createtime } = valueMap.get(uuid)!;
176-
return valueDAO.save(storageName!, {
177-
uuid,
178-
storageName,
179-
data,
180-
createtime,
181-
updatetime: 0,
182-
});
183-
})
184-
);
185-
console.log("脚本value数据迁移完成", values.length);
186-
// 迁移permission
187-
const permissions = await db.table("permission").toArray();
188-
const permissionDAO = new PermissionDAO();
189-
await Promise.all(
190-
permissions.map((p: Permission & { scriptId: number }) => {
191-
const { scriptId, permission, permissionValue, createtime, updatetime, allow } = p;
192-
return db
193-
.table("scripts")
194-
.where("id")
195-
.equals(scriptId)
196-
.first((script: Script) => {
197-
if (script) {
198-
return permissionDAO.save({
199-
uuid: script.uuid,
200-
permission,
201-
permissionValue,
202-
createtime,
203-
updatetime,
204-
allow,
205-
});
206-
}
207-
});
208-
})
209-
);
210-
console.log("脚本permission数据迁移完成", permissions.length);
211-
// 打开页面,告知数据储存+升级至了mv3,重启一次扩展
212-
setTimeout(async () => {
213-
const scripts = await scriptDAO.all();
214-
console.log("脚本数据迁移完成", scripts.length);
215-
if (scripts.length > 0) {
216-
chrome.tabs.create({
217-
url: "https://docs.scriptcat.org/docs/change/v0.17/",
218-
});
219-
setTimeout(() => {
220-
chrome.runtime.reload();
221-
}, 1000);
222-
}
223-
}, 2000);
224-
} catch (e) {
225-
console.error("脚本数据迁移失败", e);
226-
}
227-
}, 200);
232+
migrateToChromeStorage();
228233
});
229234
return db.open();
230235
}

0 commit comments

Comments
 (0)