mirror of
https://github.com/flarum/core.git
synced 2025-10-13 16:05:05 +02:00
Mostly, we only need a database connection, instead of one of Laravel's "connection resolvers". Again, this makes our life easier during installation, where we already instantiate a database connection. We can now use that to instantiate our own Migrator class, instead of using the IoC container to build one.
130 lines
2.9 KiB
PHP
130 lines
2.9 KiB
PHP
<?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\Database;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
class DatabaseMigrationRepository implements MigrationRepositoryInterface
|
|
{
|
|
/**
|
|
* The name of the database connection to use.
|
|
*
|
|
* @var ConnectionInterface
|
|
*/
|
|
protected $connection;
|
|
|
|
/**
|
|
* The name of the migration table.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table;
|
|
|
|
/**
|
|
* Create a new database migration repository instance.
|
|
*
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
|
* @param string $table
|
|
*/
|
|
public function __construct(ConnectionInterface $connection, $table)
|
|
{
|
|
$this->connection = $connection;
|
|
$this->table = $table;
|
|
}
|
|
|
|
/**
|
|
* Get the ran migrations.
|
|
*
|
|
* @param string $extension
|
|
* @return array
|
|
*/
|
|
public function getRan($extension = null)
|
|
{
|
|
return $this->table()
|
|
->where('extension', $extension)
|
|
->orderBy('migration', 'asc')
|
|
->pluck('migration')
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* 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->connection->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->connection->getSchemaBuilder();
|
|
|
|
return $schema->hasTable($this->table);
|
|
}
|
|
|
|
/**
|
|
* Get a query builder for the migration table.
|
|
*
|
|
* @return \Illuminate\Database\Query\Builder
|
|
*/
|
|
protected function table()
|
|
{
|
|
return $this->connection->table($this->table);
|
|
}
|
|
}
|