make the migration idempotetnt

This commit is contained in:
Koncept Kit
2026-01-31 01:16:02 +07:00
parent 9754f2db6e
commit 6f8ec1d254

View File

@@ -19,14 +19,22 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None: def upgrade() -> None:
conn = op.get_bind()
# Create PaymentMethodType enum # Create PaymentMethodType enum
paymentmethodtype = postgresql.ENUM( paymentmethodtype = postgresql.ENUM(
'card', 'cash', 'bank_transfer', 'check', 'card', 'cash', 'bank_transfer', 'check',
name='paymentmethodtype', name='paymentmethodtype',
create_type=False create_type=False
) )
paymentmethodtype.create(op.get_bind(), checkfirst=True) paymentmethodtype.create(conn, checkfirst=True)
# Check if stripe_customer_id column exists on users table
result = conn.execute(sa.text("""
SELECT column_name FROM information_schema.columns
WHERE table_name = 'users' AND column_name = 'stripe_customer_id'
"""))
if result.fetchone() is None:
# Add stripe_customer_id to users table # Add stripe_customer_id to users table
op.add_column('users', sa.Column( op.add_column('users', sa.Column(
'stripe_customer_id', 'stripe_customer_id',
@@ -36,6 +44,12 @@ def upgrade() -> None:
)) ))
op.create_index('ix_users_stripe_customer_id', 'users', ['stripe_customer_id']) op.create_index('ix_users_stripe_customer_id', 'users', ['stripe_customer_id'])
# Check if payment_methods table exists
result = conn.execute(sa.text("""
SELECT table_name FROM information_schema.tables
WHERE table_name = 'payment_methods'
"""))
if result.fetchone() is None:
# Create payment_methods table # Create payment_methods table
op.create_table( op.create_table(
'payment_methods', 'payment_methods',