Created
March 9, 2018 03:01
-
-
Save jaceju/cc53d2fbab6e828f69b2a3b7e267d1ed to your computer and use it in GitHub Desktop.
Use Laravel Migration standalone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
require __DIR__ . '/vendor/autoload.php'; | |
/** | |
* 設定資料庫連線 | |
*/ | |
$capsule = new Capsule(); | |
$capsule->addConnection([ | |
'driver' => 'sqlite', | |
'database' => ':memory:', | |
]); | |
$capsule->setEventDispatcher(new Dispatcher(new Container)); | |
$capsule->setAsGlobal(); | |
$capsule->bootEloquent(); | |
/** | |
* 初始化必要參數 | |
*/ | |
$container = Container::getInstance(); | |
$databaseMigrationRepository = new DatabaseMigrationRepository($capsule->getDatabaseManager(), 'migration'); | |
$databaseMigrationRepository->createRepository(); | |
$container->instance(MigrationRepositoryInterface::class, $databaseMigrationRepository); | |
$container->instance(ConnectionResolverInterface::class, $capsule->getDatabaseManager()); | |
/** | |
* 執行 migration | |
*/ | |
$paths = [ | |
__DIR__ . '/migrations', | |
]; | |
/** @var Migrator $migrator */ | |
$migrator = $container->make(Migrator::class); | |
$migrator->run($paths); | |
var_dump($migrator->getNotes()); | |
/** | |
* 執行 rollback | |
*/ | |
$migrator->rollback($paths); | |
var_dump($migrator->getNotes()); |
@31337Ghost I have try your code. It's seems working solutions for the first attempt. But when you run again it is throwing error for the migration table that it's already exits. I am looking for the solutions for this. If you can help me out.
@31337Ghost I have try your code. It's seems working solutions for the first attempt. But when you run again it is throwing error for the migration table that it's already exits. I am looking for the solutions for this. If you can help me out.
You have to check if migrations table already exists. Use repositoryExists()
method in DatabaseMigrationRepository
just like that:
if (!$databaseMigrationRepository->repositoryExists()) {
$databaseMigrationRepository->createRepository();
}
@kWeb24 Thank you for the solutions. This work 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This problem can be solved by adding 2 lines:
on the top:
use Illuminate\Support\Facades\Schema;
and in the init function at the end:
this is how you can hack some fascades, in additional funcionality of others fascades made little proxy for DB class:
and it using in migrations as DB class (example for raw function)