Skip to content

Commit

Permalink
ref #2890, no longer using process and dump
Browse files Browse the repository at this point in the history
  • Loading branch information
luceos committed May 31, 2021
1 parent 2c5e5f1 commit 8af5215
Showing 1 changed file with 22 additions and 39 deletions.
61 changes: 22 additions & 39 deletions src/Database/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
use Flarum\Extension\Extension;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\MySqlConnection;
use Illuminate\Database\Schema\Builder;
use Illuminate\Database\Schema\SchemaState;
use Illuminate\Filesystem\Filesystem;
use InvalidArgumentException;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -38,31 +36,16 @@ class Migrator
*/
protected $files;

/**
* The database schema builder instance.
*
* @var Builder
*/
protected $schemaBuilder;

/**
* The DB table prefix.
*/
protected $tablePrefix;

/**
* The database schema builder instance.
*
* @var SchemaState
*/
protected $schemaState;

/**
* The output interface implementation.
*
* @var OutputInterface
*/
protected $output;
/**
* @var ConnectionInterface|MySqlConnection
*/
protected $connection;

/**
* Create a new migrator instance.
Expand All @@ -83,9 +66,7 @@ public function __construct(
throw new InvalidArgumentException('Only MySQL connections are supported');
}

$this->tablePrefix = $connection->getTablePrefix();
$this->schemaBuilder = $connection->getSchemaBuilder();
$this->schemaState = $connection->getSchemaState();
$this->connection = $connection;

// Workaround for https://github.com/laravel/framework/issues/1186
$connection->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
Expand Down Expand Up @@ -218,7 +199,7 @@ protected function runDown($path, $file, Extension $extension = null)
protected function runClosureMigration($migration, $direction = 'up')
{
if (is_array($migration) && array_key_exists($direction, $migration)) {
call_user_func($migration[$direction], $this->schemaBuilder);
call_user_func($migration[$direction], $this->connection->getSchemaBuilder());
} else {
throw new Exception('Migration file should contain an array with up/down.');
}
Expand Down Expand Up @@ -275,27 +256,29 @@ public function installFromSchema(string $path)
{
$schemaPath = "$path/install.dump";

// If we can't create a tmp file, fall back to the vendor directory.
$schemaWithPrefixes = tempnam(sys_get_temp_dir(), 'install');
if (! $schemaWithPrefixes) {
$schemaWithPrefixes = "$path/install_dump.dump.tmp";
}
$startTime = microtime(true);

$currDumpFile = file_get_contents($schemaPath);
$dump = file_get_contents($schemaPath);

file_put_contents($schemaWithPrefixes, str_replace('db_prefix_', $this->tablePrefix, $currDumpFile));
$this->connection->getSchemaBuilder()->disableForeignKeyConstraints();

$this->note('<info>Loading stored database schema:</info>');
$startTime = microtime(true);
foreach (explode(';', $dump) as $statement) {
$statement = trim($statement);

if (empty($statement) || substr($statement, 0, 2) === '/*') continue;

$this->schemaState->handleOutputUsing(function ($type, $buffer) {
$this->output->write($buffer);
})->load($schemaWithPrefixes);
$statement = str_replace(
'db_prefix_',
$this->connection->getTablePrefix(),
$statement
);
$this->connection->statement($statement);
}

$this->connection->getSchemaBuilder()->enableForeignKeyConstraints();

$runTime = number_format((microtime(true) - $startTime) * 1000, 2);
$this->note('<info>Loaded stored database schema.</info> ('.$runTime.'ms)');

unlink($schemaWithPrefixes);
}

/**
Expand Down

0 comments on commit 8af5215

Please sign in to comment.