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

DELETE query will not run locally #2

Open
barnaclebarnes opened this issue Nov 28, 2024 · 2 comments
Open

DELETE query will not run locally #2

barnaclebarnes opened this issue Nov 28, 2024 · 2 comments

Comments

@barnaclebarnes
Copy link

The code below when run against a remote database will run delete queries. When run locally it will not.

# Remote DB
# Replace environment variables with your own values
@db = Libsql::Database.new(url: ENV.fetch("TURSO_DATABASE_URL"), auth_token: ENV.fetch("TURSO_AUTH_TOKEN"))
project_id = 1; key_type = "test"; key = "test"

@db.connect do |conn|
  sql = <<~SQL.squish
    CREATE TABLE IF NOT EXISTS keys(
      project_id INTEGER NOT NULL,
      key_type TEXT NOT NULL,
      key TEXT NOT NULL
    );
  SQL
  conn.query(sql)
end

@db.connect do |conn|
  sql = "INSERT INTO keys(`project_id`, `key_type`, `key`) VALUES(?, ?, ?);"
  conn.query(sql, [project_id, key_type, key])
end

@db.connect { |conn| conn.query("SELECT COUNT(*) FROM keys;").first.first }
# Returns 1

@db.connect do |conn|
  sql = "DELETE FROM `keys` WHERE `project_id` = ? AND `key_type` = ? AND `key` = ?"
  conn.query(sql, [project_id, key_type, key])
end

@db.connect { |conn| conn.query("SELECT COUNT(*) FROM keys;").first.first }
# returns 0

# Local DB
@db = Libsql::Database.new(path: Rails.root.join("turso.db").to_s)
project_id = 1; key_type = "test"; key = "test"

@db.connect do |conn|
  sql = <<~SQL.squish
    CREATE TABLE IF NOT EXISTS keys(
      project_id INTEGER NOT NULL,
      key_type TEXT NOT NULL,
      key TEXT NOT NULL
    );
  SQL
  conn.query(sql)
end

@db.connect do |conn|
  sql = "INSERT INTO keys(`project_id`, `key_type`, `key`) VALUES(?, ?, ?);"
  conn.query(sql, [project_id, key_type, key])
end

@db.connect { |conn| conn.query("SELECT COUNT(*) FROM keys;").first.first }
# Returns 1

@db.connect do |conn|
  sql = "DELETE FROM `keys` WHERE `project_id` = ? AND `key_type` = ? AND `key` = ?"
  conn.query(sql, [project_id, key_type, key])
end

@db.connect { |conn| conn.query("SELECT COUNT(*) FROM keys;").first.first }
# returns 1
@levydsa
Copy link
Collaborator

levydsa commented Dec 3, 2024

I was able to reproduce this behavior. I suspect a error is happening on the delete query, but something is preventing it from being thrown up to Ruby from libsql-c. In the mean time, I think that enabling WAL mode should fix it: conn.execute_batch('PRAGMA journal_mode=WAL')

@barnaclebarnes
Copy link
Author

@levydsa The workaround seems to be working. Cheers.

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

No branches or pull requests

2 participants