Skip to content

Commit

Permalink
add function for min_compatible_version
Browse files Browse the repository at this point in the history
- fix alembic setup environment
- add min_compatible_version increase version length
- update model to match database
- add edit form for firmware view
- add get_catalog filter based on min_compatible_version
  • Loading branch information
mreid-tt committed Jan 9, 2024
1 parent 2349b5d commit b81d71b
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 10 deletions.
2 changes: 1 addition & 1 deletion migrations/alembic.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[alembic]
script_location = .
script_location = ./migrations

[loggers]
keys = root,sqlalchemy,alembic
Expand Down
5 changes: 5 additions & 0 deletions migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
4 changes: 0 additions & 4 deletions migrations/versions/dc7687894ba7_increase_field_sizes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column(
"version",
"conf_dependencies",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -75,4 +72,3 @@ def downgrade():
type_=sa.VARCHAR(length=255),
existing_nullable=True,
)
# ### end Alembic commands ###
5 changes: 3 additions & 2 deletions spkrepo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions spkrepo/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`"""
Expand Down
17 changes: 16 additions & 1 deletion spkrepo/views/nas.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,34 @@ 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)
.join(Build.architectures)
.filter(Architecture.code.in_(["noarch", arch]))
.join(Build.firmware)
.filter(Firmware.build <= build)
.filter(
Firmware.version >= min_compatible_version[0]
if min_compatible_version and min_compatible_version[0] is not None
else True # Include all versions if min_compatible_version is None
)
.group_by(Version.package_id)
).subquery()

Expand Down

0 comments on commit b81d71b

Please sign in to comment.