Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In INDEXEDDB, alasql.promise fails to create multiple tables with sql in array, but works with sql as string #1641

Closed
arnemorken opened this issue Feb 18, 2023 · 8 comments

Comments

@arnemorken
Copy link

arnemorken commented Feb 18, 2023

When creating multiple tables with the CREATE statements in an array of strings, only the first (nonexisting) table is created.
When concatenating all the CREATE statements in the array into one string, all the tables are created.

In the first case, the code below will fail with

"alasql.min.js:48 Uncaught DOMException: Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found.
    at u.onsuccess (http://localhost/Prosjekter/testserver/wp-content/plugins/anyvista/view/data/alasql.min.js:48:108479)
u.onsuccess @ alasql.min.js:48"

If using LOCALSTORAGE instead of INDEXEDDB, both methods will work.

Code to reproduce (using AlaSQL v3.0.0 build: develop-4287740e):

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="./alasql.min.js"></script>
<script>
// Create and connect database
alasql.promise(
  "CREATE INDEXEDDB DATABASE IF NOT EXISTS alatest;"+
  "ATTACH INDEXEDDB DATABASE alatest;"+
  "USE alatest"
)
.then( function(res) {
  // Create tables
  return alasql.promise(
    /*
    // This works:
    "CREATE TABLE IF NOT EXISTS mytable1 ( myid STRING, myname STRING );\
     CREATE TABLE IF NOT EXISTS mytable2 ( myid STRING, myname STRING );",
    */
    // This should work too, but doesnt:
    [
    "CREATE TABLE IF NOT EXISTS mytable1 ( myid STRING, myname STRING )",
    "CREATE TABLE IF NOT EXISTS mytable2 ( myid STRING, myname STRING )",
    ]
  );
})
.then( function(res) {
  // Fill tables
  return alasql.promise(
    [
    "INSERT INTO mytable1 (myid,myname) VALUES ( '1', 'Mr. One' )",
    "INSERT INTO mytable2 (myid,myname) VALUES ( '2', 'Mr. Two' )", // Fails if using second CREATE method above
    ]
  );
})
.then( function(res) {
  return alasql.promise("SELECT * from mytable1");
})
.then( function(data1) {
  console.log("data1:");
  console.log(data1);
});
</script>
@ParardhaSarmah
Copy link

Hi. Can I work on this issue?

@dcode333
Copy link

dcode333 commented Apr 23, 2023

@mathiasrw Assign it please...will take a look into it..

@arnemorken
Copy link
Author

I think @mathiasrw or @agershun must assign people to work on an issue (?)

@mathiasrw
Copy link
Member

Happy to assign you :)

@arnemorken
Copy link
Author

No, not me, but the two other guys above - @ParardhaSarmah and/or @dcode333.

@Julias0
Copy link
Contributor

Julias0 commented Jun 11, 2023

Have added a PR for the fix. This problem is happening because of how the queries are run when passed in an array causing a race condition in how index db works (the version should be updated everytime we do a DDL query)

@mathiasrw
Copy link
Member

The reason is so simple when you tell it to us like this. Thank you!

@mathiasrw
Copy link
Member

Released as part of v4.0.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants