Skip to content

Fixed exception in generating enum column type #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
report
composer.lock
.idea
2 changes: 2 additions & 0 deletions src/Xethron/MigrationsGenerator/Generators/FieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ protected function getEnum($table)
protected function setEnum(array $fields, $table)
{
foreach ($this->getEnum($table) as $column) {
$column->column_name = property_exists($column, 'column_name') ? $column->column_name : $column->COLUMN_NAME;
$column->column_type = property_exists($column, 'column_type') ? $column->column_type : $column->COLUMN_TYPE;
$fields[$column->column_name]['type'] = 'enum';
$fields[$column->column_name]['args'] = str_replace('enum(', 'array(', $column->column_type);
}
Expand Down
67 changes: 66 additions & 1 deletion src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ class MigrateGenerateCommand extends GeneratorCommand {
*/
protected $connection = null;

/**
* @var bool
*/
protected $ignoreCreatedMigration = false;

/**
* @param \Way\Generators\Generator $generator
* @param \Way\Generators\Filesystem\Filesystem $file
Expand Down Expand Up @@ -180,6 +185,10 @@ public function fire()
$this->batch = $this->askNumeric( 'Next Batch Number is: '. $batch .'. We recommend using Batch Number 0 so that it becomes the "first" migration', 0 );
}

if($this->hasOption('ignore-created-migration')) {
$this->ignoreCreatedMigration = true;
}

$this->info( "Setting up Tables and Index Migrations" );
$this->datePrefix = date( 'Y_m_d_His' );
$this->generateTablesAndIndices( $tables );
Expand Down Expand Up @@ -240,12 +249,58 @@ protected function generateTablesAndIndices( array $tables )
foreach ( $tables as $table ) {
$this->table = $table;
$this->migrationName = 'create_'. $this->table .'_table';

if($this->ignoreCreatedMigration && $this->migrationExist($this->table)) {
$this->info( "Migration $this->migrationName already created: Skipping..." );
continue;
}

$this->fields = $this->schemaGenerator->getFields( $this->table );

$this->generate();
}
}

/**Return a substring between 2 strings
* @param $string
* @param $start
* @param $end
* @param bool $trim
* @return false|string
*/
public function getStringBetween($string, $start, $end, $trim = true) {
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
$output = substr($string, $ini, $len);
return ($trim) ? trim(strip_tags($output)) : $output;
}

/**Check if migration for table already exists
* @param $table
* @param string $prefix
* @param string $suffix
* @return bool
*/
protected function migrationExist($table, $prefix = 'create_', $suffix = '_table')
{
$exists = false;
$path = $this->getPathByOptionOrConfig( 'path', 'migration_target_path' );
$files = scandir($path);

foreach ($files as $file)
{
if($this->getStringBetween($file, $prefix, $suffix) == $table) {
$exists = true;
break;
}
}

return $exists;
}

/**
* Generate foreign key migrations.
*
Expand All @@ -255,10 +310,19 @@ protected function generateTablesAndIndices( array $tables )
protected function generateForeignKeys( array $tables )
{
$this->method = 'table';
$prefix = 'add_foreign_keys_to_';
$suffix = '_table';

foreach ( $tables as $table ) {
$this->table = $table;
$this->migrationName = 'add_foreign_keys_to_'. $this->table .'_table';

$this->migrationName = $prefix . $this->table . $suffix;
if($this->ignoreCreatedMigration && $this->migrationExist($this->table, $prefix, $suffix)) {
$this->info( "Migration $this->migrationName already created: Skipping..." );
continue;
}

$this->fields = $this->schemaGenerator->getForeignKeyConstraints( $this->table );

$this->generate();
Expand Down Expand Up @@ -363,7 +427,8 @@ protected function getOptions()
['connection', 'c', InputOption::VALUE_OPTIONAL, 'The database connection to use.', $this->config->get( 'database.default' )],
['tables', 't', InputOption::VALUE_OPTIONAL, 'A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments'],
['ignore', 'i', InputOption::VALUE_OPTIONAL, 'A list of Tables you wish to ignore, separated by a comma: users,posts,comments' ],
['path', 'p', InputOption::VALUE_OPTIONAL, 'Where should the file be created?'],
['ignore-created-migration', 'icm', null, 'Flag to ignore migrations already created' ],
['path', 'p', InputOption::VALUE_OPTIONAL, 'Where should the file be created?'],
['templatePath', 'tp', InputOption::VALUE_OPTIONAL, 'The location of the template for this generator'],
['defaultIndexNames', null, InputOption::VALUE_NONE, 'Don\'t use db index names for migrations'],
['defaultFKNames', null, InputOption::VALUE_NONE, 'Don\'t use db foreign key names for migrations'],
Expand Down