44from datetime import datetime
55from sqlite3 import OperationalError
66
7- from src .filepath import BACKUP_FOLDER , DATABASE_PATH
7+ from src .filepath import BACKUP_FOLDER , DATABASE_PATH , DEFAULT_DATABASE_PATH
88from src .logger_handler import LoggerHandler
99
1010_logger = LoggerHandler ("update_data_module" )
1111
1212
1313def execute_raw_sql (query : str , params : tuple = ()):
1414 """Execute raw SQL query using sqlite3."""
15+ if not DATABASE_PATH .exists ():
16+ _logger .log_event ("INFO" , f"Copying default database from { DEFAULT_DATABASE_PATH } to { DATABASE_PATH } " )
17+ shutil .copyfile (DEFAULT_DATABASE_PATH , DATABASE_PATH )
1518 with sqlite3 .connect (DATABASE_PATH ) as connection :
1619 cursor = connection .cursor ()
1720 cursor .execute (query , params )
@@ -177,16 +180,13 @@ def fix_amount_in_recipe():
177180 )
178181
179182
180- def remove_is_alcoholic_and_hand_from_recipe_data ():
181- """Remove the is_alcoholic and hand columns from the RecipeData table."""
182- _logger .log_event ("INFO" , "Removing is_alcoholic and hand columns from RecipeData DB" )
183+ def remove_hand_from_recipe_data ():
184+ """Remove the hand columns from the RecipeData table."""
185+ _logger .log_event ("INFO" , "Removing hand columns from RecipeData DB" )
183186 try :
184- execute_raw_sql ("ALTER TABLE RecipeData DROP COLUMN Is_alcoholic;" )
185187 execute_raw_sql ("ALTER TABLE RecipeData DROP COLUMN Hand;" )
186188 except OperationalError :
187- _logger .log_event (
188- "ERROR" , "Could not remove is_alcoholic and hand columns from DB, this may because they do not exist"
189- )
189+ _logger .log_event ("ERROR" , "Could not remove hand columns from DB, this may because they do not exist" )
190190
191191
192192def add_foreign_keys ():
@@ -207,17 +207,15 @@ def add_foreign_keys():
207207 Recipe_ID INTEGER NOT NULL,
208208 Ingredient_ID INTEGER NOT NULL,
209209 Amount INTEGER NOT NULL,
210- Is_alcoholic BOOLEAN,
211- Hand BOOLEAN,
212210 Recipe_Order INTEGER DEFAULT 1,
213211 PRIMARY KEY (Recipe_ID, Ingredient_ID),
214212 FOREIGN KEY (Recipe_ID) REFERENCES Recipes(ID) ON DELETE CASCADE,
215213 FOREIGN KEY (Ingredient_ID) REFERENCES Ingredients(ID) ON DELETE RESTRICT
216214 );
217215 """ )
218216 execute_raw_sql ("""
219- INSERT INTO RecipeData_new (Recipe_ID, Ingredient_ID, Amount, Is_alcoholic, Hand, Recipe_Order)
220- SELECT Recipe_ID, Ingredient_ID, Amount, Is_alcoholic, Hand, Recipe_Order FROM RecipeData;
217+ INSERT INTO RecipeData_new (Recipe_ID, Ingredient_ID, Amount, Recipe_Order)
218+ SELECT Recipe_ID, Ingredient_ID, Amount, Recipe_Order FROM RecipeData;
221219 """ )
222220 execute_raw_sql ("DROP TABLE RecipeData;" )
223221 execute_raw_sql ("ALTER TABLE RecipeData_new RENAME TO RecipeData;" )
0 commit comments