Rewritten the migration script in Python
Fixed the upgrade script