Skip to content
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
16 changes: 13 additions & 3 deletions src/Migrations/MigrationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function parseAndBuildMigration(array $schema)
if (count($foreignKeyData = $this->getForeignKeyData())) {
// Write foreign key migration out to disk.
$data = $this->buildForeignKeyData($foreignKeyData);
$this->createForeignKeyMigration($data['up'], $data['down']);
$this->createForeignKeyMigration($data['up'], $data['down'], $schema['database']['name']);
}

// Write the schema into a txt file.
Expand Down Expand Up @@ -196,18 +196,28 @@ private function createMigration($name, $columnData)
*
* @return string
*/
private function createForeignKeyMigration($upData, $downData)
private function createForeignKeyMigration($upData, $downData, $schemaName = null)
{
// Update time interval each time so that migration
// files are created with different timestamp.
$this->setTimeInterval($this->getTimeInterval() + 60);

$path = $this->getDatePrefix() . '_create_foreign_keys_table.php';
$schemaPart = $classPart = '';

if ($schemaName) {
$schemaPart = '_for_' . str_replace(' ', '_', $schemaName);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need to convert $schemaName to lowercase here in case anyone provides uppercase name.

}
$path = $this->getDatePrefix() . '_create_foreign_keys_table' . $schemaPart . '.php';

if ($schemaName) {
$classPart = 'For' . str_replace(' ', '', ucwords(str_replace('_', ' ', $schemaName)));
}

// First we will get the stub file for the migration, which serves as a type
// of template for the migration. Once we have those we will populate the
// various place-holders, save the file, and push it to zip archive.
$stub = $this->files->get(__DIR__ . '/stubs/foreignKey.stub');
$stub = str_replace('ForSchema', $classPart, $stub);

$contents = str_replace('return 1;', implode(PHP_EOL, $upData), $stub);
$contents = str_replace('return 2;', implode(PHP_EOL, $downData), $contents);
Expand Down
2 changes: 1 addition & 1 deletion src/Migrations/stubs/foreignKey.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateForeignKeysTable extends Migration
class CreateForeignKeysTableForSchema extends Migration
{
/**
* Run the migrations.
Expand Down