mirror of
https://github.com/flarum/core.git
synced 2025-08-08 09:26:34 +02:00
modifying migrator and moving files
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
|
||||
return [
|
||||
'up' => function (Builder $schema) {
|
||||
$db = $schema->getConnection();
|
||||
|
||||
$db->table('migrations')
|
||||
->whereNull('extension')
|
||||
->update(['migration' => $db->raw("CONCAT('v0.1/', migration)")]);
|
||||
},
|
||||
|
||||
'down' => function (Builder $schema) {
|
||||
$db = $schema->getConnection();
|
||||
|
||||
$db->table('migrations')
|
||||
->where('permission', 'LIKE', 'viewForum')
|
||||
->update(['permission' => $db->raw("REPLACE(migration, 'v0.1/', '')")]);
|
||||
}
|
||||
];
|
116
src/Database/MigrationSourceRepository.php
Normal file
116
src/Database/MigrationSourceRepository.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace Flarum\Database;
|
||||
|
||||
use Flarum\Extension\Extension;
|
||||
use Flarum\Foundation\Application;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class MigrationSourceRepository
|
||||
{
|
||||
protected $connection;
|
||||
|
||||
public function __construct(ConnectionInterface $connection)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
public function flarum(): array
|
||||
{
|
||||
if (! $this->databaseVersion()) {
|
||||
return $this->install();
|
||||
}
|
||||
|
||||
return $this->upgrade();
|
||||
}
|
||||
|
||||
public function extension(Extension $extension): ?array
|
||||
{
|
||||
if (! $extension->hasMigrations()) return [];
|
||||
|
||||
return $extension->getMigrations();
|
||||
}
|
||||
|
||||
protected function install(): array
|
||||
{
|
||||
// We read every file from the latest major/minor version migrations directory.
|
||||
// Including the create_<table>_table statements.
|
||||
$files = glob(__DIR__ . '/../../migrations/' . $this->installedVersion(true) . '/[0-9_]{15}_*.php');
|
||||
|
||||
// Sort by timestamp.
|
||||
sort($files);
|
||||
|
||||
$create = glob(__DIR__ . '/../../migrations/' . $this->installedVersion(true) . '/create_*.php');
|
||||
|
||||
return array_merge($create, $files);
|
||||
}
|
||||
|
||||
protected function upgrade(): array
|
||||
{
|
||||
$files = [];
|
||||
$add = false;
|
||||
|
||||
$directories = glob(__DIR__ . '/../../migrations/', GLOB_ONLYDIR);
|
||||
sort($directories, SORT_NATURAL);
|
||||
|
||||
// Upgrade
|
||||
// Loop over all version migrations directory until we find the version that is currently active.
|
||||
foreach ($directories as $directory) {
|
||||
// We have found the directory matching the version database version. Start adding files.
|
||||
if (substr($directory, 1) === $this->databaseVersion(true)) {
|
||||
$add = true;
|
||||
}
|
||||
|
||||
if ($add) {
|
||||
// Selectively add files, but only include those matching the format YYYY_MM_DD_HHIISS_<something>.php
|
||||
// This excludes the create_<table>_table.
|
||||
$files = array_merge($files, glob(__DIR__ . "/../../migrations/$directory/[0-9_]{15}_*.php"));
|
||||
}
|
||||
|
||||
// Once we found the version that is installed, we can quit.
|
||||
// Theoretically this should never be necessary, it could just loop over all remaining ones.
|
||||
if (substr($directory, 1) === $this->installedVersion(true)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by timestamp.
|
||||
sort($files);
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
protected function installedVersion(bool $short = false): string
|
||||
{
|
||||
$version = Application::VERSION;
|
||||
|
||||
if ($short && $version) {
|
||||
return $this->shortVersion($version);
|
||||
}
|
||||
|
||||
return $version;
|
||||
}
|
||||
|
||||
protected function shortVersion(string $version): string
|
||||
{
|
||||
if (preg_match('~(?<version>^[0-9]+\.[0-9]+)~', $version, $m)) {
|
||||
return $m['version'];
|
||||
}
|
||||
|
||||
return $version;
|
||||
}
|
||||
|
||||
protected function databaseVersion(bool $short = false): ?string
|
||||
{
|
||||
$version = $this->connection->getSchemaBuilder()->hasTable('settings')
|
||||
? $this->connection->table('settings')->where('key', 'version')->value('value')
|
||||
: null;
|
||||
|
||||
if ($short && $version) {
|
||||
return $this->shortVersion($version);
|
||||
}
|
||||
|
||||
return $version;
|
||||
}
|
||||
}
|
@@ -11,6 +11,7 @@ namespace Flarum\Database;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Extension\Extension;
|
||||
use Flarum\Foundation\Application;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
@@ -48,6 +49,10 @@ class Migrator
|
||||
* @var OutputInterface
|
||||
*/
|
||||
protected $output;
|
||||
/**
|
||||
* @var MigrationSourceRepository
|
||||
*/
|
||||
protected $source;
|
||||
|
||||
/**
|
||||
* Create a new migrator instance.
|
||||
@@ -64,6 +69,7 @@ class Migrator
|
||||
$this->files = $files;
|
||||
$this->repository = $repository;
|
||||
|
||||
$this->source = new MigrationSourceRepository($connection);
|
||||
$this->schemaBuilder = $connection->getSchemaBuilder();
|
||||
|
||||
// Workaround for https://github.com/laravel/framework/issues/1186
|
||||
@@ -79,7 +85,7 @@ class Migrator
|
||||
*/
|
||||
public function run($path, Extension $extension = null)
|
||||
{
|
||||
$files = $this->getMigrationFiles($path);
|
||||
$files = $this->getMigrationFiles($path, $extension);
|
||||
|
||||
$ran = $this->repository->getRan($extension ? $extension->getId() : null);
|
||||
|
||||
@@ -203,30 +209,13 @@ class Migrator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the migration files in a given path.
|
||||
*
|
||||
* @param string $path
|
||||
* @return array
|
||||
*/
|
||||
public function getMigrationFiles($path)
|
||||
public function getMigrationFiles(string $path, Extension $extension = null): array
|
||||
{
|
||||
$files = $this->files->glob($path.'/*_*.php');
|
||||
|
||||
if ($files === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$files = array_map(function ($file) {
|
||||
$files = $extension ? $this->source->extension($extension) : $this->source->flarum();
|
||||
dd($files);
|
||||
return array_map(function ($file) {
|
||||
return str_replace('.php', '', basename($file));
|
||||
}, $files);
|
||||
|
||||
// Once we have all of the formatted file names we will sort them and since
|
||||
// they all start with a timestamp this should give us the migrations in
|
||||
// the order they were actually created by the application developers.
|
||||
sort($files);
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -459,6 +459,17 @@ class Extension implements Arrayable
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the list of migrations of this extension.
|
||||
*
|
||||
* @return array
|
||||
* @internal
|
||||
*/
|
||||
public function getMigrations(): array
|
||||
{
|
||||
return glob($this->path.'/migrations/*_*.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether the extension has migrations.
|
||||
*
|
||||
@@ -469,22 +480,6 @@ class Extension implements Arrayable
|
||||
return realpath($this->path.'/migrations/') !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function migrate(Migrator $migrator, $direction = 'up')
|
||||
{
|
||||
if (! $this->hasMigrations()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($direction == 'up') {
|
||||
return $migrator->run($this->getPath().'/migrations', $this);
|
||||
} else {
|
||||
return $migrator->reset($this->getPath().'/migrations', $this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an array result for the object.
|
||||
*
|
||||
|
Reference in New Issue
Block a user