Skip to content

Commit aa01efd

Browse files
Merge pull request #103 from TheDragonCode/3.x
Fixed `ColumnDoesNotExist`
2 parents 79fee6e + e1750f2 commit aa01efd

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/Database/BaseChangeMigrationColumn.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public function up(): void
2222
{
2323
if ($this->hasTable()) {
2424
Schema::table($this->table(), function (Blueprint $table) {
25-
$table->renameColumn('migration', 'action');
25+
if ($this->hasColumn('migration') && $this->doesntHaveColumn('action')) {
26+
$table->renameColumn('migration', 'action');
27+
}
2628

2729
$table->unsignedInteger('batch')->change();
2830
});
@@ -45,6 +47,16 @@ protected function hasTable(): bool
4547
return Schema::hasTable($this->table());
4648
}
4749

50+
protected function hasColumn(string $column): bool
51+
{
52+
return Schema::hasColumn($this->table(), $column);
53+
}
54+
55+
protected function doesntHaveColumn(string $column): bool
56+
{
57+
return ! $this->hasColumn($column);
58+
}
59+
4860
protected function table(): string
4961
{
5062
return $this->config->table();

tests/Migrations/MigrationTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Migrations;
6+
7+
use Doctrine\DBAL\Driver\PDOSqlite\Driver;
8+
use DragonCode\LaravelActions\Constants\Names;
9+
use Illuminate\Support\Facades\DB;
10+
use Illuminate\Support\Facades\Schema;
11+
use Tests\TestCase;
12+
13+
class MigrationTest extends TestCase
14+
{
15+
public function testRunMigrationAfterInstall(): void
16+
{
17+
if (! class_exists(Driver::class)) {
18+
$this->assertTrue(true);
19+
20+
return;
21+
}
22+
23+
DB::table('migrations')->truncate();
24+
25+
Schema::connection($this->database)->dropIfExists($this->table);
26+
27+
$this->assertDatabaseCount('migrations', 0);
28+
$this->assertDatabaseDoesntTable($this->table);
29+
30+
$this->artisan(Names::INSTALL)->run();
31+
32+
$this->assertDatabaseCount('migrations', 0);
33+
$this->assertDatabaseHasTable($this->table);
34+
35+
$this->artisan('migrate')->run();
36+
37+
$this->assertDatabaseCount('migrations', 1);
38+
$this->assertDatabaseHas('migrations', ['migration' => '2022_08_18_180137_change_migration_actions_table']);
39+
}
40+
}

0 commit comments

Comments
 (0)