Refactory the migration code