mirror of
				https://github.com/mealie-recipes/mealie.git
				synced 2025-10-31 10:13:32 -04:00 
			
		
		
		
	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:
		| @@ -69,9 +69,11 @@ def _resolve_duplicate_food( | |||||||
|     ): |     ): | ||||||
|         recipe_ingredient.food_id = keep_food_id |         recipe_ingredient.food_id = keep_food_id | ||||||
|  |  | ||||||
|  |     session.commit() | ||||||
|     session.execute( |     session.execute( | ||||||
|         sa.text(f"DELETE FROM {IngredientFoodModel.__tablename__} WHERE id=:id").bindparams(id=dupe_food_id) |         sa.text(f"DELETE FROM {IngredientFoodModel.__tablename__} WHERE id=:id").bindparams(id=dupe_food_id) | ||||||
|     ) |     ) | ||||||
|  |     session.commit() | ||||||
|  |  | ||||||
|  |  | ||||||
| def _resolve_duplicate_unit( | 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(): |     for recipe_ingredient in session.query(RecipeIngredientModel).filter_by(unit_id=dupe_unit_id).all(): | ||||||
|         recipe_ingredient.unit_id = keep_unit_id |         recipe_ingredient.unit_id = keep_unit_id | ||||||
|  |  | ||||||
|  |     session.commit() | ||||||
|     session.execute( |     session.execute( | ||||||
|         sa.text(f"DELETE FROM {IngredientUnitModel.__tablename__} WHERE id=:id").bindparams(id=dupe_unit_id) |         sa.text(f"DELETE FROM {IngredientUnitModel.__tablename__} WHERE id=:id").bindparams(id=dupe_unit_id) | ||||||
|     ) |     ) | ||||||
|  |     session.commit() | ||||||
|  |  | ||||||
|  |  | ||||||
| def _resolve_duplicate_label( | 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(): |     for ingredient_food in session.query(IngredientFoodModel).filter_by(label_id=dupe_label_id).all(): | ||||||
|         ingredient_food.label_id = keep_label_id |         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.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): | 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.execute(query) | ||||||
|  |     session.commit() | ||||||
|  |  | ||||||
|  |  | ||||||
| def _remove_duplicates_from_m2m_tables(session: Session, table_metas: list[TableMeta]): | def _remove_duplicates_from_m2m_tables(session: Session, table_metas: list[TableMeta]): | ||||||
|   | |||||||
| @@ -4,13 +4,16 @@ CWD = Path(__file__).parent | |||||||
|  |  | ||||||
| locale_dir = CWD / "locale" | 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""" | """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""" | """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""" | """bcfdad6b7355: remove tool name and slug unique contraints""" | ||||||
|  |  | ||||||
| migrations_paprika = CWD / "migrations/paprika.zip" | migrations_paprika = CWD / "migrations/paprika.zip" | ||||||
|   | |||||||
							
								
								
									
										
											BIN
										
									
								
								tests/data/backups/backup_version_44e8d670719d_2.zip
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								tests/data/backups/backup_version_44e8d670719d_2.zip
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| @@ -71,14 +71,16 @@ def test_database_restore(): | |||||||
| @pytest.mark.parametrize( | @pytest.mark.parametrize( | ||||||
|     "backup_path", |     "backup_path", | ||||||
|     [ |     [ | ||||||
|         test_data.backup_version_44e8d670719d, |         test_data.backup_version_44e8d670719d_1, | ||||||
|         test_data.backup_version_ba1e4a6cfe99, |         test_data.backup_version_44e8d670719d_2, | ||||||
|         test_data.backup_version_bcfdad6b7355, |         test_data.backup_version_ba1e4a6cfe99_1, | ||||||
|  |         test_data.backup_version_bcfdad6b7355_1, | ||||||
|     ], |     ], | ||||||
|     ids=[ |     ids=[ | ||||||
|         "44e8d670719d: add extras to shopping lists, list items, and ingredient foods", |         "44e8d670719d_1: add extras to shopping lists, list items, and ingredient foods", | ||||||
|         "ba1e4a6cfe99: added plural names and alias tables for foods and units", |         "44e8d670719d_2: add extras to shopping lists, list items, and ingredient foods", | ||||||
|         "bcfdad6b7355: remove tool name and slug unique contraints", |         "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): | def test_database_restore_data(backup_path: Path): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user