|
| 1 | +import codegen |
| 2 | +from codegen import Codebase |
| 3 | +from codegen.sdk.core.detached_symbols.function_call import FunctionCall |
| 4 | +import subprocess |
| 5 | +import shutil |
| 6 | +import os |
| 7 | + |
| 8 | + |
| 9 | +def init_git_repo(repo_path: str) -> None: |
| 10 | + """Initialize a git repository in the given path.""" |
| 11 | + subprocess.run(["git", "init"], cwd=repo_path, check=True) |
| 12 | + subprocess.run(["git", "add", "."], cwd=repo_path, check=True) |
| 13 | + subprocess.run(["git", "commit", "-m", "Initial commit"], cwd=repo_path, check=True) |
| 14 | + |
| 15 | + |
| 16 | +def cleanup_git_repo(repo_path: str) -> None: |
| 17 | + """Remove the .git directory from the given path.""" |
| 18 | + git_dir = os.path.join(repo_path, ".git") |
| 19 | + if os.path.exists(git_dir): |
| 20 | + shutil.rmtree(git_dir) |
| 21 | + |
| 22 | + |
| 23 | +@codegen.function("sqlalchemy-type-annotations") |
| 24 | +def run(codebase: Codebase): |
| 25 | + """Add Mapped types to SQLAlchemy models in a codebase. |
| 26 | +
|
| 27 | + This codemod: |
| 28 | + 1. Finds all SQLAlchemy model classes |
| 29 | + 2. Converts Column type annotations to Mapped types |
| 30 | + 3. Adds necessary imports for the new type annotations |
| 31 | + """ |
| 32 | + # Define type mapping |
| 33 | + column_type_to_mapped_type = { |
| 34 | + "Integer": "Mapped[int]", |
| 35 | + "Optional[Integer]": "Mapped[int | None]", |
| 36 | + "Boolean": "Mapped[bool]", |
| 37 | + "Optional[Boolean]": "Mapped[bool | None]", |
| 38 | + "DateTime": "Mapped[datetime | None]", |
| 39 | + "Optional[DateTime]": "Mapped[datetime | None]", |
| 40 | + "String": "Mapped[str]", |
| 41 | + "Optional[String]": "Mapped[str | None]", |
| 42 | + "Numeric": "Mapped[Decimal]", |
| 43 | + "Optional[Numeric]": "Mapped[Decimal | None]", |
| 44 | + } |
| 45 | + |
| 46 | + # Track statistics |
| 47 | + classes_modified = 0 |
| 48 | + attributes_modified = 0 |
| 49 | + |
| 50 | + # Traverse the codebase classes |
| 51 | + for cls in codebase.classes: |
| 52 | + class_modified = False |
| 53 | + original_source = cls.source # Store original source before modifications |
| 54 | + |
| 55 | + for attribute in cls.attributes: |
| 56 | + if not attribute.assignment: |
| 57 | + continue |
| 58 | + |
| 59 | + assignment_value = attribute.assignment.value |
| 60 | + if not isinstance(assignment_value, FunctionCall): |
| 61 | + continue |
| 62 | + |
| 63 | + if assignment_value.name != "Column": |
| 64 | + continue |
| 65 | + |
| 66 | + db_column_call = assignment_value |
| 67 | + |
| 68 | + # Make sure we have at least one argument (the type) |
| 69 | + if len(db_column_call.args) == 0: |
| 70 | + continue |
| 71 | + |
| 72 | + # Check for nullable=True |
| 73 | + is_nullable = any( |
| 74 | + x.name == "nullable" and x.value == "True" for x in db_column_call.args |
| 75 | + ) |
| 76 | + |
| 77 | + # Extract the first argument for the column type |
| 78 | + first_argument = db_column_call.args[0].source or "" |
| 79 | + first_argument = first_argument.split("(")[0].strip() |
| 80 | + |
| 81 | + # If the type is namespaced (e.g. sa.Integer), get the last part |
| 82 | + if "." in first_argument: |
| 83 | + first_argument = first_argument.split(".")[-1] |
| 84 | + |
| 85 | + # If nullable, wrap the type in Optional[...] |
| 86 | + if is_nullable: |
| 87 | + first_argument = f"Optional[{first_argument}]" |
| 88 | + |
| 89 | + # Check if we have a corresponding mapped type |
| 90 | + if first_argument not in column_type_to_mapped_type: |
| 91 | + print(f"Skipping unmapped type: {first_argument}") |
| 92 | + continue |
| 93 | + |
| 94 | + # Build the new mapped type annotation |
| 95 | + new_type = column_type_to_mapped_type[first_argument] |
| 96 | + |
| 97 | + # Update the assignment type annotation |
| 98 | + attribute.assignment.set_type_annotation(new_type) |
| 99 | + attributes_modified += 1 |
| 100 | + class_modified = True |
| 101 | + |
| 102 | + # Add necessary imports |
| 103 | + if not cls.file.has_import("Mapped"): |
| 104 | + cls.file.add_import_from_import_string( |
| 105 | + "from sqlalchemy.orm import Mapped\n" |
| 106 | + ) |
| 107 | + |
| 108 | + if "Optional" in new_type and not cls.file.has_import("Optional"): |
| 109 | + cls.file.add_import_from_import_string("from typing import Optional\n") |
| 110 | + |
| 111 | + if "Decimal" in new_type and not cls.file.has_import("Decimal"): |
| 112 | + cls.file.add_import_from_import_string("from decimal import Decimal\n") |
| 113 | + |
| 114 | + if "datetime" in new_type and not cls.file.has_import("datetime"): |
| 115 | + cls.file.add_import_from_import_string( |
| 116 | + "from datetime import datetime\n" |
| 117 | + ) |
| 118 | + |
| 119 | + if class_modified: |
| 120 | + classes_modified += 1 |
| 121 | + # Print the diff for this class |
| 122 | + print(f"\nModified class: {cls.name}") |
| 123 | + print("Before:") |
| 124 | + print(original_source) |
| 125 | + print("\nAfter:") |
| 126 | + print(cls.source) |
| 127 | + print("-" * 80) |
| 128 | + |
| 129 | + print("\nModification complete:") |
| 130 | + print(f"Classes modified: {classes_modified}") |
| 131 | + print(f"Attributes modified: {attributes_modified}") |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + input_repo = "./input_repo" |
| 136 | + print("Initializing git repository...") |
| 137 | + init_git_repo(input_repo) |
| 138 | + |
| 139 | + print("Initializing codebase...") |
| 140 | + codebase = Codebase(input_repo) |
| 141 | + |
| 142 | + print("Running codemod...") |
| 143 | + run(codebase) |
| 144 | + |
| 145 | + print("Cleaning up git repository...") |
| 146 | + cleanup_git_repo(input_repo) |
0 commit comments