Fix problem with migration on empty database
Merge from @grzlus master