Skip to content

armgit / plane

Public Imported from GitHub
0

πŸ”₯πŸ”₯πŸ”₯ Open-source Jira, Linear, Monday, and ClickUp alternative. Plane is a modern project management platform to manage tasks, sprints, docs, and triage.

[WEB-2706] fix: Fix issue with SQLite transactions (#5934)

Satish Gandhama1bfde6af932
diff --git a/web/core/local-db/storage.sqlite.ts b/web/core/local-db/storage.sqlite.ts
index 224b5013d0..00b3a6bb14 100644
--- a/web/core/local-db/storage.sqlite.ts
+++ b/web/core/local-db/storage.sqlite.ts
@@ -37,8 +37,18 @@ export class Storage {
constructor() {
this.db = null;
+
+ if (typeof window !== "undefined") {
+ window.addEventListener("beforeunload", this.closeDBConnection);
+ }
}
+ closeDBConnection = async () => {
+ if (this.db) {
+ await this.db.close();
+ }
+ };
+
reset = () => {
if (this.db) {
this.db.close();
@@ -293,7 +303,10 @@ export class Storage {
let issuesRaw: any[] = [];
let count: any[];
try {
- [issuesRaw, count] = await Promise.all([runQuery(query), runQuery(countQuery)]);
+ [issuesRaw, count] = await startSpan(
+ { name: "GET_ISSUES" },
+ async () => await Promise.all([runQuery(query), runQuery(countQuery)])
+ );
} catch (e) {
logError(e);
const issueService = new IssueService();
diff --git a/web/core/local-db/utils/load-issues.ts b/web/core/local-db/utils/load-issues.ts
index d374d2184f..a16f6bd0ca 100644
--- a/web/core/local-db/utils/load-issues.ts
+++ b/web/core/local-db/utils/load-issues.ts
@@ -17,7 +17,7 @@ export const addIssue = async (issue: any) => {
export const addIssuesBulk = async (issues: any, batchSize = 100) => {
if (!rootStore.user.localDBEnabled || !persistence.db) return;
-
+ if (!issues.length) return;
const insertStart = performance.now();
await persistence.db.exec("BEGIN;");
diff --git a/web/core/local-db/utils/load-workspace.ts b/web/core/local-db/utils/load-workspace.ts
index bb8ee1454d..3b715735e9 100644
--- a/web/core/local-db/utils/load-workspace.ts
+++ b/web/core/local-db/utils/load-workspace.ts
@@ -115,14 +115,14 @@ export const loadWorkSpaceData = async (workspaceSlug: string) => {
const [labels, modules, cycles, states, estimates, memebers] = await Promise.all(promises);
const start = performance.now();
- await persistence.db.exec("BEGIN TRANSACTION;");
+ await persistence.db.exec("BEGIN;");
await batchInserts(labels, "labels", labelSchema);
await batchInserts(modules, "modules", moduleSchema);
await batchInserts(cycles, "cycles", cycleSchema);
await batchInserts(states, "states", stateSchema);
await batchInserts(estimates, "estimate_points", estimatePointSchema);
await batchInserts(memebers, "members", memberSchema);
- await persistence.db.exec("COMMIT");
+ await persistence.db.exec("COMMIT;");
const end = performance.now();
log("Time taken to load workspace data", end - start);
};
diff --git a/web/core/local-db/worker/db.ts b/web/core/local-db/worker/db.ts
index b14347456b..0fba3b3abf 100644
--- a/web/core/local-db/worker/db.ts
+++ b/web/core/local-db/worker/db.ts
@@ -36,7 +36,12 @@ export class DBClass {
this.sqlite3 = SQLite.Factory(m);
const vfs = await MyVFS.create("plane", m);
this.sqlite3.vfs_register(vfs, true);
- const db = await this.sqlite3.open_v2(`${dbName}.sqlite3`);
+ const db = await this.sqlite3.open_v2(
+ `${dbName}.sqlite3`,
+ this.sqlite3.OPEN_READWRITE | this.sqlite3.OPEN_CREATE,
+ "plane"
+ );
+
this.instance.db = db;
this.instance.exec = async (sql: string) => {
const rows: any[] = [];
@@ -57,6 +62,8 @@ export class DBClass {
}
async exec(props: string | TQueryProps) {
+ // @todo this will fail if the transaction is started any other way
+ // eg: BEGIN, OR BEGIN TRANSACTION
if (props === "BEGIN;") {
let promiseToAwait;
if (this.tp.length > 0) {
diff --git a/web/core/services/issue/issue.service.ts b/web/core/services/issue/issue.service.ts
index 97fef0c16b..2cef113d80 100644
--- a/web/core/services/issue/issue.service.ts
+++ b/web/core/services/issue/issue.service.ts
@@ -71,10 +71,7 @@ export class IssueService extends APIService {
if (!isEmpty(queries.expand as string) && !queries.group_by)
return await this.getIssuesFromServer(workspaceSlug, projectId, queries, config);
- const response = await startSpan({ name: "GET_ISSUES" }, async () => {
- const res = await persistence.getIssues(workspaceSlug, projectId, queries, config);
- return res;
- });
+ const response = await persistence.getIssues(workspaceSlug, projectId, queries, config);
return response as TIssuesResponse;
}