Skip to content

Commit

Permalink
feat: add support for migrations using Schema::connection (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
canvural committed Mar 4, 2022
1 parent b7892e2 commit ddb1096
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/Properties/MigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(Parser $parser, array $databaseMigrationPath, FileHe
*/
public function initializeTables(array $tables = []): array
{
if (empty($this->databaseMigrationPath)) {
if (count($this->databaseMigrationPath) === 0) {
$this->databaseMigrationPath = [database_path('migrations')];
}

Expand Down
59 changes: 38 additions & 21 deletions src/Properties/SchemaAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,50 @@ private function addUpMethodStatements(array $stmts): void

foreach ($methods as $stmt) {
if ($stmt instanceof PhpParser\Node\Stmt\Expression
&& $stmt->expr instanceof PhpParser\Node\Expr\MethodCall
&& $stmt->expr->var instanceof PhpParser\Node\Expr\StaticCall
&& $stmt->expr->var->class instanceof PhpParser\Node\Name
&& $stmt->expr->var->name instanceof PhpParser\Node\Identifier
&& ($stmt->expr->var->name->toString() === 'connection' || $stmt->expr->var->name->toString() === 'setConnection')
&& ($stmt->expr->var->class->toCodeString() === '\Illuminate\Support\Facades\Schema' || $stmt->expr->var->class->toCodeString() === '\Schema')
) {
$statement = $stmt->expr;
} elseif ($stmt instanceof PhpParser\Node\Stmt\Expression
&& $stmt->expr instanceof PhpParser\Node\Expr\StaticCall
&& ($stmt->expr->class instanceof PhpParser\Node\Name)
&& $stmt->expr->class instanceof PhpParser\Node\Name
&& $stmt->expr->name instanceof PhpParser\Node\Identifier
&& ($stmt->expr->class->toCodeString() === '\Illuminate\Support\Facades\Schema' || $stmt->expr->class->toCodeString() === '\Schema')
) {
switch ($stmt->expr->name->name) {
case 'create':
$this->alterTable($stmt->expr, true);
break;

case 'table':
$this->alterTable($stmt->expr, false);
break;

case 'drop':
case 'dropIfExists':
$this->dropTable($stmt->expr);
break;

case 'rename':
$this->renameTableThroughStaticCall($stmt->expr);
}
$statement = $stmt->expr;
} else {
continue;
}

if (! $statement->name instanceof PhpParser\Node\Identifier) {
continue;
}

switch ($statement->name->name) {
case 'create':
$this->alterTable($statement, true);
break;

case 'table':
$this->alterTable($statement, false);
break;

case 'drop':
case 'dropIfExists':
$this->dropTable($statement);
break;

case 'rename':
$this->renameTableThroughStaticCall($statement);
}
}
}

private function alterTable(PhpParser\Node\Expr\StaticCall $call, bool $creating): void
private function alterTable(PhpParser\Node\Expr\StaticCall|PhpParser\Node\Expr\MethodCall $call, bool $creating): void
{
if (! isset($call->args[0])
|| ! $call->getArgs()[0]->value instanceof PhpParser\Node\Scalar\String_
Expand Down Expand Up @@ -401,7 +418,7 @@ private function processColumnUpdates(string $tableName, string $argName, array
}
}

private function dropTable(PhpParser\Node\Expr\StaticCall $call): void
private function dropTable(PhpParser\Node\Expr\StaticCall|PhpParser\Node\Expr\MethodCall $call): void
{
if (! isset($call->args[0])
|| ! $call->getArgs()[0]->value instanceof PhpParser\Node\Scalar\String_
Expand All @@ -414,7 +431,7 @@ private function dropTable(PhpParser\Node\Expr\StaticCall $call): void
unset($this->tables[$tableName]);
}

private function renameTableThroughStaticCall(PhpParser\Node\Expr\StaticCall $call): void
private function renameTableThroughStaticCall(PhpParser\Node\Expr\StaticCall|PhpParser\Node\Expr\MethodCall $call): void
{
if (! isset($call->args[0], $call->args[1])
|| ! $call->getArgs()[0]->value instanceof PhpParser\Node\Scalar\String_
Expand Down
42 changes: 26 additions & 16 deletions tests/Unit/MigrationHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,6 @@ public function it_can_handle_alter_table_rename()
self::assertArrayHasKey('accounts', $tables);
}

/**
* @param array<string, SchemaTable> $tables
*/
private function assertUsersTableSchema(array $tables): void
{
self::assertCount(1, $tables);
self::assertArrayHasKey('users', $tables);
self::assertCount(5, $tables['users']->columns);
self::assertSame(['id', 'name', 'email', 'created_at', 'updated_at'], array_keys($tables['users']->columns));
self::assertSame('int', $tables['users']->columns['id']->readableType);
self::assertSame('string', $tables['users']->columns['name']->readableType);
self::assertSame('string', $tables['users']->columns['email']->readableType);
self::assertSame('string', $tables['users']->columns['created_at']->readableType);
self::assertSame('string', $tables['users']->columns['updated_at']->readableType);
}

/** @test */
public function it_can_handle_migrations_with_soft_deletes()
{
Expand All @@ -161,4 +145,30 @@ public function it_can_handle_migrations_with_soft_deletes_tz()
self::assertCount(6, $tables['users']->columns);
self::assertSame('string', $tables['users']->columns['deleted_at']->readableType);
}

/** @test */
public function it_can_handle_connection_before_schema_create()
{
$migrationHelper = new MigrationHelper($this->parser, [__DIR__.'/data/migration_with_schema_connection'], $this->fileHelper);

$tables = $migrationHelper->initializeTables();

$this->assertUsersTableSchema($tables);
}

/**
* @param array<string, SchemaTable> $tables
*/
private function assertUsersTableSchema(array $tables): void
{
self::assertCount(1, $tables);
self::assertArrayHasKey('users', $tables);
self::assertCount(5, $tables['users']->columns);
self::assertSame(['id', 'name', 'email', 'created_at', 'updated_at'], array_keys($tables['users']->columns));
self::assertSame('int', $tables['users']->columns['id']->readableType);
self::assertSame('string', $tables['users']->columns['name']->readableType);
self::assertSame('string', $tables['users']->columns['email']->readableType);
self::assertSame('string', $tables['users']->columns['created_at']->readableType);
self::assertSame('string', $tables['users']->columns['updated_at']->readableType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\MigrationWithSchemaConnection;

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
public function up(): void
{
Schema::connection('foo')->create('users', static function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable();
$table->string('email')->unique();
$table->timestamps();
});
}
}

0 comments on commit ddb1096

Please sign in to comment.