Skip to content

Instantly share code, notes, and snippets.

@ab5y
Last active April 25, 2024 17:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ab5y/925d2cc42c99fff48d007a85df0ee5c5 to your computer and use it in GitHub Desktop.
Save ab5y/925d2cc42c99fff48d007a85df0ee5c5 to your computer and use it in GitHub Desktop.
sqlacodegen tool to output stdout to files in models directory
import subprocess
import os
import re
# Run sqlacodegen and capture the stdout output
sqlacodegen_output = subprocess.check_output(["sqlacodegen", "your_database_connection_string"], universal_newlines=True)
# Split the output into individual model sections
model_sections = sqlacodegen_output.split('\n\n')
# Create a directory to store the model files
output_directory = "models"
os.makedirs(output_directory, exist_ok=True)
# Define a function to convert class name to lowercase filename
def class_name_to_filename(model_section):
# Use a regular expression to find the class name
class_name_match = re.search(r'class (\w+)', model_section)
if class_name_match:
class_name = class_name_match.group(1)
# Convert the class name to lowercase for the filename
filename = f"{class_name.lower()}.py"
return filename, class_name
else:
return None, None
# Create lists to store the filenames and class names
model_filenames = []
model_class_names = []
# Loop through each model section and save it to a separate file
for model_section in model_sections:
# Extract the class name from the model section
filename, class_name = class_name_to_filename(model_section)
if filename and class_name:
# Create a filename for the model and save it
with open(os.path.join(output_directory, filename), "w") as file:
file.write(model_section)
model_filenames.append(filename)
model_class_names.append(class_name)
# Create an __init__.py file in the "models" directory
init_file_path = os.path.join(output_directory, "__init__.py")
# Generate import statements with camel-cased class names
import_statements = [f"from .{filename[:-3]} import {model_class}" for filename, model_class in zip(model_filenames, model_class_names)]
# Write the import statements to the __init__.py file
with open(init_file_path, "w") as init_file:
init_file.write("\n".join(import_statements))
print(f"{len(model_sections)} model files and an __init__.py file saved in the '{output_directory}' directory.")
@devspacenine
Copy link

model_sections = sqlacodegen_output.split('\n\n')

Line 9 should split by 3 line breaks, rather than 2. Otherwise you end up with model classes that don't include fields.

model_sections = sqlacodegen_output.split('\n\n\n')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment