mirror of
https://pagure.io/fm-orchestrator.git
synced 2026-04-23 18:21:42 +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:
@@ -119,6 +119,9 @@ class AbstractQueryableBuildAPI(MethodView):
|
||||
elif short_flag == "true" or short_flag == "1":
|
||||
if hasattr(p_query.items[0], "short_json"):
|
||||
json_func_name = "short_json"
|
||||
if json_func_name == "json" or json_func_name == "extended_json":
|
||||
# Only ModuleBuild.json and ModuleBuild.extended_json has argument db_session
|
||||
json_func_kwargs["db_session"] = db.session
|
||||
json_data["items"] = [
|
||||
getattr(item, json_func_name)(**json_func_kwargs) for item in p_query.items
|
||||
]
|
||||
@@ -135,6 +138,9 @@ class AbstractQueryableBuildAPI(MethodView):
|
||||
elif short_flag == "true" or short_flag == "1":
|
||||
if getattr(instance, "short_json", None):
|
||||
json_func_name = "short_json"
|
||||
if json_func_name == "json" or json_func_name == "extended_json":
|
||||
# Only ModuleBuild.json and ModuleBuild.extended_json has argument db_session
|
||||
json_func_kwargs["db_session"] = db.session
|
||||
return jsonify(getattr(instance, json_func_name)(**json_func_kwargs)), 200
|
||||
else:
|
||||
raise NotFound("No such %s found." % self.kind)
|
||||
@@ -177,9 +183,9 @@ class ModuleBuildAPI(AbstractQueryableBuildAPI):
|
||||
modules = handler.post()
|
||||
if api_version == 1:
|
||||
# Only show the first module build for backwards-compatibility
|
||||
rv = modules[0].extended_json(True, api_version)
|
||||
rv = modules[0].extended_json(db.session, True, api_version)
|
||||
else:
|
||||
rv = [module.extended_json(True, api_version) for module in modules]
|
||||
rv = [module.extended_json(db.session, True, api_version) for module in modules]
|
||||
return jsonify(rv), 201
|
||||
|
||||
@validate_api_version()
|
||||
@@ -217,14 +223,15 @@ class ModuleBuildAPI(AbstractQueryableBuildAPI):
|
||||
raise Forbidden("You can't cancel a failed module")
|
||||
|
||||
if r["state"] == "failed" or r["state"] == str(models.BUILD_STATES["failed"]):
|
||||
module.transition(conf, models.BUILD_STATES["failed"], "Canceled by %s." % username)
|
||||
module.transition(
|
||||
db.session, conf, models.BUILD_STATES["failed"], "Canceled by %s." % username)
|
||||
else:
|
||||
log.error('The provided state change of "{}" is not supported'.format(r["state"]))
|
||||
raise ValidationError("The provided state change is not supported")
|
||||
db.session.add(module)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify(module.extended_json(True, api_version)), 200
|
||||
return jsonify(module.extended_json(db.session, True, api_version)), 200
|
||||
|
||||
|
||||
class AboutAPI(MethodView):
|
||||
@@ -291,7 +298,10 @@ class ImportModuleAPI(MethodView):
|
||||
|
||||
mmd = get_mmd_from_scm(handler.data["scmurl"])
|
||||
build, messages = import_mmd(db.session, mmd)
|
||||
json_data = {"module": build.json(show_tasks=False), "messages": messages}
|
||||
json_data = {
|
||||
"module": build.json(db.session, show_tasks=False),
|
||||
"messages": messages
|
||||
}
|
||||
|
||||
# return 201 Created if we reach this point
|
||||
return jsonify(json_data), 201
|
||||
@@ -442,7 +452,8 @@ class SCMHandler(BaseHandler):
|
||||
self.validate_optional_params()
|
||||
|
||||
def post(self):
|
||||
return submit_module_build_from_scm(self.username, self.data, allow_local_url=False)
|
||||
return submit_module_build_from_scm(
|
||||
db.session, self.username, self.data, allow_local_url=False)
|
||||
|
||||
|
||||
class YAMLFileHandler(BaseHandler):
|
||||
@@ -467,7 +478,8 @@ class YAMLFileHandler(BaseHandler):
|
||||
handle.filename = self.data["module_name"]
|
||||
else:
|
||||
handle = request.files["yaml"]
|
||||
return submit_module_build_from_yaml(self.username, handle, self.data)
|
||||
return submit_module_build_from_yaml(
|
||||
db.session, self.username, handle, self.data)
|
||||
|
||||
|
||||
def _dict_from_request(request):
|
||||
|
||||
Reference in New Issue
Block a user