<commit_before><?php namespace EnsphereCore\Commands\Ensphere\Migrate;

use EnsphereCore\Commands\Ensphere\Traits\Module as ModuleTrait;
use Illuminate\Console\Command as IlluminateCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use FilesystemIterator;
use DirectoryIterator;
use Artisan;
use Session;
use Illuminate\Database\QueryException;

class Command extends IlluminateCommand {

	use ModuleTrait;

	/**
	 * [$name description]
	 * @var string
	 */
	protected $name = 'ensphere:migrate';

	/**
	 * [$description description]
	 * @var string
	 */
	protected $description = 'Clears up the module to align with installation.';

	/**
	 * [$currentStructure description]
	 * @var [type]
	 */
	private $currentStructure;

	/**
	 * [fire description]
	 * @return [type] [description]
	 */
	public function fire()
	{
		$this->currentStructure = $this->getCurrentVendorAndModuleName();
		switch( $this->argument('migration_command') ) {
			case 'run' :
				$this->runMigration();
			break;
			case 'create' :
				$this->createMigration();
			break;
		}
	}

	/**
	 * [checkFoldersExists description]
	 * @param  [type] $vendorFolder [description]
	 * @param  [type] $moduleFolder [description]
	 * @return [type]               [description]
	 */
	protected function checkFoldersExists( $vendorFolder, $moduleFolder )
	{
        $path = base_path( 'database/migrations/vendor/' );
        if( ! file_exists( $path ) ) mkdir( $path, 0777 );
		$path = base_path( 'database/migrations/vendor/' . $vendorFolder . '/' );
		if( ! file_exists( $path ) ) mkdir( $path, 0777 );
		$path = base_path( 'database/migrations/vendor/' . $vendorFolder . '/' . $moduleFolder . '/' );
		if( ! file_exists( $path ) ) mkdir( $path, 0777 );
	}

	/**
	 * [createMigration description]
	 * @return [type] [description]
	 */
	private function createMigration() {
		$migrationName = $this->option('name');
		$this->checkFoldersExists( $this->currentStructure['vendor'], $this->currentStructure['module'] );
		$databaseFolder = base_path( 'database/migrations/vendor/' . $this->currentStructure['vendor'] . '/' . $this->currentStructure['module'] . '/' );
		$result = Artisan::call( 'make:migration', array(
			'name' => $migrationName,
			'--path' => "database/migrations/vendor/" . $this->currentStructure['vendor'] . "/" . $this->currentStructure['module']
		));
		$this->info("migration file created in: database/migrations/vendor/" . $this->currentStructure['vendor'] . "/" . $this->currentStructure['module'] . "/");
	}

	/**
	 * [runMigration description]
	 * @return [type] [description]
	 */
	public function runMigration() {

		$this->line( 'running vendor migration' );
		$folder = base_path( 'database/migrations/vendor/' );
		foreach( new DirectoryIterator( $folder ) as $vendorInfo ) {
			if( $vendorInfo->isDir() && ! $vendorInfo->isDot() ) {
				$vendor = $vendorInfo->getBasename();
				foreach( new DirectoryIterator( $folder . $vendor ) as $moduleInfo ) {
					try {
						if( $moduleInfo->isDir() && ! $moduleInfo->isDot() ) {
							$module = $moduleInfo->getBasename();
							$this->info("migratiing database/migrations/vendor/{$vendor}/{$module}/...");
							$result = Artisan::call( 'migrate', array(
								'--path' => "database/migrations/vendor/{$vendor}/{$module}/",
								'--force' => true
							));
						}
					} catch ( QueryException $e) {
                        $this->error( 'ERROR: ' . $e->getMessage() );
					}
				}
			}
		}
		$this->seed();
        $this->line( 'running application migration' );
        Artisan::call( 'migrate', [ '--force' => true ] );
	}

	/**
	 * [seed description]
	 * @return [type] [description]
	 */
	protected function seed()
	{
		$seeds = (array) Session::get( 'seed' );
		foreach( $seeds as $seed ) {
			$this->info( "seeding {$seed}...");
			(new $seed)->run();
		}
	}

	/**
	 * [getArguments description]
	 * @return [type] [description]
	 */
	protected function getArguments()
	{
		return [
			['migration_command', InputArgument::REQUIRED, 'An example argument.'],
		];
	}

	/**
	 * [getOptions description]
	 * @return [type] [description]
	 */
	protected function getOptions()
	{
		return [
			['name', null, InputOption::VALUE_OPTIONAL, 'An example option.', null],
		];
	}

}
<commit_msg>big issues when running migrations... hopefully this will fix<commit_after><?php

namespace EnsphereCore\Commands\Ensphere\Migrate;

use EnsphereCore\Commands\Ensphere\Traits\Module as ModuleTrait;
use Illuminate\Console\Command as IlluminateCommand;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Session;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Illuminate\Database\QueryException;

