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
Add this rule to wispbit and it will run when you open a pull request
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:
Use with Cline
Copy the rule below and ask Cline to review your code using this rule
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()))
```
Use with OpenAI Codex
Copy the rule below and ask OpenAI Codex to review your code using this rule
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()))
```
Use with Cursor
Copy the rule below and ask Cursor to review your code using this rule
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()))
```
Use with Claude Code
Copy the rule below and ask Claude Code to review your code using this rule
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 Windsurf
To set up rules for Windsurf Reviews, please see this documentation
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()))
```