I have a bit of a problem with my current flask + SQLAlchemy app. I create a database/engine instance that connects to database 'master'
which always exists in SQL server and after this, I create my test db and change the database of the existing engine from 'master'
to this new database. My problem is that I don't seem to be doing the 'repointing' of the databases for this engine correctly.
def create_db(self):
url = copy(self.db.engine.url) # current url points to database 'master'
engine = create_engine(url, connect_args={'autocommit': True}, isolation_level='AUTOCOMMIT')
res = engine.execute(f"exec dbo.create_my_test_temp_db")
tempdbname = res.fetchone()[0] # this is the new DB the stored proc created for us
res.close()
engine.dispose()
self.db.engine.url.database = tempdbname # repoint to the new DB (doesn't work)
return tempdbname
I have to use the stored procedure dbo.create_my_test_temp_db
which is the server-side because it does some magic when creating the database. I change the database in the URL and the connection string for the engine does show the new database but all operations are executed against master.
How can I change the database for an existing engine to another database?