Merge pull request #1445 from flarum/fl/migrations-simplify-interface

Migrations: Simplify interface
This commit is contained in:
Franz Liedke 2018-06-04 01:05:31 +02:00 committed by GitHub
commit dcd1a95d5d
5 changed files with 39 additions and 107 deletions

View File

@ -11,16 +11,16 @@
namespace Flarum\Database; namespace Flarum\Database;
use Illuminate\Database\ConnectionResolverInterface as Resolver; use Illuminate\Database\ConnectionInterface;
class DatabaseMigrationRepository implements MigrationRepositoryInterface class DatabaseMigrationRepository implements MigrationRepositoryInterface
{ {
/** /**
* The database connection resolver instance. * The name of the database connection to use.
* *
* @var \Illuminate\Database\ConnectionResolverInterface * @var ConnectionInterface
*/ */
protected $resolver; protected $connection;
/** /**
* The name of the migration table. * The name of the migration table.
@ -29,23 +29,16 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface
*/ */
protected $table; protected $table;
/**
* The name of the database connection to use.
*
* @var string
*/
protected $connection;
/** /**
* Create a new database migration repository instance. * Create a new database migration repository instance.
* *
* @param \Illuminate\Database\ConnectionResolverInterface $resolver * @param \Illuminate\Database\ConnectionInterface $connection
* @param string $table * @param string $table
*/ */
public function __construct(Resolver $resolver, $table) public function __construct(ConnectionInterface $connection, $table)
{ {
$this->connection = $connection;
$this->table = $table; $this->table = $table;
$this->resolver = $resolver;
} }
/** /**
@ -104,7 +97,7 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface
*/ */
public function createRepository() public function createRepository()
{ {
$schema = $this->getConnection()->getSchemaBuilder(); $schema = $this->connection->getSchemaBuilder();
$schema->create($this->table, function ($table) { $schema->create($this->table, function ($table) {
$table->string('migration'); $table->string('migration');
@ -119,7 +112,7 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface
*/ */
public function repositoryExists() public function repositoryExists()
{ {
$schema = $this->getConnection()->getSchemaBuilder(); $schema = $this->connection->getSchemaBuilder();
return $schema->hasTable($this->table); return $schema->hasTable($this->table);
} }
@ -131,37 +124,6 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface
*/ */
protected function table() protected function table()
{ {
return $this->getConnection()->table($this->table); return $this->connection->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;
} }
} }

View File

@ -11,8 +11,7 @@
namespace Flarum\Database; namespace Flarum\Database;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\DatabaseSettingsRepository;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder; use Illuminate\Database\Schema\Builder;
@ -100,12 +99,20 @@ abstract class Migration
public static function addSettings(array $defaults) public static function addSettings(array $defaults)
{ {
return [ return [
'up' => function (SettingsRepositoryInterface $settings) use ($defaults) { 'up' => function (Builder $schema) use ($defaults) {
$settings = new DatabaseSettingsRepository(
$schema->getConnection()
);
foreach ($defaults as $key => $value) { foreach ($defaults as $key => $value) {
$settings->set($key, $value); $settings->set($key, $value);
} }
}, },
'down' => function (SettingsRepositoryInterface $settings) use ($defaults) { 'down' => function (Builder $schema) use ($defaults) {
$settings = new DatabaseSettingsRepository(
$schema->getConnection()
);
foreach (array_keys($defaults) as $key) { foreach (array_keys($defaults) as $key) {
$settings->delete($key); $settings->delete($key);
} }
@ -130,7 +137,9 @@ abstract class Migration
} }
return [ return [
'up' => function (ConnectionInterface $db) use ($keys) { 'up' => function (Builder $schema) use ($keys) {
$db = $schema->getConnection();
foreach ($keys as $key) { foreach ($keys as $key) {
$instance = $db->table('permissions')->where($key)->first(); $instance = $db->table('permissions')->where($key)->first();
@ -140,7 +149,9 @@ abstract class Migration
} }
}, },
'down' => function (ConnectionInterface $db) use ($keys) { 'down' => function (Builder $schema) use ($keys) {
$db = $schema->getConnection();
foreach ($keys as $key) { foreach ($keys as $key) {
$db->table('permissions')->where($key)->delete(); $db->table('permissions')->where($key)->delete();
} }

View File

@ -52,12 +52,4 @@ interface MigrationRepositoryInterface
* @return bool * @return bool
*/ */
public function repositoryExists(); public function repositoryExists();
/**
* Set the information source to gather data.
*
* @param string $name
* @return void
*/
public function setSource($name);
} }

View File

@ -22,7 +22,7 @@ class MigrationServiceProvider extends AbstractServiceProvider
public function register() public function register()
{ {
$this->app->singleton('Flarum\Database\MigrationRepositoryInterface', function ($app) { $this->app->singleton('Flarum\Database\MigrationRepositoryInterface', function ($app) {
return new DatabaseMigrationRepository($app['db'], 'migrations'); return new DatabaseMigrationRepository($app['flarum.db'], 'migrations');
}); });
$this->app->bind(MigrationCreator::class, function (Application $app) { $this->app->bind(MigrationCreator::class, function (Application $app) {

View File

@ -13,7 +13,8 @@ namespace Flarum\Database;
use Exception; use Exception;
use Flarum\Extension\Extension; use Flarum\Extension\Extension;
use Illuminate\Database\ConnectionResolverInterface as Resolver; use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Schema\Builder;
use Illuminate\Filesystem\Filesystem; use Illuminate\Filesystem\Filesystem;
class Migrator class Migrator
@ -33,18 +34,11 @@ class Migrator
protected $files; protected $files;
/** /**
* The connection resolver instance. * The database schema builder instance.
* *
* @var \Illuminate\Database\ConnectionResolverInterface * @var Builder
*/ */
protected $resolver; protected $schemaBuilder;
/**
* The name of the default connection.
*
* @var string
*/
protected $connection;
/** /**
* The notes for the current operation. * The notes for the current operation.
@ -57,17 +51,18 @@ class Migrator
* Create a new migrator instance. * Create a new migrator instance.
* *
* @param MigrationRepositoryInterface $repository * @param MigrationRepositoryInterface $repository
* @param Resolver $resolver * @param ConnectionInterface $connection
* @param Filesystem $files * @param Filesystem $files
*/ */
public function __construct( public function __construct(
MigrationRepositoryInterface $repository, MigrationRepositoryInterface $repository,
Resolver $resolver, ConnectionInterface $connection,
Filesystem $files Filesystem $files
) { ) {
$this->files = $files; $this->files = $files;
$this->resolver = $resolver;
$this->repository = $repository; $this->repository = $repository;
$this->schemaBuilder = $connection->getSchemaBuilder();
} }
/** /**
@ -199,7 +194,7 @@ class Migrator
protected function runClosureMigration($migration, $direction = 'up') protected function runClosureMigration($migration, $direction = 'up')
{ {
if (is_array($migration) && array_key_exists($direction, $migration)) { if (is_array($migration) && array_key_exists($direction, $migration)) {
app()->call($migration[$direction]); call_user_func($migration[$direction], $this->schemaBuilder);
} else { } else {
throw new Exception('Migration file should contain an array with up/down.'); throw new Exception('Migration file should contain an array with up/down.');
} }
@ -268,34 +263,6 @@ class Migrator
return $this->notes; 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. * Get the migration repository instance.
* *