mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-08-04 21:15:22 +02:00
fix: Only run migration data fixes on migrations (#5038)
This commit is contained in:
parent
09234e3bf0
commit
6271b33b1b
1 changed files with 11 additions and 7 deletions
|
@ -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)
|
||||||
safe_try(lambda: fix_migration_data(session))
|
|
||||||
safe_try(lambda: fix_slug_food_names(db))
|
|
||||||
safe_try(lambda: fix_group_with_no_name(session))
|
|
||||||
|
|
||||||
if db.users.get_all():
|
if db.users.get_all():
|
||||||
logger.debug("Database exists")
|
logger.debug("Database exists")
|
||||||
|
if run_fixes:
|
||||||
|
safe_try(lambda: fix_migration_data(session))
|
||||||
|
safe_try(lambda: fix_slug_food_names(db))
|
||||||
|
safe_try(lambda: fix_group_with_no_name(session))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logger.info("Database contains no users, initializing...")
|
logger.info("Database contains no users, initializing...")
|
||||||
init_db(session)
|
init_db(session)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue