Skip to content

Commit 573bc3e

Browse files
committed
Added force option for update-url command
Includes test to cover. Closes #4223
1 parent 57bdd83 commit 573bc3e

2 files changed

Lines changed: 29 additions & 24 deletions

File tree

app/Console/Commands/UpdateUrl.php

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class UpdateUrl extends Command
1414
*/
1515
protected $signature = 'bookstack:update-url
1616
{oldUrl : URL to replace}
17-
{newUrl : URL to use as the replacement}';
17+
{newUrl : URL to use as the replacement}
18+
{--force : Force the operation to run, ignoring confirmations}';
1819

1920
/**
2021
* The console command description.
@@ -23,25 +24,12 @@ class UpdateUrl extends Command
2324
*/
2425
protected $description = 'Find and replace the given URLs in your BookStack database';
2526

26-
protected $db;
27-
28-
/**
29-
* Create a new command instance.
30-
*
31-
* @return void
32-
*/
33-
public function __construct(Connection $db)
34-
{
35-
$this->db = $db;
36-
parent::__construct();
37-
}
38-
3927
/**
4028
* Execute the console command.
4129
*
4230
* @return mixed
4331
*/
44-
public function handle()
32+
public function handle(Connection $db)
4533
{
4634
$oldUrl = str_replace("'", '', $this->argument('oldUrl'));
4735
$newUrl = str_replace("'", '', $this->argument('newUrl'));
@@ -67,7 +55,7 @@ public function handle()
6755

6856
foreach ($columnsToUpdateByTable as $table => $columns) {
6957
foreach ($columns as $column) {
70-
$changeCount = $this->replaceValueInTable($table, $column, $oldUrl, $newUrl);
58+
$changeCount = $this->replaceValueInTable($db, $table, $column, $oldUrl, $newUrl);
7159
$this->info("Updated {$changeCount} rows in {$table}->{$column}");
7260
}
7361
}
@@ -80,7 +68,7 @@ public function handle()
8068
foreach ($columns as $column) {
8169
$oldJson = trim(json_encode($oldUrl), '"');
8270
$newJson = trim(json_encode($newUrl), '"');
83-
$changeCount = $this->replaceValueInTable($table, $column, $oldJson, $newJson);
71+
$changeCount = $this->replaceValueInTable($db, $table, $column, $oldJson, $newJson);
8472
$this->info("Updated {$changeCount} JSON encoded rows in {$table}->{$column}");
8573
}
8674
}
@@ -97,13 +85,18 @@ public function handle()
9785
* Perform a find+replace operations in the provided table and column.
9886
* Returns the count of rows changed.
9987
*/
100-
protected function replaceValueInTable(string $table, string $column, string $oldUrl, string $newUrl): int
101-
{
102-
$oldQuoted = $this->db->getPdo()->quote($oldUrl);
103-
$newQuoted = $this->db->getPdo()->quote($newUrl);
104-
105-
return $this->db->table($table)->update([
106-
$column => $this->db->raw("REPLACE({$column}, {$oldQuoted}, {$newQuoted})"),
88+
protected function replaceValueInTable(
89+
Connection $db,
90+
string $table,
91+
string $column,
92+
string $oldUrl,
93+
string $newUrl
94+
): int {
95+
$oldQuoted = $db->getPdo()->quote($oldUrl);
96+
$newQuoted = $db->getPdo()->quote($newUrl);
97+
98+
return $db->table($table)->update([
99+
$column => $db->raw("REPLACE({$column}, {$oldQuoted}, {$newQuoted})"),
107100
]);
108101
}
109102

@@ -113,6 +106,10 @@ protected function replaceValueInTable(string $table, string $column, string $ol
113106
*/
114107
protected function checkUserOkayToProceed(string $oldUrl, string $newUrl): bool
115108
{
109+
if ($this->option('force')) {
110+
return true;
111+
}
112+
116113
$dangerWarning = "This will search for \"{$oldUrl}\" in your database and replace it with \"{$newUrl}\".\n";
117114
$dangerWarning .= 'Are you sure you want to proceed?';
118115
$backupConfirmation = 'This operation could cause issues if used incorrectly. Have you made a backup of your existing database?';

tests/Commands/UpdateUrlCommandTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Commands;
44

5+
use Illuminate\Support\Facades\Artisan;
56
use Symfony\Component\Console\Exception\RuntimeException;
67
use Tests\TestCase;
78

@@ -34,6 +35,13 @@ public function test_command_requires_valid_url()
3435
$this->artisan('bookstack:update-url https://cats.example.com');
3536
}
3637

38+
public function test_command_force_option_skips_prompt()
39+
{
40+
$this->artisan('bookstack:update-url --force https://cats.example.com/donkey https://cats.example.com/monkey')
41+
->expectsOutputToContain('URL update procedure complete')
42+
->assertSuccessful();
43+
}
44+
3745
public function test_command_updates_settings()
3846
{
3947
setting()->put('my-custom-item', 'https://example.com/donkey/cat');

0 commit comments

Comments
 (0)