Fixed legacy db upgrade code