duckdb_engine icon indicating copy to clipboard operation
duckdb_engine copied to clipboard

fix: handle `set_isolation_level` gracefully

Open caiohamamura opened this issue 8 months ago • 4 comments

Whenever the sqlachemy tries to use set_isolation_level without it being defined it will cast object has no attribute 'set_isolation_level' instead of gracefully handling the error by explicitly raising NotImplementedError

caiohamamura avatar Apr 15 '25 16:04 caiohamamura

hey there, thanks for submitting this PR! any chance we could see a test demonstrating the issue and proving that it's fixed?

Mause avatar Sep 28 '25 12:09 Mause

You can check this by using:

from sqlalchemy import create_engine

eng = create_engine("duckdb:///:memory:", execution_options={"isolation_level": "AUTOCOMMIT"})
try:
    conn = eng.connect()
except NotImplementedError:
    print('Expected not implemented error raised!')
except Exception as e:
    raise(e)

This will allow other libraries using duckdb-engine to more gracefully handle expected errors, since engine PGDialect implements set_isolation_level, but duckdb does not.

caiohamamura avatar Sep 29 '25 21:09 caiohamamura

I rebased against main for merging!

caiohamamura avatar Sep 29 '25 21:09 caiohamamura

Or if you want to wrap it within unittest for automated tests:

from sqlalchemy import create_engine
import unittest

class TestDuckDBDialect(unittest.TestCase):
    def test_set_isolation_level_not_supported(self):
        eng = create_engine("duckdb:///:memory:", execution_options={"isolation_level": "AUTOCOMMIT"})
        with self.assertRaises(NotImplementedError): 
            eng.connect()
            eng.dispose()

if __name__ == '__main__':
    unittest.main()

caiohamamura avatar Nov 04 '25 14:11 caiohamamura