[1.7.x] Wrapped migrations in a transaction only on DBs with transactional DDL.

Backport of e74d2183 from master
[1.7.x] Used the same instance of atomic for entry and exit.

Since all state is maintained on the connection at this time and none in
the atomic, it doesn't matter, but it could introduce some subtle bugs
if the implementation changed in the future.

Backport of 0aa4c6c3 from master