Skip to content

fix(DM01-6184): filter migrations by file number + skip comment-only files - #638

Closed
Jiachen0715 wants to merge 2 commits into
masterfrom
fix/DM01-6184-plus-empty-file-guard
Closed

fix(DM01-6184): filter migrations by file number + skip comment-only files#638
Jiachen0715 wants to merge 2 commits into
masterfrom
fix/DM01-6184-plus-empty-file-guard

Conversation

@Jiachen0715

Copy link
Copy Markdown
Contributor

Summary

Builds on #636 (fix(DM01-6184): filter migrations by file number instead of list index) and adds a second fix that is needed to fully unblock the infrabox-db migration Job.

Two independent bugs, one root symptom

test-new databases starting below schema_version=46 currently see the migration Job crash with:

psycopg2.ProgrammingError: can't execute an empty query

There are actually two bugs at play, so #636 alone is not enough. This PR keeps #636's fix and adds the missing one.

Bug 1 — get_files used list index instead of file number ✅ fixed by #636

Historically 00046.sql did not exist (the sequence went 45 → 47), and files = files[current_schema_version:] used list index to slice pending migrations. Any gap in the numbering desynchronised the index from the file number, silently skipping later migrations. #636 replaces the slice with a file-number filter — kept as-is in this PR.

Bug 2 — apply_migration treats comment-only files as executable ← this PR adds

Even with #636's filter selecting the correct files, once 00046.sql (an intentionally empty placeholder that only contains -- ... lines) is chosen, the runner still fails:

sql = sql_file.read().strip()   # keeps `--` comment lines
if sql:                          # non-empty string of comments → guard passes
    cur.execute(sql)             # psycopg2 rejects: "can't execute an empty query"

The result: the migration Job crashes before ever reaching 00047.sql (which creates mcp_token, mcp_access_log, and adds build.source) or 00048.sql (secret_read_token). Downstream, MCP-related endpoints return 500 because the tables don't exist.

Fix in this PR

Add a small _has_executable_sql() helper that returns True only if the file has at least one non-blank line that isn't a -- comment. apply_migration uses it to decide whether to call cur.execute():

if _has_executable_sql(sql):
    cur.execute(sql)
else:
    logger.info("Skipping execute for %s (no non-comment SQL statements)", filename)

schema_version is still bumped afterwards, so the placeholder still serves its purpose (closing a numbering gap).

Scope

Behaviour after this PR

File type Before After
Regular .sql with statements executed executed (unchanged)
.sql with only -- comments crashes with "empty query" logs "Skipping execute", schema_version advances
.py migration import + migrate(conn) unchanged

Test plan

  • Reproduce the failure locally: point migrate.py at a DB at schema_version=45, run against a migrations folder containing the current 00046.sql (comment-only) + 00047.sql. Confirm the pre-fix code crashes and the post-fix code logs Skipping execute for ...00046.sql and completes.
  • Deploy to a real environment (e.g. test-new) that is currently stuck at schema_version=45. Expect the infrabox-db Job to succeed and MCP endpoints (GET/POST /api/v1/mcp/tokens) to return 200 instead of 500.

Notes

  • _has_executable_sql() is a lightweight line-based heuristic. It only recognises -- line comments; it does not parse SQL and does not understand /* block */ comments. That's fine for the current use case (the placeholder file uses -- comments), but if we ever want to be stricter we can swap in a real tokenizer later.
  • This PR is complementary to the effort in SAP/InfraBox#637 (unrelated feature branch) which also had to work around this same crash. Once this PR merges, that workaround (adding SELECT 1; inside 00046.sql) becomes unnecessary and can be reverted.

liuwei08 and others added 2 commits August 20, 2026 10:49
Follow-up on the get_files() fix that filters migrations by file number
instead of list index: even with the correct file selected, the runner
still failed on a comment-only migration file (like 00046.sql).

apply_migration() only did sql.strip() and then guarded with 'if sql:',
which considers any non-empty string as executable. For a file that
contains only SQL line comments, strip() keeps all the '--' lines, so
the guard passes and psycopg2 rejects the call with:

  ProgrammingError: can't execute an empty query

This blocked the infrabox-db migration Job whenever a comment-only
placeholder was in the pending set (observed on databases at
schema_version < 46, where 00046.sql is a pure comment file).

Add a _has_executable_sql() helper that treats a file as empty when
every non-blank line is a '-- ...' comment. When empty, skip the
cur.execute() call but still advance schema_version, so subsequent
migrations (00047, 00048, ...) can proceed.
@Jiachen0715

Copy link
Copy Markdown
Contributor Author

Superseded — pushed the additional commit directly to the PR #636 branch (DM01-6184) instead.

@Jiachen0715
Jiachen0715 deleted the fix/DM01-6184-plus-empty-file-guard branch August 24, 2026 05:56
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.

2 participants