Skip to content
Permalink
Browse files

0.68.19 Move migrate command to separate file

- Remove Log from autoloader
  • Loading branch information...
brakerb committed Jun 7, 2019
1 parent f498aa0 commit 92e7392ac140c6c52069c21cbc11173ac995e2ea
Showing with 144 additions and 128 deletions.
  1. +4 −2 core/autoload.php
  2. +138 −0 core/migrate.php
  3. +2 −126 esc
@@ -35,7 +35,8 @@ function getNameSpaces(\Illuminate\Support\Collection $classFiles)
$classFile->namespace = $matches[1] . '\\' . $classFile->class;
} else {
//Abort execution when class wasn't loaded correctly
\esc\Classes\Log::logAddLine('autoload', "Class without namespace found: $classFile->file");
// \esc\Classes\Log::logAddLine('autoload', "Class without namespace found: $classFile->file");
var_dump("Class without namespace found: $classFile->file");
}
return $classFile;
@@ -75,7 +76,8 @@ function esc_class_loader($className)
die("Trying to load non-existant file: " . $class->file);
}
} else {
\esc\Classes\Log::logAddLine('class_loader', 'Class not found: ' . $className, isVeryVerbose());
// \esc\Classes\Log::logAddLine('class_loader', 'Class not found: ' . $className, isVeryVerbose());
var_dump('Class not found: ' . $className);
}
}
@@ -0,0 +1,138 @@
<?php
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Illuminate\Database\Capsule\Manager as Capsule;
class Migrate extends Command
{
protected function configure()
{
$this->setName('migrate')->setDescription('Run all database migrations. Run after pulling updates');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
global $_isVerbose;
global $_isVeryVerbose;
global $_isDebug;
$_isVerbose = $output->isVerbose();
$_isVeryVerbose = $output->isVeryVerbose();
$_isDebug = $output->isDebug();
$output->writeln('Executing migrations...');
$config = json_decode(file_get_contents('config/database.config.json'));
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'mysql',
'host' => $config->host,
'database' => $config->db,
'username' => $config->user,
'password' => $config->password,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => $config->prefix,
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
$connection = $capsule->getConnection();
$schemaBuilder = $connection->getSchemaBuilder();
$schemaBuilder::defaultStringLength(191);
if (!$schemaBuilder->hasTable('migrations')) {
$output->writeln('Creating migrations table');
$schemaBuilder->create('migrations', function (\Illuminate\Database\Schema\Blueprint $table) {
$table->increments('id');
$table->string('file')->unique();
$table->integer('batch');
});
}
$previousBatch = $connection->table('migrations')
->get(['batch'])
->sortByDesc('batch')
->first();
if ($previousBatch) {
$batch = $previousBatch->batch + 1;
} else {
$batch = 1;
}
$migrations = $this->getMigrations();
$migrationsTable = $connection->table('migrations');
$executedMigrations = $migrationsTable->get(['file']);
$migrations->each(function ($migration) use ($executedMigrations, $batch, $schemaBuilder, $migrationsTable, $output) {
if ($executedMigrations->where('file', $migration->file)->isNotEmpty()) {
//Skip already executed migrations
return;
}
$content = file_get_contents($migration->path);
if (preg_match('/class (.+) extends/', $content, $matches)) {
$class = 'esc\\Migrations\\' . $matches[1];
require_once $migration->path;
$instance = new $class;
$instance->up($schemaBuilder);
$migrationsTable->insert(['file' => $migration->file, 'batch' => $batch]);
$output->writeln('Migrated: ' . $migration->file);
}
});
}
private function getMigrations(): \Illuminate\Support\Collection
{
$migrations = collect();
$files = collect(scandir('Migrations'))->filter(function ($file) {
return preg_match('/\.php$/', $file);
})->filter(function ($file) {
$content = file_get_contents('Migrations/' . $file);
return preg_match('/extends Migration/', $content);
})->map(function ($migration) {
$col = collect();
$col->path = "Migrations/$migration";
$col->file = $migration;
return $col;
});
$migrations = $migrations->merge($files);
collect(scandir('core/Modules'))->filter(function ($moduleDir) {
return is_dir("core/Modules/$moduleDir") && !in_array($moduleDir, ['.', '..']);
})->filter(function ($moduleDir) {
return is_dir("core/Modules/$moduleDir/Migrations");
})->each(function ($moduleDir) use (&$migrations) {
$moduleMigrations = collect(scandir("core/Modules/$moduleDir/Migrations"))->filter(function ($file) {
return preg_match('/\.php$/', $file);
})->filter(function ($file) use ($moduleDir) {
$content = file_get_contents("core/Modules/$moduleDir/Migrations/" . $file);
return preg_match('/extends Migration/', $content);
})->map(function ($migration) use ($moduleDir) {
$col = collect();
$col->path = "core/Modules/$moduleDir/Migrations/$migration";
$col->file = $migration;
return $col;
});
$migrations = $migrations->merge($moduleMigrations);
});
return $migrations;
}
}
128 esc
@@ -10,6 +10,7 @@ use Symfony\Component\Console\Output\OutputInterface;
use Illuminate\Database\Capsule\Manager as Capsule;

