-- ============================================================
-- EXPENSES TABLE FIX - RUN IN PHPMYADMIN
-- ============================================================
-- BEFORE YOU START:
-- 1. Open phpMyAdmin in cPanel
-- 2. Click on database: acbptxvs_branch_system (left sidebar)
-- 3. Click the SQL tab
-- 4. Copy and paste queries ONE AT A TIME
-- ============================================================

-- STEP 1: Check current table structure
DESCRIBE expenses;
-- Look at the 'id' column type

-- STEP 2: Count existing records
SELECT COUNT(*) as total_records FROM expenses;
-- Remember this number!

-- STEP 3: Drop old backup if exists
DROP TABLE IF EXISTS expenses_backup;
-- Should show: "0 rows affected"

-- STEP 4: Create backup
CREATE TABLE expenses_backup AS SELECT * FROM expenses;
-- Should show: "X rows affected" (same as step 2)

-- STEP 5: Verify backup
SELECT COUNT(*) as backup_records FROM expenses_backup;
-- Should match the number from step 2

-- STEP 6: Truncate expenses table (DELETES ALL RECORDS!)
TRUNCATE TABLE expenses;
-- Should show: "0 rows affected"

-- STEP 7: Fix the id column
ALTER TABLE expenses MODIFY COLUMN id bigint NOT NULL AUTO_INCREMENT;
-- Should show: "0 rows affected"

-- STEP 8: Verify the fix
DESCRIBE expenses;
-- Check that 'id' shows: bigint(20) | auto_increment

-- STEP 9: Confirm table is empty
SELECT COUNT(*) as current_records FROM expenses;
-- Should show: 0

-- STEP 10: Confirm backup has data
SELECT COUNT(*) as backup_records FROM expenses_backup;
-- Should show your original number

-- ============================================================
-- ✅ DATABASE FIX COMPLETE!
-- ============================================================
-- NEXT STEPS:
-- 1. Upload these files to production:
--    - expenses/models.py
--    - expenses/forms.py
--    - expenses/views.py
-- 2. Restart app in cPanel (Setup Python App > Restart)
-- 3. Test by creating a new expense
-- ============================================================
