1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-08-04 04:55:21 +02:00

fix: Only run migration data fixes on migrations (#5038)

This commit is contained in:
Michael Genson 2025-02-24 02:29:12 -06:00 committed by GitHub
parent 09234e3bf0
commit 6271b33b1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -69,16 +69,16 @@ def db_is_at_head(alembic_cfg: config.Config) -> bool:
def safe_try(func: Callable): def safe_try(func: Callable):
try: try:
func() func()
except Exception as e: except Exception:
logger.error(f"Error calling '{func.__name__}': {e}") logger.exception(f"Error calling '{func.__name__}'")
def connect(session: orm.Session) -> bool: def connect(session: orm.Session) -> bool:
try: try:
session.execute(text("SELECT 1")) session.execute(text("SELECT 1"))
return True return True
except Exception as e: except Exception:
logger.error(f"Error connecting to database: {e}") logger.exception("Error connecting to database")
return False return False
@ -106,23 +106,27 @@ def main():
if not os.path.isfile(alembic_cfg_path): if not os.path.isfile(alembic_cfg_path):
raise Exception("Provided alembic config path doesn't exist") raise Exception("Provided alembic config path doesn't exist")
run_fixes = False
alembic_cfg = Config(alembic_cfg_path) alembic_cfg = Config(alembic_cfg_path)
if db_is_at_head(alembic_cfg): if db_is_at_head(alembic_cfg):
logger.debug("Migration not needed.") logger.debug("Migration not needed.")
else: else:
logger.info("Migration needed. Performing migration...") logger.info("Migration needed. Performing migration...")
command.upgrade(alembic_cfg, "head") command.upgrade(alembic_cfg, "head")
run_fixes = True
if session.get_bind().name == "postgresql": # needed for fuzzy search and fast GIN text indices if session.get_bind().name == "postgresql": # needed for fuzzy search and fast GIN text indices
session.execute(text("CREATE EXTENSION IF NOT EXISTS pg_trgm;")) session.execute(text("CREATE EXTENSION IF NOT EXISTS pg_trgm;"))
db = get_repositories(session, group_id=None, household_id=None) db = get_repositories(session, group_id=None, household_id=None)
if db.users.get_all():
logger.debug("Database exists")
if run_fixes:
safe_try(lambda: fix_migration_data(session)) safe_try(lambda: fix_migration_data(session))
safe_try(lambda: fix_slug_food_names(db)) safe_try(lambda: fix_slug_food_names(db))
safe_try(lambda: fix_group_with_no_name(session)) safe_try(lambda: fix_group_with_no_name(session))
if db.users.get_all():
logger.debug("Database exists")
else: else:
logger.info("Database contains no users, initializing...") logger.info("Database contains no users, initializing...")
init_db(session) init_db(session)