Skip to content

Commit

Permalink
feat: support after method with clsorue in migrations (#972)
Browse files Browse the repository at this point in the history
Co-authored-by: Jim O'Halloran <jim.ohalloran@incore.com.au>
  • Loading branch information
jimohalloran and Jim O'Halloran committed Oct 19, 2021
1 parent d4766ed commit 4f54a8b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Fixed template resolution when `flatMap`'s callback returns an `Enumerable`

### Added

- Detection of fields added/modified a migration $table->after() method (Added in Laravel 8.27). ([#971](https://github.com/nunomaduro/larastan/pull/971))

## [0.7.13] - 2021-10-14

### Fixed
Expand Down
9 changes: 9 additions & 0 deletions src/Properties/SchemaAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ private function processColumnUpdates(string $tableName, string $argName, array
$table->setColumn(new SchemaColumn($columnName, 'float', $nullable));
break;

case 'after':
if ($secondArg instanceof PhpParser\Node\Expr\Closure
&& $secondArg->params[0]->var instanceof PhpParser\Node\Expr\Variable
&& ! ($secondArg->params[0]->var->name instanceof PhpParser\Node\Expr)) {
$argName = $secondArg->params[0]->var->name;
$this->processColumnUpdates($tableName, $argName, $secondArg->stmts);
}
break;

case 'dropcolumn':
case 'dropifexists':
case 'dropsoftdeletes':
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/MigrationHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ public function it_can_read_additional_directories(): void
self::assertArrayHasKey('teams', $tables);
}

/** @test */
public function it_can_handle_use_of_after_method_in_migration(): void
{
$migrationHelper = new MigrationHelper($this->cachedParser, '', [
__DIR__.'/data/migrations_using_after_method',
]);

$tables = $migrationHelper->initializeTables();

self::assertCount(1, $tables);
self::assertArrayHasKey('users', $tables);
self::assertCount(5, $tables['users']->columns);
self::assertSame(['id', 'name', 'created_at', 'updated_at', 'email'], 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);
}

/**
* @param array<string, SchemaTable> $tables
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\MigrationsUsingAfterMethod;

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

class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('users', static function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable();
$table->timestamps();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\MigrationsUsingAfterMethod;

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

class AlterUsersTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', static function (Blueprint $table) {
$table->after('name', function (Blueprint $table) {
$table->string('email')->unique();
});
});
}
}

0 comments on commit 4f54a8b

Please sign in to comment.