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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| <?php
use Illuminate\Config\Repository; use Illuminate\Console\OutputStyle; use Illuminate\Container\Container; use Illuminate\Database\Connectors\ConnectionFactory; use Illuminate\Database\Console\Migrations\TableGuesser; use Illuminate\Database\DatabaseManager; use Illuminate\Database\Migrations\DatabaseMigrationRepository; use Illuminate\Database\Migrations\MigrationCreator; use Illuminate\Database\Migrations\Migrator; use Illuminate\Database\Schema\Builder; use Illuminate\Events\Dispatcher; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\Facade; use Illuminate\Support\Str; use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Output\ConsoleOutput;
define("ROOT_PATH", realpath(__DIR__ . "/../"));
require ROOT_PATH . "/vendor/autoload.php";
if (!isset($argv[1])) { $argv[1] = null; }
$container = new Container();
$config = new Repository();
$config->set("database", [ "default" => "mysql", "connections" => [ "mysql" => [ 'driver' => 'mysql', 'url' => "", 'host' => "127.0.0.1", 'port' => "3306", 'database' => "test", 'username' => "root", 'password' => "baoguoxiao", 'unix_socket' => '', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => [], ] ] ]);
$container->instance("config", $config);
$file = new Filesystem();
$container->singleton("db", function ($container) { $db = new DatabaseManager($container, new ConnectionFactory($container)); $db->connection("mysql"); return $db; });
Facade::setFacadeApplication($container);
Container::setInstance($container);
$repository = new DatabaseMigrationRepository($container["db"], "migrations");
$event = new Dispatcher($container);
$migrator = new Migrator($repository, $container["db"], $file, $event);
$output = new OutputStyle(new ArgvInput(), new ConsoleOutput());
if ($argv[1] == "create") { $creator = new MigrationCreator($file); $name = Str::snake($argv[2]);
[$table, $create] = TableGuesser::guess($name);
try { $file_path = $creator->create($name, ROOT_PATH . "/migrations", $table, $create); $file_path = pathinfo($file_path, PATHINFO_FILENAME);
$output->success("Created Migration: {$file_path}"); } catch (\InvalidArgumentException $exception) { $output->error($exception->getMessage()); } } elseif ($argv[1] == "up") { if (!$migrator->repositoryExists()) { $repository->createRepository(); }
$migrator->setOutput($output)->run(ROOT_PATH . "/migrations", [ "pretend" => false, "step" => false ]); } elseif ($argv[1] == "down") { if (!$migrator->repositoryExists()) { $repository->createRepository(); }
$migrator->setOutput($output)->rollback(ROOT_PATH . "/migrations", [ "pretend" => false, "step" => 0 ]); }else { $output->text(<<<EOF 操作方法: php bin/migrate.php create {xxx} 创建迁移,命名规则为Laravel php bin/migrate.php up 执行迁移 php bin/migrate.php down 回滚迁移 EOF ); }
|