From dd78bcb977efdb6caed17baf1bae17a5737464b6 Mon Sep 17 00:00:00 2001 From: Caio Carrara Date: Wed, 16 Sep 2015 14:58:07 -0300 Subject: [PATCH] Add support to more than one db provider for tests As db api worked with one EngineFacade singleton, the migrations tests could not run successfully to MySQL and PostgreSQL database. The migrations started being applied to the MySQL database (and EngineFacade was instanciated based on it) and after to PostgresSQL, but EngineFacade was not instantiated anymore, keeping with the MySQL engine. In order to fix that issue, this commit implement a way so that the db api works with one EngineFacade by database vendor. Co-Authored-By: Moises Trovo Change-Id: I07898bd963c08e7a2eff946b8f0c0886f2143474 --- subunit2sql/db/api.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/subunit2sql/db/api.py b/subunit2sql/db/api.py index 644cb46..2f4616c 100644 --- a/subunit2sql/db/api.py +++ b/subunit2sql/db/api.py @@ -18,6 +18,7 @@ from oslo_config import cfg from oslo_db.sqlalchemy import session as db_session from oslo_db.sqlalchemy import utils as db_utils import sqlalchemy +from sqlalchemy.engine.url import make_url from subunit2sql.db import models from subunit2sql import exceptions @@ -27,16 +28,20 @@ CONF = cfg.CONF DAY_SECONDS = 60 * 60 * 24 -_FACADE = None +_facades = {} def _create_facade_lazily(): - global _FACADE - if _FACADE is None: - _FACADE = db_session.EngineFacade( + global _facades + db_url = make_url(CONF.database.connection) + db_backend = db_url.get_backend_name() + facade = _facades.get(db_backend) + if facade is None: + facade = db_session.EngineFacade( CONF.database.connection, **dict(CONF.database.iteritems())) - return _FACADE + _facades[db_backend] = facade + return facade def get_session(autocommit=True, expire_on_commit=False):