Fix issue with SQLite transactions (#5919)
This commit is contained in:
parent
6d08cf2757
commit
6113aefde0
3 changed files with 36 additions and 22 deletions
|
|
@ -40,13 +40,14 @@ export const addIssuesBulk = async (issues: any, batchSize = 100) => {
|
||||||
export const deleteIssueFromLocal = async (issue_id: any) => {
|
export const deleteIssueFromLocal = async (issue_id: any) => {
|
||||||
if (!rootStore.user.localDBEnabled || !persistence.db) return;
|
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}'`;
|
const deleteMetaQuery = `delete from issue_meta where issue_id='${issue_id}'`;
|
||||||
|
|
||||||
persistence.db.exec("BEGIN;");
|
await persistence.db.exec("BEGIN;");
|
||||||
persistence.db.exec(deleteQuery);
|
|
||||||
persistence.db.exec(deleteMetaQuery);
|
await persistence.db.exec(deleteQuery);
|
||||||
persistence.db.exec("COMMIT;");
|
await persistence.db.exec(deleteMetaQuery);
|
||||||
|
await persistence.db.exec("COMMIT;");
|
||||||
};
|
};
|
||||||
// @todo: Update deletes the issue description from local. Implement a separate update.
|
// @todo: Update deletes the issue description from local. Implement a separate update.
|
||||||
export const updateIssue = async (issue: TIssue & { is_local_update: number }) => {
|
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;
|
const issue_id = issue.id;
|
||||||
// delete the issue and its meta data
|
// delete the issue and its meta data
|
||||||
await deleteIssueFromLocal(issue_id);
|
await deleteIssueFromLocal(issue_id);
|
||||||
addIssue(issue);
|
await addIssue(issue);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const syncDeletesToLocal = async (workspaceId: string, projectId: string, queries: any) => {
|
export const syncDeletesToLocal = async (workspaceId: string, projectId: string, queries: any) => {
|
||||||
|
|
@ -98,27 +99,33 @@ const stageIssueInserts = async (issue: any) => {
|
||||||
.join(", ");
|
.join(", ");
|
||||||
|
|
||||||
const query = `INSERT OR REPLACE INTO issues (${columns}) VALUES (${values});`;
|
const query = `INSERT OR REPLACE INTO issues (${columns}) VALUES (${values});`;
|
||||||
persistence.db.exec(query);
|
await persistence.db.exec(query);
|
||||||
|
|
||||||
await persistence.db.exec({
|
await persistence.db.exec({
|
||||||
sql: `DELETE from issue_meta where issue_id='${issue_id}'`,
|
sql: `DELETE from issue_meta where issue_id='${issue_id}'`,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const metaPromises: Promise<any>[] = [];
|
||||||
|
|
||||||
ARRAY_FIELDS.forEach((field) => {
|
ARRAY_FIELDS.forEach((field) => {
|
||||||
const values = issue[field];
|
const values = issue[field];
|
||||||
if (values && values.length) {
|
if (values && values.length) {
|
||||||
values.forEach((val: any) => {
|
values.forEach((val: any) => {
|
||||||
persistence.db.exec({
|
const p = persistence.db.exec({
|
||||||
sql: `INSERT OR REPLACE into issue_meta(issue_id,key,value) values (?,?,?) `,
|
sql: `INSERT OR REPLACE into issue_meta(issue_id,key,value) values (?,?,?) `,
|
||||||
bind: [issue_id, field, val],
|
bind: [issue_id, field, val],
|
||||||
});
|
});
|
||||||
|
metaPromises.push(p);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Added for empty fields?
|
// Added for empty fields?
|
||||||
persistence.db.exec({
|
const p = persistence.db.exec({
|
||||||
sql: `INSERT OR REPLACE into issue_meta(issue_id,key,value) values (?,?,?) `,
|
sql: `INSERT OR REPLACE into issue_meta(issue_id,key,value) values (?,?,?) `,
|
||||||
bind: [issue_id, field, ""],
|
bind: [issue_id, field, ""],
|
||||||
});
|
});
|
||||||
|
metaPromises.push(p);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await Promise.all(metaPromises);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ export const updatePersistentLayer = async (issueIds: string | string[]) => {
|
||||||
"type_id",
|
"type_id",
|
||||||
"description_html",
|
"description_html",
|
||||||
]);
|
]);
|
||||||
updateIssue({ ...issuePartial, is_local_update: 1 });
|
await updateIssue({ ...issuePartial, is_local_update: 1 });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,8 @@ interface SQLiteInstance {
|
||||||
export class DBClass {
|
export class DBClass {
|
||||||
private instance: SQLiteInstance = {} as SQLiteInstance;
|
private instance: SQLiteInstance = {} as SQLiteInstance;
|
||||||
private sqlite3: any;
|
private sqlite3: any;
|
||||||
private tp: Promise<any> | null = null;
|
private tp: Promise<any>[] = [];
|
||||||
private tpResolver: any;
|
private tpResolver: any = [];
|
||||||
async init(dbName: string) {
|
async init(dbName: string) {
|
||||||
if (!dbName || typeof dbName !== "string") {
|
if (!dbName || typeof dbName !== "string") {
|
||||||
throw new Error("Invalid database name");
|
throw new Error("Invalid database name");
|
||||||
|
|
@ -57,8 +57,19 @@ export class DBClass {
|
||||||
}
|
}
|
||||||
|
|
||||||
async exec(props: string | TQueryProps) {
|
async exec(props: string | TQueryProps) {
|
||||||
if (this.tp && props === "BEGIN;") {
|
if (props === "BEGIN;") {
|
||||||
await this.tp;
|
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[];
|
let sql: string, bind: any[];
|
||||||
if (typeof props === "string") {
|
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) {
|
if (sql === "COMMIT;" && this.tp) {
|
||||||
await this.instance.exec(sql);
|
await this.instance.exec(sql);
|
||||||
this.tpResolver.resolve();
|
if (this.tp.length > 0) {
|
||||||
this.tp = null;
|
const { resolve } = this.tpResolver.shift();
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return await this.instance.exec(sql);
|
return await this.instance.exec(sql);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue