tursodatabase / libsql-experimental-python

libSQL API for Python (experimental)
108 stars 10 forks source link

bug: unable to make new insertions from the sync connection #15

Closed avinassh closed 11 months ago

avinassh commented 11 months ago

If I create a new connection which I also use for embedded replica, with such connection I am not able to make new inserts

sample script:

import os

import libsql_experimental as libsql

print(F"syncing with {os.getenv('LIBSQL_URL')}")
conn = libsql.connect("hello_sync.db", 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 (1);")
conn.sync()

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

this outputs:

[(1,)]

But when I inspect the same DB in turso shell, I don't see any row inserted. But I do see a new table created:

→  .tables
users_sync

→  select * from users_sync;
ID

library version: v0.0.19, also tried with #12 libsql-server: running in fly with extensions enabled, v0.22.4-1700159808

(internal slack thread)

penberg commented 11 months ago

Hey @avinassh, I don't think this is actually a libSQL bug. You get the same behavior with SQLite too because it defaults to autocommit disabled:

import sqlite3

conn = sqlite3.connect("hello.db")
conn.execute("CREATE TABLE IF NOT EXISTS users_sync (id INTEGER);")
conn.execute("INSERT INTO users_sync(id) VALUES (1);")
% python3 sqlite.py
% sqlite3 hello.db "SELECT * FROM users_sync"

You need to either pass isolation_level=None as an argument to connect() or do a conn.commit() after the insertion.

avinassh commented 11 months ago

Doing conn.commit() worked. Thank you!