mirror of
https://github.com/flarum/framework.git
synced 2024-11-28 03:32:49 +08:00
Improve install command, add custom migrations system
Implemented our own migration repository + migrator (based on Laravel's stuff) so that we can keep track of which migrations have been run for core and per-extension. That way we can simple call the migrator to upgrade core/extensions, and to uninstall extensions.
This commit is contained in:
parent
5228628a71
commit
2edcbacccc
|
@ -1,8 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Flarum\Install\Migration;
|
||||
use Flarum\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateAccessTokensTable extends Migration
|
||||
{
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
<?php
|
||||
|
||||
use Flarum\Install\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateActivityTable extends Migration
|
||||
{
|
||||
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
$this->schema->create('activity', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->integer('user_id')->unsigned();
|
||||
$table->string('type', 100);
|
||||
$table->integer('subject_id')->unsigned()->nullable();
|
||||
$table->binary('data')->nullable();
|
||||
$table->dateTime('time');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
$this->schema->drop('activity');
|
||||
}
|
||||
}
|
|
@ -1,8 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Flarum\Install\Migration;
|
||||
use Flarum\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateConfigTable extends Migration
|
||||
{
|
||||
|
@ -15,7 +14,6 @@ class CreateConfigTable extends Migration
|
|||
public function up()
|
||||
{
|
||||
$this->schema->create('config', function (Blueprint $table) {
|
||||
|
||||
$table->string('key', 100)->primary();
|
||||
$table->binary('value')->nullable();
|
||||
});
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Flarum\Install\Migration;
|
||||
use Flarum\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateDiscussionsTable extends Migration
|
||||
{
|
||||
|
@ -15,7 +14,6 @@ class CreateDiscussionsTable extends Migration
|
|||
public function up()
|
||||
{
|
||||
$this->schema->create('discussions', function (Blueprint $table) {
|
||||
|
||||
$table->increments('id');
|
||||
$table->string('title', 200);
|
||||
$table->integer('comments_count')->unsigned()->default(0);
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Flarum\Install\Migration;
|
||||
use Flarum\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateEmailTokensTable extends Migration
|
||||
{
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Flarum\Install\Migration;
|
||||
use Flarum\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateGroupsTable extends Migration
|
||||
{
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Flarum\Install\Migration;
|
||||
use Flarum\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateNotificationsTable extends Migration
|
||||
{
|
||||
|
@ -15,7 +14,6 @@ class CreateNotificationsTable extends Migration
|
|||
public function up()
|
||||
{
|
||||
$this->schema->create('notifications', function (Blueprint $table) {
|
||||
|
||||
$table->increments('id');
|
||||
$table->integer('user_id')->unsigned();
|
||||
$table->integer('sender_id')->unsigned()->nullable();
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Flarum\Install\Migration;
|
||||
use Flarum\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreatePasswordTokensTable extends Migration
|
||||
{
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Flarum\Install\Migration;
|
||||
use Flarum\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreatePermissionsTable extends Migration
|
||||
{
|
||||
|
@ -15,7 +14,6 @@ class CreatePermissionsTable extends Migration
|
|||
public function up()
|
||||
{
|
||||
$this->schema->create('permissions', function (Blueprint $table) {
|
||||
|
||||
$table->integer('group_id')->unsigned();
|
||||
$table->string('permission', 100);
|
||||
$table->primary(['group_id', 'permission']);
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Flarum\Install\Migration;
|
||||
use Flarum\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreatePostsTable extends Migration
|
||||
{
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Flarum\Install\Migration;
|
||||
use Flarum\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateUsersDiscussionsTable extends Migration
|
||||
{
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Flarum\Install\Migration;
|
||||
use Flarum\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateUsersGroupsTable extends Migration
|
||||
{
|
||||
|
@ -15,7 +14,6 @@ class CreateUsersGroupsTable extends Migration
|
|||
public function up()
|
||||
{
|
||||
$this->schema->create('users_groups', function (Blueprint $table) {
|
||||
|
||||
$table->integer('user_id')->unsigned();
|
||||
$table->integer('group_id')->unsigned();
|
||||
$table->primary(['user_id', 'group_id']);
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
<?php
|
||||
|
||||
use Flarum\Install\Migration;
|
||||
use Flarum\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateUsersTable extends Migration
|
||||
{
|
||||
|
|
29
migrations/2015_08_14_000000_create_test_table.php
Normal file
29
migrations/2015_08_14_000000_create_test_table.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
use Flarum\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
class CreateTestTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
$this->schema->create('test', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
$this->schema->drop('test');
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
<?php namespace Flarum\Console;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class ConsoleServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap the application events.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
//$this->commands('Flarum\Console\InstallCommand');
|
||||
$this->commands('Flarum\Console\SeedCommand');
|
||||
$this->commands('Flarum\Console\ImportCommand');
|
||||
$this->commands('Flarum\Console\GenerateExtensionCommand');
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,37 +1,28 @@
|
|||
<?php namespace Flarum\Console;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
|
||||
class GenerateExtensionCommand extends Command
|
||||
{
|
||||
|
||||
/**
|
||||
* The console command name.
|
||||
*
|
||||
* @var string
|
||||
* @var Container
|
||||
*/
|
||||
protected $name = 'generate:extension';
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generate a Flarum extension skeleton.';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Application $app)
|
||||
public function __construct(Container $container)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->container = $container;
|
||||
|
||||
$this->app = $app;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('generate:extension')
|
||||
->setDescription("Generate a Flarum extension skeleton.");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -39,7 +30,7 @@ class GenerateExtensionCommand extends Command
|
|||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function fire()
|
||||
protected function fire()
|
||||
{
|
||||
do {
|
||||
$vendor = $this->ask('Vendor name:');
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
<?php namespace Flarum\Console;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
|
||||
class SeedCommand extends Command
|
||||
{
|
||||
|
||||
/**
|
||||
* The console command name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'seed';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Seed Flarum\'s database with fake data.';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function fire()
|
||||
{
|
||||
$this->call('db:seed', ['--class' => 'Flarum\Core\Seeders\UsersTableSeeder']);
|
||||
$this->call('db:seed', ['--class' => 'Flarum\Core\Seeders\DiscussionsTableSeeder']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command arguments.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getArguments()
|
||||
{
|
||||
return [
|
||||
// ['example', InputArgument::REQUIRED, 'An example argument.'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
return [
|
||||
// ['example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null],
|
||||
];
|
||||
}
|
||||
}
|
56
src/Console/UpgradeCommand.php
Normal file
56
src/Console/UpgradeCommand.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php namespace Flarum\Console;
|
||||
|
||||
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 UpgradeCommand extends Command
|
||||
{
|
||||
/**
|
||||
* @var Container
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
public function __construct(Container $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('upgrade')
|
||||
->setDescription("Run Flarum's upgrade script");
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function fire()
|
||||
{
|
||||
$this->info('Upgrading Flarum...');
|
||||
|
||||
$this->upgrade();
|
||||
|
||||
$this->info('DONE.');
|
||||
}
|
||||
|
||||
protected function upgrade()
|
||||
{
|
||||
$this->container->bind('Illuminate\Database\Schema\Builder', function($container) {
|
||||
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
|
||||
});
|
||||
|
||||
$migrator = $this->container->make('Flarum\Migrations\Migrator');
|
||||
|
||||
$migrator->run(base_path('core/migrations'));
|
||||
|
||||
foreach ($migrator->getNotes() as $note) {
|
||||
$this->info($note);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ use Illuminate\Database\ConnectionResolver;
|
|||
use Illuminate\Database\Connectors\ConnectionFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use PDO;
|
||||
use Flarum\Migrations\DatabaseMigrationRepository;
|
||||
|
||||
class DatabaseServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
@ -44,5 +45,10 @@ class DatabaseServiceProvider extends ServiceProvider
|
|||
Model::setEventDispatcher($this->app->make('events'));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
$this->app->singleton('Flarum\Migrations\MigrationRepositoryInterface', function ($app) {
|
||||
return new DatabaseMigrationRepository($app['db'], 'migrations');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,6 +43,35 @@ class DataFromUser implements ProvidesData
|
|||
];
|
||||
}
|
||||
|
||||
public function getSettings()
|
||||
{
|
||||
$baseUrl = rtrim($this->ask('Base URL:'), '/');
|
||||
$title = $this->ask('Forum title:');
|
||||
|
||||
return [
|
||||
'admin_url' => $baseUrl . '/admin',
|
||||
'allow_post_editing' => 'reply',
|
||||
'allow_renaming' => '10',
|
||||
'allow_sign_up' => '1',
|
||||
'api_url' => $baseUrl . '/api',
|
||||
'base_url' => $baseUrl,
|
||||
'custom_less' => '',
|
||||
'default_locale' => 'en',
|
||||
'default_route' => '/all',
|
||||
'extensions_enabled' => '[]',
|
||||
'forum_title' => $title,
|
||||
'forum_description' => '',
|
||||
'mail_driver' => 'mail',
|
||||
'mail_from' => 'noreply@' . preg_replace('/^www\./i', '', parse_url($baseUrl, PHP_URL_HOST)),
|
||||
'theme_colored_header' => '0',
|
||||
'theme_dark_mode' => '0',
|
||||
'theme_primary_color' => '#29415E',
|
||||
'theme_secondary_color' => '#29415E',
|
||||
'welcome_message' => 'This is beta software and you should not use it in production.',
|
||||
'welcome_title' => 'Welcome to ' . $title,
|
||||
];
|
||||
}
|
||||
|
||||
protected function ask($question, $default = null)
|
||||
{
|
||||
$question = new Question("<question>$question</question> ", $default);
|
||||
|
|
|
@ -7,7 +7,7 @@ class DefaultData implements ProvidesData
|
|||
return [
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'database' => 'flarum',
|
||||
'database' => 'flarum_console',
|
||||
'username' => 'root',
|
||||
'password' => 'root',
|
||||
'prefix' => '',
|
||||
|
@ -22,4 +22,30 @@ class DefaultData implements ProvidesData
|
|||
'email' => 'admin@example.com',
|
||||
];
|
||||
}
|
||||
|
||||
public function getSettings()
|
||||
{
|
||||
return [
|
||||
'admin_url' => 'http://flarum.dev/admin',
|
||||
'allow_post_editing' => 'reply',
|
||||
'allow_renaming' => '10',
|
||||
'allow_sign_up' => '1',
|
||||
'api_url' => 'http://flarum.dev/api',
|
||||
'base_url' => 'http://flarum.dev',
|
||||
'custom_less' => '',
|
||||
'default_locale' => 'en',
|
||||
'default_route' => '/all',
|
||||
'extensions_enabled' => '[]',
|
||||
'forum_title' => 'Development Forum',
|
||||
'forum_description' => '',
|
||||
'mail_driver' => 'mail',
|
||||
'mail_from' => 'noreply@flarum.dev',
|
||||
'theme_colored_header' => '0',
|
||||
'theme_dark_mode' => '0',
|
||||
'theme_primary_color' => '#29415E',
|
||||
'theme_secondary_color' => '#29415E',
|
||||
'welcome_message' => 'This is beta software and you should not use it in production.',
|
||||
'welcome_title' => 'Welcome to Development Forum',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
<?php namespace Flarum\Install\Console;
|
||||
|
||||
use Flarum\Console\Command;
|
||||
use Flarum\Core\Model;
|
||||
use Flarum\Core\Users\User;
|
||||
use Flarum\Core\Groups\Group;
|
||||
use Flarum\Core\Groups\Permission;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
@ -23,10 +27,6 @@ class InstallCommand extends Command
|
|||
{
|
||||
$this->container = $container;
|
||||
|
||||
$this->container->bind('Illuminate\Database\Schema\Builder', function($container) {
|
||||
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
|
||||
});
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
@ -72,7 +72,19 @@ class InstallCommand extends Command
|
|||
|
||||
$this->runMigrations();
|
||||
|
||||
$this->writeSettings();
|
||||
|
||||
$this->container->register('Flarum\Core\CoreServiceProvider');
|
||||
|
||||
$resolver = $this->container->make('Illuminate\Database\ConnectionResolverInterface');
|
||||
Model::setConnectionResolver($resolver);
|
||||
Model::setEventDispatcher($this->container->make('events'));
|
||||
|
||||
$this->seedGroups();
|
||||
$this->seedPermissions();
|
||||
|
||||
$this->createAdminUser();
|
||||
|
||||
}
|
||||
|
||||
protected function storeConfiguration()
|
||||
|
@ -87,8 +99,8 @@ class InstallCommand extends Command
|
|||
'database' => $dbConfig['database'],
|
||||
'username' => $dbConfig['username'],
|
||||
'password' => $dbConfig['password'],
|
||||
'charset' => 'utf8',
|
||||
'collation' => 'utf8_unicode_ci',
|
||||
'charset' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_unicode_ci',
|
||||
'prefix' => $dbConfig['prefix'],
|
||||
'strict' => false
|
||||
],
|
||||
|
@ -105,24 +117,80 @@ class InstallCommand extends Command
|
|||
|
||||
protected function runMigrations()
|
||||
{
|
||||
$migrationDir = base_path('core/migrations');
|
||||
$this->container->bind('Illuminate\Database\Schema\Builder', function($container) {
|
||||
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
|
||||
});
|
||||
|
||||
$files = glob("$migrationDir/*_*.php") or [];
|
||||
sort($files);
|
||||
$migrator = $this->container->make('Flarum\Migrations\Migrator');
|
||||
$migrator->getRepository()->createRepository();
|
||||
|
||||
foreach ($files as $file) {
|
||||
require $file;
|
||||
$migrator->run(base_path('core/migrations'));
|
||||
|
||||
$migrationClass = studly_case(substr(basename($file), 18));
|
||||
$migrationClass = str_replace('.php', '', $migrationClass);
|
||||
$migration = $this->container->make($migrationClass);
|
||||
|
||||
$this->info("Migrating $migrationClass");
|
||||
|
||||
$migration->up();
|
||||
foreach ($migrator->getNotes() as $note) {
|
||||
$this->info($note);
|
||||
}
|
||||
}
|
||||
|
||||
protected function writeSettings()
|
||||
{
|
||||
$data = $this->dataSource->getSettings();
|
||||
$settings = $this->container->make('Flarum\Core\Settings\SettingsRepository');
|
||||
|
||||
$this->info('Writing default settings');
|
||||
|
||||
foreach ($data as $k => $v) {
|
||||
$settings->set($k, $v);
|
||||
}
|
||||
}
|
||||
|
||||
protected function seedGroups()
|
||||
{
|
||||
Group::unguard();
|
||||
|
||||
$groups = [
|
||||
['Admin', 'Admins', '#B72A2A', 'wrench'],
|
||||
['Guest', 'Guests', null, null],
|
||||
['Member', 'Members', null, null],
|
||||
['Mod', 'Mods', '#80349E', 'bolt']
|
||||
];
|
||||
|
||||
foreach ($groups as $group) {
|
||||
Group::create([
|
||||
'name_singular' => $group[0],
|
||||
'name_plural' => $group[1],
|
||||
'color' => $group[2],
|
||||
'icon' => $group[3]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function seedPermissions()
|
||||
{
|
||||
$permissions = [
|
||||
// Guests can view the forum
|
||||
[2, 'forum.view'],
|
||||
|
||||
// Members can create and reply to discussions
|
||||
[3, 'forum.startDiscussion'],
|
||||
[3, 'discussion.reply'],
|
||||
|
||||
// Moderators can edit + delete stuff
|
||||
[4, 'discussion.delete'],
|
||||
[4, 'discussion.deletePosts'],
|
||||
[4, 'discussion.editPosts'],
|
||||
[4, 'discussion.rename'],
|
||||
];
|
||||
|
||||
foreach ($permissions as &$permission) {
|
||||
$permission = [
|
||||
'group_id' => $permission[0],
|
||||
'permission' => $permission[1]
|
||||
];
|
||||
}
|
||||
|
||||
Permission::insert($permissions);
|
||||
}
|
||||
|
||||
protected function createAdminUser()
|
||||
{
|
||||
$admin = $this->dataSource->getAdminUser();
|
||||
|
@ -130,7 +198,14 @@ class InstallCommand extends Command
|
|||
|
||||
$this->info('Creating admin user '.$admin['username']);
|
||||
|
||||
$db->table('users')->insert($admin);
|
||||
User::unguard();
|
||||
|
||||
$user = new User($admin);
|
||||
$user->is_activated = 1;
|
||||
$user->join_time = time();
|
||||
$user->save();
|
||||
|
||||
$user->groups()->sync([1]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -5,4 +5,6 @@ interface ProvidesData
|
|||
public function getDatabaseConfiguration();
|
||||
|
||||
public function getAdminUser();
|
||||
|
||||
public function getSettings();
|
||||
}
|
||||
|
|
157
src/Migrations/DatabaseMigrationRepository.php
Executable file
157
src/Migrations/DatabaseMigrationRepository.php
Executable file
|
@ -0,0 +1,157 @@
|
|||
<?php
|
||||
|
||||
namespace Flarum\Migrations;
|
||||
|
||||
use Illuminate\Database\ConnectionResolverInterface as Resolver;
|
||||
|
||||
class DatabaseMigrationRepository implements MigrationRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* The database connection resolver instance.
|
||||
*
|
||||
* @var \Illuminate\Database\ConnectionResolverInterface
|
||||
*/
|
||||
protected $resolver;
|
||||
|
||||
/**
|
||||
* The name of the migration table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table;
|
||||
|
||||
/**
|
||||
* The name of the database connection to use.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* Create a new database migration repository instance.
|
||||
*
|
||||
* @param \Illuminate\Database\ConnectionResolverInterface $resolver
|
||||
* @param string $table
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Resolver $resolver, $table)
|
||||
{
|
||||
$this->table = $table;
|
||||
$this->resolver = $resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ran migrations.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRan($extension = null)
|
||||
{
|
||||
return $this->table()
|
||||
->where('extension', $extension)
|
||||
->orderBy('migration', 'asc')
|
||||
->lists('migration');
|
||||
}
|
||||
|
||||
/**
|
||||
* Log that a migration was run.
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $extension
|
||||
* @return void
|
||||
*/
|
||||
public function log($file, $extension = null)
|
||||
{
|
||||
$record = ['migration' => $file, 'extension' => $extension];
|
||||
|
||||
$this->table()->insert($record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a migration from the log.
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $extension
|
||||
* @return void
|
||||
*/
|
||||
public function delete($file, $extension = null)
|
||||
{
|
||||
$query = $this->table()->where('migration', $file);
|
||||
|
||||
if (is_null($extension)) {
|
||||
$query->whereNull('extension');
|
||||
} else {
|
||||
$query->where('extension', $extension);
|
||||
}
|
||||
|
||||
$query->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the migration repository data store.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function createRepository()
|
||||
{
|
||||
$schema = $this->getConnection()->getSchemaBuilder();
|
||||
|
||||
$schema->create($this->table, function ($table) {
|
||||
$table->string('migration');
|
||||
$table->string('extension')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the migration repository exists.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function repositoryExists()
|
||||
{
|
||||
$schema = $this->getConnection()->getSchemaBuilder();
|
||||
|
||||
return $schema->hasTable($this->table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a query builder for the migration table.
|
||||
*
|
||||
* @return \Illuminate\Database\Query\Builder
|
||||
*/
|
||||
protected function table()
|
||||
{
|
||||
return $this->getConnection()->table($this->table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the connection resolver instance.
|
||||
*
|
||||
* @return \Illuminate\Database\ConnectionResolverInterface
|
||||
*/
|
||||
public function getConnectionResolver()
|
||||
{
|
||||
return $this->resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the database connection instance.
|
||||
*
|
||||
* @return \Illuminate\Database\Connection
|
||||
*/
|
||||
public function getConnection()
|
||||
{
|
||||
return $this->resolver->connection($this->connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the information source to gather data.
|
||||
*
|
||||
* @param string $name
|
||||
* @return void
|
||||
*/
|
||||
public function setSource($name)
|
||||
{
|
||||
$this->connection = $name;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
<?php namespace Flarum\Install;
|
||||
<?php namespace Flarum\Migrations;
|
||||
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
|
53
src/Migrations/MigrationRepositoryInterface.php
Executable file
53
src/Migrations/MigrationRepositoryInterface.php
Executable file
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace Flarum\Migrations;
|
||||
|
||||
interface MigrationRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* Get the ran migrations for the given extension.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRan($extension = null);
|
||||
|
||||
/**
|
||||
* Log that a migration was run.
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $extension
|
||||
* @return void
|
||||
*/
|
||||
public function log($file, $extension = null);
|
||||
|
||||
/**
|
||||
* Remove a migration from the log.
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $extension
|
||||
* @return void
|
||||
*/
|
||||
public function delete($file, $extension = null);
|
||||
|
||||
/**
|
||||
* Create the migration repository data store.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function createRepository();
|
||||
|
||||
/**
|
||||
* Determine if the migration repository exists.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function repositoryExists();
|
||||
|
||||
/**
|
||||
* Set the information source to gather data.
|
||||
*
|
||||
* @param string $name
|
||||
* @return void
|
||||
*/
|
||||
public function setSource($name);
|
||||
}
|
322
src/Migrations/Migrator.php
Executable file
322
src/Migrations/Migrator.php
Executable file
|
@ -0,0 +1,322 @@
|
|||
<?php
|
||||
|
||||
namespace Flarum\Migrations;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use Illuminate\Database\ConnectionResolverInterface as Resolver;
|
||||
|
||||
class Migrator
|
||||
{
|
||||
/**
|
||||
* The migration repository implementation.
|
||||
*
|
||||
* @var \Flarum\Migrations\MigrationRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* The filesystem instance.
|
||||
*
|
||||
* @var \Illuminate\Filesystem\Filesystem
|
||||
*/
|
||||
protected $files;
|
||||
|
||||
/**
|
||||
* The connection resolver instance.
|
||||
*
|
||||
* @var \Illuminate\Database\ConnectionResolverInterface
|
||||
*/
|
||||
protected $resolver;
|
||||
|
||||
/**
|
||||
* The name of the default connection.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* The notes for the current operation.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $notes = [];
|
||||
|
||||
/**
|
||||
* Create a new migrator instance.
|
||||
*
|
||||
* @param \Flarum\Migrations\MigrationRepositoryInterface $repository
|
||||
* @param \Illuminate\Database\ConnectionResolverInterface $resolver
|
||||
* @param \Illuminate\Filesystem\Filesystem $files
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(MigrationRepositoryInterface $repository,
|
||||
Resolver $resolver,
|
||||
Filesystem $files)
|
||||
{
|
||||
$this->files = $files;
|
||||
$this->resolver = $resolver;
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the outstanding migrations at a given path.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $extension
|
||||
* @return void
|
||||
*/
|
||||
public function run($path, $extension = null)
|
||||
{
|
||||
$this->notes = [];
|
||||
|
||||
$files = $this->getMigrationFiles($path);
|
||||
|
||||
$ran = $this->repository->getRan($extension);
|
||||
|
||||
$migrations = array_diff($files, $ran);
|
||||
|
||||
$this->requireFiles($path, $migrations);
|
||||
|
||||
$this->runMigrationList($migrations, $extension);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run an array of migrations.
|
||||
*
|
||||
* @param array $migrations
|
||||
* @param bool $pretend
|
||||
* @return void
|
||||
*/
|
||||
public function runMigrationList($migrations, $extension)
|
||||
{
|
||||
// First we will just make sure that there are any migrations to run. If there
|
||||
// aren't, we will just make a note of it to the developer so they're aware
|
||||
// that all of the migrations have been run against this database system.
|
||||
if (count($migrations) == 0) {
|
||||
$this->note('<info>Nothing to migrate.</info>');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Once we have the array of migrations, we will spin through them and run the
|
||||
// migrations "up" so the changes are made to the databases. We'll then log
|
||||
// that the migration was run so we don't repeat it next time we execute.
|
||||
foreach ($migrations as $file) {
|
||||
$this->runUp($file, $extension);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run "up" a migration instance.
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $extension
|
||||
* @return void
|
||||
*/
|
||||
protected function runUp($file, $extension)
|
||||
{
|
||||
// First we will resolve a "real" instance of the migration class from this
|
||||
// migration file name. Once we have the instances we can run the actual
|
||||
// command such as "up" or "down", or we can just simulate the action.
|
||||
$migration = $this->resolve($file);
|
||||
|
||||
$migration->up();
|
||||
|
||||
// Once we have run a migrations class, we will log that it was run in this
|
||||
// repository so that we don't try to run it next time we do a migration
|
||||
// in the application. A migration repository keeps the migrate order.
|
||||
$this->repository->log($file, $extension);
|
||||
|
||||
$this->note("<info>Migrated:</info> $file");
|
||||
}
|
||||
|
||||
/**
|
||||
* Rolls all of the currently applied migrations back.
|
||||
*
|
||||
* @param bool $pretend
|
||||
* @return int
|
||||
*/
|
||||
public function reset($path, $extension = null)
|
||||
{
|
||||
$this->notes = [];
|
||||
|
||||
$migrations = array_reverse($this->repository->getRan($extension));
|
||||
|
||||
$this->requireFiles($path, $migrations);
|
||||
|
||||
$count = count($migrations);
|
||||
|
||||
if ($count === 0) {
|
||||
$this->note('<info>Nothing to rollback.</info>');
|
||||
} else {
|
||||
foreach ($migrations as $migration) {
|
||||
$this->runDown($migration, $extension);
|
||||
}
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run "down" a migration instance.
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $extension
|
||||
* @return void
|
||||
*/
|
||||
protected function runDown($file, $extension = null)
|
||||
{
|
||||
// First we will get the file name of the migration so we can resolve out an
|
||||
// instance of the migration. Once we get an instance we can either run a
|
||||
// pretend execution of the migration or we can run the real migration.
|
||||
$instance = $this->resolve($file);
|
||||
|
||||
$instance->down();
|
||||
|
||||
// Once we have successfully run the migration "down" we will remove it from
|
||||
// the migration repository so it will be considered to have not been run
|
||||
// by the application then will be able to fire by any later operation.
|
||||
$this->repository->delete($file, $extension);
|
||||
|
||||
$this->note("<info>Rolled back:</info> $file");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the migration files in a given path.
|
||||
*
|
||||
* @param string $path
|
||||
* @return array
|
||||
*/
|
||||
public function getMigrationFiles($path)
|
||||
{
|
||||
$files = $this->files->glob($path.'/*_*.php');
|
||||
|
||||
// Once we have the array of files in the directory we will just remove the
|
||||
// extension and take the basename of the file which is all we need when
|
||||
// finding the migrations that haven't been run against the databases.
|
||||
if ($files === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$files = 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Require in all the migration files in a given path.
|
||||
*
|
||||
* @param string $path
|
||||
* @param array $files
|
||||
* @return void
|
||||
*/
|
||||
public function requireFiles($path, array $files)
|
||||
{
|
||||
foreach ($files as $file) {
|
||||
$this->files->requireOnce($path.'/'.$file.'.php');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a migration instance from a file.
|
||||
*
|
||||
* @param string $file
|
||||
* @return object
|
||||
*/
|
||||
public function resolve($file)
|
||||
{
|
||||
$file = implode('_', array_slice(explode('_', $file), 4));
|
||||
|
||||
$class = Str::studly($file);
|
||||
|
||||
return app()->make($class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Raise a note event for the migrator.
|
||||
*
|
||||
* @param string $message
|
||||
* @return void
|
||||
*/
|
||||
protected function note($message)
|
||||
{
|
||||
$this->notes[] = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notes for the last operation.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getNotes()
|
||||
{
|
||||
return $this->notes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the database connection instance.
|
||||
*
|
||||
* @param string $connection
|
||||
* @return \Illuminate\Database\Connection
|
||||
*/
|
||||
public function resolveConnection($connection)
|
||||
{
|
||||
return $this->resolver->connection($connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default connection name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return void
|
||||
*/
|
||||
public function setConnection($name)
|
||||
{
|
||||
if (!is_null($name)) {
|
||||
$this->resolver->setDefaultConnection($name);
|
||||
}
|
||||
|
||||
$this->repository->setSource($name);
|
||||
|
||||
$this->connection = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the migration repository instance.
|
||||
*
|
||||
* @return \Illuminate\Database\Migrations\MigrationRepositoryInterface
|
||||
*/
|
||||
public function getRepository()
|
||||
{
|
||||
return $this->repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the migration repository exists.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function repositoryExists()
|
||||
{
|
||||
return $this->repository->repositoryExists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file system instance.
|
||||
*
|
||||
* @return \Illuminate\Filesystem\Filesystem
|
||||
*/
|
||||
public function getFilesystem()
|
||||
{
|
||||
return $this->files;
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
use Flarum\Support\ServiceProvider;
|
||||
use Flarum\Core\Settings\SettingsRepository;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Flarum\Migrations\Migrator;
|
||||
|
||||
class ExtensionManager
|
||||
{
|
||||
|
@ -43,8 +44,7 @@ class ExtensionManager
|
|||
|
||||
$class->install();
|
||||
|
||||
// run migrations
|
||||
// vendor publish
|
||||
$this->migrate($extension);
|
||||
|
||||
$this->setEnabled($enabled);
|
||||
}
|
||||
|
@ -69,7 +69,24 @@ class ExtensionManager
|
|||
|
||||
$class->uninstall();
|
||||
|
||||
// run migrations
|
||||
$this->migrate($extension, false);
|
||||
}
|
||||
|
||||
protected function migrate($extension, $up = true)
|
||||
{
|
||||
$migrationDir = base_path('../extensions/' . $extension . '/migrations');
|
||||
|
||||
$this->app->bind('Illuminate\Database\Schema\Builder', function($container) {
|
||||
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
|
||||
});
|
||||
|
||||
$migrator = $this->app->make('Flarum\Migrations\Migrator');
|
||||
|
||||
if ($up) {
|
||||
$migrator->run($migrationDir, $extension);
|
||||
} else {
|
||||
$migrator->reset($migrationDir, $extension);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getEnabled()
|
||||
|
|
Loading…
Reference in New Issue
Block a user