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

Commit behavior is different between local and remote #27

Open
penberg opened this issue Dec 6, 2023 · 3 comments
Open

Commit behavior is different between local and remote #27

penberg opened this issue Dec 6, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@penberg
Copy link
Contributor

penberg commented Dec 6, 2023

Python SQLite interface expects caller to call commit() explicitly but @avinassh points out this does not work with remote databases:

ValueError: Hrana: `stream error: `StreamResponseError { error: Error { message: "SQLite error: cannot commit - no transaction is active" } }``

I am guessing the remote protocol semantics with is_autocommit() are wrong in libsql crate.

@penberg penberg added the bug Something isn't working label Dec 6, 2023
@avinassh
Copy link
Member

avinassh commented Dec 6, 2023

here is a simple reproducer:

import os

import libsql_experimental as libsql

print(F"connecting to {os.getenv('LIBSQL_URL')}")
conn = libsql.connect(database=os.getenv('LIBSQL_URL'), sync_url=os.getenv("LIBSQL_URL"),
                      auth_token=os.getenv("LIBSQL_AUTH_TOKEN"))
conn.execute("CREATE TABLE IF NOT EXISTS users_sync (id INTEGER);")
conn.execute("INSERT INTO users_sync(id) VALUES (10);")
conn.commit()

print(conn.execute("select * from users_sync").fetchall())

@andybarilla
Copy link

I'm getting the same error in Go. Some more info based off the below code:

  1. If you remove the SELECT * FROM users_sync chunk of code it works fine. And it returns the proper results if you move that SELECT out of the transaction.
  2. It works fine against a sqld image running in Docker just fine.
package main

import (
	"context"
	"database/sql"
	"fmt"
	"os"

	_ "github.com/tursodatabase/libsql-client-go/libsql"
)

func main() {
	db, err := sql.Open("libsql", os.Getenv("LIBSQL_URL"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to open db %s", err)
		os.Exit(1)
	}
	defer db.Close()

	tx, err := db.BeginTx(context.Background(), &sql.TxOptions{})
	if err != nil {
		panic(err)
	}

	_, err = tx.Exec("CREATE TABLE IF NOT EXISTS users_sync (id INTEGER)")
	if err != nil {
		panic(err)
	}

	_, err = tx.Exec("INSERT INTO users_sync(id) VALUES (10)")
	if err != nil {
		panic(err)
	}

	rows, err := tx.Query("SELECT * FROM users_sync")
	if err != nil {
		panic(err)
	}
	defer rows.Close()

	for rows.Next() {
		var id int

		if err := rows.Scan(&id); err != nil {
			fmt.Println("Error scanning row:", err)
			return
		}

		fmt.Println("id:", id)
	}

	err = tx.Commit()
	if err != nil {
		panic(err)
	}
}

@andybarilla
Copy link

And I confirmed with my project code where I'm getting this during my account provisioning routine that if I remove all of the selects I don't get this error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants