Skip to content

Fix migrate:status when migrations table is absent#57

Closed
davidwyly wants to merge 1 commit into
masterfrom
codex/fix-migrate-status-error-handling
Closed

Fix migrate:status when migrations table is absent#57
davidwyly wants to merge 1 commit into
masterfrom
codex/fix-migrate-status-error-handling

Conversation

@davidwyly

Copy link
Copy Markdown
Owner

Motivation

  • migrate:status now constructs Migration with ensureTable=false, so the rxn_migrations table may not exist on fresh schemas and must be handled gracefully.
  • The framework Query layer wraps PDO errors in QueryException, so Migration::applied() catching only \PDOException could miss the real exception and crash instead of returning an empty applied list.
  • The change restores the previous behavior of showing pending migrations on a fresh database by recognizing missing-table errors regardless of the exception wrapper.

Description

  • Import QueryException in src/Rxn/Framework/Data/Migration.php and extend the catch in applied() to \PDOException | QueryException so both error types are handled.
  • Add a private helper isMissingMigrationsTable() that detects missing-table conditions by checking the SQLSTATE 42S02, MySQL driver message text 1146, or SQLite-style no such table in the exception message or code.
  • Return an empty applied list when the helper detects a missing migrations table and rethrow the exception for all other failures, preserving existing behavior for non-missing-table errors.

Testing

  • Ran a PHP syntax check with php -l src/Rxn/Framework/Data/Migration.php, which succeeded (no syntax errors).

Codex Task

@davidwyly davidwyly added the codex label May 3, 2026
Copilot AI review requested due to automatic review settings May 3, 2026 19:37
@davidwyly

Copy link
Copy Markdown
Owner Author

duplicate of #58

@davidwyly davidwyly closed this May 3, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bb3cf2e3eb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +85 to +87
return strpos($code, '42S02') !== false
|| strpos($message, '1146') !== false
|| stripos($message, 'no such table') !== false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Detect wrapped SQLSTATE 42S02 in missing-table check

isMissingMigrationsTable() only looks for 42S02 in $exception->getCode(), but Query::prepare() wraps PDO errors into QueryException with code 500 and puts the SQLSTATE into the message ("PDO Exception (code 42S02)"). In environments where the missing-table failure is raised at prepare time and the message does not include 1146/no such table, this branch returns false and migrate:status still crashes instead of returning an empty applied list.

Useful? React with 👍 / 👎.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes migrate:status behavior on fresh schemas by making Migration::applied() treat a missing rxn_migrations table as “no applied migrations”, even when the DB layer throws a wrapped QueryException.

Changes:

  • Catch QueryException as well as \PDOException in Migration::applied().
  • Add isMissingMigrationsTable() helper to detect missing-table errors and return an empty applied list.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 69 to 75
try {
$rows = $this->database->fetchAll("SELECT filename FROM `" . self::TABLE . "` ORDER BY filename ASC");
} catch (\PDOException $exception) {
if ((int)$exception->getCode() === 1146) {
} catch (\PDOException | QueryException $exception) {
if ($this->isMissingMigrationsTable($exception)) {
return [];
}
throw $exception;
Comment on lines +82 to +87
$message = $exception->getMessage();
$code = (string)$exception->getCode();

return strpos($code, '42S02') !== false
|| strpos($message, '1146') !== false
|| stripos($message, 'no such table') !== false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants