1
0
mirror of https://github.com/flarum/core.git synced 2025-10-18 10:16:09 +02:00

Revert "Squash core migrations (#2842)"

This reverts commit 6ecca9565a.
This commit is contained in:
Daniel Klabbers
2021-05-29 22:38:25 +02:00
parent a976a2118a
commit 16f3ae9d1e
7 changed files with 44 additions and 510 deletions

View File

@@ -64,8 +64,6 @@ class ConsoleServiceProvider extends AbstractServiceProvider
ResetCommand::class,
ScheduleListCommand::class,
ScheduleRunCommand::class
// Used internally to create DB dumps before major releases.
// \Flarum\Database\Console\GenerateDumpCommand::class
];
});

View File

@@ -1,87 +0,0 @@
<?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));
}
}

View File

@@ -88,6 +88,22 @@ 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.
*

View File

@@ -37,6 +37,13 @@ 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.
*

View File

@@ -12,11 +12,8 @@ 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;
/**
@@ -45,18 +42,6 @@ class Migrator
*/
protected $schemaBuilder;
/**
* The DB table prefix.
*/
protected $tablePrefix;
/**
* The database schema builder instance.
*
* @var SchemaState
*/
protected $schemaState;
/**
* The output interface implementation.
*
@@ -79,13 +64,7 @@ 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');
@@ -266,38 +245,6 @@ 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.
*
@@ -324,6 +271,16 @@ class Migrator
}
}
/**
* Get the migration repository instance.
*
* @return MigrationRepositoryInterface
*/
public function getRepository()
{
return $this->repository;
}
/**
* Determine if the migration repository exists.
*
@@ -333,4 +290,14 @@ class Migrator
{
return $this->repository->repositoryExists();
}
/**
* Get the file system instance.
*
* @return \Illuminate\Filesystem\Filesystem
*/
public function getFilesystem()
{
return $this->files;
}
}

View File

@@ -42,7 +42,7 @@ class RunMigrations implements Step
{
$migrator = $this->getMigrator();
$migrator->installFromSchema($this->path);
$migrator->getRepository()->createRepository();
$migrator->run($this->path);
}