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

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
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
91 changes: 11 additions & 80 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 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,92 +11,23 @@ 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-20.04
strategy:
matrix:
python-version: [3.7, 3.8, 3.9]
requirements-level: [pypi]
db-service: [postgresql11, postgresql14, mysql8, sqlite]
exclude:
- python-version: 3.7
db-service: postgresql14
requirements-level: pypi

- python-version: 3.7
db-service: mysql8
requirements-level: pypi

- python-version: 3.8
db-service: postgresql11

- python-version: 3.9
db-service: postgresql11

- python-version: 3.7
db-service: sqlite

- python-version: 3.8
db-service: 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@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Generate dependencies
run: |
pip install wheel requirements-builder
requirements-builder -e "$EXTRAS" --level=${{ matrix.requirements-level }} setup.py > .${{ matrix.requirements-level }}-${{ matrix.python-version }}-requirements.txt

- name: Cache pip
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('.${{ matrix.requirements-level }}-${{ matrix.python-version }}-requirements.txt') }}

- name: Install dependencies
run: |
pip install -r .${{ matrix.requirements-level }}-${{ matrix.python-version }}-requirements.txt -c constraints-${{ matrix.requirements-level }}.txt
pip install ".[$EXTRAS]"
pip freeze
docker --version
docker-compose --version

- name: Run tests
run: |
./run-tests.sh
uses: inveniosoftware/workflows/.github/workflows/tests-python.yml@master
with:
extras: "tests,postgresql"
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
44 changes: 17 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_sqlalchemy
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_sqlalchemy.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_sqlalchemy.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_sqlalchemy.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_sqlalchemy.engine, checkfirst=True)
drop_alembic_version_table()
click.secho("Dropped all tables!", fg="green")

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

if not database_exists(database_url):
create_database(database_url)

Expand All @@ -108,12 +96,14 @@ def init():
@with_appcontext
def destroy():
"""Drop database."""
displayed_database = render_url(_db.engine.url)
displayed_database = render_url(current_sqlalchemy.engine.url)
click.secho(f"Destroying database {displayed_database}", fg="red", bold=True)
if _db.engine.name == "sqlite":

plain_url = current_sqlalchemy.engine.url.render_as_string(hide_password=False)
if current_sqlalchemy.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)
23 changes: 11 additions & 12 deletions invenio_db/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This file is part of Invenio.
# Copyright (C) 2015-2018 CERN.
# Copyright (C) 2022 RERO.
# 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 Down 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 Expand Up @@ -93,6 +91,7 @@ def init_db(self, app, entry_point_group="invenio_db.models", **kwargs):

# All models should be loaded by now.
sa.orm.configure_mappers()

# Ensure that versioning classes have been built.
if app.config["DB_VERSIONING"]:
manager = self.versioning_manager
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_sqlalchemy = LocalProxy(lambda: current_app.extensions["sqlalchemy"])
19 changes: 10 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) 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,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_sqlalchemy
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,13 @@ 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_sqlalchemy.engine, "alembic_version"):
alembic_version = current_sqlalchemy.Table(
"alembic_version",
current_sqlalchemy.metadata,
autoload_with=current_sqlalchemy.engine,
)
alembic_version.drop(bind=_db.engine)
alembic_version.drop(bind=current_sqlalchemy.engine)


def versioning_model_classname(manager, model):
Expand Down
19 changes: 9 additions & 10 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Copyright (C) 2015-2022 CERN.
# Copyright (C) 2021 Northwestern University.
# Copyright (C) 2022 RERO.
# 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 @@ -29,22 +29,21 @@ packages = find:
python_requires = >=3.7
zip_safe = False
install_requires =
# due to incompatibility on the 1.11 release
alembic>=1.10.0,<1.11.0
Flask-Alembic>=2.0.1
Flask-SQLAlchemy>=2.1,<3.0.0
alembic>=1.10.0
Flask-Alembic>=3.0.0
Flask-SQLAlchemy>=3.0
invenio-base>=1.2.10
SQLAlchemy-Continuum>=1.3.12
SQLAlchemy-Utils>=0.33.1,<0.39
SQLAlchemy[asyncio]>=1.2.18,<1.5.0
SQLAlchemy-Utils>=0.33.1
SQLAlchemy>=2.0.0

[options.extras_require]
tests =
pytest-black>=0.3.0
six>=1.0.0
pytest-black-ng>=0.4.0
cryptography>=2.1.4
pytest-invenio>=1.4.5
Sphinx>=4.5.0
# Left here for backward compatibility
mysql =
pymysql>=0.10.1
postgresql =
Expand All @@ -69,7 +68,7 @@ all_files = 1
universal = 1

[pydocstyle]
add_ignore = D401
add_ignore = D401, D202

[isort]
profile=black
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from invenio_db.utils import alembic_test_context


@pytest.fixture()
def db():
@pytest.fixture(name="db")
def fixture_db():
"""Database fixture with session sharing."""
import invenio_db
from invenio_db import shared
Expand Down
Loading