I am writing a migration test to ensure that a user was created by the migration. If the user does not exist, the test should throw an error. At first, I thought I could just use a division by zero error to get what I wanted:
SET SESSION sql_mode = 'error_for_division_by_zero';
SELECT 1/COUNT(*) FROM mysql.user WHERE user = 'foo';
However, this does not throw an error if foo
does not exist. Turns out that error_for_division_by_zero
affects only INSERT
and UPDATE
statements.
Then I thought maybe I could just call some function with the wrong number of arguments:
SELECT IF(COUNT(*) = 1, 1, date_format(1, 2, 3))
FROM mysql.user WHERE user = 'foo';
But this dies even when foo
does exist, presumably because the parser notices the incorrect parameter count.
I could write a a function that emulates raising an exception, but I was trying to avoid that. Is there no way to coerce MySQL into conditionally throwing a runtime exception?