class Command extends IlluminateCommand
{

    use ModuleTrait;

    /**
     * @var string
     */
    protected $name = 'ensphere:migrate';

    /**
     * @var string
     */
    protected $description = 'Clears up the module to align with installation.';

    /**
     * @var
     */
    private $currentStructure;

    /**
     * @var array
     */
    private $moduleMigrationFiles = [];

    /**
     * @return void
     */
    public function fire()
    {
        $this->currentStructure = $this->getCurrentVendorAndModuleName();
        switch( $this->argument('migration_command') ) {
            case 'run' :
                $this->runMigration();
                break;
            case 'create' :
                $this->createMigration();
                break;
        }
    }

    /**
     * @param $vendorFolder
     * @param $moduleFolder
     */
    protected function checkFoldersExists( $vendorFolder, $moduleFolder )
    {
        $path = base_path( 'database/migrations/vendor/' );
        if( ! file_exists( $path ) ) mkdir( $path, 0777 );
        $path = base_path( 'database/migrations/vendor/' . $vendorFolder . '/' );
        if( ! file_exists( $path ) ) mkdir( $path, 0777 );
        $path = base_path( 'database/migrations/vendor/' . $vendorFolder . '/' . $moduleFolder . '/' );
        if( ! file_exists( $path ) ) mkdir( $path, 0777 );
    }

    /**
     * @return void
     */
    private function createMigration()
    {
        $migrationName = $this->option('name');
        $this->checkFoldersExists( $this->currentStructure['vendor'], $this->currentStructure['module'] );
        Artisan::call( 'make:migration', array(
            'name' => $migrationName,
            '--path' => "database/migrations/vendor/" . $this->currentStructure['vendor'] . "/" . $this->currentStructure['module']
        ));
        $this->info( "migration file created in: database/migrations/vendor/" . $this->currentStructure['vendor'] . "/" . $this->currentStructure['module'] . "/" );
    }

    /**
     * @param $path
     * @param bool $cacheResponse
     * @return array
     */
    protected function getAllModuleMigrationFiles( $path, $cacheResponse = false )
    {
        if( ! empty( $this->moduleMigrationFiles ) ) {
            return $this->moduleMigrationFiles;
        }
        $rii = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path ) );
        $files = [];
        foreach( $rii as $file ) {
            if( ! $file->isDir() && $file->getExtension() === 'php' ){
                if( ! isset( $files[$file->getPath()] ) ) {
                    $files[$file->getPath()] = [];
                }
                $files[$file->getPath()][] = $file->getBasename();
            }
        }
        if( $cacheResponse ) {
            $this->moduleMigrationFiles = $files;
        }
        return $files;
    }

    /**
     * @param $migrations
     */
    protected function addToMigrationsFolder( $migrations )
    {
        foreach( $migrations as $path => $files ) {
            foreach( $files as $fileName ) {
                copy( "{$path}/{$fileName}", base_path( "database/migrations/{$fileName}" ) );
            }
        }
    }

    /**
     * @param $migrations
     */
    protected function returnMigrations( $migrations )
    {
        foreach( $migrations as $path => $files ) {
            foreach( $files as $fileName ) {
                copy( base_path( "database/migrations/{$fileName}" ), "{$path}/{$fileName}" );
                unlink( base_path( "database/migrations/{$fileName}" ) );
            }
        }
    }


    /**
     * @return void
     */
    public function runMigration()
    {
        try {
            $migrations = $this->getAllModuleMigrationFiles( base_path( 'database/migrations/vendor' ), true );
            $this->addToMigrationsFolder( $migrations );
            $this->line( 'running application migration' );
            Artisan::call( 'migrate', [ '--force' => true ] );
            $this->returnMigrations( $migrations );
            $this->seed();
        } catch( QueryException $e ) {
            $this->error( $e->getMessage() );
        }
    }

    /**
     * @return void
     */
    protected function seed()
    {
        $seeds = (array) Session::get( 'seed' );
        foreach( $seeds as $seed ) {
            $this->info( "seeding {$seed}...");
            (new $seed)->run();
        }
    }

    /**
     * @return array
     */
    protected function getArguments()
    {
        return [
            ['migration_command', InputArgument::REQUIRED, 'An example argument.'],
        ];
    }

    /**
     * @return array
     */
    protected function getOptions()
    {
        return [
            ['name', null, InputOption::VALUE_OPTIONAL, 'An example option.', null],
        ];
    }

}

{'fraction_non_alphanumeric': 0.16265184270125593, 'fraction_numerical': 0.0024706609017912293, 'mean_word_length': 2.5690668626010287, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4660780', 'n_tokens_mistral': 3073, 'n_tokens_neox': 2939, 'n_words': 583}