mirror of
https://pagure.io/fm-orchestrator.git
synced 2026-11-30 16:14:44 +08:00
Separate use of database sessions
This patch separates the use of database session in different MBS components and do not mix them together. In general, MBS components could be separated as the REST API (implemented based on Flask) and non-REST API including the backend build workflow (implemented as a fedmsg consumer on top of fedmsg-hub and running independently) and library shared by them. As a result, there are two kind of database session used in MBS, one is created and managed by Flask-SQLAlchemy, and another one is created from SQLAclhemy Session API directly. The goal of this patch is to make ensure session object is used properly in the right place. All the changes follow these rules: * REST API related code uses the session object db.session created and managed by Flask-SQLAlchemy. * Non-REST API related code uses the session object created with SQLAlchemy Session API. Function make_db_session does that. * Shared code does not created a new session object as much as possible. Instead, it accepts an argument db_session. The first two rules are applicable to tests as well. Major changes: * Switch tests back to run with a file-based SQLite database. * make_session is renamed to make_db_session and SQLAlchemy connection pool options are applied for PostgreSQL backend. * Frontend Flask related code uses db.session * Shared code by REST API and backend build workflow accepts SQLAlchemy session object as an argument. For example, resolver class is constructed with a database session, and some functions accepts an argument for database session. * Build workflow related code use session object returned from make_db_session and ensure db.session is not used. * Only tests for views use db.session, and other tests use db_session fixture to access database. * All argument name session, that is for database access, are renamed to db_session. * Functions model_tests_init_data, reuse_component_init_data and reuse_shared_userspace_init_data, which creates fixture data for tests, are converted into pytest fixtures from original function called inside setup_method or a test method. The reason of this conversion is to use fixture ``db_session`` rather than create a new one. That would also benefit the whole test suite to reduce the number of SQLAlchemy session objects. Signed-off-by: Chenxiong Qi <cqi@redhat.com>
This commit is contained in:
@@ -161,13 +161,15 @@ class KojiModuleBuilder(GenericBuilder):
|
||||
region = dogpile.cache.make_region().configure("dogpile.cache.memory")
|
||||
|
||||
@module_build_service.utils.validate_koji_tag("tag_name")
|
||||
def __init__(self, owner, module, config, tag_name, components):
|
||||
def __init__(self, db_session, owner, module, config, tag_name, components):
|
||||
"""
|
||||
:param db_session: SQLAlchemy session object.
|
||||
:param owner: a string representing who kicked off the builds
|
||||
:param module: module_build_service.models.ModuleBuild instance.
|
||||
:param config: module_build_service.config.Config instance
|
||||
:param tag_name: name of tag for given module
|
||||
"""
|
||||
self.db_session = db_session
|
||||
self.owner = owner
|
||||
self.module_str = module.name
|
||||
self.module = module
|
||||
@@ -248,7 +250,7 @@ class KojiModuleBuilder(GenericBuilder):
|
||||
"""
|
||||
# filtered_rpms will contain the NVRs of non-reusable component's RPMs
|
||||
filtered_rpms = list(set(filtered_rpms_of_dep))
|
||||
with models.make_session(conf) as db_session:
|
||||
with models.make_db_session(conf) as db_session:
|
||||
# Get a module build that can be reused, which will likely be the
|
||||
# build dep that is used since it relies on itself
|
||||
reusable_module = get_reusable_module(db_session, module_build)
|
||||
@@ -739,7 +741,7 @@ class KojiModuleBuilder(GenericBuilder):
|
||||
# If the build cannot be found in the tags, it may be untagged as a result
|
||||
# of some earlier inconsistent situation. Let's find the task_info
|
||||
# based on the list of untagged builds
|
||||
release = module_build_service.utils.get_rpm_release(self.module)
|
||||
release = module_build_service.utils.get_rpm_release(self.db_session, self.module)
|
||||
untagged = self.koji_session.untaggedBuilds(name=component_build.package)
|
||||
for untagged_build in untagged:
|
||||
if untagged_build["release"].endswith(release):
|
||||
@@ -1272,7 +1274,7 @@ class KojiModuleBuilder(GenericBuilder):
|
||||
:param Modulemd mmd: Modulemd to get the built RPMs from.
|
||||
:return: list of NVRs
|
||||
"""
|
||||
with models.make_session(conf) as db_session:
|
||||
with models.make_db_session(conf) as db_session:
|
||||
build = models.ModuleBuild.get_build_from_nsvc(
|
||||
db_session,
|
||||
mmd.get_module_name(),
|
||||
|
||||
Reference in New Issue
Block a user