Skip to content

Commit

Permalink
0.68.19 Move migrate command to separate file
Browse files Browse the repository at this point in the history
- Remove Log from autoloader
  • Loading branch information
araszka committed Jun 7, 2019
1 parent f498aa0 commit 92e7392
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 128 deletions.
6 changes: 4 additions & 2 deletions core/autoload.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
138 changes: 138 additions & 0 deletions core/migrate.php
@@ -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 changes: 2 additions & 126 deletions esc
Expand Up @@ -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';

Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 92e7392

Please sign in to comment.