Fix migration to handle case where users table already exists
This commit is contained in:
parent
1710f43c85
commit
0c2239fb90
@ -16,7 +16,14 @@ depends_on = None
|
|||||||
|
|
||||||
|
|
||||||
def upgrade():
|
def upgrade():
|
||||||
# Create users table
|
# Create users table if it doesn't exist
|
||||||
|
from sqlalchemy.exc import OperationalError
|
||||||
|
try:
|
||||||
|
# Use inspector to check if the table exists
|
||||||
|
from sqlalchemy import inspect
|
||||||
|
conn = op.get_bind()
|
||||||
|
inspector = inspect(conn)
|
||||||
|
if 'users' not in inspector.get_table_names():
|
||||||
op.create_table(
|
op.create_table(
|
||||||
'users',
|
'users',
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
@ -37,8 +44,20 @@ def upgrade():
|
|||||||
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
|
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
|
||||||
op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
|
op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
|
||||||
op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True)
|
op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True)
|
||||||
|
except OperationalError:
|
||||||
|
# Table already exists, skip creation
|
||||||
|
pass
|
||||||
|
|
||||||
# Add owner_id column to todos table
|
# Add owner_id column to todos table if it doesn't exist
|
||||||
|
from sqlalchemy.exc import OperationalError
|
||||||
|
|
||||||
|
# Check if owner_id column exists in todos table
|
||||||
|
conn = op.get_bind()
|
||||||
|
inspector = inspect(conn)
|
||||||
|
columns = [col['name'] for col in inspector.get_columns('todos')]
|
||||||
|
|
||||||
|
if 'owner_id' not in columns:
|
||||||
|
try:
|
||||||
op.add_column('todos', sa.Column('owner_id', sa.Integer(), nullable=True))
|
op.add_column('todos', sa.Column('owner_id', sa.Integer(), nullable=True))
|
||||||
|
|
||||||
# Create foreign key from todos to users
|
# Create foreign key from todos to users
|
||||||
@ -53,17 +72,46 @@ def upgrade():
|
|||||||
|
|
||||||
# Make owner_id non-nullable after updating existing todos
|
# Make owner_id non-nullable after updating existing todos
|
||||||
op.alter_column('todos', 'owner_id', nullable=False)
|
op.alter_column('todos', 'owner_id', nullable=False)
|
||||||
|
except OperationalError as e:
|
||||||
|
# Log the error but continue
|
||||||
|
print(f"Operation failed with error: {str(e)}")
|
||||||
|
# If this is because the column already exists, we can continue
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def downgrade():
|
def downgrade():
|
||||||
# Drop foreign key constraint
|
from sqlalchemy import inspect
|
||||||
op.drop_constraint('fk_todos_owner_id_users', 'todos', type_='foreignkey')
|
from sqlalchemy.exc import OperationalError
|
||||||
|
|
||||||
# Drop owner_id column from todos
|
conn = op.get_bind()
|
||||||
|
inspector = inspect(conn)
|
||||||
|
|
||||||
|
# Check if the constraint exists before trying to drop it
|
||||||
|
try:
|
||||||
|
constraints = inspector.get_foreign_keys('todos')
|
||||||
|
constraint_name = 'fk_todos_owner_id_users'
|
||||||
|
fk_exists = any(constraint['name'] == constraint_name for constraint in constraints)
|
||||||
|
if fk_exists:
|
||||||
|
op.drop_constraint('fk_todos_owner_id_users', 'todos', type_='foreignkey')
|
||||||
|
except OperationalError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Check if owner_id column exists before trying to drop it
|
||||||
|
columns = [col['name'] for col in inspector.get_columns('todos')]
|
||||||
|
if 'owner_id' in columns:
|
||||||
op.drop_column('todos', 'owner_id')
|
op.drop_column('todos', 'owner_id')
|
||||||
|
|
||||||
# Drop users table
|
# Check if users table exists before trying to drop it
|
||||||
|
if 'users' in inspector.get_table_names():
|
||||||
|
# Check for each index before dropping
|
||||||
|
indices = inspector.get_indexes('users')
|
||||||
|
index_names = [index['name'] for index in indices]
|
||||||
|
|
||||||
|
if 'ix_users_username' in index_names:
|
||||||
op.drop_index(op.f('ix_users_username'), table_name='users')
|
op.drop_index(op.f('ix_users_username'), table_name='users')
|
||||||
|
if 'ix_users_id' in index_names:
|
||||||
op.drop_index(op.f('ix_users_id'), table_name='users')
|
op.drop_index(op.f('ix_users_id'), table_name='users')
|
||||||
|
if 'ix_users_email' in index_names:
|
||||||
op.drop_index(op.f('ix_users_email'), table_name='users')
|
op.drop_index(op.f('ix_users_email'), table_name='users')
|
||||||
|
|
||||||
op.drop_table('users')
|
op.drop_table('users')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user