Skip to content

Commit 4f7651d

Browse files
CodingAnarchyclaude
andcommitted
fix(testing): check migration count directly instead of information_schema
Query _sqlx_migrations table directly to check if migrations exist. This handles the case where the table exists with entries from a previous run more reliably than checking information_schema.tables. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 130c66e commit 4f7651d

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

sqlx-mysql/src/testing/mod.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,19 @@ async fn get_or_create_template(
9595
.await?;
9696

9797
// Check if this is a fresh database or one left over from a previous run
98-
// by checking if it already has the migrations table with entries
98+
// by checking if it already has migrations recorded
9999
let template_opts = master_opts.clone().database(&tpl_name);
100100
let mut template_conn: MySqlConnection = template_opts.connect().await?;
101101

102-
let migrations_exist: Option<i64> = query_scalar(
103-
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = '_sqlx_migrations'"
104-
)
105-
.bind(&tpl_name)
106-
.fetch_optional(&mut template_conn)
107-
.await?;
102+
// Try to count migrations - if the table doesn't exist or is empty, we need to run migrations
103+
let migration_count: Result<i64, _> = query_scalar("SELECT COUNT(*) FROM _sqlx_migrations")
104+
.fetch_one(&mut template_conn)
105+
.await;
108106

109-
let needs_migrations = migrations_exist.unwrap_or(0) == 0;
107+
let needs_migrations = match migration_count {
108+
Ok(count) => count == 0, // Table exists but is empty
109+
Err(_) => true, // Table doesn't exist (error 1146) or other error
110+
};
110111

111112
// Only run migrations if the database is fresh (no migrations table)
112113
if needs_migrations {

0 commit comments

Comments
 (0)