Skip to content

Instantly share code, notes, and snippets.

@breadlesscode
Created February 25, 2020 14:32
Show Gist options
  • Save breadlesscode/53f0cf384d404bffaf7997b22e00136e to your computer and use it in GitHub Desktop.
Save breadlesscode/53f0cf384d404bffaf7997b22e00136e to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
require './vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Migrations\DatabaseMigrationRepository;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Filesystem\Filesystem;
define('MIGRATION_PATH', '../database/migrations');
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'mysql',
'host' => getenv('MYSQL_HOST'),
'database' => getenv('MYSQL_DATABASE'),
'username' => getenv('MYSQL_USER'),
'password' => getenv('MYSQL_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();
$capsule->getContainer()->bind('db', function() use($capsule) { return $capsule->getConnection(); });
// evil things.. but its always in cli context, so no worry
$GLOBALS['illuminateContainer'] = $capsule->getContainer();
// validate argument
$command = @$argv[1];
$migrationRepository = new DatabaseMigrationRepository($capsule->getDatabaseManager(), 'migrations');
if (!$migrationRepository->repositoryExists()) {
$migrationRepository->createRepository();
}
$migrator = new Migrator(
$migrationRepository,
$capsule->getDatabaseManager(),
new Filesystem()
);
switch ($command) {
case 'up':
$migrator->run(MIGRATION_PATH);
break;
case 'down':
$migrator->rollback(MIGRATION_PATH);
break;
case 'reset':
$migrator->reset(MIGRATION_PATH);
break;
default:
echo "Please use one of this arguments:\n\n- up\n- down\n- reset\n\n";
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment