USE lifanghe_education; SET @schema_name = DATABASE(); SET @has_trial_booking_limit := ( SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'users' AND COLUMN_NAME = 'trial_booking_limit' ); SET @add_trial_booking_limit_sql := IF( @has_trial_booking_limit = 0, 'ALTER TABLE users ADD COLUMN trial_booking_limit INT NOT NULL DEFAULT 1 AFTER child_focus', 'SELECT 1' ); PREPARE add_trial_booking_limit_stmt FROM @add_trial_booking_limit_sql; EXECUTE add_trial_booking_limit_stmt; DEALLOCATE PREPARE add_trial_booking_limit_stmt; SET @has_unique_user_index := ( SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'appointments' AND INDEX_NAME = 'uk_appointments_user' ); SET @drop_unique_user_index_sql := IF( @has_unique_user_index > 0, 'ALTER TABLE appointments DROP INDEX uk_appointments_user', 'SELECT 1' ); PREPARE drop_unique_user_index_stmt FROM @drop_unique_user_index_sql; EXECUTE drop_unique_user_index_stmt; DEALLOCATE PREPARE drop_unique_user_index_stmt; SET @has_appointment_user_index := ( SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = @schema_name AND TABLE_NAME = 'appointments' AND INDEX_NAME = 'idx_appointments_user' ); SET @add_appointment_user_index_sql := IF( @has_appointment_user_index = 0, 'ALTER TABLE appointments ADD INDEX idx_appointments_user (user_id)', 'SELECT 1' ); PREPARE add_appointment_user_index_stmt FROM @add_appointment_user_index_sql; EXECUTE add_appointment_user_index_stmt; DEALLOCATE PREPARE add_appointment_user_index_stmt;