📚 Encyclopedia

Databases · updated 2026-10-26

Changing a production MariaDB safely (schema, data fixes, id generation)

mariadbmysqlmigrationsbackupsalter-tableutf8mb4get_lockproduction

The TLDR database is small (under 10 MB) but it is production, it holds E2E key backups that can never be regenerated, and it has been changed by many hands. These are the rules that the db-change workflow settled on, plus schema facts that tripped people up.

Know the engine

The server runs MariaDB 10.11, not MySQL 8, even though every tool and file calls it "mysql". Answer questions about ALTER TABLE behavior, JSON functions, window functions and strict mode from MariaDB's documentation. Strict mode rejects NULL for NOT NULL columns and over-long strings (error 1406, "Data too long"), so an UPDATE that used to silently truncate now fails.

Before any change

  1. Back up the affected tables with mysqldump into ~/backups/pre-<change>-<date>.sql on the server, and copy anything irreplaceable (key tables) off the box.
  2. Verify the backup: check its size and count CREATE TABLE lines (grep -c 'CREATE TABLE'). A schema-only backup is fine for purely additive migrations.
  3. Write the inverse statement before running the forward one.
  4. Check table size before an ALTER that rewrites the table (the big ones here were views, notifications and messages).
  5. Put the migration in api/migrations/ in git, and make it re-runnable (IF EXISTS / IF NOT EXISTS) so it can be applied, re-run, and proven idempotent.

Data fixes

Wrap in a transaction and compare ROW_COUNT() to the number you expect before committing:

START TRANSACTION;
UPDATE post_meta SET meta_value = 'answered' WHERE post_id IN (1449, 1435) AND meta_key = 'ai_status';
SELECT ROW_COUNT();   -- expect 2, else ROLLBACK
COMMIT;

After any production DB change, run the smoke test (Writing a production smoke test that tells you why it failed).

Patterns and lessons

Renaming a key that other tables copy (usernames)

TLDR identifies users by username in many tables, with no numeric id. Renaming one account (October 2026) worked like this: - Find every column holding the exact username (information_schema.COLUMNS plus a COUNT(*) per candidate), not just the declared foreign keys. - Collations are case-insensitive (utf8mb4_unicode_ci): renaming Bean to aaron collides with an existing Aaron. Delete the conflicting account and its rows before the rename. - Do it as one transaction with FOREIGN_KEY_CHECKS=0, deletes first, then one UPDATE per column; check for orphans afterward (0 expected). Back up first. - Look for things derived from the name, which an UPDATE cannot fix: here the E2E conversation key's HKDF info string contains both usernames, so the renamed user's existing DMs became undecryptable and were deleted by choice. Existing tokens for the old name stop resolving to a user (401), and saved logins on devices still hold the old name.

Naming traps in this schema

Singular table names; post_tag.postid (no underscore) next to post_meta.post_id; times as unix ints in some tables (response.created_date, dm_message.sent_date, all group-chat tables) and DATETIME in others. Check DESCRIBE before writing a query from memory.

Access

Interactive access is on the server with the app's MySQL user (credentials live in the global CLAUDE.md and the server's DB config file, not here). Pass passwords to scripts via MYSQL_PWD, not on the command line where ps shows them.

See also

Linked from

Sources: tldr memory (tools-smoke-test, release-v1.31.0-audit, plan-dm-archive-rekey, bug-notification-privacy-leak, completed-work, MEMORY.md DB notes, bug-pass-reset-500, feature-ai-guest-mode, feature-ai-request-table), posts #1608, #1612, #1694, #1695, #1796, #1822, #1824