Skip to content

Commit 26fe12d

Browse files
authored
列舉檔案清單的時候標註已安裝 (#2)
* 調整測試 * 在列舉遷移檔的時候標記出已安裝 * 修改範例檔 * 修正說明
1 parent c8c38e0 commit 26fe12d

File tree

5 files changed

+85
-46
lines changed

5 files changed

+85
-46
lines changed

publishes/patches/2022_07_19_000000_add_priority_to_products_table.php renamed to publishes/patches/2022_07_19_000000_add_soft_deletes_to_users_table.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
class AddPriorityToProductsTable extends Migration
7+
class AddSoftDeletesToUsersTable extends Migration
88
{
99
/**
1010
* Run the migrations.
@@ -13,9 +13,8 @@ class AddPriorityToProductsTable extends Migration
1313
*/
1414
public function up()
1515
{
16-
Schema::table('products', function (Blueprint $table) {
17-
$table->tinyInteger('priority')->comment('優先度')
18-
->default(0);
16+
Schema::table('users', function (Blueprint $table) {
17+
$table->softDeletes();
1918
});
2019
}
2120

@@ -26,9 +25,9 @@ public function up()
2625
*/
2726
public function down()
2827
{
29-
Schema::table('products', function (Blueprint $table) {
30-
if (Schema::hasColumn($table->getTable(), 'priority')) {
31-
$table->dropColumn('priority');
28+
Schema::table('users', function (Blueprint $table) {
29+
if (Schema::hasColumn($table->getTable(), 'deleted_at')) {
30+
$table->dropColumn('deleted_at');
3231
}
3332
});
3433
}

publishes/patches/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Stubs
1+
# Patches
22

3-
此目錄用來放置 `patches` 檔案
3+
此目錄用來放置作為補丁的遷移檔案
44

5-
`artisan db:patch` 將會讀取當前目錄的遷移檔案
5+
執行 `artisan db:patch` 將會讀取當前目錄檔案

src/Commands/DbPatchCommand.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace A2Workspace\DatabasePatcher\Commands;
44

5+
use RuntimeException;
56
use Illuminate\Support\Str;
6-
use Illuminate\Console\Command;
77
use Illuminate\Support\Collection;
8+
use Illuminate\Support\Facades\DB;
9+
use Illuminate\Support\Facades\Schema;
10+
use Illuminate\Console\Command;
811
use Symfony\Component\Finder\Finder;
912
use Symfony\Component\Finder\SplFileInfo;
1013

@@ -149,13 +152,19 @@ protected function getFileListInDirectory(string $path): array
149152
*/
150153
protected function choiceFromFileList($question, Collection $files): SplFileInfo
151154
{
152-
$formattedFiles = $files->map(function (SplFileInfo $file) {
153-
$label = $file->getRelativePathname();
155+
$migrations = $this->getInstalledMigrations();
154156

155-
// 加上符號隔開避免 choice 時索引與檔案名稱混淆 (這應該是 Symfony 的 bug 待查證)
156-
$label = "-> {$label}";
157+
$formattedFiles = $files->map(function (SplFileInfo $file) use ($migrations) {
158+
$filename = $file->getRelativePathname();
159+
160+
$filename = Str::beforeLast($filename, '.php');
157161

158-
return [$label, $file];
162+
$label = (in_array($filename, $migrations))
163+
? "<fg=yellow>Installed: {$filename}</fg=yellow>"
164+
: $filename;
165+
166+
// 加上符號隔開避免 choice 時索引與檔案名稱混淆 (這應該是 Symfony 的 bug 待查證)
167+
return ["-> {$label}", $file];
159168
});
160169

161170
$options = $formattedFiles->pluck(0)->toArray();
@@ -167,6 +176,23 @@ protected function choiceFromFileList($question, Collection $files): SplFileInfo
167176
})[1];
168177
}
169178

179+
/**
180+
* Get the list of installed migration.
181+
*
182+
* @return array
183+
*/
184+
private function getInstalledMigrations(): array
185+
{
186+
if (! Schema::hasTable('migrations')) {
187+
return [];
188+
}
189+
190+
return DB::table('migrations')
191+
->get('migration')
192+
->pluck('migration')
193+
->toArray();
194+
}
195+
170196
// =========================================================================
171197
// = UsingRevertion()
172198
// =========================================================================

tests/CallDbPatchArtisanCommandTest.php

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Tests;
44

5-
use Illuminate\Testing\PendingCommand;
5+
use Illuminate\Filesystem\Filesystem;
66
use Illuminate\Support\Facades\Schema;
77
use Illuminate\Support\Facades\Artisan;
88
use Illuminate\Database\Schema\Blueprint;
@@ -18,10 +18,9 @@ protected function setUp(): void
1818
{
1919
parent::setUp();
2020

21-
Schema::create('products', function (Blueprint $table) {
21+
Schema::create('users', function (Blueprint $table) {
2222
$table->increments('id');
23-
$table->string('ian')->unique();
24-
$table->string('name');
23+
$table->string('username');
2524
});
2625

2726
if (empty(static::$published)) {
@@ -35,8 +34,7 @@ protected function setUp(): void
3534

3635
public static function tearDownAfterClass(): void
3736
{
38-
@unlink(static::$published);
39-
@rmdir(static::$published);
37+
(new Filesystem)->cleanDirectory(static::$published);
4038
}
4139

4240
// =========================================================================
@@ -45,93 +43,99 @@ public static function tearDownAfterClass(): void
4543

4644
public function test_call_artisan_command()
4745
{
48-
// =====================================================================
49-
// = Step 1: Install specified patch file.
50-
// =====================================================================
51-
5246
$command = $this->artisan('db:patch');
5347

5448
$command->expectsChoice(
5549
'選擇補丁檔案',
56-
$this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php'),
50+
$this->parseLabel('2022_07_19_000000_add_soft_deletes_to_users_table'),
5751
[
58-
$this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php')
52+
$this->parseLabel('2022_07_19_000000_add_soft_deletes_to_users_table')
5953
],
6054
);
6155

6256
$command->expectsOutput(sprintf(
6357
'Running: php artisan migrate --path=%s',
64-
$this->resolvePath('/database/patches/2022_07_19_000000_add_priority_to_products_table.php')
58+
$this->resolvePath('/database/patches/2022_07_19_000000_add_soft_deletes_to_users_table.php')
6559
));
6660

6761
$command->assertExitCode(0);
6862
$command->run();
6963

7064
$this->assertDatabaseHas(
7165
'migrations',
72-
['migration' => '2022_07_19_000000_add_priority_to_products_table']
66+
['migration' => '2022_07_19_000000_add_soft_deletes_to_users_table']
7367
);
7468

75-
$this->assertDatabaseTableHasColumn('products', 'priority');
69+
$this->assertDatabaseTableHasColumn('users', 'deleted_at');
70+
}
7671

77-
// =====================================================================
78-
// = Step 2: Revert it.
79-
// =====================================================================
72+
public function test_call_artisan_command_and_revert()
73+
{
74+
$this->artisan('db:patch')
75+
->expectsChoice(
76+
'選擇補丁檔案',
77+
$this->parseLabel('2022_07_19_000000_add_soft_deletes_to_users_table'),
78+
[
79+
$this->parseLabel('2022_07_19_000000_add_soft_deletes_to_users_table')
80+
],
81+
)
82+
->assertExitCode(0)
83+
->run();
8084

8185
$command2 = $this->artisan('db:patch', ['--revert' => true]);
8286

8387
$command2->expectsChoice(
8488
'選擇補丁檔案',
85-
$this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php'),
89+
$this->parseInstalledLabel('2022_07_19_000000_add_soft_deletes_to_users_table'),
8690
[
87-
$this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php')
91+
$this->parseInstalledLabel('2022_07_19_000000_add_soft_deletes_to_users_table')
8892
],
8993
);
9094

9195
$command2->expectsOutput(sprintf(
9296
'Running: php artisan migrate:rollback --path=%s',
93-
$this->resolvePath('/database/patches/2022_07_19_000000_add_priority_to_products_table.php')
97+
$this->resolvePath('/database/patches/2022_07_19_000000_add_soft_deletes_to_users_table.php')
9498
));
9599

96100
$command2->assertExitCode(0);
97101
$command2->run();
98102

99-
$this->assertDatabaseTableMissingColumn('products', 'priority');
103+
$this->assertDatabaseTableMissingColumn('users', 'deleted_at');
100104

101105
$this->assertDatabaseMissing(
102106
'migrations',
103-
['migration' => '2022_07_19_000000_add_priority_to_products_table']
107+
['migration' => '2022_07_19_000000_add_soft_deletes_to_users_table']
104108
);
105109
}
106110

107111
public function test_call_artisan_command_with_filter()
108112
{
109113
$command = $this->artisan('db:patch', [
110-
'filter' => 'product',
114+
'filter' => 'users',
111115
]);
112116

113117
$command->expectsChoice(
114118
'選擇補丁檔案',
115-
$this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php'),
119+
$this->parseLabel('2022_07_19_000000_add_soft_deletes_to_users_table'),
116120
[
117-
$this->parseLabel('2022_07_19_000000_add_priority_to_products_table.php')
121+
$this->parseLabel('2022_07_19_000000_add_soft_deletes_to_users_table')
118122
],
119123
);
120124

121125
$command->expectsOutput(sprintf(
122126
'Running: php artisan migrate --path=%s',
123-
$this->resolvePath('/database/patches/2022_07_19_000000_add_priority_to_products_table.php')
127+
$this->resolvePath('/database/patches/2022_07_19_000000_add_soft_deletes_to_users_table.php')
124128
));
125129

126130
$command->assertExitCode(0);
127131
$command->run();
128132

129133
$this->assertDatabaseHas(
130134
'migrations',
131-
['migration' => '2022_07_19_000000_add_priority_to_products_table']
135+
['migration' => '2022_07_19_000000_add_soft_deletes_to_users_table']
132136
);
133137

134-
$this->assertDatabaseTableHasColumn('products', 'priority');
138+
$this->assertDatabaseTableHasColumn('users', 'deleted_at');
135139
}
136140

137141
public function test_call_artisan_command_with_filter_then_not_found()

tests/TestArtisanCommandHelpers.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ private function parseLabel(string $option): string
1616
}
1717

1818
/**
19-
* @param string $table
19+
* @param string $option
20+
* @return string
21+
*/
22+
private function parseInstalledLabel(string $option): string
23+
{
24+
return "-> <fg=yellow>Installed: {$option}</fg=yellow>";
25+
}
26+
27+
/**
28+
* @param strle
2029
* @param string $column
2130
* @return self
2231
*/
@@ -53,3 +62,4 @@ private function assertDatabaseTableMissingColumn($table, $column)
5362
return $this;
5463
}
5564
}
65+

0 commit comments

Comments
 (0)