Skip to content

Commit

Permalink
Amend major_version check
Browse files Browse the repository at this point in the history
- Add type column to Firmware table
- Increase length of version column
- Filter by type for closest firmware when getting catalog
- Update populate_db with firmware type
  • Loading branch information
mreid-tt committed Jan 15, 2024
1 parent 73981ae commit ad1cf17
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Add firmware type and increase version length
Revision ID: f95855ce9471
Revises: 76d559b4e873
Create Date: 2024-01-15 13:58:34.160242
"""
revision = "f95855ce9471"
down_revision = "76d559b4e873"

import sqlalchemy as sa
from alembic import op


def upgrade():
op.add_column("firmware", sa.Column("type", sa.Unicode(length=4)))
# Set type based on version
op.execute(
"""
UPDATE firmware
SET type = CASE
WHEN version LIKE '1.%' THEN 'srm'
ELSE 'dsm'
END
"""
)
# Modify the column to be NOT NULL after setting the values
op.alter_column("firmware", "type", nullable=False)

op.alter_column(
"firmware",
"version",
existing_type=sa.VARCHAR(length=3),
type_=sa.Unicode(length=4),
existing_nullable=False,
)


def downgrade():
op.alter_column(
"firmware",
"version",
existing_type=sa.Unicode(length=4),
type_=sa.VARCHAR(length=3),
existing_nullable=False,
)
op.drop_column("firmware", "type")
3 changes: 2 additions & 1 deletion 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(4), nullable=False)
build = db.Column(db.Integer, unique=True, nullable=False)
type = db.Column(db.Unicode(4), nullable=False)

@classmethod
def find(cls, build):
Expand Down
5 changes: 4 additions & 1 deletion spkrepo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,10 @@ def populate_db():
)
db.session.execute(
Firmware.__table__.insert().values(
[{"version": "3.1", "build": 1594}, {"version": "5.0", "build": 4458}]
[
{"version": "3.1", "build": 1594, "type": "dsm"},
{"version": "5.0", "build": 4458, "type": "dsm"},
]
)
)
db.session.execute(
Expand Down
2 changes: 1 addition & 1 deletion spkrepo/views/nas.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def is_valid_language(language):
def get_catalog(arch, build, language, beta):
# Find the closest matching firmware for the provided build
closest_firmware = (
Firmware.query.filter(Firmware.build <= build)
Firmware.query.filter(Firmware.build <= build, Firmware.type == "dsm")
.order_by(Firmware.build.desc())
.first()
)
Expand Down

0 comments on commit ad1cf17

Please sign in to comment.