Skip to content
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions sqlalchemy_1.6_to_2.0/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# SQLAlchemy 1.6 to 2.0 Migration Example

[![Documentation](https://img.shields.io/badge/docs-docs.codegen.com-blue)](https://docs.codegen.com/tutorials/sqlalchemy-1.6-to-2.0)
This codemod demonstrates how to use Codegen to automatically migrate SQLAlchemy 1.6 code to the new 2.0-style query interface. For a complete walkthrough, check out our [tutorial](https://docs.codegen.com/tutorials/sqlalchemy-1.6-to-2.0).

This example demonstrates how to use Codegen to automatically migrate SQLAlchemy 1.6 code to the new 2.0-style query interface. For a complete walkthrough, check out our [tutorial](https://docs.codegen.com/tutorials/sqlalchemy-1.6-to-2.0).
## How the Migration Script Works

## What This Example Does

The migration script handles four key transformations:
The codemod script handles four key transformations:

1. **Convert Query to Select**
```python
Expand All @@ -18,6 +16,7 @@ The migration script handles four key transformations:
select(User).where(User.name == 'john')
).scalars().all()
```
This transformation replaces the legacy Query interface with the new Select-based API, providing better type safety and consistency.

2. **Update Session Execution**
```python
Expand All @@ -29,6 +28,7 @@ The migration script handles four key transformations:
users = session.execute(select(User)).scalars().all()
first_user = session.execute(select(User)).scalars().first()
```
Session execution is updated to use the new execute() method, which provides clearer separation between SQL construction and execution.

3. **Modernize ORM Relationships**
```python
Expand All @@ -42,6 +42,7 @@ The migration script handles four key transformations:
class Address(Base):
user = relationship("User", back_populates="addresses")
```
Relationships are modernized to use explicit back_populates instead of backref, making bidirectional relationships more maintainable and explicit.

4. **Add Type Annotations**
```python
Expand All @@ -59,6 +60,7 @@ The migration script handles four key transformations:
name: Mapped[str] = mapped_column()
addresses: Mapped[List["Address"]] = relationship()
```
Type annotations are added using SQLAlchemy 2.0's Mapped[] syntax, enabling better IDE support and runtime type checking.

## Running the Example

Expand All @@ -70,13 +72,7 @@ pip install codegen
python run.py
```

The script will process all Python files in the `repo-before` directory and apply the transformations in the correct order.

## Understanding the Code

- `run.py` - The migration script
- `repo-before/` - Sample SQLAlchemy 1.6 application to migrate
- `guide.md` - Additional notes and explanations
The script will process all Python files in the `input_repo` directory and apply the transformations in the correct order.

## Learn More

Expand Down
96 changes: 0 additions & 96 deletions sqlalchemy_1.6_to_2.0/guide.md

This file was deleted.

4 changes: 3 additions & 1 deletion sqlalchemy_1.6_to_2.0/input_repo/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

SQLALCHEMY_DATABASE_URL = "postgresql://user:password@localhost/dbname" # Change to your database URL
SQLALCHEMY_DATABASE_URL = (
"postgresql://user:password@localhost/dbname" # Change to your database URL
)

engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Expand Down
5 changes: 3 additions & 2 deletions sqlalchemy_1.6_to_2.0/input_repo/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from sqlalchemy import Column, Integer, String, ForeignKey, Index
from sqlalchemy.orm import relationship, backref
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from database import Base


class Publisher(Base):
__tablename__ = "publishers"

Expand Down
11 changes: 0 additions & 11 deletions sqlalchemy_1.6_to_2.0/output_repo/database.py

This file was deleted.

101 changes: 0 additions & 101 deletions sqlalchemy_1.6_to_2.0/output_repo/main.py

This file was deleted.

32 changes: 0 additions & 32 deletions sqlalchemy_1.6_to_2.0/output_repo/models.py

This file was deleted.

31 changes: 0 additions & 31 deletions sqlalchemy_1.6_to_2.0/output_repo/schemas.py

This file was deleted.

Loading