Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove upper limit #174

Merged
merged 16 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 13 additions & 53 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# This file is part of Invenio.
# Copyright (C) 2020-2024 CERN.
# Copyright (C) 2022 Graz University of Technology.
# Copyright (C) 2022-2024 Graz University of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -11,65 +11,25 @@ name: CI

on:
push:
branches: master
branches:
- master
pull_request:
branches: master
branches:
- master
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '0 3 * * 6'
- cron: "0 3 * * 6"
workflow_dispatch:
inputs:
reason:
description: 'Reason'
description: "Reason"
required: false
default: 'Manual trigger'
default: "Manual trigger"

jobs:
Tests:
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
python-version: [3.9, 3.12]
db-service: [postgresql11, postgresql14, mysql8, sqlite]

include:
- db-service: postgresql11
EXTRAS: "tests,postgresql"

- db-service: postgresql14
EXTRAS: "tests,postgresql"

- db-service: mysql8
EXTRAS: "tests,mysql"

- db-service: sqlite
EXTRAS: "tests"

env:
DB: ${{ matrix.db-service }}
EXTRAS: ${{ matrix.EXTRAS }}

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: pip
cache-dependency-path: setup.cfg

- name: Pre-install
uses: ./.github/actions/pre-install
if: ${{ hashFiles('.github/actions/pre-install/action.yml') != '' }}

- name: Install dependencies
run: |
pip install ".[$EXTRAS]"
pip freeze
docker version
uses: inveniosoftware/workflows/.github/workflows/tests-python.yml@master
with:
extras: "tests,postgresql"
search-service: '[""]'

- name: Run tests
run: ./run-tests.sh
18 changes: 18 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
..
This file is part of Invenio.
Copyright (C) 2015-2024 CERN.
Copyright (C) 2024 Graz University of Technology.

Invenio is free software; you can redistribute it and/or modify it
under the terms of the MIT License; see LICENSE file for more details.

Changes
=======

Version v2.0.0 (released 2024-11-19)

- uow: possible solution for the rollback problem
- fix: select of BinaryExpressions
- setup: increase flask-sqlalchemy
- ci: change to reusable workflows
- setup: increase min sqlalchemy dependency
- cli: password is per default hided
- refactor: move MockEntryPoint to independent file
- setup: remove upper pins
- fix: remove warning and black problems
- fix: WeakKeyDictionary
- change: add proxy file
- change: remove click 3 compatibility
- fix: tests LocalProxy
- setup: increase flask-sqlalchemy version

Version v1.3.1 (released 2024-11-14)

- uow: improve decorator to create uow if it's None
Expand Down
3 changes: 2 additions & 1 deletion invenio_db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# This file is part of Invenio.
# Copyright (C) 2015-2024 CERN.
# Copyright (C) 2024 Graz University of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -91,7 +92,7 @@ class User(db.Model):
from .ext import InvenioDB
from .shared import db

__version__ = "1.3.1"
__version__ = "2.0.0"

