Skip to content
Open
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
28 changes: 27 additions & 1 deletion bazarr/app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def _stop_worker_threads():
Base = declarative_base()
metadata = Base.metadata


class System(Base):
__tablename__ = 'system'

Expand Down Expand Up @@ -309,6 +308,9 @@ def init_db():
# Create tables if they don't exist.
metadata.create_all(engine)

# Apply any missing index
ensure_indexes()


def create_db_revision(app):
logging.info("Creating a new database revision for future migration")
Expand Down Expand Up @@ -568,3 +570,27 @@ def fix_languages_profiles_with_duplicate_ids():
.values({"items": json.dumps(languages_profile_items)})
.where(TableLanguagesProfiles.profileId == languages_profile.profileId)
)

def ensure_indexes():
"""Create indexes needed for fast look‑ups (no‑op if they exist)."""
index_ddl = [
# Series history
"CREATE INDEX IF NOT EXISTS idx_table_history_timestamp ON table_history (timestamp)",
"CREATE INDEX IF NOT EXISTS idx_table_history_action ON table_history (action)",
"CREATE INDEX IF NOT EXISTS idx_table_history_provider ON table_history (provider)",
"CREATE INDEX IF NOT EXISTS idx_table_history_language ON table_history (language)",

# Movie history
"CREATE INDEX IF NOT EXISTS idx_table_history_movie_timestamp ON table_history_movie (timestamp)",
"CREATE INDEX IF NOT EXISTS idx_table_history_movie_action ON table_history_movie (action)",
"CREATE INDEX IF NOT EXISTS idx_table_history_movie_provider ON table_history_movie (provider)",
"CREATE INDEX IF NOT EXISTS idx_table_history_movie_language ON table_history_movie (language)",

# Episodes
"CREATE INDEX IF NOT EXISTS idx_table_episodes_episodeid ON table_episodes (sonarrEpisodeId)",
"CREATE INDEX IF NOT EXISTS idx_table_episodes_seriesid_season_episode ON table_episodes (sonarrSeriesId, season DESC, episode DESC)",
]

with engine.begin() as conn:
for ddl in index_ddl:
conn.execute(text(ddl))