[1.7.x] Avoided transactional DDL on castrated databases.

Fixed a test failure that appeared after 753a22a6, although the bug
existed before that commit.

Refs #22308 and #23089.

Backport of 0757e0f30d from master