sql/upgrade: improve detection of already applied

schema updates