Skip to content

Commit 97bb774

Browse files
committed
Add SQLite batch operations support for column type changes
- SQLite doesn't support ALTER COLUMN TYPE directly - Use batch_alter_table for SQLite (creates temp table, copies data, renames) - Keep direct ALTER COLUMN for PostgreSQL and MySQL - Applies to both upgrade and downgrade operations - Fixes: sqlalchemy.exc.OperationalError with SQLite
1 parent 02fe1f2 commit 97bb774

File tree

1 file changed

+66
-28
lines changed

1 file changed

+66
-28
lines changed

mcpgateway/alembic/versions/878e287d2366_resize_url_and_slug_columns_to_191.py

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,73 @@ def upgrade() -> None:
5858
""")
5959

6060
# Resize columns to String(191)
61-
op.alter_column(
62-
'gateways',
63-
'slug',
64-
existing_type=sa.String(length=255),
65-
type_=sa.String(length=191),
66-
existing_nullable=False
67-
)
68-
op.alter_column(
69-
'gateways',
70-
'url',
71-
existing_type=sa.String(length=767),
72-
type_=sa.String(length=191),
73-
existing_nullable=False
74-
)
61+
# SQLite requires batch operations for ALTER COLUMN
62+
if dialect_name == 'sqlite':
63+
with op.batch_alter_table('gateways', schema=None) as batch_op:
64+
batch_op.alter_column(
65+
'slug',
66+
existing_type=sa.String(length=255),
67+
type_=sa.String(length=191),
68+
existing_nullable=False
69+
)
70+
batch_op.alter_column(
71+
'url',
72+
existing_type=sa.String(length=767),
73+
type_=sa.String(length=191),
74+
existing_nullable=False
75+
)
76+
else:
77+
# PostgreSQL and MySQL support direct ALTER COLUMN
78+
op.alter_column(
79+
'gateways',
80+
'slug',
81+
existing_type=sa.String(length=255),
82+
type_=sa.String(length=191),
83+
existing_nullable=False
84+
)
85+
op.alter_column(
86+
'gateways',
87+
'url',
88+
existing_type=sa.String(length=767),
89+
type_=sa.String(length=191),
90+
existing_nullable=False
91+
)
7592

7693

7794
def downgrade() -> None:
7895
"""Downgrade schema."""
79-
op.alter_column(
80-
'gateways',
81-
'slug',
82-
existing_type=sa.String(length=191),
83-
type_=sa.String(length=255),
84-
existing_nullable=False
85-
)
86-
op.alter_column(
87-
'gateways',
88-
'url',
89-
existing_type=sa.String(length=191),
90-
type_=sa.String(length=767),
91-
existing_nullable=False
92-
)
96+
# Get database dialect for dialect-specific operations
97+
bind = op.get_bind()
98+
dialect_name = bind.dialect.name
99+
100+
# SQLite requires batch operations for ALTER COLUMN
101+
if dialect_name == 'sqlite':
102+
with op.batch_alter_table('gateways', schema=None) as batch_op:
103+
batch_op.alter_column(
104+
'slug',
105+
existing_type=sa.String(length=191),
106+
type_=sa.String(length=255),
107+
existing_nullable=False
108+
)
109+
batch_op.alter_column(
110+
'url',
111+
existing_type=sa.String(length=191),
112+
type_=sa.String(length=767),
113+
existing_nullable=False
114+
)
115+
else:
116+
# PostgreSQL and MySQL support direct ALTER COLUMN
117+
op.alter_column(
118+
'gateways',
119+
'slug',
120+
existing_type=sa.String(length=191),
121+
type_=sa.String(length=255),
122+
existing_nullable=False
123+
)
124+
op.alter_column(
125+
'gateways',
126+
'url',
127+
existing_type=sa.String(length=191),
128+
type_=sa.String(length=767),
129+
existing_nullable=False
130+
)

0 commit comments

Comments
 (0)