Reputation: 709
I want to use my development database during testing of my flask app (it contains oauth tokens that I can't get during testing). I was hoping to be able to copy the disk based development database to memory and use that for testing so that it did not alter the development db.
so here is what I have done so far:
in my app when testing is selected use:
SQLALCHEMY_DATABASE_URI = "sqlite:///:memory:"
Now I wish to copy my existing db into this temporary memory-based db. I have searched other answers but the closest I come is:
# open existing db
old_db = sqlite3.connect('app.db')
# get the connection to the in memory db
connection = db.get_engine().raw_connection()
# copy old_db into memory
old_db.backup(connection)
but this results in
backup() argument 1 must be sqlite3.Connection, not _ConnectionFairy
Is there a way to use the connection like this to achieve my desired aim?
Thank you
Martyn
Upvotes: 0
Views: 886
Reputation: 709
If anyone else comes to this here's what I missed - the raw_connection() object needs to be turned into an sqlite connection by:
connection = db.get_engine().raw_connection().connection
This the full code:
# open existing db
old_db = sqlite3.connect('app.db')
# get the connection to the in memory db
conn = db.get_engine().raw_connection().connection
# copy old_db into memory
old_db.backup(conn)
Martyn
Upvotes: 1