-- Let the fee waiver / discount note hold Bangla.
-- Run the three statements below in phpMyAdmin (or the mysql client) on the
-- live database. Safe to run more than once.
--
-- Why it is needed: `fees` is a legacy table whose DEFAULT CHARACTER SET is
-- latin1, and `note` was added without naming a charset, so it inherited
-- latin1. The app connects as utf8mb4, and MySQL replaces every character a
-- latin1 column cannot store with a literal '?' on write - which is why a
-- Bangla waiver note came back as "???????? ?????" in the student ledger.
--
-- Laravel equivalent: 2026_11_23_000000_convert_fees_note_to_utf8mb4.php

-- 1+2. Convert the column to utf8mb4. Going through BLOB first keeps the
-- stored bytes exactly as they are, so any Bangla written back when the app
-- still connected as latin1 starts displaying correctly instead of being
-- re-encoded into mojibake. Plain ASCII is unaffected either way.
ALTER TABLE `fees` MODIFY `note` BLOB;
ALTER TABLE `fees` MODIFY `note` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL;

-- 3. Stop the next text column added to this table from inheriting latin1.
-- Metadata only: existing columns keep whatever charset they already have.
ALTER TABLE `fees` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Check afterwards - both should read utf8mb4:
-- SELECT COLUMN_NAME, CHARACTER_SET_NAME, COLLATION_NAME
--   FROM information_schema.COLUMNS
--  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'fees' AND COLUMN_NAME = 'note';

-- Notes already stored as '?' cannot be recovered - those bytes are gone.
-- Re-enter such a note from the student ledger: ছাড় বাতিল on the row, then
-- give the discount again with the Bangla note.

-- ───────────────────────────────────────────────────────────────────────────
-- OPTIONAL: the same latin1 defect affects these other free-text columns, so
-- they mangle Bangla the same way. Uncomment the ones you want (orders.note
-- is the invoice note, students.name2 is the Bangla student name).
-- ───────────────────────────────────────────────────────────────────────────
-- ALTER TABLE `orders` MODIFY `note` BLOB;
-- ALTER TABLE `orders` MODIFY `note` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL;
-- ALTER TABLE `orders` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
--
-- ALTER TABLE `students` MODIFY `name2` VARBINARY(255);
-- ALTER TABLE `students` MODIFY `name2` VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL;
--
-- ALTER TABLE `bank_checks` MODIFY `note` BLOB;
-- ALTER TABLE `bank_checks` MODIFY `note` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL;
--
-- ALTER TABLE `hr_attendances` MODIFY `leave_note` BLOB;
-- ALTER TABLE `hr_attendances` MODIFY `leave_note` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL;
--
-- ALTER TABLE `hr_employees` MODIFY `deduction_notes` BLOB;
-- ALTER TABLE `hr_employees` MODIFY `deduction_notes` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL;
-- ALTER TABLE `hr_employees` MODIFY `short_description` BLOB;
-- ALTER TABLE `hr_employees` MODIFY `short_description` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL;
--
-- ALTER TABLE `products` MODIFY `short_description` BLOB;
-- ALTER TABLE `products` MODIFY `short_description` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL;
-- ALTER TABLE `products` MODIFY `description` LONGBLOB;
-- ALTER TABLE `products` MODIFY `description` LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL;
--
-- Before converting a column that already has data in it, check the sizes
-- match what is declared above:
-- SELECT COLUMN_NAME, COLUMN_TYPE, CHARACTER_SET_NAME FROM information_schema.COLUMNS
--  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'students' AND COLUMN_NAME = 'name2';
