Only drop indexes if migrations are run (#515)

This commit is contained in:
Tessa Walsh 2023-01-25 20:46:10 -05:00 committed by GitHub
parent 7dc85cc33c
commit 9f0abd6a28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 5 deletions

View File

@ -66,8 +66,8 @@ async def update_and_prepare_db(
Run all tasks in order in a single worker.
"""
await run_db_migrations(mdb)
await drop_indexes(mdb)
if await run_db_migrations(mdb):
await drop_indexes(mdb)
await create_indexes(org_ops, crawl_config_ops, crawls_ops, coll_ops)
await user_manager.create_super_user()
await org_ops.create_default_org()
@ -77,6 +77,7 @@ async def update_and_prepare_db(
# ============================================================================
async def run_db_migrations(mdb):
"""Run database migrations."""
migrations_run = False
migrations_path = "/app/btrixcloud/migrations"
module_files = [
f
@ -95,12 +96,14 @@ async def run_db_migrations(mdb):
migration_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(migration_module)
migration = migration_module.Migration(mdb)
await migration.run()
if await migration.run():
migrations_run = True
except ImportError as err:
print(
f"Error importing Migration class from module {module_file}: {err}",
flush=True,
)
return migrations_run
# ============================================================================

View File

@ -112,10 +112,11 @@ class Migration:
await self.set_db_version()
except OperationFailure as err:
print(f"Error running migration {self.MIGRATION_VERSION}: {err}")
return
return False
else:
print("No migration to apply - skipping", flush=True)
return
return False
print(f"Database successfully migrated to {self.MIGRATION_VERSION}", flush=True)
return True