__all__ = (
"__version__",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def upgrade():
"""Update database."""
op.create_table(
"transaction",
sa.Column("issued_at", sa.DateTime(), nullable=True),
sa.Column("id", sa.BigInteger(), nullable=False),
sa.Column("remote_addr", sa.String(length=50), nullable=True),
sa.Column("issued_at", sa.DateTime(), nullable=True),
)
op.create_primary_key("pk_transaction", "transaction", ["id"])
if op._proxy.migration_context.dialect.supports_sequences:
Expand Down
42 changes: 15 additions & 27 deletions invenio_db/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,20 @@
#
# This file is part of Invenio.
# Copyright (C) 2015-2018 CERN.
# Copyright (C) 2024 Graz University of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Click command-line interface for database management."""

import sys

import click
from click import _termui_impl
from flask import current_app
from flask.cli import with_appcontext
from sqlalchemy_utils.functions import create_database, database_exists, drop_database
from werkzeug.local import LocalProxy

from .proxies import current_db
from .utils import create_alembic_version_table, drop_alembic_version_table

_db = LocalProxy(lambda: current_app.extensions["sqlalchemy"].db)

# Fix Python 3 compatibility issue in click
if sys.version_info > (3,):
_termui_impl.long = int # pragma: no cover


def abort_if_false(ctx, param, value):
"""Abort command is value is False."""
Expand All @@ -34,11 +25,7 @@ def abort_if_false(ctx, param, value):

def render_url(url):
"""Render the URL for CLI output."""
try:
return url.render_as_string(hide_password=True)
except AttributeError:
# SQLAlchemy <1.4
return url.__to_string__(hide_password=True)
return url.render_as_string(hide_password=True)


#
Expand All @@ -55,11 +42,11 @@ def db():
def create(verbose):
"""Create tables."""
click.secho("Creating all tables!", fg="yellow", bold=True)
with click.progressbar(_db.metadata.sorted_tables) as bar:
with click.progressbar(current_db.metadata.sorted_tables) as bar:
for table in bar:
if verbose:
click.echo(" Creating table {0}".format(table))
table.create(bind=_db.engine, checkfirst=True)
table.create(bind=current_db.engine, checkfirst=True)
create_alembic_version_table()
click.secho("Created all tables!", fg="green")

Expand All @@ -77,11 +64,11 @@ def create(verbose):
def drop(verbose):
"""Drop tables."""
click.secho("Dropping all tables!", fg="red", bold=True)
with click.progressbar(reversed(_db.metadata.sorted_tables)) as bar:
with click.progressbar(reversed(current_db.metadata.sorted_tables)) as bar:
for table in bar:
if verbose:
click.echo(" Dropping table {0}".format(table))
table.drop(bind=_db.engine, checkfirst=True)
table.drop(bind=current_db.engine, checkfirst=True)
drop_alembic_version_table()
click.secho("Dropped all tables!", fg="green")

Expand All @@ -90,9 +77,9 @@ def drop(verbose):
@with_appcontext
def init():
"""Create database."""
displayed_database = render_url(_db.engine.url)
displayed_database = render_url(current_db.engine.url)
click.secho(f"Creating database {displayed_database}", fg="green")
database_url = str(_db.engine.url)
database_url = current_db.engine.url.render_as_string(hide_password=False)
if not database_exists(database_url):
create_database(database_url)

Expand All @@ -108,12 +95,13 @@ def init():
@with_appcontext
def destroy():
"""Drop database."""
displayed_database = render_url(_db.engine.url)
displayed_database = render_url(current_db.engine.url)
click.secho(f"Destroying database {displayed_database}", fg="red", bold=True)
if _db.engine.name == "sqlite":
plain_url = current_db.engine.url.render_as_string(hide_password=False)
if current_db.engine.name == "sqlite":
try:
drop_database(_db.engine.url)
except FileNotFoundError as e:
drop_database(plain_url)
except FileNotFoundError:
click.secho("Sqlite database has not been initialised", fg="red", bold=True)
else:
drop_database(_db.engine.url)
drop_database(plain_url)
20 changes: 9 additions & 11 deletions invenio_db/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,17 @@ def init_app(self, app, **kwargs):
"""Initialize application object."""
self.init_db(app, **kwargs)

script_location = str(importlib_resources.files("invenio_db") / "alembic")
version_locations = [
(
base_entry.name,
str(
importlib_resources.files(base_entry.module)
/ os.path.join(base_entry.attr)
),
)
for base_entry in importlib_metadata.entry_points(
group="invenio_db.alembic"
def pathify(base_entry):
return str(
importlib_resources.files(base_entry.module)
/ os.path.join(base_entry.attr)
)

entry_points = importlib_metadata.entry_points(group="invenio_db.alembic")
version_locations = [
(base_entry.name, pathify(base_entry)) for base_entry in entry_points
]
script_location = str(importlib_resources.files("invenio_db") / "alembic")
app.config.setdefault(
"ALEMBIC",
{
Expand Down
15 changes: 15 additions & 0 deletions invenio_db/proxies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2022 Graz University of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Helper proxy to the state object."""


from flask import current_app
from werkzeug.local import LocalProxy

current_db = LocalProxy(lambda: current_app.extensions["sqlalchemy"])
2 changes: 2 additions & 0 deletions invenio_db/uow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2024 CERN.
# Copyright (C) 2024 Graz University of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -167,6 +168,7 @@ def __init__(self, session=None):

def __enter__(self):
"""Entering the context."""
self.session.begin_nested()
return self

def __exit__(self, exc_type, exc_value, traceback):
Expand Down
17 changes: 8 additions & 9 deletions invenio_db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# This file is part of Invenio.
# Copyright (C) 2017-2018 CERN.
# Copyright (C) 2022 Graz University of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -11,14 +12,12 @@

from flask import current_app
from sqlalchemy import inspect
from werkzeug.local import LocalProxy

from .shared import db
from .proxies import current_db
from .shared import db as _db

_db = LocalProxy(lambda: current_app.extensions["sqlalchemy"].db)


def rebuild_encrypted_properties(old_key, model, properties):
def rebuild_encrypted_properties(old_key, model, properties, db=_db):
"""Rebuild model's EncryptedType properties when the SECRET_KEY is changed.

:param old_key: old SECRET_KEY.
Expand Down Expand Up @@ -73,11 +72,11 @@ def create_alembic_version_table():

def drop_alembic_version_table():
"""Drop alembic_version table."""
if has_table(_db.engine, "alembic_version"):
alembic_version = _db.Table(
"alembic_version", _db.metadata, autoload_with=_db.engine
if has_table(current_db.engine, "alembic_version"):
alembic_version = current_db.Table(
"alembic_version", current_db.metadata, autoload_with=current_db.engine
)
alembic_version.drop(bind=_db.engine)
alembic_version.drop(bind=current_db.engine)


def versioning_model_classname(manager, model):
Expand Down
Loading