Skip to content

fix: chat_sessions table not created on SQLite due to COMMENT clauses in dbDelta#1054

Merged
chubes4 merged 1 commit intomainfrom
fix/chat-cleanup-missing-table-guard
Apr 14, 2026
Merged

fix: chat_sessions table not created on SQLite due to COMMENT clauses in dbDelta#1054
chubes4 merged 1 commit intomainfrom
fix/chat-cleanup-missing-table-guard

Conversation

@chubes4
Copy link
Copy Markdown
Member

@chubes4 chubes4 commented Apr 14, 2026

Summary

  • Removes column COMMENT clauses from the chat_sessions CREATE TABLE SQL — dbDelta() silently fails to parse them (especially strings with parentheses), preventing the table from ever being created on SQLite
  • Fixes mixed tab/space indentation in the SQL string (matches every other Data Machine table)
  • Adds table_exists() guard to the datamachine_cleanup_chat_sessions Action Scheduler callback as defense-in-depth

Root Cause

chat_sessions was the only Data Machine table with COMMENT clauses in its column definitions. WordPress dbDelta() parses SQL with regex and is notoriously fragile with non-standard extensions. The COMMENT strings (e.g. COMMENT 'First-class agent identity (nullable for backward compatibility)') caused dbDelta() to silently fail on SQLite via Studio.

The version was still bumped at the end of datamachine_activate_for_site(), so the migration never retried. Meanwhile, the scheduled cleanup action fired repeatedly → DELETE FROM wp_datamachine_chat_sessionsno such table → 70+ identical error log entries.

After This Fix

On the next deploy/version bump, datamachine_maybe_run_migrations() will re-run Chat::create_table() with the clean SQL and the table will be created. The cleanup guard prevents log spam if table creation fails for any other reason in the future.

… in dbDelta

The chat_sessions table was the only Data Machine table using column
COMMENT clauses in its CREATE TABLE SQL. WordPress dbDelta() parses
SQL with regex and silently fails on COMMENT strings (especially ones
with parentheses). On SQLite via Studio, this caused the table to
never be created, while the db_version was still bumped — so the
migration never retried.

The scheduled cleanup action then spammed the error log with 'no such
table: wp_datamachine_chat_sessions' on every Action Scheduler run.

- Remove all COMMENT clauses from CREATE TABLE (matches every other
  Data Machine table; column docs belong in code, not schema)
- Fix mixed tab/space indentation in the SQL string
- Add table_exists() guard to the cleanup action as defense-in-depth
@chubes4 chubes4 merged commit ec4ca4e into main Apr 14, 2026
1 check passed
@chubes4 chubes4 deleted the fix/chat-cleanup-missing-table-guard branch April 14, 2026 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant