Skip to content
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
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
!backend/launch_*.py
!backend/generate_types.py

# DB migrations
!backend/alembic
!backend/alembic.ini

!configs/

!frontend/src/
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- We now use `uv` (Universal Virtualenv) to manage python dependencies and run scripts in CI/CD. This should improve dependency resolution and installation times.
- We now ship a static ffmpeg binary instead of installing ffmpeg via apt. This should reduce image size and improve compatibility across different host systems.
- Added a database migration setup using [Alembic](https://alembic.sqlalchemy.org/) for future database migrations.

## [1.2.0] - 25-12-17

Expand Down
14 changes: 14 additions & 0 deletions backend/alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Alembic Configuration
# Docs: https://alembic.sqlalchemy.org/en/latest/index.html

[alembic]
script_location = %(here)s/alembic
file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
prepend_sys_path = .
path_separator = os

[post_write_hooks]
hooks = ruff
ruff.type = module
ruff.module = ruff
ruff.options = check --fix REVISION_SCRIPT_FILENAME
95 changes: 95 additions & 0 deletions backend/alembic/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""Alembic environment configuration for beets-flask database migrations.

This module configures Alembic to use the beets-flask database configuration
for both autogenerate support and runtime migrations.
"""

from alembic import context

# Import beets_flask database components
from beets_flask.config.flask_config import get_flask_config
from beets_flask.database.models.base import Base

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config


# add your model's MetaData object here
# for 'autogenerate' support
# This is crucial for autogenerate to detect model changes
target_metadata = Base.metadata


def get_url() -> str:
"""Get the database URL from beets-flask configuration.

Returns
-------
str: The database connection URI.

"""
flask_config = get_flask_config()
return flask_config["DATABASE_URI"]


def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.

This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.

Calls to context.execute() here emit the given string to the
script output.

"""
url = get_url()
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)

with context.begin_transaction():
context.run_migrations()


def run_migrations_online() -> None:
"""Run migrations in 'online' mode.

In this scenario we need to create an Engine
and associate a connection with the context.

"""
from sqlalchemy import engine_from_config, pool

# Get the database URL from beets-flask config
url = get_url()

# Create engine configuration with our URL
configuration = config.get_section(config.config_ini_section) or {}
configuration["sqlalchemy.url"] = url

connectable = engine_from_config(
configuration,
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
)

with context.begin_transaction():
context.run_migrations()


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
31 changes: 31 additions & 0 deletions backend/alembic/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""

from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

${imports if imports else ""}

# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: str | Sequence[str] | None = ${repr(down_revision)}
branch_labels: str | Sequence[str] | None = ${repr(branch_labels)}
depends_on: str | Sequence[str] | None = ${repr(depends_on)}


def upgrade() -> None:
"""Upgrade schema."""
${upgrades if upgrades else "pass"}


