Skip to content

Commit

Permalink
Add connection to generated migrations if not default
Browse files Browse the repository at this point in the history
  • Loading branch information
Xethron committed Nov 20, 2016
1 parent 0a0a174 commit 968849f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
21 changes: 14 additions & 7 deletions src/Xethron/MigrationsGenerator/MigrateGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
use Way\Generators\Compilers\TemplateCompiler;
use Illuminate\Database\Migrations\MigrationRepositoryInterface;

use Way\Generators\Syntax\DroppedTable;

use Xethron\MigrationsGenerator\Generators\SchemaGenerator;
use Xethron\MigrationsGenerator\Syntax\AddToTable;
use Xethron\MigrationsGenerator\Syntax\DroppedTable;
use Xethron\MigrationsGenerator\Syntax\AddForeignKeysToTable;
use Xethron\MigrationsGenerator\Syntax\RemoveForeignKeysFromTable;

Expand Down Expand Up @@ -101,7 +100,12 @@ class MigrateGenerateCommand extends GeneratorCommand {
*/
protected $table;

/**
/**
* @var string|null
*/
protected $connection = null;

/**
* @param \Way\Generators\Generator $generator
* @param \Way\Generators\Filesystem\Filesystem $file
* @param \Way\Generators\Compilers\TemplateCompiler $compiler
Expand Down Expand Up @@ -132,6 +136,9 @@ public function __construct(
public function fire()
{
$this->info( 'Using connection: '. $this->option( 'connection' ) ."\n" );
if ($this->option('connection') !== $this->config->get('database.default')) {
$this->connection = $this->option('connection');
}
$this->schemaGenerator = new SchemaGenerator(
$this->option('connection'),
$this->option('defaultIndexNames'),
Expand Down Expand Up @@ -278,11 +285,11 @@ protected function getDatePrefix()
protected function getTemplateData()
{
if ( $this->method == 'create' ) {
$up = (new AddToTable($this->file, $this->compiler))->run($this->fields, $this->table, 'create');
$down = (new DroppedTable)->drop($this->table);
$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);
$down = (new RemoveForeignKeysFromTable($this->file, $this->compiler))->run($this->fields,$this->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
20 changes: 20 additions & 0 deletions src/Xethron/MigrationsGenerator/Syntax/DroppedTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Xethron\MigrationsGenerator\Syntax;

class DroppedTable
{
/**
* Get string for dropping a table
*
* @param $tableName
* @param null $connection
*
* @return string
*/
public function drop($tableName, $connection = null)
{
if (!is_null($connection)) $connection = 'connection(\''.$connection.'\')->';
return "Schema::{$connection}drop('$tableName');";
}
}
17 changes: 10 additions & 7 deletions src/Xethron/MigrationsGenerator/Syntax/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ abstract class Table extends \Way\Generators\Syntax\Table{
*/
protected $table;

/**
* @param array $fields
* @param string $table
* @param string $method
* @return string
*/
public function run(array $fields, $table, $method = 'table')
/**
* @param array $fields
* @param string $table
* @param string $method
* @param null $connection
*
* @return string
*/
public function run(array $fields, $table, $connection = null, $method = 'table')
{
$table = substr($table, strlen(\DB::getTablePrefix()));
$this->table = $table;
if (!is_null($connection)) $method = 'connection(\''.$connection.'\')->'.$method;
$compiled = $this->compiler->compile($this->getTemplate(), ['table'=>$table,'method'=>$method]);
return $this->replaceFieldsWith($this->getItems($fields), $compiled);
}
Expand Down

0 comments on commit 968849f

Please sign in to comment.