-
Notifications
You must be signed in to change notification settings - Fork 11
Fix #134: Add option to force delete and recreate existing database #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d034314
refactor database creation logic with RecreateDatabase action class a…
mehrancodes 14c17df
uncomment RemoveDatabaseUser pipeline
mehrancodes 6639b13
update GitHub Actions cache to v4
mehrancodes 2143c56
wrap the first force delete info log inside database deletation condi…
mehrancodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of Laravel Harbor. | ||
* | ||
* (c) Mehran Rasulian <mehran.rasulian@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Services\Forge\Actions; | ||
|
||
use App\Services\Forge\ForgeService; | ||
use App\Traits\Outputifier; | ||
|
||
class RecreateDatabase | ||
{ | ||
use Outputifier; | ||
|
||
/** | ||
* Handle database recreation and creation process | ||
* | ||
* @param ForgeService $service The forge service | ||
* @param string $dbName The database name | ||
* @param string $dbPassword Password for database creation | ||
* @return bool True if database needs to be updated in the env file | ||
*/ | ||
public function handle(ForgeService $service, string $dbName, string $dbPassword): bool | ||
{ | ||
$databaseId = null; | ||
foreach ($service->forge->databases($service->server->id) as $database) { | ||
if (isset($database->name) && $database->name === $dbName) { | ||
$databaseId = $database->id; | ||
} | ||
} | ||
|
||
// If the setting is not enabled, we skip the deletion of existing databases. | ||
if (isset($databaseId) && !$service->setting->forceDeleteOldDatabase) { | ||
$this->warning('It seems there is an existing database with the same name. Skipping database creation. Ensure to update the database password in the .env file manually.'); | ||
return true; // Still need to update env vars even if we don't create DB | ||
} | ||
|
||
if (isset($databaseId)) { | ||
$this->information('Force deleting existing matched database.'); | ||
|
||
$service->forge->deleteDatabase($service->server->id, $databaseId); | ||
$this->information('---> Existing database deleted: ' . $dbName); | ||
} | ||
|
||
foreach ($service->forge->databaseUsers($service->server->id) as $user) { | ||
if (isset($user->name) && $user->name === $dbName) { | ||
$service->forge->deleteDatabaseUser($service->server->id, $user->id); | ||
$this->information('---> Existing database user found and deleted: ' . $dbName); | ||
} | ||
} | ||
|
||
if (isset($databaseId)) { | ||
$this->information('---> Waiting for the database deletion to complete...'); | ||
$this->waitForDatabaseDeletion(); | ||
} | ||
|
||
// Create the database with the provided password | ||
$this->information('Creating database.'); | ||
$this->createDatabase($service, $dbName, $dbPassword); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Wait for database deletion to complete | ||
*/ | ||
protected function waitForDatabaseDeletion(): void | ||
{ | ||
sleep(6); | ||
} | ||
|
||
/** | ||
* Create a new database on the server | ||
*/ | ||
public function createDatabase(ForgeService $service, string $dbName, string $dbPassword): void | ||
{ | ||
$service->forge->createDatabase($service->server->id, [ | ||
'name' => $dbName, | ||
'user' => $dbName, | ||
'password' => $dbPassword, | ||
]); | ||
|
||
$this->information('Database created: ' . $dbName); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
tests/Unit/Services/Forge/Actions/RecreateDatabaseTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
|
||
use App\Services\Forge\Actions\RecreateDatabase; | ||
use App\Services\Forge\ForgeSetting; | ||
use App\Services\Forge\ForgeService; | ||
use Laravel\Forge\Forge; | ||
use Laravel\Forge\Resources\Server; | ||
|
||
beforeEach(function () { | ||
// Setup action and common variables | ||
$this->action = new RecreateDatabase(); | ||
$this->serverId = '12345'; | ||
$this->dbName = 'test_db'; | ||
$this->dbPassword = 'test_password'; | ||
|
||
// Create mocks that will be used by all tests | ||
$this->forgeMock = Mockery::mock(Forge::class); | ||
$this->settingMock = Mockery::mock(ForgeSetting::class); | ||
$this->server = new Server(['id' => $this->serverId]); | ||
|
||
// Setup base service mock that can be customized per test | ||
$this->service = Mockery::mock(ForgeService::class); | ||
$this->service->forge = $this->forgeMock; | ||
$this->service->server = $this->server; | ||
}); | ||
|
||
afterEach(function () { | ||
Mockery::close(); | ||
}); | ||
|
||
it('skips recreation when database exists and force delete disabled', function () { | ||
// Configure setting for this specific test | ||
$this->settingMock->forceDeleteOldDatabase = false; | ||
$this->service->setting = $this->settingMock; | ||
|
||
// Mock database exists | ||
$this->forgeMock->shouldReceive('databases') | ||
->once() | ||
->with($this->serverId) | ||
->andReturn([ | ||
(object) ['name' => $this->dbName, 'id' => 99], | ||
]); | ||
|
||
// Database should not be deleted | ||
$this->forgeMock->shouldNotReceive('deleteDatabase'); | ||
$this->forgeMock->shouldNotReceive('deleteDatabaseUser'); | ||
|
||
// Database should not be created when skipping recreation | ||
$this->forgeMock->shouldNotReceive('createDatabase'); | ||
|
||
// Act | ||
$result = $this->action->handle($this->service, $this->dbName, $this->dbPassword); | ||
|
||
// Assert | ||
expect($result)->toBeTrue(); // Should return true to indicate env vars need updating | ||
}); | ||
|
||
it('recreates database when exists and force delete enabled', function () { | ||
// Configure setting for this specific test | ||
$this->settingMock->forceDeleteOldDatabase = true; | ||
$this->service->setting = $this->settingMock; | ||
|
||
// Mock existing database | ||
$dbId = 99; | ||
$userId = 88; | ||
|
||
$this->forgeMock->shouldReceive('databases') | ||
->once() | ||
->with($this->serverId) | ||
->andReturn([ | ||
(object) ['name' => $this->dbName, 'id' => $dbId], | ||
]); | ||
|
||
// Should delete the database | ||
$this->forgeMock->shouldReceive('deleteDatabase') | ||
->once() | ||
->with($this->serverId, $dbId); | ||
|
||
// Mock existing user | ||
$this->forgeMock->shouldReceive('databaseUsers') | ||
->once() | ||
->with($this->serverId) | ||
->andReturn([ | ||
(object) ['name' => $this->dbName, 'id' => $userId], | ||
]); | ||
|
||
// Should delete the database user | ||
$this->forgeMock->shouldReceive('deleteDatabaseUser') | ||
->once() | ||
->with($this->serverId, $userId); | ||
|
||
// Should create a new database | ||
$this->forgeMock->shouldReceive('createDatabase') | ||
->once() | ||
->with($this->serverId, [ | ||
'name' => $this->dbName, | ||
'user' => $this->dbName, | ||
'password' => $this->dbPassword, | ||
]); | ||
|
||
// Create a partial mock to skip actual waiting | ||
$actionSpy = Mockery::mock(RecreateDatabase::class)->makePartial(); | ||
$actionSpy->shouldAllowMockingProtectedMethods(); | ||
$actionSpy->shouldReceive('waitForDatabaseDeletion')->once()->andReturn(null); | ||
|
||
// Act | ||
$result = $actionSpy->handle($this->service, $this->dbName, $this->dbPassword); | ||
|
||
// Assert | ||
expect($result)->toBeTrue(); | ||
}); | ||
|
||
it('creates new database when none exists', function () { | ||
// Set the service to use the mocked setting | ||
$this->service->setting = $this->settingMock; | ||
|
||
// No existing database | ||
$this->forgeMock->shouldReceive('databases') | ||
->once() | ||
->with($this->serverId) | ||
->andReturn([ | ||
(object) ['name' => 'other_db', 'id' => 55], | ||
]); | ||
|
||
// Check for users (none match) | ||
$this->forgeMock->shouldReceive('databaseUsers') | ||
->once() | ||
->with($this->serverId) | ||
->andReturn([ | ||
(object) ['name' => 'other_user', 'id' => 66], | ||
]); | ||
|
||
// Should create a new database | ||
$this->forgeMock->shouldReceive('createDatabase') | ||
->once() | ||
->with($this->serverId, [ | ||
'name' => $this->dbName, | ||
'user' => $this->dbName, | ||
'password' => $this->dbPassword, | ||
]); | ||
|
||
// We should not wait for deletion since nothing was deleted | ||
$actionSpy = Mockery::mock(RecreateDatabase::class)->makePartial(); | ||
$actionSpy->shouldAllowMockingProtectedMethods(); | ||
$actionSpy->shouldNotReceive('waitForDatabaseDeletion'); | ||
|
||
// Act | ||
$result = $actionSpy->handle($this->service, $this->dbName, $this->dbPassword); | ||
|
||
// Assert | ||
expect($result)->toBeTrue(); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.