def downgrade() -> None:
"""Downgrade schema."""
${downgrades if downgrades else "pass"}
168 changes: 168 additions & 0 deletions backend/alembic/versions/2026_04_08_1846-a986c03d9ba3_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
"""initial

Revision ID: a986c03d9ba3
Revises:
Create Date: 2026-04-08 18:46:00.556681

"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "a986c03d9ba3"
down_revision: str | Sequence[str] | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
"""Upgrade schema."""
import beets_flask.database.models.types

# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"folder",
sa.Column("full_path", sa.String(), nullable=False),
sa.Column("is_album", sa.Boolean(), nullable=True),
sa.Column("id", sa.String(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint("full_path", "id"),
)
op.create_index(
op.f("ix_folder_created_at"), "folder", ["created_at"], unique=False
)
op.create_index(op.f("ix_folder_full_path"), "folder", ["full_path"], unique=False)
op.create_table(
"session",
sa.Column("folder_hash", sa.String(), nullable=False),
sa.Column("folder_revision", sa.Integer(), nullable=False),
sa.Column(
"progress",
sa.Enum(
"NOT_STARTED",
"READING_FILES",
"GROUPING_ALBUMS",
"LOOKING_UP_CANDIDATES",
"IDENTIFYING_DUPLICATES",
"PREVIEW_COMPLETED",
"DELETION_COMPLETED",
"OFFERING_MATCHES",
"MATCH_THRESHOLD",
"WAITING_FOR_USER_SELECTION",
"EARLY_IMPORTING",
"IMPORTING",
"MANIPULATING_FILES",
"IMPORT_COMPLETED",
"DELETING",
name="progress",
),
nullable=False,
),
sa.Column("exc", sa.LargeBinary(), nullable=True),
sa.Column("id", sa.String(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(
["folder_hash"],
["folder.id"],
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint(
"folder_hash", "folder_revision", name="uq_folder_hash_revision"
),
)
op.create_index(
op.f("ix_session_created_at"), "session", ["created_at"], unique=False
)
op.create_table(
"task",
sa.Column("session_id", sa.String(), nullable=False),
sa.Column("chosen_candidate_id", sa.String(), nullable=True),
sa.Column("toppath", sa.LargeBinary(), nullable=True),
sa.Column("paths", sa.LargeBinary(), nullable=False),
sa.Column("old_paths", sa.LargeBinary(), nullable=True),
sa.Column("items", sa.LargeBinary(), nullable=False),
sa.Column(
"choice_flag",
sa.Enum(
"SKIP", "ASIS", "TRACKS", "APPLY", "ALBUMS", "RETAG", name="action"
),
nullable=True,
),
sa.Column("cur_artist", sa.String(), nullable=True),
sa.Column("cur_album", sa.String(), nullable=True),
sa.Column(
"progress",
sa.Enum(
"NOT_STARTED",
"READING_FILES",
"GROUPING_ALBUMS",
"LOOKING_UP_CANDIDATES",
"IDENTIFYING_DUPLICATES",
"PREVIEW_COMPLETED",
"DELETION_COMPLETED",
"OFFERING_MATCHES",
"MATCH_THRESHOLD",
"WAITING_FOR_USER_SELECTION",
"EARLY_IMPORTING",
"IMPORTING",
"MANIPULATING_FILES",
"IMPORT_COMPLETED",
"DELETING",
name="progress",
),
nullable=False,
),
sa.Column("id", sa.String(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(
["chosen_candidate_id"], ["candidate.id"], use_alter=True
),
sa.ForeignKeyConstraint(
["session_id"],
["session.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(op.f("ix_task_created_at"), "task", ["created_at"], unique=False)
op.create_table(
"candidate",
sa.Column("task_id", sa.String(), nullable=False),
sa.Column("match", sa.LargeBinary(), nullable=False),
sa.Column("duplicate_ids", sa.String(), nullable=False),
sa.Column(
"mapping", beets_flask.database.models.types.IntDictType(), nullable=False
),
sa.Column("id", sa.String(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(
["task_id"],
["task.id"],
),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
op.f("ix_candidate_created_at"), "candidate", ["created_at"], unique=False
)
# ### end Alembic commands ###


def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f("ix_candidate_created_at"), table_name="candidate")
op.drop_table("candidate")
op.drop_index(op.f("ix_task_created_at"), table_name="task")
op.drop_table("task")
op.drop_index(op.f("ix_session_created_at"), table_name="session")
op.drop_table("session")
op.drop_index(op.f("ix_folder_full_path"), table_name="folder")
op.drop_index(op.f("ix_folder_created_at"), table_name="folder")
op.drop_table("folder")
# ### end Alembic commands ###
3 changes: 1 addition & 2 deletions backend/beets_flask/database/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from .setup import db_session_factory, setup_database, with_db_session
from .setup import db_session_factory, setup_database

__all__ = [
"setup_database",
"db_session_factory",
"with_db_session",
]
Loading
Loading