|
| 1 | +# SQLAlchemy Soft Delete Codemod |
| 2 | + |
| 3 | +This codemod automatically adds soft delete conditions to SQLAlchemy join queries in your codebase. It ensures that joins only include non-deleted records by adding appropriate `deleted_at` checks. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The codemod analyzes your codebase and automatically adds soft delete conditions to SQLAlchemy join methods (`join`, `outerjoin`, `innerjoin`) for specified models. This helps prevent accidentally including soft-deleted records in query results. |
| 8 | + |
| 9 | +## How It Works |
| 10 | + |
| 11 | +The codemod processes your codebase in several steps: |
| 12 | + |
| 13 | +1. **Join Detection** |
| 14 | + ```python |
| 15 | + def should_process_join_call(call, soft_delete_models, join_methods): |
| 16 | + if str(call.name) not in join_methods: |
| 17 | + return False |
| 18 | + |
| 19 | + call_args = list(call.args) |
| 20 | + if not call_args: |
| 21 | + return False |
| 22 | + |
| 23 | + model_name = str(call_args[0].value) |
| 24 | + return model_name in soft_delete_models |
| 25 | + ``` |
| 26 | + - Scans for SQLAlchemy join method calls (`join`, `outerjoin`, `innerjoin`) |
| 27 | + - Identifies joins involving soft-deletable models |
| 28 | + - Analyzes existing join conditions |
| 29 | + |
| 30 | +2. **Condition Addition** |
| 31 | + ```python |
| 32 | + def add_deleted_at_check(file, call, model_name): |
| 33 | + call_args = list(call.args) |
| 34 | + deleted_at_check = f"{model_name}.deleted_at.is_(None)" |
| 35 | + |
| 36 | + if len(call_args) == 1: |
| 37 | + call_args.append(deleted_at_check) |
| 38 | + return |
| 39 | + |
| 40 | + second_arg = call_args[1].value |
| 41 | + if isinstance(second_arg, FunctionCall) and second_arg.name == "and_": |
| 42 | + second_arg.args.append(deleted_at_check) |
| 43 | + else: |
| 44 | + call_args[1].edit(f"and_({second_arg.source}, {deleted_at_check})") |
| 45 | + ``` |
| 46 | + - Adds `deleted_at.is_(None)` checks to qualifying joins |
| 47 | + - Handles different join condition patterns: |
| 48 | + - Simple joins with no conditions |
| 49 | + - Joins with existing conditions (combines using `and_`) |
| 50 | + - Preserves existing conditions while adding soft delete checks |
| 51 | + |
| 52 | +3. **Import Management** |
| 53 | + ```python |
| 54 | + def ensure_and_import(file): |
| 55 | + if not any("and_" in imp.name for imp in file.imports): |
| 56 | + file.add_import_from_import_string("from sqlalchemy import and_") |
| 57 | + ``` |
| 58 | + - Automatically adds required SQLAlchemy imports (`and_`) |
| 59 | + - Prevents duplicate imports |
| 60 | + |
| 61 | +## Configuration |
| 62 | + |
| 63 | +### Soft Delete Models |
| 64 | + |
| 65 | +The codemod processes joins for the following models: |
| 66 | +```python |
| 67 | +soft_delete_models = { |
| 68 | + "User", |
| 69 | + "Update", |
| 70 | + "Proposal", |
| 71 | + "Comment", |
| 72 | + "Project", |
| 73 | + "Team", |
| 74 | + "SavedSession" |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### Join Methods |
| 79 | + |
| 80 | +The codemod handles these SQLAlchemy join methods: |
| 81 | +```python |
| 82 | +join_methods = {"join", "outerjoin", "innerjoin"} |
| 83 | +``` |
| 84 | + |
| 85 | +## Code Transformations |
| 86 | + |
| 87 | +### Simple Join with Model Reference |
| 88 | +```python |
| 89 | +# Before |
| 90 | +query.join(Project, Session.project) |
| 91 | + |
| 92 | +# After |
| 93 | +from sqlalchemy import and_ |
| 94 | +query.join(Project, and_(Session.project, Project.deleted_at.is_(None))) |
| 95 | +``` |
| 96 | + |
| 97 | +### Join with Column Equality |
| 98 | +```python |
| 99 | +# Before |
| 100 | +query.join(Project, Session.project_id == Project.id) |
| 101 | + |
| 102 | +# After |
| 103 | +from sqlalchemy import and_ |
| 104 | +query.join(Project, and_(Session.project_id == Project.id, Project.deleted_at.is_(None))) |
| 105 | +``` |
| 106 | + |
| 107 | +### Multiple Joins in Query Chain |
| 108 | +```python |
| 109 | +# Before |
| 110 | +Session.query.join(Project, Session.project)\ |
| 111 | + .join(Account, Project.account)\ |
| 112 | + .outerjoin(Proposal, Session.proposal) |
| 113 | + |
| 114 | +# After |
| 115 | +from sqlalchemy import and_ |
| 116 | +Session.query.join(Project, and_(Session.project, Project.deleted_at.is_(None)))\ |
| 117 | + .join(Account, Project.account)\ |
| 118 | + .outerjoin(Proposal, and_(Session.proposal, Proposal.deleted_at.is_(None))) |
| 119 | +``` |
| 120 | + |
| 121 | +## Graph Disable Mode |
| 122 | + |
| 123 | +This codemod includes support for running without the graph feature enabled. This is useful for the faster processing of large codebases and reduced memory usage. |
| 124 | + |
| 125 | +To run in no-graph mode: |
| 126 | +```python |
| 127 | +codebase = Codebase( |
| 128 | + str(repo_path), |
| 129 | + programming_language=ProgrammingLanguage.PYTHON, |
| 130 | + config=CodebaseConfig( |
| 131 | + feature_flags=GSFeatureFlags(disable_graph=True) |
| 132 | + ) |
| 133 | +) |
| 134 | +``` |
| 135 | + |
| 136 | +## Running the Conversion |
| 137 | + |
| 138 | +```bash |
| 139 | +# Install Codegen |
| 140 | +pip install codegen |
| 141 | + |
| 142 | +# Run the conversion |
| 143 | +python run.py |
| 144 | +``` |
| 145 | + |
| 146 | +## Learn More |
| 147 | + |
| 148 | +- [SQLAlchemy Documentation](https://docs.sqlalchemy.org/en/20/) |
| 149 | +- [Codegen Documentation](https://docs.codegen.com) |
| 150 | + |
| 151 | +## Contributing |
| 152 | + |
| 153 | +Feel free to submit issues and enhancement requests! |
0 commit comments