# Generated by Django 5.2.7 on 2025-11-12 23:46

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('loans', '0020_alter_mpesatransaction_status'),
    ]

    operations = [
        migrations.RunSQL(
            # Add column if it doesn't exist (MySQL compatible)
            sql="""
                SET @dbname = DATABASE();
                SET @tablename = 'mpesa_transactions';
                SET @columnname = 'business_short_code';
                SET @preparedStatement = (SELECT IF(
                    (
                        SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
                        WHERE
                            TABLE_SCHEMA = @dbname
                            AND TABLE_NAME = @tablename
                            AND COLUMN_NAME = @columnname
                    ) > 0,
                    'SELECT 1',
                    CONCAT('ALTER TABLE ', @tablename, ' ADD COLUMN ', @columnname, ' VARCHAR(10) NULL')
                ));
                PREPARE alterIfNotExists FROM @preparedStatement;
                EXECUTE alterIfNotExists;
                DEALLOCATE PREPARE alterIfNotExists;
            """,
            reverse_sql=migrations.RunSQL.noop,
        ),
    ]
