mirror of
https://github.com/flarum/framework.git
synced 2024-12-04 08:13:39 +08:00
Update generate:migration command to deal with new migration structure
This commit is contained in:
parent
14ecd325e1
commit
0411cc5137
|
@ -10,8 +10,8 @@
|
||||||
|
|
||||||
namespace Flarum\Database;
|
namespace Flarum\Database;
|
||||||
|
|
||||||
|
use Flarum\Extension\Extension;
|
||||||
use Illuminate\Filesystem\Filesystem;
|
use Illuminate\Filesystem\Filesystem;
|
||||||
use Illuminate\Support\Str;
|
|
||||||
|
|
||||||
class MigrationCreator
|
class MigrationCreator
|
||||||
{
|
{
|
||||||
|
@ -43,7 +43,7 @@ class MigrationCreator
|
||||||
* Create a new migration for the given extension.
|
* Create a new migration for the given extension.
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string $path
|
* @param Extension $extension
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @param bool $create
|
* @param bool $create
|
||||||
* @return string
|
* @return string
|
||||||
|
@ -56,7 +56,7 @@ class MigrationCreator
|
||||||
|
|
||||||
$stub = $this->getStub($table, $create);
|
$stub = $this->getStub($table, $create);
|
||||||
|
|
||||||
$this->files->put($path, $this->populateStub($extension, $name, $stub, $table));
|
$this->files->put($path, $this->populateStub($stub, $table));
|
||||||
|
|
||||||
return $path;
|
return $path;
|
||||||
}
|
}
|
||||||
|
@ -85,18 +85,13 @@ class MigrationCreator
|
||||||
/**
|
/**
|
||||||
* Populate the place-holders in the migration stub.
|
* Populate the place-holders in the migration stub.
|
||||||
*
|
*
|
||||||
* @param string $name
|
|
||||||
* @param string $stub
|
* @param string $stub
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function populateStub($extension, $name, $stub, $table)
|
protected function populateStub($stub, $table)
|
||||||
{
|
{
|
||||||
list($vendor, $package) = explode('-', $extension, 2);
|
|
||||||
|
|
||||||
$replacements = [
|
$replacements = [
|
||||||
'{{namespace}}' => Str::studly($vendor).'\\'.Str::studly($package) ?: 'Flarum\Core',
|
|
||||||
'{{name}}' => Str::studly($name),
|
|
||||||
'{{table}}' => $table
|
'{{table}}' => $table
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
namespace Flarum\Database;
|
namespace Flarum\Database;
|
||||||
|
|
||||||
use Flarum\Extension\Extension;
|
use Flarum\Extension\Extension;
|
||||||
|
use Illuminate\Container\Container;
|
||||||
use Illuminate\Database\ConnectionResolverInterface as Resolver;
|
use Illuminate\Database\ConnectionResolverInterface as Resolver;
|
||||||
use Illuminate\Filesystem\Filesystem;
|
use Illuminate\Filesystem\Filesystem;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
@ -87,18 +88,18 @@ class Migrator
|
||||||
|
|
||||||
$migrations = array_diff($files, $ran);
|
$migrations = array_diff($files, $ran);
|
||||||
|
|
||||||
$this->requireFiles($path, $migrations);
|
$this->runMigrationList($migrations, $path, $extension);
|
||||||
|
|
||||||
$this->runMigrationList($migrations, $extension);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run an array of migrations.
|
* Run an array of migrations.
|
||||||
*
|
*
|
||||||
* @param array $migrations
|
* @param array $migrations
|
||||||
* @param Extension $extension
|
* @param string $path
|
||||||
|
* @param Extension $extension
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function runMigrationList($migrations, Extension $extension = null)
|
public function runMigrationList($migrations, $path, Extension $extension = null)
|
||||||
{
|
{
|
||||||
// First we will just make sure that there are any migrations to run. If there
|
// First we will just make sure that there are any migrations to run. If there
|
||||||
// aren't, we will just make a note of it to the developer so they're aware
|
// aren't, we will just make a note of it to the developer so they're aware
|
||||||
|
@ -113,7 +114,7 @@ class Migrator
|
||||||
// migrations "up" so the changes are made to the databases. We'll then log
|
// migrations "up" so the changes are made to the databases. We'll then log
|
||||||
// that the migration was run so we don't repeat it next time we execute.
|
// that the migration was run so we don't repeat it next time we execute.
|
||||||
foreach ($migrations as $file) {
|
foreach ($migrations as $file) {
|
||||||
$this->runUp($file, $extension);
|
$this->runUp($file, $path, $extension);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,17 +122,20 @@ class Migrator
|
||||||
* Run "up" a migration instance.
|
* Run "up" a migration instance.
|
||||||
*
|
*
|
||||||
* @param string $file
|
* @param string $file
|
||||||
|
* @param string $path
|
||||||
* @param Extension $extension
|
* @param Extension $extension
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function runUp($file, Extension $extension = null)
|
protected function runUp($file, $path, Extension $extension = null)
|
||||||
{
|
{
|
||||||
// First we will resolve a "real" instance of the migration class from this
|
// First we will resolve a "real" instance of the migration class from this
|
||||||
// migration file name. Once we have the instances we can run the actual
|
// migration file name. Once we have the instances we can run the actual
|
||||||
// command such as "up" or "down", or we can just simulate the action.
|
// command such as "up" or "down", or we can just simulate the action.
|
||||||
$migration = $this->resolve($file, $extension);
|
$migration = $this->resolve($file, $path);
|
||||||
|
|
||||||
$migration->up();
|
if (isset($migration['up'])) {
|
||||||
|
app()->call($migration['up']);
|
||||||
|
}
|
||||||
|
|
||||||
// Once we have run a migrations class, we will log that it was run in this
|
// Once we have run a migrations class, we will log that it was run in this
|
||||||
// repository so that we don't try to run it next time we do a migration
|
// repository so that we don't try to run it next time we do a migration
|
||||||
|
@ -154,15 +158,13 @@ class Migrator
|
||||||
|
|
||||||
$migrations = array_reverse($this->repository->getRan($extension->getId()));
|
$migrations = array_reverse($this->repository->getRan($extension->getId()));
|
||||||
|
|
||||||
$this->requireFiles($path, $migrations);
|
|
||||||
|
|
||||||
$count = count($migrations);
|
$count = count($migrations);
|
||||||
|
|
||||||
if ($count === 0) {
|
if ($count === 0) {
|
||||||
$this->note('<info>Nothing to rollback.</info>');
|
$this->note('<info>Nothing to rollback.</info>');
|
||||||
} else {
|
} else {
|
||||||
foreach ($migrations as $migration) {
|
foreach ($migrations as $migration) {
|
||||||
$this->runDown($migration, $extension);
|
$this->runDown($migration, $path, $extension);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,17 +175,20 @@ class Migrator
|
||||||
* Run "down" a migration instance.
|
* Run "down" a migration instance.
|
||||||
*
|
*
|
||||||
* @param string $file
|
* @param string $file
|
||||||
|
* @param string $path
|
||||||
* @param Extension $extension
|
* @param Extension $extension
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function runDown($file, Extension $extension = null)
|
protected function runDown($file, $path, Extension $extension = null)
|
||||||
{
|
{
|
||||||
// First we will get the file name of the migration so we can resolve out an
|
// First we will get the file name of the migration so we can resolve out an
|
||||||
// instance of the migration. Once we get an instance we can either run a
|
// instance of the migration. Once we get an instance we can either run a
|
||||||
// pretend execution of the migration or we can run the real migration.
|
// pretend execution of the migration or we can run the real migration.
|
||||||
$instance = $this->resolve($file, $extension);
|
$migration = $this->resolve($file, $path);
|
||||||
|
|
||||||
$instance->down();
|
if (isset($migration['down'])) {
|
||||||
|
app()->call($migration['down']);
|
||||||
|
}
|
||||||
|
|
||||||
// Once we have successfully run the migration "down" we will remove it from
|
// Once we have successfully run the migration "down" we will remove it from
|
||||||
// the migration repository so it will be considered to have not been run
|
// the migration repository so it will be considered to have not been run
|
||||||
|
@ -223,42 +228,15 @@ class Migrator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Require in all the migration files in a given path.
|
* Load a migration instance from a file.
|
||||||
*
|
*
|
||||||
|
* @param string $file
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @param array $files
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function requireFiles($path, array $files)
|
|
||||||
{
|
|
||||||
foreach ($files as $file) {
|
|
||||||
$this->files->requireOnce($path . '/' . $file . '.php');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resolve a migration instance from a file.
|
|
||||||
*
|
|
||||||
* @param string $file
|
|
||||||
* @param Extension $extension
|
|
||||||
* @return object
|
* @return object
|
||||||
*/
|
*/
|
||||||
public function resolve($file, Extension $extension = null)
|
public function resolve($file, $path)
|
||||||
{
|
{
|
||||||
$file = implode('_', array_slice(explode('_', $file), 4));
|
return $this->files->getRequire("$path/$file.php");
|
||||||
|
|
||||||
// flagrow/image-upload
|
|
||||||
if ($extension) {
|
|
||||||
$class = str_replace('/', '\\', $extension->name);
|
|
||||||
} else {
|
|
||||||
$class = 'Flarum\\Core';
|
|
||||||
}
|
|
||||||
|
|
||||||
$class .= '\\Migration\\';
|
|
||||||
|
|
||||||
$class .= Str::studly($file);
|
|
||||||
|
|
||||||
return app()->make($class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,18 +1,13 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace {{namespace}}\Migration;
|
use Illuminate\Database\Schema\Builder;
|
||||||
|
|
||||||
use Flarum\Database\AbstractMigration;
|
return [
|
||||||
|
'up' => function (Builder $schema) {
|
||||||
|
//
|
||||||
|
},
|
||||||
|
|
||||||
class {{name}} extends AbstractMigration
|
'down' => function (Builder $schema) {
|
||||||
{
|
|
||||||
public function up()
|
|
||||||
{
|
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
];
|
||||||
public function down()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,21 +1,16 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace {{namespace}}\Migration;
|
|
||||||
|
|
||||||
use Flarum\Database\AbstractMigration;
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Schema\Builder;
|
||||||
|
|
||||||
class {{name}} extends AbstractMigration
|
return [
|
||||||
{
|
'up' => function (Builder $schema) {
|
||||||
public function up()
|
$schema->create('{{table}}', function (Blueprint $table) {
|
||||||
{
|
|
||||||
$this->schema->create('{{table}}', function (Blueprint $table) {
|
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
|
|
||||||
public function down()
|
'down' => function (Builder $schema) {
|
||||||
{
|
$schema->drop('{{table}}');
|
||||||
$this->schema->drop('{{table}}');
|
|
||||||
}
|
}
|
||||||
}
|
];
|
||||||
|
|
|
@ -1,23 +1,18 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace {{namespace}}\Migration;
|
|
||||||
|
|
||||||
use Flarum\Database\AbstractMigration;
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Schema\Builder;
|
||||||
|
|
||||||
class {{name}} extends AbstractMigration
|
return [
|
||||||
{
|
'up' => function (Builder $schema) {
|
||||||
public function up()
|
$schema->table('{{table}}', function (Blueprint $table) {
|
||||||
{
|
//
|
||||||
$this->schema->table('{{table}}', function (Blueprint $table) {
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
'down' => function (Builder $schema) {
|
||||||
|
$schema->table('{{table}}', function (Blueprint $table) {
|
||||||
//
|
//
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
];
|
||||||
public function down()
|
|
||||||
{
|
|
||||||
$this->schema->table('{{table}}', function (Blueprint $table) {
|
|
||||||
//
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user