Skip to content

Commit

Permalink
Merge 09f5e3e into fecb567
Browse files Browse the repository at this point in the history
  • Loading branch information
mvalik committed May 30, 2023
2 parents fecb567 + 09f5e3e commit e7be8c9
Show file tree
Hide file tree
Showing 9 changed files with 1,092 additions and 1,118 deletions.
15 changes: 0 additions & 15 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@
import os
import sys

# Hot-fix broken sphinxcontrib.httpdomain
# See: https://github.com/sphinx-contrib/httpdomain/pull/54
try:
import pkg_resources

if pkg_resources.require("sphinxcontrib_httpdomain")[0].version == '1.7.0':
from sphinxcontrib import httpdomain

for typed_field in httpdomain.HTTPResource.doc_field_types:
typed_field.typerolename = None
for object_type in httpdomain.HTTPDomain.object_types.values():
object_type.roles = tuple(r for r in object_type.roles if r != 'obj')
except Exception:
pass

# This will cause the Flask application to be created with development configs
os.environ['DEV'] = 'true'

Expand Down
2,149 changes: 1,064 additions & 1,085 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ itsdangerous = {version = "==2.0.1", optional = true}
gssapi = "^1.8.2"
python-ldap = "^3.4.3"

SQLAlchemy = "^1.4.45"
SQLAlchemy = "^2.0"
psycopg2-binary = "^2.9.6"

gunicorn = "^20.1.0"
Expand Down
13 changes: 11 additions & 2 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from mock import ANY, call, patch

from waiverdb import app, config
from flask_sqlalchemy import SignallingSession
from waiverdb.models import db
import sqlalchemy


class DisabledMessagingConfig(config.Config):
Expand Down Expand Up @@ -36,4 +37,12 @@ def test_enabled_messaging_should_register_events(mock_listen):
calls = [
c for c in mock_listen.mock_calls if c == call(ANY, ANY, app.publish_new_waiver)
]
assert calls == [call(SignallingSession, "after_commit", app.publish_new_waiver)]
assert calls == [call(db.session, "after_commit", app.publish_new_waiver)]


def test_sqlalchemy_version():
"""
Tests whether SQLAlchemy version is 2
:return:
"""
assert sqlalchemy.__version__.startswith('2')
4 changes: 2 additions & 2 deletions waiverdb/api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def get(self, query: CreateWaiver):
class WaiverResource(Resource):
@jsonp
@marshal_with(waiver_fields)
def get(self, waiver_id):
def get(self, waiver_id) -> Waiver:
"""
Get a single waiver by waiver ID.
Expand All @@ -438,7 +438,7 @@ def get(self, waiver_id):
:statuscode 404: No waiver exists with that ID.
"""
try:
return Waiver.query.get_or_404(waiver_id)
return db.get_or_404(Waiver, waiver_id)
except Exception as NotFound:
raise type(NotFound)('Waiver not found')

Expand Down
14 changes: 6 additions & 8 deletions waiverdb/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from flask import Flask, current_app, send_from_directory
from flask_cors import CORS
from flask_migrate import Migrate
from sqlalchemy import event
from sqlalchemy import event, text
from sqlalchemy.exc import ProgrammingError
import requests

Expand Down Expand Up @@ -119,7 +119,8 @@ def create_app(config_obj=None):
register_event_handlers(app)

# initialize DB event listeners from the monitor module
app.before_first_request(db_hook_event_listeners)
with app.app_context():
db_hook_event_listeners()

enable_cors(app)

Expand All @@ -135,12 +136,12 @@ def healthcheck():
Returns a 200 response if the application is alive and able to serve requests.
"""
try:
db.session.execute("SELECT 1 FROM waiver LIMIT 0").fetchall()
db.session.execute(text("SELECT 1 FROM waiver LIMIT 0")).fetchall()
except ProgrammingError:
current_app.logger.exception('Healthcheck failed on DB query.')
raise RuntimeError('Unable to communicate with database.')

return ('Health check OK', 200, [('Content-Type', 'text/plain')])
return 'Health check OK', 200, [('Content-Type', 'text/plain')]


@oidc.require_login
Expand All @@ -160,10 +161,7 @@ def register_event_handlers(app):
attached as the ``session`` attribute.
"""
if app.config['MESSAGE_BUS_PUBLISH']:
# A workaround for https://github.com/mitsuhiko/flask-sqlalchemy/pull/364
# can be removed after python-flask-sqlalchemy is upgraded to 2.2
from flask_sqlalchemy import SignallingSession
event.listen(SignallingSession, 'after_commit', publish_new_waiver)
event.listen(db.session, 'after_commit', publish_new_waiver)


def favicon():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
from sqlalchemy import Text, text
from sqlalchemy.dialects.postgresql import JSON
from waiverdb.models.base import json_serializer
from waiverdb.models import db


def upgrade():
# Re-serialize all subject values to ensure they match the new, consistent
# serialization we are using.
connection = op.get_bind()
for row in connection.execute('SELECT id, subject FROM waiver'):
for row in db.session.execute(text('SELECT id, subject FROM waiver')):
fixed_subject = json_serializer(json.loads(row['subject']))
connection.execute(text('UPDATE waiver SET subject = :subject WHERE id = :id'),
db.session.execute(text('UPDATE waiver SET subject = :subject WHERE id = :id'),
subject=fixed_subject, id=row['id'])

op.drop_index('ix_waiver_subject')
Expand Down
2 changes: 2 additions & 0 deletions waiverdb/models/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ class TestSubject(BaseModel):
item: Optional[str]
original_spec_nvr: Optional[str]
productmd_compose_id: Optional[str] = Field(alias='productmd.compose.id', default=None)
__test__ = False # to tell the PyTest that this is not a test class


class TestResult(BaseModel):
testcase: str
subject: TestSubject
__test__ = False # to tell the PyTest that this is not a test class


class CreateWaiver(BaseModel):
Expand Down
6 changes: 3 additions & 3 deletions waiverdb/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
# Service-specific imports


if not os.environ.get('prometheus_multiproc_dir'):
os.environ.setdefault('prometheus_multiproc_dir', tempfile.mkdtemp())
if not os.environ.get('PROMETHEUS_MULTIPROC_DIR'):
os.environ.setdefault('PROMETHEUS_MULTIPROC_DIR', tempfile.mkdtemp())
registry = CollectorRegistry()
ProcessCollector(registry=registry)
multiprocess.MultiProcessCollector(registry)
Expand Down Expand Up @@ -75,7 +75,7 @@ def db_hook_event_listeners(target=None):
target = db.engine

@event.listens_for(target, 'engine_connect')
def receive_engine_connect(conn, branch):
def receive_engine_connect(conn):
db_engine_connect_counter.inc()

@event.listens_for(target, 'handle_error')
Expand Down

0 comments on commit e7be8c9

Please sign in to comment.