From a5ec46f14cbadaaf3c6736f8b47d9c19e4b1680d Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 23 Apr 2023 21:49:53 +0100 Subject: [PATCH] Update database dependencies --- app/__init__.py | 4 ++-- app/blueprints/admin/audit.py | 2 +- app/blueprints/api/endpoints.py | 4 ++-- app/blueprints/api/tokens.py | 4 ++-- app/blueprints/packages/packages.py | 5 +++-- app/blueprints/packages/reviews.py | 2 +- app/blueprints/threads/__init__.py | 2 +- app/blueprints/todo/__init__.py | 2 +- app/tasks/usertasks.py | 3 ++- app/tests/integ/utils.py | 17 +++++++++-------- app/utils/models.py | 5 +++++ migrations/env.py | 7 +++++-- requirements.lock.txt | 8 ++++---- 13 files changed, 38 insertions(+), 27 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 2f429775..47627281 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -126,7 +126,7 @@ def check_for_ban(): models.db.session.commit() -from .utils import clearNotifications, is_safe_url +from .utils import clearNotifications, is_safe_url, create_session @app.before_request @@ -159,7 +159,7 @@ def get_locale(): locale = request.accept_languages.best_match(locales) if locale and current_user.is_authenticated: - with models.db.create_session({})() as new_session: + with create_session() as new_session: new_session.query(models.User) \ .filter(models.User.username == current_user.username) \ .update({"locale": locale}) diff --git a/app/blueprints/admin/audit.py b/app/blueprints/admin/audit.py index 284f6cbd..b40f1401 100644 --- a/app/blueprints/admin/audit.py +++ b/app/blueprints/admin/audit.py @@ -35,7 +35,7 @@ def audit(): abort(404) query = query.filter_by(causer=user) - pagination = query.paginate(page, num, True) + pagination = query.paginate(page=page, per_page=num) return render_template("admin/audit.html", log=pagination.items, pagination=pagination) diff --git a/app/blueprints/api/endpoints.py b/app/blueprints/api/endpoints.py index 00583e9d..73ce8aeb 100644 --- a/app/blueprints/api/endpoints.py +++ b/app/blueprints/api/endpoints.py @@ -448,7 +448,7 @@ def list_all_reviews(): if q: query = query.filter(PackageReview.thread.has(Thread.title.ilike(f"%{q}%"))) - pagination: flask_sqlalchemy.Pagination = query.paginate(page, num, True) + pagination: flask_sqlalchemy.Pagination = query.paginate(page=page, per_page=num) return jsonify({ "page": pagination.page, "per_page": pagination.per_page, @@ -601,7 +601,7 @@ def all_deps(): page = get_int_or_abort(request.args.get("page"), 1) num = min(get_int_or_abort(request.args.get("n"), 100), 300) - pagination: flask_sqlalchemy.Pagination = query.paginate(page, num, True) + pagination: flask_sqlalchemy.Pagination = query.paginate(page=page, per_page=num) return jsonify({ "page": pagination.page, "per_page": pagination.per_page, diff --git a/app/blueprints/api/tokens.py b/app/blueprints/api/tokens.py index acd737bb..c8edb19c 100644 --- a/app/blueprints/api/tokens.py +++ b/app/blueprints/api/tokens.py @@ -85,12 +85,12 @@ def create_edit_token(username, id=None): if form.validate_on_submit(): if is_new: token = APIToken() + db.session.add(token) token.owner = user token.access_token = randomString(32) form.populate_obj(token) - db.session.add(token) - db.session.commit() # save + db.session.commit() if is_new: # Store token so it can be shown in the edit page diff --git a/app/blueprints/packages/packages.py b/app/blueprints/packages/packages.py index 1a8ae885..7bafeca7 100644 --- a/app/blueprints/packages/packages.py +++ b/app/blueprints/packages/packages.py @@ -76,7 +76,7 @@ def list_all(): page = get_int_or_abort(request.args.get("page"), 1) num = min(40, get_int_or_abort(request.args.get("n"), 100)) - query = query.paginate(page, num, True) + query = query.paginate(page=page, per_page=num) search = request.args.get("q") type_name = request.args.get("type") @@ -273,6 +273,7 @@ def handle_create_edit(package: typing.Optional[Package], form: PackageForm, aut return None package = Package() + db.session.add(package) package.author = author package.maintainers.append(author) wasNew = True @@ -563,7 +564,7 @@ def audit(package): query = package.audit_log_entries.order_by(db.desc(AuditLogEntry.created_at)) - pagination = query.paginate(page, num, True) + pagination = query.paginate(page=page, per_page=num) return render_template("packages/audit.html", log=pagination.items, pagination=pagination, package=package, tabs=get_package_tabs(current_user, package), current_tab="audit") diff --git a/app/blueprints/packages/reviews.py b/app/blueprints/packages/reviews.py index d0dfc197..5a10212f 100644 --- a/app/blueprints/packages/reviews.py +++ b/app/blueprints/packages/reviews.py @@ -36,7 +36,7 @@ def list_reviews(): page = get_int_or_abort(request.args.get("page"), 1) num = min(40, get_int_or_abort(request.args.get("n"), 100)) - pagination = PackageReview.query.order_by(db.desc(PackageReview.created_at)).paginate(page, num, True) + pagination = PackageReview.query.order_by(db.desc(PackageReview.created_at)).paginate(page=page, per_page=num) return render_template("packages/reviews_list.html", pagination=pagination, reviews=pagination.items) diff --git a/app/blueprints/threads/__init__.py b/app/blueprints/threads/__init__.py index d208f6b0..1150271d 100644 --- a/app/blueprints/threads/__init__.py +++ b/app/blueprints/threads/__init__.py @@ -50,7 +50,7 @@ def list_all(): page = get_int_or_abort(request.args.get("page"), 1) num = min(40, get_int_or_abort(request.args.get("n"), 100)) - pagination = query.paginate(page, num, True) + pagination = query.paginate(page=page, per_page=num) return render_template("threads/list.html", pagination=pagination, threads=pagination.items, package=package) diff --git a/app/blueprints/todo/__init__.py b/app/blueprints/todo/__init__.py index 960c4932..f53dbe15 100644 --- a/app/blueprints/todo/__init__.py +++ b/app/blueprints/todo/__init__.py @@ -108,7 +108,7 @@ def topics(): if num > 100 and not current_user.rank.atLeast(UserRank.APPROVER): num = 100 - query = query.paginate(page, num, True) + query = query.paginate(page=page, per_page=num) next_url = url_for("todo.topics", page=query.next_num, query=qb.search, show_discarded=qb.show_discarded, n=num, sort=qb.order_by) \ if query.has_next else None diff --git a/app/tasks/usertasks.py b/app/tasks/usertasks.py index 0f45457a..05546b22 100644 --- a/app/tasks/usertasks.py +++ b/app/tasks/usertasks.py @@ -20,6 +20,7 @@ import datetime from sqlalchemy import or_, and_ from app.models import User, db, UserRank, ThreadReply, Package +from app.utils.models import create_session from app.tasks import celery @@ -37,7 +38,7 @@ def delete_inactive_users(): @celery.task() def upgrade_new_members(): - with db.create_session({})() as session: + with create_session() as session: threshold = datetime.datetime.now() - datetime.timedelta(days=7) session.query(User).filter(and_(User.rank == UserRank.NEW_MEMBER, or_( diff --git a/app/tests/integ/utils.py b/app/tests/integ/utils.py index ecf70e63..dc3ebb12 100644 --- a/app/tests/integ/utils.py +++ b/app/tests/integ/utils.py @@ -34,17 +34,18 @@ def is_int(v): @pytest.fixture def client(): - app.config["TESTING"] = True - app.config['WTF_CSRF_ENABLED'] = False + with app.app_context(): + app.config["TESTING"] = True + app.config['WTF_CSRF_ENABLED'] = False - recreate_db() - assert User.query.count() == 2 + recreate_db() + assert User.query.count() == 2 - with app.test_client() as client: - yield client + with app.test_client() as client: + yield client - app.config["TESTING"] = False - app.config['WTF_CSRF_ENABLED'] = True + app.config["TESTING"] = False + app.config['WTF_CSRF_ENABLED'] = True def validate_package_list(packages, strict=False): diff --git a/app/utils/models.py b/app/utils/models.py index 499d7524..e1d23d6d 100644 --- a/app/utils/models.py +++ b/app/utils/models.py @@ -22,6 +22,7 @@ import sqlalchemy.orm from flask import abort, redirect, url_for, request from flask_login import current_user from sqlalchemy import or_, and_ +from sqlalchemy.orm import sessionmaker from app.models import User, NotificationType, Package, UserRank, Notification, db, AuditSeverity, AuditLogEntry, ThreadReply, Thread, PackageState, PackageType, PackageAlias @@ -149,3 +150,7 @@ def get_games_from_csv(session: sqlalchemy.orm.Session, csv: str) -> List[Packag retval.extend(games) return retval + + +def create_session(): + return sessionmaker(bind=db.engine)() diff --git a/migrations/env.py b/migrations/env.py index e5b9792e..587d3db8 100755 --- a/migrations/env.py +++ b/migrations/env.py @@ -70,11 +70,14 @@ def run_migrations_online(): poolclass=pool.NullPool) connection = engine.connect() + + args = current_app.extensions['migrate'].configure_args + args["compare_type"] = True + context.configure(connection=connection, target_metadata=target_metadata, process_revision_directives=process_revision_directives, - compare_type=True, - **current_app.extensions['migrate'].configure_args) + **args) try: with context.begin_transaction(): diff --git a/requirements.lock.txt b/requirements.lock.txt index 6b2a77a8..4913e09d 100644 --- a/requirements.lock.txt +++ b/requirements.lock.txt @@ -25,8 +25,8 @@ Flask-FlatPages==0.8.1 Flask-Gravatar==0.5.0 Flask-Login==0.6.2 Flask-Mail==0.9.1 -Flask-Migrate==3.1.0 -Flask-SQLAlchemy==2.5.1 +Flask-Migrate==4.0.4 +Flask-SQLAlchemy==3.0.3 Flask-WTF==1.1.1 git-archive-all==1.23.1 gitdb==4.0.10 @@ -60,9 +60,9 @@ requests==2.28.2 six==1.16.0 smmap==5.0.0 soupsieve==2.4.1 -SQLAlchemy==1.4.31 +SQLAlchemy==2.0.9 SQLAlchemy-Searchable==1.4.1 -SQLAlchemy-Utils==0.38.2 +SQLAlchemy-Utils==0.41.0 tomli==2.0.1 typing_extensions==4.5.0 ua-parser==0.16.1