|
| 1 | +import mysql.connector |
| 2 | +from django.core.management.base import BaseCommand, CommandError |
| 3 | + |
| 4 | +from core.utils.logs_helpers import log_command_event |
| 5 | + |
| 6 | + |
| 7 | +def log_event(info: str): |
| 8 | + log_command_event(command_name="import_from_lucca", info=info) |
| 9 | + |
| 10 | + |
| 11 | +class Command(BaseCommand): |
| 12 | + help = "Import data from Lucca analytics database" |
| 13 | + |
| 14 | + def add_arguments(self, parser): |
| 15 | + parser.add_argument( |
| 16 | + "--db-host", |
| 17 | + type=str, |
| 18 | + help="Database host address", |
| 19 | + default="lucca-analy-5318.mysql.c.osc-fr1.scalingo-dbs.com", |
| 20 | + ) |
| 21 | + parser.add_argument( |
| 22 | + "--db-name", type=str, help="Database name", default="stats" |
| 23 | + ) |
| 24 | + parser.add_argument( |
| 25 | + "--db-user", type=str, help="Database user", default="lucca_analy_5318" |
| 26 | + ) |
| 27 | + parser.add_argument( |
| 28 | + "--db-port", type=int, default=36355, help="Database port (default: 36355)" |
| 29 | + ) |
| 30 | + parser.add_argument( |
| 31 | + "--db-password", type=str, required=True, help="Database password" |
| 32 | + ) |
| 33 | + |
| 34 | + def handle(self, *args, **options): |
| 35 | + db_host = options["db_host"] |
| 36 | + db_name = options["db_name"] |
| 37 | + db_user = options["db_user"] |
| 38 | + db_password = options["db_password"] |
| 39 | + db_port = options["db_port"] |
| 40 | + |
| 41 | + connection = None |
| 42 | + cursor = None |
| 43 | + |
| 44 | + try: |
| 45 | + # Establish SSL connection to remote MySQL database |
| 46 | + log_event( |
| 47 | + f"Connecting to MySQL database {db_name} at {db_host}:{db_port} with SSL..." |
| 48 | + ) |
| 49 | + connection = mysql.connector.connect( |
| 50 | + host=db_host, |
| 51 | + database=db_name, |
| 52 | + user=db_user, |
| 53 | + password=db_password, |
| 54 | + port=db_port, |
| 55 | + ssl_disabled=False, |
| 56 | + ssl_verify_cert=False, |
| 57 | + ) |
| 58 | + cursor = connection.cursor() |
| 59 | + log_event("Database connection established successfully") |
| 60 | + |
| 61 | + # Example: Run a test query |
| 62 | + cursor.execute("SELECT VERSION();") |
| 63 | + db_version = cursor.fetchone() |
| 64 | + if db_version: |
| 65 | + log_event("Successfuly connected to the db") |
| 66 | + |
| 67 | + import pdb |
| 68 | + |
| 69 | + pdb.set_trace() |
| 70 | + log_event("Import completed successfully") |
| 71 | + |
| 72 | + except mysql.connector.Error as e: |
| 73 | + log_event(f"Database error: {e}") |
| 74 | + raise CommandError(f"Database connection failed: {e}") |
| 75 | + |
| 76 | + except Exception as e: |
| 77 | + log_event(f"Unexpected error: {e}") |
| 78 | + raise CommandError(f"Import failed: {e}") |
| 79 | + |
| 80 | + finally: |
| 81 | + # Clean up database connections |
| 82 | + if cursor: |
| 83 | + cursor.close() |
| 84 | + if connection: |
| 85 | + connection.close() |
| 86 | + log_event("Database connection closed") |
0 commit comments