Skip to content

Commit

Permalink
fix: order of table creations
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed Jan 27, 2024
1 parent 4337252 commit 4b6620e
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions src/lib/sqlite2sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,51 @@ export async function sqlite2sql(sqlite3: ArrayBuffer): Promise<string> {
let sql = "";

// list all tables
const tables = db.exec("SELECT name, sql FROM sqlite_master WHERE type='table';")[0];
console.log({ tables });
if (tables) {
tables.values = tables.values.filter((table) => {
const tables_res = db.exec("SELECT name, sql FROM sqlite_master WHERE type='table';")[0];
console.log({ tables_res });
const tables: [string, string][] = [];
if (tables_res) {
const unresolved = new Set<string>();
const t = tables_res.values.filter((table) => {
const name = table[0] as string;
return (
!name.startsWith("sqlite_") && !name.startsWith("d1_") && !name.startsWith("_cf_")
);
});
for (const table of tables.values) {
if (table[1]) {
sql += table[1] + "\n";
for (const table of t) {
unresolved.add(table[0] as string);
}

// table order
let resolving = true;
while (resolving) {
resolving = false;
for (const table of t) {
const name = table[0] as string;
const sql = table[1] as string;
// if sql incults any of the unresolved tables, skip it
if (
Array.from(unresolved)
.filter((x) => name !== x)
.some((name) => sql.includes(name))
) {
continue;
}
// otherwise, add it to the resolved list
tables.push(table as [string, string]);
unresolved.delete(table[0] as string);
t.splice(t.indexOf(table), 1);
resolving = true;
}
}
for (const table of unresolved) {
console.log(`Unresolved table ${table}`);
tables.push([table, (t.find((t) => t[0] === table)?.[1] || "") as string]);
}

for (const stmt of tables) {
sql += stmt[1] + "\n";
}
}

// list all indexes
Expand Down Expand Up @@ -71,7 +102,7 @@ export async function sqlite2sql(sqlite3: ArrayBuffer): Promise<string> {

// add all data
if (tables) {
for (const table of tables.values) {
for (const table of tables) {
const name = table[0];
const rows = db.exec(`SELECT * FROM \`${name}\`;`)[0];
if (!rows) {
Expand Down

0 comments on commit 4b6620e

Please sign in to comment.