mirror of
https://github.com/flarum/core.git
synced 2025-10-18 02:06:08 +02:00
Revert "Revert "Squash core migrations (#2842)""
This reverts commit 16f3ae9d1e
.
This commit is contained in:
@@ -64,6 +64,8 @@ class ConsoleServiceProvider extends AbstractServiceProvider
|
||||
ResetCommand::class,
|
||||
ScheduleListCommand::class,
|
||||
ScheduleRunCommand::class
|
||||
// Used internally to create DB dumps before major releases.
|
||||
// \Flarum\Database\Console\GenerateDumpCommand::class
|
||||
];
|
||||
});
|
||||
|
||||
|
87
src/Database/Console/GenerateDumpCommand.php
Normal file
87
src/Database/Console/GenerateDumpCommand.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Database\Console;
|
||||
|
||||
use Flarum\Console\AbstractCommand;
|
||||
use Flarum\Foundation\Paths;
|
||||
use Illuminate\Database\Connection;
|
||||
|
||||
class GenerateDumpCommand extends AbstractCommand
|
||||
{
|
||||
/**
|
||||
* @var Connection
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* @var Paths
|
||||
*/
|
||||
protected $paths;
|
||||
|
||||
/**
|
||||
* @param Connection $connection
|
||||
* @param Paths $paths
|
||||
*/
|
||||
public function __construct(Connection $connection, Paths $paths)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
$this->paths = $paths;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('schema:dump')
|
||||
->setDescription('Dump DB schema');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function fire()
|
||||
{
|
||||
$dumpPath = __DIR__.'/../../../migrations/install.dump';
|
||||
/** @var Connection */
|
||||
$connection = resolve('db.connection');
|
||||
|
||||
$connection
|
||||
->getSchemaState()
|
||||
->withMigrationTable($connection->getTablePrefix().'migrations')
|
||||
->handleOutputUsing(function ($type, $buffer) {
|
||||
$this->output->write($buffer);
|
||||
})
|
||||
->dump($connection, $dumpPath);
|
||||
|
||||
// We need to remove any data migrations, as those won't be captured
|
||||
// in the schema dump, and must be run separately.
|
||||
$coreDataMigrations = [
|
||||
'2018_07_21_000000_seed_default_groups',
|
||||
'2018_07_21_000100_seed_default_group_permissions',
|
||||
];
|
||||
|
||||
$newDump = [];
|
||||
$dump = file($dumpPath);
|
||||
foreach ($dump as $line) {
|
||||
foreach ($coreDataMigrations as $excludeMigrationId) {
|
||||
if (strpos($line, $excludeMigrationId) !== false) {
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
$newDump[] = $line;
|
||||
}
|
||||
|
||||
file_put_contents($dumpPath, implode($newDump));
|
||||
}
|
||||
}
|
@@ -88,22 +88,6 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface
|
||||
$query->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the migration repository data store.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function createRepository()
|
||||
{
|
||||
$schema = $this->connection->getSchemaBuilder();
|
||||
|
||||
$schema->create($this->table, function ($table) {
|
||||
$table->increments('id');
|
||||
$table->string('migration');
|
||||
$table->string('extension')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the migration repository exists.
|
||||
*
|
||||
|
@@ -37,13 +37,6 @@ interface MigrationRepositoryInterface
|
||||
*/
|
||||
public function delete($file, $extension = null);
|
||||
|
||||
/**
|
||||
* Create the migration repository data store.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function createRepository();
|
||||
|
||||
/**
|
||||
* Determine if the migration repository exists.
|
||||
*
|
||||
|
@@ -12,8 +12,11 @@ namespace Flarum\Database;
|
||||
use Exception;
|
||||
use Flarum\Extension\Extension;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Illuminate\Database\MySqlConnection;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
use Illuminate\Database\Schema\SchemaState;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use InvalidArgumentException;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
@@ -42,6 +45,18 @@ class Migrator
|
||||
*/
|
||||
protected $schemaBuilder;
|
||||
|
||||
/**
|
||||
* The DB table prefix.
|
||||
*/
|
||||
protected $tablePrefix;
|
||||
|
||||
/**
|
||||
* The database schema builder instance.
|
||||
*
|
||||
* @var SchemaState
|
||||
*/
|
||||
protected $schemaState;
|
||||
|
||||
/**
|
||||
* The output interface implementation.
|
||||
*
|
||||
@@ -64,7 +79,13 @@ class Migrator
|
||||
$this->files = $files;
|
||||
$this->repository = $repository;
|
||||
|
||||
if (! ($connection instanceof MySqlConnection)) {
|
||||
throw new InvalidArgumentException('Only MySQL connections are supported');
|
||||
}
|
||||
|
||||
$this->tablePrefix = $connection->getTablePrefix();
|
||||
$this->schemaBuilder = $connection->getSchemaBuilder();
|
||||
$this->schemaState = $connection->getSchemaState();
|
||||
|
||||
// Workaround for https://github.com/laravel/framework/issues/1186
|
||||
$connection->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
|
||||
@@ -245,6 +266,38 @@ class Migrator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the Flarum database from a schema dump.
|
||||
*
|
||||
* @param string $path to the directory containing the dump.
|
||||
*/
|
||||
public function installFromSchema(string $path)
|
||||
{
|
||||
$schemaPath = "$path/install.dump";
|
||||
|
||||
// If we can't create a tmp file, fall back to the vendor directory.
|
||||
$schemaWithPrefixes = tempnam(sys_get_temp_dir(), 'install');
|
||||
if (! $schemaWithPrefixes) {
|
||||
$schemaWithPrefixes = "$path/install_dump.dump.tmp";
|
||||
}
|
||||
|
||||
$currDumpFile = file_get_contents($schemaPath);
|
||||
|
||||
file_put_contents($schemaWithPrefixes, str_replace('db_prefix_', $this->tablePrefix, $currDumpFile));
|
||||
|
||||
$this->note('<info>Loading stored database schema:</info>');
|
||||
$startTime = microtime(true);
|
||||
|
||||
$this->schemaState->handleOutputUsing(function ($type, $buffer) {
|
||||
$this->output->write($buffer);
|
||||
})->load($schemaWithPrefixes);
|
||||
|
||||
$runTime = number_format((microtime(true) - $startTime) * 1000, 2);
|
||||
$this->note('<info>Loaded stored database schema.</info> ('.$runTime.'ms)');
|
||||
|
||||
unlink($schemaWithPrefixes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the output implementation that should be used by the console.
|
||||
*
|
||||
@@ -271,16 +324,6 @@ class Migrator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the migration repository instance.
|
||||
*
|
||||
* @return MigrationRepositoryInterface
|
||||
*/
|
||||
public function getRepository()
|
||||
{
|
||||
return $this->repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the migration repository exists.
|
||||
*
|
||||
@@ -290,14 +333,4 @@ class Migrator
|
||||
{
|
||||
return $this->repository->repositoryExists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file system instance.
|
||||
*
|
||||
* @return \Illuminate\Filesystem\Filesystem
|
||||
*/
|
||||
public function getFilesystem()
|
||||
{
|
||||
return $this->files;
|
||||
}
|
||||
}
|
||||
|
@@ -42,7 +42,7 @@ class RunMigrations implements Step
|
||||
{
|
||||
$migrator = $this->getMigrator();
|
||||
|
||||
$migrator->getRepository()->createRepository();
|
||||
$migrator->installFromSchema($this->path);
|
||||
$migrator->run($this->path);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user