diff --git a/migrations/alembic.ini b/migrations/alembic.ini index 7eae6d2..ea28df6 100644 --- a/migrations/alembic.ini +++ b/migrations/alembic.ini @@ -1,5 +1,5 @@ [alembic] -script_location = . +script_location = ./migrations [loggers] keys = root,sqlalchemy,alembic diff --git a/migrations/env.py b/migrations/env.py index 2127eba..6e177d5 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -5,9 +5,14 @@ from flask import current_app from sqlalchemy import engine_from_config, pool +from spkrepo import create_app + config = context.config fileConfig(config.config_file_name) +# Set up the Flask application context +app = create_app() # Create the Flask app +app.app_context().push() # Push the app context config.set_main_option( "sqlalchemy.url", current_app.config.get("SQLALCHEMY_DATABASE_URI") diff --git a/migrations/versions/426e7f8fe7c6_increase_version_length_and_add_min_.py b/migrations/versions/426e7f8fe7c6_increase_version_length_and_add_min_.py new file mode 100644 index 0000000..9bff797 --- /dev/null +++ b/migrations/versions/426e7f8fe7c6_increase_version_length_and_add_min_.py @@ -0,0 +1,37 @@ +"""Increase version length and add min_compatible_version + +Revision ID: 426e7f8fe7c6 +Revises: 76d559b4e873 +Create Date: 2024-01-09 09:56:56.088816 + +""" +revision = "426e7f8fe7c6" +down_revision = "76d559b4e873" + +import sqlalchemy as sa +from alembic import op + + +def upgrade(): + op.add_column( + "firmware", + sa.Column("min_compatible_version", sa.Unicode(length=10), nullable=True), + ) + op.alter_column( + "firmware", + "version", + existing_type=sa.VARCHAR(length=3), + type_=sa.Unicode(length=10), + existing_nullable=False, + ) + + +def downgrade(): + op.alter_column( + "firmware", + "version", + existing_type=sa.Unicode(length=10), + type_=sa.VARCHAR(length=3), + existing_nullable=False, + ) + op.drop_column("firmware", "min_compatible_version") diff --git a/migrations/versions/dc7687894ba7_increase_field_sizes.py b/migrations/versions/dc7687894ba7_increase_field_sizes.py index c4209fe..b1bb233 100644 --- a/migrations/versions/dc7687894ba7_increase_field_sizes.py +++ b/migrations/versions/dc7687894ba7_increase_field_sizes.py @@ -13,7 +13,6 @@ def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### op.alter_column( "version", "conf_dependencies", @@ -42,11 +41,9 @@ def upgrade(): type_=sa.UnicodeText(), existing_nullable=True, ) - # ### end Alembic commands ### def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### op.alter_column( "version", "conf_resource", @@ -75,4 +72,3 @@ def downgrade(): type_=sa.VARCHAR(length=255), existing_nullable=True, ) - # ### end Alembic commands ### diff --git a/spkrepo/models.py b/spkrepo/models.py index c94dc53..0923488 100644 --- a/spkrepo/models.py +++ b/spkrepo/models.py @@ -134,8 +134,9 @@ class Firmware(db.Model): # Columns id = db.Column(db.Integer, primary_key=True) - version = db.Column(db.Unicode(3), nullable=False) + version = db.Column(db.Unicode(10), nullable=False) build = db.Column(db.Integer, unique=True, nullable=False) + min_compatible_version = db.Column(db.Unicode(10)) @classmethod def find(cls, build): @@ -329,7 +330,7 @@ class Build(db.Model): publisher_user_id = db.Column(db.Integer, db.ForeignKey("user.id")) checksum = db.Column(db.Unicode(32)) extract_size = db.Column(db.Integer) - path = db.Column(db.Unicode(100)) + path = db.Column(db.Unicode(2048)) md5 = db.Column(db.Unicode(32)) insert_date = db.Column(db.DateTime, default=db.func.now(), nullable=False) active = db.Column(db.Boolean(), default=False, nullable=False) diff --git a/spkrepo/views/admin.py b/spkrepo/views/admin.py index 681bdcc..6ddd2f3 100644 --- a/spkrepo/views/admin.py +++ b/spkrepo/views/admin.py @@ -118,10 +118,13 @@ def __init__(self, **kwargs): def is_accessible(self): return current_user.is_authenticated and current_user.has_role("package_admin") - can_edit = False - can_delete = False + # Form + def on_form_prefill(self, form, id): + form.version.render_kw = {"readonly": True} + form.build.render_kw = {"readonly": True} + class ServiceView(ModelView): """View for :class:`~spkrepo.models.Service`""" diff --git a/spkrepo/views/nas.py b/spkrepo/views/nas.py index 3db8f56..b85a35d 100644 --- a/spkrepo/views/nas.py +++ b/spkrepo/views/nas.py @@ -44,12 +44,22 @@ def is_valid_language(language): @cache.memoize(timeout=600) def get_catalog(arch, build, language, beta): - # latest version per package + # Get min_compatible_version from the database based on the provided build + min_compatible_version = ( + Firmware.query.filter(Firmware.build == build) + .with_entities(Firmware.min_compatible_version) + .first() + ) + + # latest version per package with min_compatible_version latest_version = db.session.query( Version.package_id, db.func.max(Version.version).label("latest_version") ).select_from(Version) + if not beta: latest_version = latest_version.filter(Version.report_url.is_(None)) + + # Apply the filter conditionally based on min_compatible_version latest_version = ( latest_version.join(Build) .filter(Build.active) @@ -57,6 +67,11 @@ def get_catalog(arch, build, language, beta): .filter(Architecture.code.in_(["noarch", arch])) .join(Build.firmware) .filter(Firmware.build <= build) + .filter( + Firmware.version >= min_compatible_version[0] + if min_compatible_version[0] is not None + else True # Include all versions if min_compatible_version is None + ) .group_by(Version.package_id) ).subquery()