Fix the type of the 'masterid' case statement

It seems that newish versions of pg8000 complain (probably correctly)
that a case statement is of text type, but the masterid column should be
an integer.  This adds an explicit cast, which works fine across all
three database engines.
Merge pull request #1255 from djmitche/025-masterid-type-fix

Fix the type of the 'masterid' case statement

See ticket:2933