Skip to content

Commit

Permalink
Squash core migrations (#2842)
Browse files Browse the repository at this point in the history
Data migrations (seed default groups, seed default permissions) are deliberately excluded.
This also allows us to remove a lot of now unnecessary public API from the migrator and migration repository.
  • Loading branch information
askvortsov1 authored May 10, 2021
1 parent a28009f commit 6ecca95
Show file tree
Hide file tree
Showing 7 changed files with 510 additions and 44 deletions.
367 changes: 367 additions & 0 deletions migrations/install.dump

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Console/ConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public function register()
ResetCommand::class,
ScheduleListCommand::class,
ScheduleRunCommand::class
// Used internally to create DB dumps before major releases.
// \Flarum\Database\Console\GenerateDumpCommand::class
];
});

Expand Down
87 changes: 87 additions & 0 deletions src/Database/Console/GenerateDumpCommand.php
Original file line number Diff line number Diff line change
@@ -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));
}
}
16 changes: 0 additions & 16 deletions src/Database/DatabaseMigrationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,6 @@ public function delete($file, $extension = null)
$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.
*
Expand Down
7 changes: 0 additions & 7 deletions src/Database/MigrationRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ public function log($file, $extension = null);
*/
public function delete($file, $extension = null);

/**
* Create the migration repository data store.
*
* @return void
*/
public function createRepository();

/**
* Determine if the migration repository exists.
*
Expand Down
73 changes: 53 additions & 20 deletions src/Database/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
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;

class Migrator
Expand All @@ -39,6 +42,18 @@ class Migrator
*/
protected $schemaBuilder;

/**
* The DB table prefix.
*/
protected $tablePrefix;

/**
* The database schema builder instance.
*
* @var SchemaState
*/
protected $schemaState;

/**
* The output interface implementation.
*
Expand All @@ -61,7 +76,13 @@ public function __construct(
$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');
Expand Down Expand Up @@ -242,6 +263,38 @@ public function resolve($path, $file)
}
}

/**
* 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.
*
Expand All @@ -268,16 +321,6 @@ protected function note($message)
}
}

/**
* Get the migration repository instance.
*
* @return MigrationRepositoryInterface
*/
public function getRepository()
{
return $this->repository;
}

/**
* Determine if the migration repository exists.
*
Expand All @@ -287,14 +330,4 @@ public function repositoryExists()
{
return $this->repository->repositoryExists();
}

/**
* Get the file system instance.
*
* @return \Illuminate\Filesystem\Filesystem
*/
public function getFilesystem()
{
return $this->files;
}
}
2 changes: 1 addition & 1 deletion src/Install/Steps/RunMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function run()
{
$migrator = $this->getMigrator();

$migrator->getRepository()->createRepository();
$migrator->installFromSchema($this->path);
$migrator->run($this->path);
}

Expand Down

0 comments on commit 6ecca95

Please sign in to comment.