mirror of
https://github.com/flarum/core.git
synced 2025-10-12 23:44:27 +02:00
Implement proper update process
If the version in the settings table mismatches the code version, then we return a 503 error for all requests coming through index.php and api.php, while admin.php serves up a form prompting for the database password which will run outstanding migrations.
This commit is contained in:
93
src/Update/Console/MigrateCommand.php
Normal file
93
src/Update/Console/MigrateCommand.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Update\Console;
|
||||
|
||||
use Flarum\Console\Command\AbstractCommand;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class MigrateCommand extends AbstractCommand
|
||||
{
|
||||
/**
|
||||
* @var Container
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* @param Container $container
|
||||
*/
|
||||
public function __construct(Container $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('migrate')
|
||||
->setDescription("Run outstanding migrations.");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function fire()
|
||||
{
|
||||
$this->info('Migrating Flarum...');
|
||||
|
||||
$this->upgrade();
|
||||
|
||||
$this->info('DONE.');
|
||||
}
|
||||
|
||||
public function upgrade()
|
||||
{
|
||||
$this->container->bind('Illuminate\Database\Schema\Builder', function ($container) {
|
||||
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
|
||||
});
|
||||
|
||||
$migrator = $this->container->make('Flarum\Database\Migrator');
|
||||
|
||||
$migrator->run(base_path('core/migrations'));
|
||||
|
||||
foreach ($migrator->getNotes() as $note) {
|
||||
$this->info($note);
|
||||
}
|
||||
|
||||
$extensions = $this->container->make('Flarum\Extension\ExtensionManager');
|
||||
|
||||
$migrator = $extensions->getMigrator();
|
||||
|
||||
foreach ($extensions->getInfo() as $name => $extension) {
|
||||
if (! $extensions->isEnabled($name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->info('Migrating extension: '.$name);
|
||||
|
||||
$extensions->migrate($name);
|
||||
|
||||
foreach ($migrator->getNotes() as $note) {
|
||||
$this->info($note);
|
||||
}
|
||||
}
|
||||
|
||||
$this->container->make('Flarum\Settings\SettingsRepositoryInterface')->set('version', $this->container->version());
|
||||
}
|
||||
}
|
45
src/Update/Controller/IndexController.php
Normal file
45
src/Update/Controller/IndexController.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Update\Controller;
|
||||
|
||||
use Flarum\Http\Controller\AbstractHtmlController;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
|
||||
class IndexController extends AbstractHtmlController
|
||||
{
|
||||
/**
|
||||
* @var Factory
|
||||
*/
|
||||
protected $view;
|
||||
|
||||
/**
|
||||
* @param Factory $view
|
||||
*/
|
||||
public function __construct(Factory $view)
|
||||
{
|
||||
$this->view = $view;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param array $routeParams
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
public function render(Request $request, array $routeParams = [])
|
||||
{
|
||||
$view = $this->view->make('flarum.update::app')->with('title', 'Update Flarum');
|
||||
|
||||
$view->with('content', $this->view->make('flarum.update::update'));
|
||||
|
||||
return $view;
|
||||
}
|
||||
}
|
68
src/Update/Controller/UpdateController.php
Normal file
68
src/Update/Controller/UpdateController.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Update\Controller;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Foundation\Application;
|
||||
use Flarum\Http\Controller\ControllerInterface;
|
||||
use Flarum\Update\Console\MigrateCommand;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Symfony\Component\Console\Input\StringInput;
|
||||
use Symfony\Component\Console\Output\StreamOutput;
|
||||
use Zend\Diactoros\Response;
|
||||
use Zend\Diactoros\Response\HtmlResponse;
|
||||
|
||||
class UpdateController implements ControllerInterface
|
||||
{
|
||||
protected $command;
|
||||
|
||||
/**
|
||||
* @var Application
|
||||
*/
|
||||
protected $app;
|
||||
|
||||
/**
|
||||
* @param MigrateCommand $command
|
||||
* @param Application $app
|
||||
*/
|
||||
public function __construct(MigrateCommand $command, Application $app)
|
||||
{
|
||||
$this->command = $command;
|
||||
$this->app = $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param array $routeParams
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
*/
|
||||
public function handle(Request $request, array $routeParams = [])
|
||||
{
|
||||
$input = $request->getParsedBody();
|
||||
|
||||
if (array_get($input, 'databasePassword') !== $this->app->config('database.password')) {
|
||||
return new HtmlResponse('Incorrect database password.', 500);
|
||||
}
|
||||
|
||||
$body = fopen('php://temp', 'wb+');
|
||||
$input = new StringInput('');
|
||||
$output = new StreamOutput($body);
|
||||
|
||||
try {
|
||||
$this->command->run($input, $output);
|
||||
} catch (Exception $e) {
|
||||
return new HtmlResponse($e->getMessage(), 500);
|
||||
}
|
||||
|
||||
return new Response($body, 200);
|
||||
}
|
||||
}
|
57
src/Update/UpdateServiceProvider.php
Normal file
57
src/Update/UpdateServiceProvider.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Update;
|
||||
|
||||
use Flarum\Http\GenerateRouteHandlerTrait;
|
||||
use Flarum\Http\RouteCollection;
|
||||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class UpdateServiceProvider extends AbstractServiceProvider
|
||||
{
|
||||
use GenerateRouteHandlerTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->singleton('flarum.update.routes', function () {
|
||||
return $this->getRoutes();
|
||||
});
|
||||
|
||||
$this->loadViewsFrom(__DIR__.'/../../views/install', 'flarum.update');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return RouteCollection
|
||||
*/
|
||||
protected function getRoutes()
|
||||
{
|
||||
$routes = new RouteCollection;
|
||||
|
||||
$toController = $this->getHandlerGenerator($this->app);
|
||||
|
||||
$routes->get(
|
||||
'/',
|
||||
'index',
|
||||
$toController('Flarum\Update\Controller\IndexController')
|
||||
);
|
||||
|
||||
$routes->post(
|
||||
'/',
|
||||
'update',
|
||||
$toController('Flarum\Update\Controller\UpdateController')
|
||||
);
|
||||
|
||||
return $routes;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user