Split migrate.php for in-script usage

Extracted db.php from migrate.php to handle commandline interaction while migrate.php can be used from other scripts