Hi!
On Wed, Oct 16, 2013 at 09:41:54AM +0100, "Maciej (Matchek) Blizi??ski" <ma...@op...> wrote:
> Hi guys,
>
> I'd like to use an in-memory sqlite database for testing, and I have a
> problem where I don't seem to be able to let go of a database which was
> once created. Here's a test that shows what I mean:
>
> class Foo(sqlobject.SQLObject):
> bar = sqlobject.UnicodeCol(length=250, unique=True)
>
> db_uri = 'sqlite:/:memory:'
> conn = sqlobject.connectionForURI(db_uri)
> sqlobject.sqlhub.processConnection = conn
> Foo.createTable()
> # All is good so far. Now let's drop the database.
> conn.close()
> del conn
> # From http://www.sqlite.org/inmemorydb.html:
> # "The database is automatically deleted and memory is reclaimed when the
> last connection to the database closes."
> # Let's create a fresh in-memory database.
> conn = sqlobject.connectionForURI(db_uri)
> sqlobject.sqlhub.processConnection = conn
> Foo.createTable()
> # Here, an exception is thrown:
> sqlobject.dberrors.OperationalError: table foo already exists
>
> I must have somehow gotten a connection to the same in-memory database that
> was created on the first call to connectionForURI(). After looking at
Yes. Actually, even worse than that - connectionForURI caches
SQLObject's connections by URIs (you get the same SQLObject's connection
with the same URI) and SQLiteConnection caches a DBAPI connection to the
in-memory DB.
> dbconnection.py, I came up with an invasive method:
>
> sqlobject.dbconnection.TheURIOpener.cachedURIs = {}
> conn = sqlobject.connectionForURI(db_uri)
> sqlobject.sqlhub.processConnection = conn
> Foo.createTable()
>
> It does what I need, but it doesn't seem like it's what SQLObject
> developers intended. Do you have any recommendations?
I don't. Do you want to propose an API? Or better a patch?
Oleg.
--
Oleg Broytman http://phdru.name/ ph...@ph...
Programmers don't die, they just GOSUB without RETURN.
|