fix: Migration Failure On Postgres Due To Foreign Key Error (#2923)

* added test data to (hopefully) expose fk error

* added additional commits during migration

---------

Co-authored-by: Kuchenpirat <24235032+Kuchenpirat@users.noreply.github.com>
This commit is contained in:
Michael Genson 2024-01-08 05:01:44 -06:00 committed by GitHub
parent b1fa089236
commit 8ca5a9454e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 9 deletions

View File

@ -69,9 +69,11 @@ def _resolve_duplicate_food(
):
recipe_ingredient.food_id = keep_food_id
session.commit()
session.execute(
sa.text(f"DELETE FROM {IngredientFoodModel.__tablename__} WHERE id=:id").bindparams(id=dupe_food_id)
)
session.commit()
def _resolve_duplicate_unit(
@ -85,9 +87,11 @@ def _resolve_duplicate_unit(
for recipe_ingredient in session.query(RecipeIngredientModel).filter_by(unit_id=dupe_unit_id).all():
recipe_ingredient.unit_id = keep_unit_id
session.commit()
session.execute(
sa.text(f"DELETE FROM {IngredientUnitModel.__tablename__} WHERE id=:id").bindparams(id=dupe_unit_id)
)
session.commit()
def _resolve_duplicate_label(
@ -101,7 +105,9 @@ def _resolve_duplicate_label(
for ingredient_food in session.query(IngredientFoodModel).filter_by(label_id=dupe_label_id).all():
ingredient_food.label_id = keep_label_id
session.commit()
session.execute(sa.text(f"DELETE FROM {MultiPurposeLabel.__tablename__} WHERE id=:id").bindparams(id=dupe_label_id))
session.commit()
def _resolve_duplicate_foods_units_labels(session: Session):
@ -140,6 +146,7 @@ def _remove_duplicates_from_m2m_table(session: Session, table_meta: TableMeta):
)
session.execute(query)
session.commit()
def _remove_duplicates_from_m2m_tables(session: Session, table_metas: list[TableMeta]):

View File

@ -4,13 +4,16 @@ CWD = Path(__file__).parent
locale_dir = CWD / "locale"
backup_version_44e8d670719d = CWD / "backups/backup_version_44e8d670719d.zip"
backup_version_44e8d670719d_1 = CWD / "backups/backup_version_44e8d670719d_1.zip"
"""44e8d670719d: add extras to shopping lists, list items, and ingredient foods"""
backup_version_ba1e4a6cfe99 = CWD / "backups/backup_version_ba1e4a6cfe99.zip"
backup_version_44e8d670719d_2 = CWD / "backups/backup_version_44e8d670719d_2.zip"
"""44e8d670719d: add extras to shopping lists, list items, and ingredient foods"""
backup_version_ba1e4a6cfe99_1 = CWD / "backups/backup_version_ba1e4a6cfe99_1.zip"
"""ba1e4a6cfe99: added plural names and alias tables for foods and units"""
backup_version_bcfdad6b7355 = CWD / "backups/backup_version_bcfdad6b7355.zip"
backup_version_bcfdad6b7355_1 = CWD / "backups/backup_version_bcfdad6b7355_1.zip"
"""bcfdad6b7355: remove tool name and slug unique contraints"""
migrations_paprika = CWD / "migrations/paprika.zip"

Binary file not shown.

View File

@ -71,14 +71,16 @@ def test_database_restore():
@pytest.mark.parametrize(
"backup_path",
[
test_data.backup_version_44e8d670719d,
test_data.backup_version_ba1e4a6cfe99,
test_data.backup_version_bcfdad6b7355,
test_data.backup_version_44e8d670719d_1,
test_data.backup_version_44e8d670719d_2,
test_data.backup_version_ba1e4a6cfe99_1,
test_data.backup_version_bcfdad6b7355_1,
],
ids=[
"44e8d670719d: add extras to shopping lists, list items, and ingredient foods",
"ba1e4a6cfe99: added plural names and alias tables for foods and units",
"bcfdad6b7355: remove tool name and slug unique contraints",
"44e8d670719d_1: add extras to shopping lists, list items, and ingredient foods",
"44e8d670719d_2: add extras to shopping lists, list items, and ingredient foods",
"ba1e4a6cfe99_1: added plural names and alias tables for foods and units",
"bcfdad6b7355_1: remove tool name and slug unique contraints",
],
)
def test_database_restore_data(backup_path: Path):