require 'core/fix_scores.php';
require 'core/migrate.php';
require 'core/import_uaseco.php';
require 'core/run.php';

@@ -73,138 +74,13 @@ class {className} extends Migration
}
}

class Migrate extends Command
{
protected function configure()
{
$this->setName('migrate')->setDescription('Run all database migrations. Run after pulling updates');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
global $_isVerbose;
global $_isVeryVerbose;
global $_isDebug;

$_isVerbose = $output->isVerbose();
$_isVeryVerbose = $output->isVeryVerbose();
$_isDebug = $output->isDebug();

$output->writeln('Executing migrations...');

$config = json_decode(file_get_contents('config/database.config.json'));

$capsule = new Capsule();

$capsule->addConnection([
'driver' => 'mysql',
'host' => $config->host,
'database' => $config->db,
'username' => $config->user,
'password' => $config->password,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => $config->prefix,
]);

$capsule->setAsGlobal();
$capsule->bootEloquent();

$connection = $capsule->getConnection();

$schemaBuilder = $connection->getSchemaBuilder();
$schemaBuilder::defaultStringLength(191);

if (!$schemaBuilder->hasTable('migrations')) {
$output->writeln('Creating migrations table');
$schemaBuilder->create('migrations', function (\Illuminate\Database\Schema\Blueprint $table) {
$table->increments('id');
$table->string('file')->unique();
$table->integer('batch');
});
}

$previousBatch = $connection->table('migrations')
->get(['batch'])
->sortByDesc('batch')
->first();

if ($previousBatch) {
$batch = $previousBatch->batch + 1;
} else {
$batch = 1;
}

$migrations = $this->getMigrations();
$migrationsTable = $connection->table('migrations');
$executedMigrations = $migrationsTable->get(['file']);

$migrations->each(function ($migration) use ($executedMigrations, $batch, $schemaBuilder, $migrationsTable, $output) {
if ($executedMigrations->where('file', $migration->file)->isNotEmpty()) {
//Skip already executed migrations
return;
}

$content = file_get_contents($migration->path);

if (preg_match('/class (.+) extends/', $content, $matches)) {
$class = 'esc\\Migrations\\' . $matches[1];
require_once $migration->path;
$instance = new $class;
$instance->up($schemaBuilder);

$migrationsTable->insert(['file' => $migration->file, 'batch' => $batch]);
$output->writeln('Migrated: ' . $migration->file);
}
});
}

private function getMigrations(): \Illuminate\Support\Collection
{
$migrations = collect();

$files = collect(scandir('Migrations'))->filter(function ($file) {
return preg_match('/\.php$/', $file);
})->filter(function ($file) {
$content = file_get_contents('Migrations/' . $file);
return preg_match('/extends Migration/', $content);
})->map(function ($migration) {
$col = collect();
$col->path = "Migrations/$migration";
$col->file = $migration;
return $col;
});

$migrations = $migrations->merge($files);

collect(scandir('core/Modules'))->filter(function ($moduleDir) {
return is_dir("core/Modules/$moduleDir") && !in_array($moduleDir, ['.', '..']);
})->filter(function ($moduleDir) {
return is_dir("core/Modules/$moduleDir/Migrations");
})->each(function ($moduleDir) use (&$migrations) {
$moduleMigrations = collect(scandir("core/Modules/$moduleDir/Migrations"))->filter(function ($file) {
return preg_match('/\.php$/', $file);
})->filter(function ($file) use ($moduleDir) {
$content = file_get_contents("core/Modules/$moduleDir/Migrations/" . $file);
return preg_match('/extends Migration/', $content);
})->map(function ($migration) use ($moduleDir) {
$col = collect();
$col->path = "core/Modules/$moduleDir/Migrations/$migration";
$col->file = $migration;
return $col;
});

$migrations = $migrations->merge($moduleMigrations);
});

return $migrations;
}
}

$application = new Application();

$run = new EscRun();

$application->add(new Migrate());
$application->add(new MakeMigration());
$application->add(new Migrate());
$application->add(new ImportUaseco());

0 comments on commit 92e7392

Please sign in to comment.
You can’t perform that action at this time.