Skip to content

Commit 04e5ec4

Browse files
committed
sql en 2 partes
1 parent 8a17635 commit 04e5ec4

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

curation-pipeline/src/components/steps/Step7CurationInfo.jsx

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,51 @@ function getStep5ForSite(step5Data, site) {
5252
return step5Data?.annotations?.[site] || null;
5353
}
5454

55+
/**
56+
* Split a big SQL string into exactly 2 parts, cutting at a safe boundary.
57+
* We cut near the middle, but always at the last "\n\n" before the target.
58+
* Each part gets its own BEGIN/COMMIT wrapper, so it can run independently.
59+
*
60+
* NOTE: This does NOT guarantee semantic correctness for all DB schemas,
61+
* but it typically works if your SQL uses INSERT/UPDATE with SELECT subqueries.
62+
*/
63+
function splitSqlIntoTwoTransactions(sqlString) {
64+
const s = String(sqlString || "").trim();
65+
if (!s) return ["", ""];
66+
67+
// Remove outer wrappers if present (we'll re-add per chunk).
68+
let body = s
69+
.replace(/^PRAGMA foreign_keys\s*=\s*ON;\s*/i, "")
70+
.replace(/^BEGIN TRANSACTION;\s*/i, "")
71+
.replace(/\s*COMMIT;\s*$/i, "")
72+
.trim();
73+
74+
// If it's already small, just send as one.
75+
if (body.length < 12000) {
76+
const one = ["PRAGMA foreign_keys = ON;", "BEGIN TRANSACTION;", body, "COMMIT;"].join("\n");
77+
return [one, ""];
78+
}
79+
80+
// Target midpoint and try to split on a blank line boundary.
81+
const mid = Math.floor(body.length / 2);
82+
const left = body.slice(0, mid);
83+
const splitAt = left.lastIndexOf("\n\n");
84+
85+
// Fallback if no double-newline exists.
86+
const cut = splitAt > 0 ? splitAt : left.lastIndexOf("\n");
87+
const idx = cut > 0 ? cut : mid;
88+
89+
const part1Body = body.slice(0, idx).trim();
90+
const part2Body = body.slice(idx).trim();
91+
92+
const part1 = ["PRAGMA foreign_keys = ON;", "BEGIN TRANSACTION;", part1Body, "COMMIT;"].join("\n");
93+
const part2 = part2Body
94+
? ["PRAGMA foreign_keys = ON;", "BEGIN TRANSACTION;", part2Body, "COMMIT;"].join("\n")
95+
: "";
96+
97+
return [part1, part2];
98+
}
99+
55100
export default function Step7CurationInfo() {
56101
const {
57102
publication,
@@ -580,10 +625,17 @@ WHERE ${geneIdExpr} IS NOT NULL;
580625

581626
try {
582627
const sqlString = buildFullSql();
628+
const [part1, part2] = splitSqlIntoTwoTransactions(sqlString);
583629

584-
await dispatchWorkflow({
585-
inputs: { queries: sqlString },
586-
});
630+
// Enviar parte 1
631+
if (part1) {
632+
await dispatchWorkflow({ inputs: { queries: part1 } });
633+
}
634+
635+
// Enviar parte 2 (si existe)
636+
if (part2) {
637+
await dispatchWorkflow({ inputs: { queries: part2 } });
638+
}
587639

588640
setStep7Data({
589641
revisionReason,
@@ -592,7 +644,7 @@ WHERE ${geneIdExpr} IS NOT NULL;
592644
submittedAt: new Date().toISOString(),
593645
});
594646

595-
setMsg("✅ Submit OK: inserts/updates executed successfully.");
647+
setMsg("✅ Submit OK: inserts/updates executed successfully (2 chunks).");
596648
} catch (e) {
597649
console.error("Submit error full:", e);
598650
console.error("Submit error payload:", e?.payload);

0 commit comments

Comments
 (0)