0

I am trying to generate SQLAlchemy classes automatically in runtime using AWS Lambda, but I am facing some issues. I want to use the sqlacodegen package to create the model code from an existing MySQL database. However, when I try to use the CodeGenerator.from_engine method, I get an error saying that it does not exist. Here is the code that I am using:

import os
from sqlalchemy import create_engine
from sqlacodegen.codegen import CodeGenerator

def lambda_handler(event, context):
    # Configure the database
    db_user = os.environ['DB_USER']
    db_password = os.environ['DB_PASSWORD']
    db_name = os.environ['DB_NAME']
    db_host = os.environ['DB_HOST']

    # Create the connection to the database
    engine = create_engine(f'mysql://{db_user}:{db_password}@{db_host}/{db_name}')

    # Generate the SQLAlchemy model code
    generator = CodeGenerator.from_engine(engine)
    code = generator.render()

    # Do something with the generated code
    print(code)

I found this code on the internet, but it seems that it is outdated or incorrect. How can I fix this problem and generate SQLAlchemy classes in runtime using AWS Lambda and sqlacodegen? Any help would be appreciated. Thank you.

EDIT: I need to use in runtime not with subprocess or os.system, i need to use on python line code.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470

0 Answers0