Change column types safely in SQLAlchemy
When changing a column type that requires a table rewrite, follow these steps:
1. Create a new column with the desired type
2. Write to both columns during the transition period
3. Backfill data from the old column to the new column
4. Move reads from the old column to the new column
5. Stop writing to the old column
6. Drop the old column
Install this rule for wispbit
Quick Install
Run this one command to automatically install the rule:
Manual install
Copy the rule
---
include: *.py
---
When changing a column type that requires a table rewrite, follow these steps:
1. Create a new column with the desired type
2. Write to both columns during the transition period
3. Backfill data from the old column to the new column
4. Move reads from the old column to the new column
5. Stop writing to the old column
6. Drop the old column
Bad:
```python
def upgrade():
# Directly changing a column type can cause table locks
op.alter_column('users', 'some_column',
type_=sa.String(50),
existing_type=sa.Integer())
def downgrade():
op.alter_column('users', 'some_column',
type_=sa.Integer(),
existing_type=sa.String(50))
```
Good:
```python
# Migration 1: Add new column
def upgrade():
# Adding a new column first
op.add_column('users', sa.Column('some_column_new', sa.String(50)))
def downgrade():
op.drop_column('users', 'some_column_new')
```
```python
# Migration 2: Complete the transition (after backfilling data)
def upgrade():
# After ensuring all data is migrated
op.drop_column('users', 'some_column')
op.alter_column('users', 'some_column_new',
new_column_name='some_column')
def downgrade():
op.alter_column('users', 'some_column',
new_column_name='some_column_new')
op.add_column('users', sa.Column('some_column', sa.Integer()))
```
Add the rule into your project
.wispbit/rules/sqlalchemy-change-column-type.md
Install this rule for Coderabbit
Copy the configuration below and add it to your repository as .coderabbit.yml
in your project root.
reviews:
path_instructions:
- path: "*.py"
instructions: |
When changing a column type that requires a table rewrite, follow these steps:
1. Create a new column with the desired type
2. Write to both columns during the transition period
3. Backfill data from the old column to the new column
4. Move reads from the old column to the new column
5. Stop writing to the old column
6. Drop the old column
Bad:
```python
def upgrade():
# Directly changing a column type can cause table locks
op.alter_column('users', 'some_column',
type_=sa.String(50),
existing_type=sa.Integer())
def downgrade():
op.alter_column('users', 'some_column',
type_=sa.Integer(),
existing_type=sa.String(50))
```
Good:
```python
# Migration 1: Add new column
def upgrade():
# Adding a new column first
op.add_column('users', sa.Column('some_column_new', sa.String(50)))
def downgrade():
op.drop_column('users', 'some_column_new')
```
```python
# Migration 2: Complete the transition (after backfilling data)
def upgrade():
# After ensuring all data is migrated
op.drop_column('users', 'some_column')
op.alter_column('users', 'some_column_new',
new_column_name='some_column')
def downgrade():
op.alter_column('users', 'some_column',
new_column_name='some_column_new')
op.add_column('users', sa.Column('some_column', sa.Integer()))
```
Install this rule for Greptile
Greptile rules can be added through the web interface. Please see this documentation for details on how to add custom rules and context.
When changing a column type that requires a table rewrite, follow these steps:
1. Create a new column with the desired type
2. Write to both columns during the transition period
3. Backfill data from the old column to the new column
4. Move reads from the old column to the new column
5. Stop writing to the old column
6. Drop the old column
Bad:
```python
def upgrade():
# Directly changing a column type can cause table locks
op.alter_column('users', 'some_column',
type_=sa.String(50),
existing_type=sa.Integer())
def downgrade():
op.alter_column('users', 'some_column',
type_=sa.Integer(),
existing_type=sa.String(50))
```
Good:
```python
# Migration 1: Add new column
def upgrade():
# Adding a new column first
op.add_column('users', sa.Column('some_column_new', sa.String(50)))
def downgrade():
op.drop_column('users', 'some_column_new')
```
```python
# Migration 2: Complete the transition (after backfilling data)
def upgrade():
# After ensuring all data is migrated
op.drop_column('users', 'some_column')
op.alter_column('users', 'some_column_new',
new_column_name='some_column')
def downgrade():
op.alter_column('users', 'some_column',
new_column_name='some_column_new')
op.add_column('users', sa.Column('some_column', sa.Integer()))
```
File Path Patterns:
Install this rule for GitHub Copilot
Copilot instructions can be added through the interface. See the documentation for details on how to create coding guidelines.
When changing a column type that requires a table rewrite, follow these steps:
1. Create a new column with the desired type
2. Write to both columns during the transition period
3. Backfill data from the old column to the new column
4. Move reads from the old column to the new column
5. Stop writing to the old column
6. Drop the old column
Bad:
```python
def upgrade():
# Directly changing a column type can cause table locks
op.alter_column('users', 'some_column',
type_=sa.String(50),
existing_type=sa.Integer())
def downgrade():
op.alter_column('users', 'some_column',
type_=sa.Integer(),
existing_type=sa.String(50))
```
Good:
```python
# Migration 1: Add new column
def upgrade():
# Adding a new column first
op.add_column('users', sa.Column('some_column_new', sa.String(50)))
def downgrade():
op.drop_column('users', 'some_column_new')
```
```python
# Migration 2: Complete the transition (after backfilling data)
def upgrade():
# After ensuring all data is migrated
op.drop_column('users', 'some_column')
op.alter_column('users', 'some_column_new',
new_column_name='some_column')
def downgrade():
op.alter_column('users', 'some_column',
new_column_name='some_column_new')
op.add_column('users', sa.Column('some_column', sa.Integer()))
```
File Path Patterns:
Install this rule for Graphite Diamond
Diamond custom rules can be added through the interface. See the documentation for details on how to create custom rules.
When changing a column type that requires a table rewrite, follow these steps:
1. Create a new column with the desired type
2. Write to both columns during the transition period
3. Backfill data from the old column to the new column
4. Move reads from the old column to the new column
5. Stop writing to the old column
6. Drop the old column
Bad:
```python
def upgrade():
# Directly changing a column type can cause table locks
op.alter_column('users', 'some_column',
type_=sa.String(50),
existing_type=sa.Integer())
def downgrade():
op.alter_column('users', 'some_column',
type_=sa.Integer(),
existing_type=sa.String(50))
```
Good:
```python
# Migration 1: Add new column
def upgrade():
# Adding a new column first
op.add_column('users', sa.Column('some_column_new', sa.String(50)))
def downgrade():
op.drop_column('users', 'some_column_new')
```
```python
# Migration 2: Complete the transition (after backfilling data)
def upgrade():
# After ensuring all data is migrated
op.drop_column('users', 'some_column')
op.alter_column('users', 'some_column_new',
new_column_name='some_column')
def downgrade():
op.alter_column('users', 'some_column',
new_column_name='some_column_new')
op.add_column('users', sa.Column('some_column', sa.Integer()))
```
File Path Patterns: