Fix issue with SQLite transactions (#5919)

This commit is contained in:
Satish Gandham
2024-10-28 14:14:57 +05:30
committed by GitHub
parent 6d08cf2757
commit 6113aefde0
3 changed files with 36 additions and 22 deletions

View File

@@ -40,13 +40,14 @@ export const addIssuesBulk = async (issues: any, batchSize = 100) => {
export const deleteIssueFromLocal = async (issue_id: any) => {
if (!rootStore.user.localDBEnabled || !persistence.db) return;
const deleteQuery = `delete from issues where id='${issue_id}'`;
const deleteQuery = `DELETE from issues where id='${issue_id}'`;
const deleteMetaQuery = `delete from issue_meta where issue_id='${issue_id}'`;
persistence.db.exec("BEGIN;");
persistence.db.exec(deleteQuery);
persistence.db.exec(deleteMetaQuery);
persistence.db.exec("COMMIT;");
await persistence.db.exec("BEGIN;");
await persistence.db.exec(deleteQuery);
await persistence.db.exec(deleteMetaQuery);
await persistence.db.exec("COMMIT;");
};
// @todo: Update deletes the issue description from local. Implement a separate update.
export const updateIssue = async (issue: TIssue & { is_local_update: number }) => {
@@ -55,7 +56,7 @@ export const updateIssue = async (issue: TIssue & { is_local_update: number }) =
const issue_id = issue.id;
// delete the issue and its meta data
await deleteIssueFromLocal(issue_id);
addIssue(issue);
await addIssue(issue);
};
export const syncDeletesToLocal = async (workspaceId: string, projectId: string, queries: any) => {
@@ -98,27 +99,33 @@ const stageIssueInserts = async (issue: any) => {
.join(", ");
const query = `INSERT OR REPLACE INTO issues (${columns}) VALUES (${values});`;
persistence.db.exec(query);
await persistence.db.exec(query);
await persistence.db.exec({
sql: `DELETE from issue_meta where issue_id='${issue_id}'`,
});
const metaPromises: Promise<any>[] = [];
ARRAY_FIELDS.forEach((field) => {
const values = issue[field];
if (values && values.length) {
values.forEach((val: any) => {
persistence.db.exec({
const p = persistence.db.exec({
sql: `INSERT OR REPLACE into issue_meta(issue_id,key,value) values (?,?,?) `,
bind: [issue_id, field, val],
});
metaPromises.push(p);
});
} else {
// Added for empty fields?
persistence.db.exec({
const p = persistence.db.exec({
sql: `INSERT OR REPLACE into issue_meta(issue_id,key,value) values (?,?,?) `,
bind: [issue_id, field, ""],
});
metaPromises.push(p);
}
});
await Promise.all(metaPromises);
};

View File

@@ -59,7 +59,7 @@ export const updatePersistentLayer = async (issueIds: string | string[]) => {
"type_id",
"description_html",
]);
updateIssue({ ...issuePartial, is_local_update: 1 });
await updateIssue({ ...issuePartial, is_local_update: 1 });
}
});
};

View File

@@ -24,8 +24,8 @@ interface SQLiteInstance {
export class DBClass {
private instance: SQLiteInstance = {} as SQLiteInstance;
private sqlite3: any;
private tp: Promise<any> | null = null;
private tpResolver: any;
private tp: Promise<any>[] = [];
private tpResolver: any = [];
async init(dbName: string) {
if (!dbName || typeof dbName !== "string") {
throw new Error("Invalid database name");
@@ -57,8 +57,19 @@ export class DBClass {
}
async exec(props: string | TQueryProps) {
if (this.tp && props === "BEGIN;") {
await this.tp;
if (props === "BEGIN;") {
let promiseToAwait;
if (this.tp.length > 0) {
promiseToAwait = this.tp.shift();
}
const p = new Promise((resolve, reject) => {
this.tpResolver.push({ resolve, reject });
});
this.tp.push(p);
if (promiseToAwait) {
await promiseToAwait;
}
}
let sql: string, bind: any[];
if (typeof props === "string") {
@@ -84,16 +95,12 @@ export class DBClass {
}
}
if (sql === "BEGIN;") {
this.tp = new Promise((resolve, reject) => {
this.tpResolver = { resolve, reject };
});
}
if (sql === "COMMIT;" && this.tp) {
await this.instance.exec(sql);
this.tpResolver.resolve();
this.tp = null;
if (this.tp.length > 0) {
const { resolve } = this.tpResolver.shift();
resolve();
}
return;
}
return await this.instance.exec(sql);