Skip to content

Commit 8a9e187

Browse files
authored
Merge pull request #109 from sebdesign/refactor-generate-method
Refactor generate method
2 parents 2a11872 + 01dccdd commit 8a9e187

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
lines changed

src/Xethron/MigrationsGenerator/MethodNotFoundException.php

-3
This file was deleted.

src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php

+51-30
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ public function fire()
172172

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

@@ -218,37 +218,56 @@ protected function askNumeric( $question, $default = null ) {
218218
}
219219

220220
/**
221-
* Generate Migrations
221+
* Generate tables and index migrations.
222222
*
223-
* @param string $method Create Tables or Foreign Keys ['create', 'foreign_keys']
224-
* @param array $tables List of tables to create migrations for
225-
* @throws MethodNotFoundException
223+
* @param array $tables List of tables to create migrations for
226224
* @return void
227225
*/
228-
protected function generate( $method, $tables )
226+
protected function generateTablesAndIndices( array $tables )
229227
{
230-
if ( $method == 'create' ) {
231-
$function = 'getFields';
232-
$prefix = 'create';
233-
} elseif ( $method = 'foreign_keys' ) {
234-
$function = 'getForeignKeyConstraints';
235-
$prefix = 'add_foreign_keys_to';
236-
$method = 'table';
237-
} else {
238-
throw new MethodNotFoundException( $method );
228+
$this->method = 'create';
229+
230+
foreach ( $tables as $table ) {
231+
$this->table = $table;
232+
$this->migrationName = 'create_'. $this->table .'_table';
233+
$this->fields = $this->schemaGenerator->getFields( $this->table );
234+
235+
$this->generate();
239236
}
237+
}
238+
239+
/**
240+
* Generate foreign key migrations.
241+
*
242+
* @param array $tables List of tables to create migrations for
243+
* @return void
244+
*/
245+
protected function generateForeignKeys( array $tables )
246+
{
247+
$this->method = 'table';
240248

241249
foreach ( $tables as $table ) {
242-
$this->migrationName = $prefix .'_'. $table .'_table';
243-
$this->method = $method;
244250
$this->table = $table;
245-
$this->fields = $this->schemaGenerator->{$function}( $table );
246-
if ( $this->fields ) {
247-
parent::fire();
248-
if ( $this->log ) {
249-
$file = $this->datePrefix . '_' . $this->migrationName;
250-
$this->repository->log($file, $this->batch);
251-
}
251+
$this->migrationName = 'add_foreign_keys_to_'. $this->table .'_table';
252+
$this->fields = $this->schemaGenerator->getForeignKeyConstraints( $this->table );
253+
254+
$this->generate();
255+
}
256+
}
257+
258+
/**
259+
* Generate Migration for the current table.
260+
*
261+
* @return void
262+
*/
263+
protected function generate()
264+
{
265+
if ( $this->fields ) {
266+
parent::fire();
267+
268+
if ( $this->log ) {
269+
$file = $this->datePrefix . '_' . $this->migrationName;
270+
$this->repository->log($file, $this->batch);
252271
}
253272
}
254273
}
@@ -287,9 +306,11 @@ protected function getTemplateData()
287306
if ( $this->method == 'create' ) {
288307
$up = (new AddToTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection, 'create');
289308
$down = (new DroppedTable)->drop($this->table, $this->connection);
290-
} else {
291-
$up = (new AddForeignKeysToTable($this->file, $this->compiler))->run($this->fields,$this->table, $this->connection);
292-
$down = (new RemoveForeignKeysFromTable($this->file, $this->compiler))->run($this->fields,$this->table, $this->connection);
309+
}
310+
311+
if ( $this->method == 'table' ) {
312+
$up = (new AddForeignKeysToTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection);
313+
$down = (new RemoveForeignKeysFromTable($this->file, $this->compiler))->run($this->fields, $this->table, $this->connection);
293314
}
294315

295316
return [
@@ -342,11 +363,11 @@ protected function getOptions()
342363
/**
343364
* Remove all the tables to exclude from the array of tables
344365
*
345-
* @param $tables
366+
* @param array $tables
346367
*
347368
* @return array
348369
*/
349-
protected function removeExcludedTables($tables)
370+
protected function removeExcludedTables( array $tables )
350371
{
351372
$excludes = $this->getExcludedTables();
352373
$tables = array_diff($tables, $excludes);

0 commit comments

Comments
 (0)