Refactor and fix load certificate for Server API.

Use migrations instead of postupgrade operation to avoid problems
with changes on database schema.