Align databases with migration lifecycle.

We've seen evidence of the remote caller attempting multiple
migrations, so adjust the logic to ensure that we always have a
valid database while an active migration is taking place.

Also protect the Mainline code from a legacy provided that is
having trouble with lifecycle events.

Bug: 151960212
Test: atest --test-mapping packages/providers/MediaProvider
Change-Id: I3920bb652352ca3a08d2004fb5e3e135cbc0ce82
2 files changed