Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
214 lines (173 sloc) 6.74 KB
<?php
require 'vendor/autoload.php';
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Illuminate\Database\Capsule\Manager as Capsule;
require 'core/fix_scores.php';
require 'core/import_uaseco.php';
require 'core/run.php';
class MakeMigration extends Command
{
protected function configure()
{
$this->setName('make:migration')
->setDescription('Create migration, placed in /Migrations')
->addArgument('migration_name', InputArgument::REQUIRED, 'The migration name');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('migration_name');
if (preg_match_all('/([A-Z][a-z]+)/', $name, $matches)) {
$filename = 'Migrations/' . time() . '_' . str_slug(implode(' ', $matches[0])) . '.php';
$template = str_replace('{className}', $name, '<?php
namespace esc\Migrations;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class {className} extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(Builder $schemaBuilder)
{
$schemaBuilder->create(\'table-name\', function (Blueprint $table) {
$table->increments(\'id\');
$table->string(\'column1\');
$table->integer(\'column2\')->nullable();
$table->boolean(\'column3\')->default(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(Builder $schemaBuilder)
{
$schemaBuilder->drop(\'table-name\');
}
}');
file_put_contents($filename, $template);
} else {
$output->writeln('Error: Invalid name entered, please use camel case (example: CreatePlayersTable)');
}
}
}
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)
{
$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 MakeMigration());
$application->add(new Migrate());
$application->add(new ImportUaseco());
$application->add(new FixScores());
$application->add($run);
$application->setDefaultCommand($run);
try {
$application->run();
} catch (\Exception $e) {
die($e);
}
You can’t perform that action at this time.