Skip to content

Commit c5dec9f

Browse files
Merge pull request #284 from AndreWohnsland/sqlalchemy
create local db in migrator if not exist
2 parents c264ab9 + fe5faf5 commit c5dec9f

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

src/migration/migrator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
add_virgin_flag_to_db,
4040
change_slower_flag_to_pump_speed,
4141
fix_amount_in_recipe,
42-
remove_is_alcoholic_and_hand_from_recipe_data,
42+
remove_hand_from_recipe_data,
4343
remove_is_alcoholic_column,
4444
remove_old_recipe_columns,
4545
rename_database_to_english,
@@ -136,7 +136,7 @@ def make_migrations(self):
136136
],
137137
"2.2.0": [
138138
add_foreign_keys,
139-
remove_is_alcoholic_and_hand_from_recipe_data,
139+
remove_hand_from_recipe_data,
140140
],
141141
}
142142

src/migration/update_data.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
from datetime import datetime
55
from 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
88
from src.logger_handler import LoggerHandler
99

1010
_logger = LoggerHandler("update_data_module")
1111

1212

1313
def 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

192192
def 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

Comments
 (0)