-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.php
60 lines (45 loc) · 1.57 KB
/
migrate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
require __DIR__ . '/vendor/autoload.php';
use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Database\Migrations\DatabaseMigrationRepository;
use Illuminate\Database\Migrations\MigrationRepositoryInterface;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Events\Dispatcher;
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'mysql',
'host' => $_ENV['DB_HOST'],
'database' => $_ENV['DB_NAME'],
'username' => $_ENV['DB_USER'],
'password' => $_ENV['DB_PASS'],
]);
$capsule->setEventDispatcher(new Dispatcher(new Container));
$capsule->setAsGlobal();
$capsule->bootEloquent();
$container = Container::getInstance();
$databaseMigrationRepository = new DatabaseMigrationRepository($capsule->getDatabaseManager(), 'migration');
if (!$databaseMigrationRepository->repositoryExists()) {
$databaseMigrationRepository->createRepository();
}
$container->instance(MigrationRepositoryInterface::class, $databaseMigrationRepository);
$container->instance(ConnectionResolverInterface::class, $capsule->getDatabaseManager());
$paths = [
__DIR__ . '/migrations',
];
/** @var Migrator $migrator */
$migrator = $container->make(Migrator::class);
$type = $argv[1] ?? '';
if ($type === 'rollback') {
$migrator->rollback($paths);
die;
}
if ($type === 'refresh') {
$migrator->rollback($paths);
$migrator->run($paths);
die;
}
$migrator->run($paths);