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;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
use Illuminate\Database\ConnectionInterface;
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.
@ -29,23 +29,16 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface
*/
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
* @param \Illuminate\Database\ConnectionInterface $connection
* @param string $table
*/
public function __construct(Resolver $resolver, $table)
public function __construct(ConnectionInterface $connection, $table)
{
$this->connection = $connection;
$this->table = $table;
$this->resolver = $resolver;
}
/**
@ -104,7 +97,7 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface
*/
public function createRepository()
{
$schema = $this->getConnection()->getSchemaBuilder();
$schema = $this->connection->getSchemaBuilder();
$schema->create($this->table, function ($table) {
$table->string('migration');
@ -119,7 +112,7 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface
*/
public function repositoryExists()
{
$schema = $this->getConnection()->getSchemaBuilder();
$schema = $this->connection->getSchemaBuilder();
return $schema->hasTable($this->table);
}
@ -131,37 +124,6 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface
*/
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;
return $this->connection->table($this->table);
}
}

View File

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

View File

@ -52,12 +52,4 @@ interface MigrationRepositoryInterface
* @return bool
*/
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()
{
$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) {

View File

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