import os import pymysql import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "car_inventory.settings") django.setup() import json from datetime import datetime from tqdm import tqdm from inventory.models import ( CarMake, CarModel, CarSerie, CarTrim, CarEquipment, CarSpecification, CarSpecificationValue, CarOption, CarOptionValue ) # Step 1: Perform MySQL Dump def dump_mysql_database(): print("Starting MySQL dump...") db_user = "root" db_password = "Kfsh&rc9788" db_name = "trucks2db" dump_file = f"trucks2db_{datetime.now().strftime('%Y%m%d')}.sql" os.system(f"mysqldump -u {db_user} -p{db_password} {db_name} > {dump_file}") print(f"MySQL dump completed: {dump_file}") # Step 2: Connect to MySQL and export data to JSON def export_database_to_json(): print("Starting export to JSON...") db_config = { 'host': 'localhost', 'user': 'root', 'password': 'Kfsh&rc9788', 'database': 'trucks2db', } connection = pymysql.connect(**db_config) cursor = connection.cursor() cursor.execute("SHOW TABLES") tables = cursor.fetchall() database_json = {} for table in tqdm(tables, desc="Exporting tables"): table_name = table[0] cursor.execute(f"SELECT * FROM {table_name}") rows = cursor.fetchall() columns = [col[0] for col in cursor.description] database_json[table_name] = [dict(zip(columns, row)) for row in rows] json_file_name = "trucks20250101.json" with open(json_file_name, "w") as file: json.dump(database_json, file, indent=4, ensure_ascii=False) connection.close() print(f"Database exported to JSON successfully: {json_file_name}") return json_file_name # Step 3: Run the JSON processing script (previously created) def process_json_data(json_file_name): print(f"Starting JSON processing for file: {json_file_name}") with open(json_file_name, "r") as file: data = json.load(file) # Step 1: Populate CarMake for item in tqdm(data["car_make"], desc="Populating CarMake"): CarMake.objects.update_or_create( id_car_make=item["id_car_make"], defaults={ "name": item["name"], "arabic_name": item.get("arabic_name", ""), "logo": item.get("Logo", ""), "is_sa_import": item.get("is_sa_import", False), } ) # Step 2: Populate CarModel for item in tqdm(data["car_model"], desc="Populating CarModel"): CarMake.objects.get(id_car_make=item["id_car_make"]) # Ensures foreign key exists CarModel.objects.update_or_create( id_car_model=item["id_car_model"], defaults={ "id_car_make_id": item["id_car_make"], "name": item["name"], "arabic_name": item.get("arabic_name", ""), } ) # Step 3: Populate CarSerie for item in tqdm(data["car_serie"], desc="Populating CarSerie"): CarModel.objects.get(id_car_model=item["id_car_model"]) # Ensures foreign key exists CarSerie.objects.update_or_create( id_car_serie=item["id_car_serie"], defaults={ "id_car_model_id": item["id_car_model"], "name": item["name"], "arabic_name": item.get("arabic_name", ""), "year_begin": item.get("year_begin"), "year_end": item.get("year_end"), "generation_name": item.get("generation_name", ""), } ) # Step 4: Populate CarTrim for item in tqdm(data["car_trim"], desc="Populating CarTrim"): CarSerie.objects.get(id_car_serie=item["id_car_serie"]) # Ensures foreign key exists CarTrim.objects.update_or_create( id_car_trim=item["id_car_trim"], defaults={ "id_car_serie_id": item["id_car_serie"], "name": item["name"], "arabic_name": item.get("arabic_name", ""), "start_production_year": item["start_production_year"], "end_production_year": item["end_production_year"], "date_create": item["date_create"], "date_update": item["date_update"], "id_car_type": item.get("id_car_type", 1), } ) # Step 5: Populate CarEquipment for item in tqdm(data["car_equipment"], desc="Populating CarEquipment"): CarTrim.objects.get(id_car_trim=item["id_car_trim"]) # Ensures foreign key exists CarEquipment.objects.update_or_create( id_car_equipment=item["id_car_equipment"], defaults={ "id_car_trim_id": item["id_car_trim"], "name": item["name"], "year_begin": item.get("year"), } ) # Step 6: Populate CarSpecification for item in tqdm(data["car_specification"], desc="Populating CarSpecification"): CarSpecification.objects.update_or_create( id_car_specification=item["id_car_specification"], defaults={ "name": item["name"], "arabic_name": item.get("arabic_name", ""), "id_parent_id": item.get("id_parent"), } ) # Step 7: Populate CarSpecificationValue for item in tqdm(data["car_specification_value"], desc="Populating CarSpecificationValue"): CarTrim.objects.get(id_car_trim=item["id_car_trim"]) # Ensures foreign key exists CarSpecification.objects.get(id_car_specification=item["id_car_specification"]) # Ensures foreign key exists CarSpecificationValue.objects.update_or_create( id_car_specification_value=item["id_car_specification_value"], defaults={ "id_car_trim_id": item["id_car_trim"], "id_car_specification_id": item["id_car_specification"], "value": item["value"], "unit": item.get("unit", ""), } ) # Step 8: Populate CarOption for item in tqdm(data["car_option"], desc="Populating CarOption"): CarOption.objects.update_or_create( id_car_option=item["id_car_option"], defaults={ "name": item["name"], "arabic_name": item.get("arabic_name", ""), "id_parent_id": item.get("id_parent"), } ) # Step 9: Populate CarOptionValue for item in tqdm(data["car_option_value"], desc="Populating CarOptionValue"): CarEquipment.objects.get(id_car_equipment=item["id_car_equipment"]) # Ensures foreign key exists CarOption.objects.get(id_car_option=item["id_car_option"]) # Ensures foreign key exists CarOptionValue.objects.update_or_create( id_car_option_value=item["id_car_option_value"], defaults={ "id_car_option_id": item["id_car_option"], "id_car_equipment_id": item["id_car_equipment"], "is_base": item["is_base"], } ) print("Data population completed.") os.system(f"python process_car_data.py {json_file_name}") print("JSON processing completed.") # Main function to run all steps def main(): dump_mysql_database() # Step 1: Dump the database json_file_name = export_database_to_json() # Step 2: Export to JSON # process_json_data(json_file_name) # Step 3: Process the JSON if __name__ == "__main__": main()