Skip to content

Commit

Permalink
Updated migration commands (#255)
Browse files Browse the repository at this point in the history
* Rename getStubPath to stubPath

* Require illuminate/console

* Consistency with Laravel stubs

* Get migrations

* Consistency with Laravel commands

* Use random_bytes instead of uniqid

* Update dates comparision order in test
  • Loading branch information
holix authored and Mulkave committed Dec 21, 2017
1 parent d85e4f5 commit 5f7b8ee
Show file tree
Hide file tree
Showing 14 changed files with 318 additions and 258 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"illuminate/database": "5.5.*",
"illuminate/pagination": "5.5.*",
"illuminate/events": "5.5.*",
"illuminate/console": "5.5.*",
"heydavid713/neo4jphp": "0.1.*",
"nesbot/carbon": "~1.0"
},
Expand Down
61 changes: 25 additions & 36 deletions src/Console/Migrations/BaseCommand.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php namespace Vinelab\NeoEloquent\Console\Migrations;
<?php

use Illuminate\Console\Command;

class BaseCommand extends Command {
namespace Vinelab\NeoEloquent\Console\Migrations;

use Illuminate\Console\Command;

class BaseCommand extends Command
{
/**
* Directory for Neo4j labels migrations.
*
Expand All @@ -14,43 +17,29 @@ class BaseCommand extends Command {
/**
* Get the path to the migration directory.
*
* @return string
* @return array
*/
protected function getMigrationPath()
protected function getMigrationPaths()
{
$path = $this->input->getOption('path');

// First, we will check to see if a path option has been defined. If it has
// we will use the path relative to the root of this installation folder
// so that migrations may be run for any path within the applications.
if ( ! is_null($path))
{
return $this->laravel['path.base'].'/'.$path;
// Here, we will check to see if a path option has been defined. If it has we will
// use the path relative to the root of the installation folder so our database
// migrations may be run for any customized path from within the application.
if ($this->input->hasOption('path') && $this->option('path')) {
return collect($this->option('path'))->map(function ($path) {
return $this->laravel->basePath().'/'.$path;
})->all();
}

$package = $this->input->getOption('package');

// If the package is in the list of migration paths we received we will put
// the migrations in that path. Otherwise, we will assume the package is
// is in the package directories and will place them in that location.
if ( ! is_null($package))
{
return $this->packagePath.'/'.$package.'/src/' . self::LABELS_DIRECTORY;
}

$bench = $this->input->getOption('bench');

// Finally we will check for the workbench option, which is a shortcut into
// specifying the full path for a "workbench" project. Workbenches allow
// developers to develop packages along side a "standard" app install.
if ( ! is_null($bench))
{
$path = "/workbench/{$bench}/src/" . self::LABELS_DIRECTORY;

return $this->laravel['path.base'].$path;
}

return $this->laravel['path.database'].'/'.self::LABELS_DIRECTORY;
return [$this->getMigrationPath()];
}

/**
* Get the path to the migration directory.
*
* @return string
*/
protected function getMigrationPath()
{
return $this->laravel->databasePath().DIRECTORY_SEPARATOR.self::LABELS_DIRECTORY;
}
}
97 changes: 35 additions & 62 deletions src/Console/Migrations/MigrateCommand.php
Original file line number Diff line number Diff line change
@@ -1,110 +1,83 @@
<?php namespace Vinelab\NeoEloquent\Console\Migrations;
<?php


namespace Vinelab\NeoEloquent\Console\Migrations;

use Illuminate\Console\ConfirmableTrait;
use Illuminate\Database\Migrations\Migrator;
use Symfony\Component\Console\Input\InputOption;

class MigrateCommand extends BaseCommand {

class MigrateCommand extends BaseCommand
{
use ConfirmableTrait;

/**
* {@inheritDoc}
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'neo4j:migrate';
protected $signature = 'neo4j:migrate {--database= : The database connection to use.}
{--force : Force the operation to run when in production.}
{--path= : The path of migrations files to be executed.}
{--pretend : Dump the SQL queries that would be run.}
{--seed : Indicates if the seed task should be re-run.}
{--step : Force the migrations to be run so they can be rolled back individually.}';

/**
* {@inheritDoc}
* The console command description.
*
* @var string
*/
protected $description = 'Run the database migrations';

/**
* The migrator instance.
*
* @var \Vinelab\NeoEloquent\Migrations\Migrator
* @var \Illuminate\Database\Migrations\Migrator
*/
protected $migrator;

/**
* The path to the packages directory (vendor).
*/
protected $packagePath;

/**
* @param \Vinelab\NeoEloquent\Migrations\Migrator $migrator
* @param string $packagePath
* @param \Illuminate\Database\Migrations\Migrator $migrator
* @return void
*/
public function __construct(Migrator $migrator, $packagePath)
public function __construct(Migrator $migrator)
{
parent::__construct();

$this->migrator = $migrator;
$this->packagePath = $packagePath;
}

/**
* {@inheritDoc}
* Execute the console command.
*
* @return void
*/
public function fire()
public function handle()
{
if ( ! $this->confirmToProceed())
{
if (! $this->confirmToProceed()) {
return;
}

// The pretend option can be used for "simulating" the migration and grabbing
// the SQL queries that would fire if the migration were to be run against
// a database for real, which is helpful for double checking migrations.
$pretend = $this->input->getOption('pretend');
$this->migrator->setConnection($this->option('database'));

$path = $this->getMigrationPath();

$this->migrator->setConnection($this->input->getOption('database'));
$this->migrator->run($path, [
'pretend' => $pretend,
'step' => $this->input->getOption('step'),
// Next, we will check to see if a path option has been defined. If it has
// we will use the path relative to the root of this installation folder
// so that migrations may be run for any path within the applications.
$this->migrator->run($this->getMigrationPaths(), [
'pretend' => $this->option('pretend'),
'step' => $this->option('step'),
]);

// Once the migrator has run we will grab the note output and send it out to
// the console screen, since the migrator itself functions without having
// any instances of the OutputInterface contract passed into the class.
foreach ($this->migrator->getNotes() as $note)
{
foreach ($this->migrator->getNotes() as $note) {
$this->output->writeln($note);
}

// Finally, if the "seed" option has been given, we will re-run the database
// seed task to re-populate the database, which is convenient when adding
// a migration and a seed at the same time, as it is only this command.
if ($this->input->getOption('seed'))
{
if ($this->option('seed')) {
$this->call('db:seed', ['--force' => true]);
}
}

/**
* {@inheritDoc}
*/
protected function getOptions()
{
return array(
array('bench', null, InputOption::VALUE_OPTIONAL, 'The name of the workbench to migrate.', null),

array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'),

array('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'),

array('path', null, InputOption::VALUE_OPTIONAL, 'The path to migration files.', null),

array('package', null, InputOption::VALUE_OPTIONAL, 'The package to migrate.', null),

array('pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'),

array('seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'),

array('step', null, InputOption::VALUE_NONE, 'Force the migrations to be run so they can be rolled back individually.'),
);
}

}
Loading

0 comments on commit 5f7b8ee

Please sign in to comment.