Skip to content

Commit

Permalink
Merge pull request #109 from sebdesign/refactor-generate-method
Browse files Browse the repository at this point in the history
Refactor generate method
  • Loading branch information
Xethron authored Feb 15, 2017
2 parents 2a11872 + 01dccdd commit 8a9e187
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 33 deletions.
3 changes: 0 additions & 3 deletions src/Xethron/MigrationsGenerator/MethodNotFoundException.php

This file was deleted.

81 changes: 51 additions & 30 deletions src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ public function fire()

$this->info( "Setting up Tables and Index Migrations" );
$this->datePrefix = date( 'Y_m_d_His' );
$this->generate( 'create', $tables );
$this->generateTablesAndIndices( $tables );
$this->info( "\nSetting up Foreign Key Migrations\n" );
$this->datePrefix = date( 'Y_m_d_His', strtotime( '+1 second' ) );
$this->generate( 'foreign_keys', $tables );
$this->generateForeignKeys( $tables );
$this->info( "\nFinished!\n" );
}

Expand Down Expand Up @@ -218,37 +218,56 @@ protected function askNumeric( $question, $default = null ) {
}

/**
* Generate Migrations
* Generate tables and index migrations.
*
* @param string $method Create Tables or Foreign Keys ['create', 'foreign_keys']
* @param array $tables List of tables to create migrations for
* @throws MethodNotFoundException
* @param array $tables List of tables to create migrations for
* @return void
*/
protected function generate( $method, $tables )
protected function generateTablesAndIndices( array $tables )
{
if ( $method == 'create' ) {
$function = 'getFields';
$prefix = 'create';
} elseif ( $method = 'foreign_keys' ) {
$function = 'getForeignKeyConstraints';
$prefix = 'add_foreign_keys_to';
$method = 'table';
} else {
throw new MethodNotFoundException( $method );
$this->method = 'create';

foreach ( $tables as $table ) {
$this->table = $table;
$this->migrationName = 'create_'. $this->table .'_table';
$this->fields = $this->schemaGenerator->getFields( $this->table );

$this->generate();
}
}

/**
* Generate foreign key migrations.
*
* @param array $tables List of tables to create migrations for
* @return void
*/
protected function generateForeignKeys( array $tables )
{
$this->method = 'table';

foreach ( $tables as $table ) {
$this->migrationName = $prefix .'_'. $table .'_table';
$this->method = $method;
$this->table = $table;
$this->fields = $this->schemaGenerator->{$function}( $table );
if ( $this->fields ) {
parent::fire();
if ( $this->log ) {
$file = $this->datePrefix . '_' . $this->migrationName;
$this->repository->log($file, $this->batch);
}
$this->migrationName = 'add_foreign_keys_to_'. $this->table .'_table';
$this->fields = $this->schemaGenerator->getForeignKeyConstraints( $this->table );

$this->generate();
}
}

/**
* Generate Migration for the current table.
*
* @return void
*/
protected function generate()
{
if ( $this->fields ) {
parent::fire();

if ( $this->log ) {
$file = $this->datePrefix . '_' . $this->migrationName;
$this->repository->log($file, $this->batch);
}
}
}
Expand Down Expand Up @@ -287,9 +306,11 @@ protected function getTemplateData()
if ( $this->method == 'create' ) {
$up = (new AddToTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection, 'create');
$down = (new DroppedTable)->drop($this->table, $this->connection);
} else {
$up = (new AddForeignKeysToTable($this->file, $this->compiler))->run($this->fields,$this->table, $this->connection);
$down = (new RemoveForeignKeysFromTable($this->file, $this->compiler))->run($this->fields,$this->table, $this->connection);
}

if ( $this->method == 'table' ) {
$up = (new AddForeignKeysToTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection);
$down = (new RemoveForeignKeysFromTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection);
}

return [
Expand Down Expand Up @@ -342,11 +363,11 @@ protected function getOptions()
/**
* Remove all the tables to exclude from the array of tables
*
* @param $tables
* @param array $tables
*
* @return array
*/
protected function removeExcludedTables($tables)
protected function removeExcludedTables( array $tables )
{
$excludes = $this->getExcludedTables();
$tables = array_diff($tables, $excludes);
Expand Down

0 comments on commit 8a9e187

Please sign in to comment.