Skip to content

Commit

Permalink
[Stable9.2] Cherry pick all of the pouch DB fixes to minecraft stable (
Browse files Browse the repository at this point in the history
…#9824)

* Stop using PouchDB (#9810)

* migrate pouchDB without a dependency on pouchDB

* obliterate PouchDb

* copy extra info to current db

* fix copyToLegacyEditor

* don't delete projects from pouchdb

* Don't give a ridiculously high pouchdb version (#9819)

* fix pouch db migration edge cases (#9820)

* delete the _doc_id_rev when migrating entries (#9822)
  • Loading branch information
riknoll authored Jan 22, 2024
1 parent b3bb9e0 commit c632bb8
Show file tree
Hide file tree
Showing 7 changed files with 429 additions and 136 deletions.
35 changes: 34 additions & 1 deletion pxtlib/browserutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,8 @@ namespace pxt.BrowserUtils {
private name: string,
private version: number,
private upgradeHandler?: IDBUpgradeHandler,
private quotaExceededHandler?: () => void) {
private quotaExceededHandler?: () => void,
private skipErrorLog = false) {
}

private throwIfNotOpened(): void {
Expand All @@ -845,6 +846,10 @@ namespace pxt.BrowserUtils {
}

private errorHandler(err: Error, op: string, reject: (err: Error) => void): void {
if (this.skipErrorLog) {
reject(err);
return;
}
console.error(new Error(`${this.name} IDBWrapper error for ${op}: ${err.message}`));
reject(err);
// special case for quota exceeded
Expand Down Expand Up @@ -945,6 +950,34 @@ namespace pxt.BrowserUtils {
request.onerror = () => this.errorHandler(request.error, "deleteAll", reject);
});
}

public getObjectStoreWrapper<T>(storeName: string): IDBObjectStoreWrapper<T> {
return new IDBObjectStoreWrapper(this, storeName);
}
}

export class IDBObjectStoreWrapper<T> {
constructor(protected db: IDBWrapper, protected storeName: string) {}

public getAsync(id: string): Promise<T> {
return this.db.getAsync(this.storeName, id);
}

public getAllAsync(): Promise<T[]> {
return this.db.getAllAsync(this.storeName);
}

public setAsync(data: T): Promise<void> {
return this.db.setAsync(this.storeName, data);
}

public async deleteAsync(id: string): Promise<void> {
await this.db.deleteAsync(this.storeName, id);
}

public async deleteAllAsync(): Promise<void> {
await this.db.deleteAllAsync(this.storeName);
}
}

class IndexedDbTranslationDb implements ITranslationDb {
Expand Down
3 changes: 3 additions & 0 deletions webapp/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import { CodeCardView } from "./codecard";
import { mergeProjectCode, appendTemporaryAssets } from "./mergeProjects";
import { Tour } from "./components/onboarding/Tour";
import { parseTourStepsAsync } from "./onboarding";
import { initGitHubDb } from "./idbworkspace";

pxsim.util.injectPolyphils();

Expand Down Expand Up @@ -5735,6 +5736,8 @@ document.addEventListener("DOMContentLoaded", async () => {
pxt.analytics.consoleTicks = pxt.analytics.ConsoleTickOptions.Short;
}

initGitHubDb();

pxt.perf.measureStart("setAppTarget");
pkg.setupAppTarget((window as any).pxtTargetBundle);

Expand Down
11 changes: 2 additions & 9 deletions webapp/src/browserworkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,13 @@ function setCoreAsync(headers: db.Table, texts: db.Table, h: Header, prevVer: an
return headerRes
}

export function copyProjectToLegacyEditor(h: Header, majorVersion: number): Promise<Header> {
export async function copyProjectToLegacyEditor(header: Header, script: pxt.workspace.ScriptText, majorVersion: number): Promise<void> {
const prefix = pxt.appTarget.appTheme.browserDbPrefixes && pxt.appTarget.appTheme.browserDbPrefixes[majorVersion];

const oldHeaders = new db.Table(prefix ? `${prefix}-header` : `header`);
const oldTexts = new db.Table(prefix ? `${prefix}-text` : `text`);

const header = pxt.Util.clone(h);
delete (header as any)._id;
delete header._rev;
header.id = pxt.Util.guidGen();

return getAsync(h)
.then(resp => setCoreAsync(oldHeaders, oldTexts, header, undefined, resp.text))
.then(rev => header);
await setCoreAsync(oldHeaders, oldTexts, header, undefined, script);
}

function deleteAsync(h: Header, prevVer: any) {
Expand Down
65 changes: 1 addition & 64 deletions webapp/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,67 +88,4 @@ export class Table {
obj._id = this.name + "--" + obj.id
return getDbAsync().then(db => db.put(obj)).then((resp: any) => resp.rev)
}
}

class GithubDb implements pxt.github.IGithubDb {
// in memory cache
private mem = new pxt.github.MemoryGithubDb();
private table = new Table("github");

latestVersionAsync(repopath: string, config: pxt.PackagesConfig): Promise<string> {
return this.mem.latestVersionAsync(repopath, config)
}

loadConfigAsync(repopath: string, tag: string): Promise<pxt.PackageConfig> {
// don't cache master
if (tag == "master")
return this.mem.loadConfigAsync(repopath, tag);

const id = `config-${repopath}-${tag}`;
return this.table.getAsync(id).then(
entry => {
pxt.debug(`github offline cache hit ${id}`);
return entry.config as pxt.PackageConfig;
},
e => {
pxt.debug(`github offline cache miss ${id}`);
return this.mem.loadConfigAsync(repopath, tag)
.then(config => {
return this.table.forceSetAsync({
id,
config
}).then(() => config, e => config);
})
} // not found
);
}
loadPackageAsync(repopath: string, tag: string): Promise<pxt.github.CachedPackage> {
if (!tag) {
pxt.debug(`dep: default to master`)
tag = "master"
}
// don't cache master
if (tag == "master")
return this.mem.loadPackageAsync(repopath, tag);

const id = `pkg-${repopath}-${tag}`;
return this.table.getAsync(id).then(
entry => {
pxt.debug(`github offline cache hit ${id}`);
return entry.package as pxt.github.CachedPackage;
},
e => {
pxt.debug(`github offline cache miss ${id}`);
return this.mem.loadPackageAsync(repopath, tag)
.then(p => {
return this.table.forceSetAsync({
id,
package: p
}).then(() => p, e => p);
})
} // not found
);
}
}

pxt.github.db = new GithubDb();
}
Loading

0 comments on commit c632bb8

Please sign in to comment.