-
Notifications
You must be signed in to change notification settings - Fork 7
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
Options for importing database #107
Open
gbradley
wants to merge
21
commits into
mehrancodes:main
Choose a base branch
from
gbradley:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ec38bb0
Added import command
cca9e92
Require fork
30b63cd
Fixed tests
f0bafdd
Handle unspecified port
89ec477
debugging site command
cbfb14d
Handle command status.
1fa9c3e
Fixed Forge service injection.
b2b6749
Handling array return type.
6928b6b
Added seed command.
8dbbb09
Supports custom seeder class
ff700de
Removed information calls.
70a41bf
Allow strings for seeder.
9ab5062
Add executable workaround.
f4d45a8
Added validation rule.
1835299
Updated tests
b0d1144
Updated tests
d1474c1
Added tests
706c994
Reverted package name.
61671e7
Rename pipeline.
4c4c1fd
Rename property.
1ebb0c7
Tidying up tests
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 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 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,16 @@ | ||
<?php | ||
|
||
namespace App\Rules; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
|
||
class DBSeed implements ValidationRule | ||
{ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
if (!is_string($value) && !is_bool($value)) { | ||
$fail('The DB seed value must be a string or boolean.'); | ||
} | ||
} | ||
} |
This file contains 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 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 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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of Laravel Harbor. | ||
* | ||
* (c) Mehran Rasulian <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Services\Forge; | ||
|
||
use Laravel\Forge\Forge; | ||
use Laravel\Forge\Resources\SiteCommand; | ||
use Illuminate\Support\Sleep; | ||
|
||
class ForgeSiteCommandWaiter | ||
{ | ||
/** | ||
* The number of seconds to wait between querying Forge for the command status. | ||
*/ | ||
public int $retrySeconds = 10; | ||
|
||
/** | ||
* The number of attempts to make before returning the command. | ||
*/ | ||
public int $maxAttempts = 60; | ||
|
||
/** | ||
* The current number of attempts. | ||
*/ | ||
protected int $attempts = 0; | ||
|
||
public function __construct(public Forge $forge) | ||
{ | ||
} | ||
|
||
public function waitFor(SiteCommand $site_command): SiteCommand | ||
{ | ||
$this->attempts = 0; | ||
|
||
while ( | ||
$this->commandIsRunning($site_command) | ||
&& $this->attempts++ < $this->maxAttempts | ||
) { | ||
Sleep::for($this->retrySeconds)->seconds(); | ||
|
||
$site_command = $this->forge->getSiteCommand( | ||
$site_command->serverId, | ||
$site_command->siteId, | ||
$site_command->id | ||
)[0]; | ||
} | ||
|
||
return $site_command; | ||
} | ||
|
||
protected function commandIsRunning(SiteCommand $site_command): bool | ||
{ | ||
return !isset($site_command->status) | ||
|| in_array($site_command->status, ['running', 'waiting']); | ||
} | ||
|
||
} |
This file contains 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,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of Laravel Harbor. | ||
* | ||
* (c) Mehran Rasulian <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Services\Forge\Pipeline; | ||
|
||
use App\Services\Forge\ForgeService; | ||
use App\Traits\Outputifier; | ||
use Closure; | ||
|
||
class ImportDatabaseFromSql | ||
{ | ||
use Outputifier; | ||
|
||
public function __invoke(ForgeService $service, Closure $next) | ||
{ | ||
if (!($file = $service->setting->dbImportSql)) { | ||
return $next($service); | ||
} | ||
|
||
if (!$service->siteNewlyMade && !$service->setting->dbImportOnDeployment) { | ||
return $next($service); | ||
} | ||
|
||
return $this->attemptImport($service, $next, $file); | ||
} | ||
|
||
public function attemptImport(ForgeService $service, Closure $next, string $file) | ||
{ | ||
$this->information(sprintf('Importing database from %s.', $file)); | ||
|
||
$content = $this->buildImportCommandContent($service, $file); | ||
|
||
$site_command = $service->waitForSiteCommand( | ||
$service->forge->executeSiteCommand( | ||
$service->setting->server, | ||
$service->site->id, | ||
['command' => $content] | ||
) | ||
); | ||
|
||
if ($site_command->status === 'failed') { | ||
$this->fail(sprintf('---> Database import failed with message: %s', $site_command->output)); | ||
return $next; | ||
|
||
} elseif ($site_command->status !== 'finished') { | ||
$this->fail('---> Database import did not finish in time.'); | ||
return $next; | ||
} | ||
|
||
return $next($service); | ||
} | ||
|
||
public function buildImportCommandContent(ForgeService $service, string $file): string | ||
{ | ||
$extract = match(pathinfo($file, PATHINFO_EXTENSION)) { | ||
'gz' => "gunzip < {$file}", | ||
'zip' => "unzip -p {$file}", | ||
default => "cat {$file}" | ||
}; | ||
|
||
return implode(' ', [ | ||
$extract, | ||
'|', | ||
$this->buildDatabaseConnection($service) | ||
]); | ||
} | ||
|
||
protected function buildDatabaseConnection(ForgeService $service): string | ||
{ | ||
if (str_contains($service->server->databaseType, 'postgres')) { | ||
return sprintf( | ||
'pgsql postgres://%s:%s%s/%s', | ||
$service->database['DB_USERNAME'], | ||
$service->database['DB_PASSWORD'], | ||
isset($service->database['DB_HOST']) | ||
? sprintf( | ||
'@%s:%s', | ||
$service->database['DB_HOST'], | ||
$service->database['DB_PORT'] ?? '5432', | ||
) | ||
: '', | ||
$service->getFormattedDatabaseName(), | ||
); | ||
} | ||
|
||
return sprintf( | ||
'%s -u %s -p%s %s %s %s', | ||
str_contains($service->server->databaseType, 'mariadb') ? 'mariadb' : 'mysql', | ||
$service->database['DB_USERNAME'], | ||
$service->database['DB_PASSWORD'], | ||
isset($service->database['DB_HOST']) ? '-h ' . $service->database['DB_HOST'] : '', | ||
isset($service->database['DB_PORT']) ? '-P ' . $service->database['DB_PORT'] : '', | ||
$service->getFormattedDatabaseName(), | ||
); | ||
} | ||
} |
This file contains 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,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of Laravel Harbor. | ||
* | ||
* (c) Mehran Rasulian <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Services\Forge\Pipeline; | ||
|
||
use App\Services\Forge\ForgeService; | ||
use App\Traits\Outputifier; | ||
use Closure; | ||
|
||
class SeedDatabase | ||
{ | ||
use Outputifier; | ||
|
||
public function __invoke(ForgeService $service, Closure $next) | ||
{ | ||
if (!($seeder = $service->setting->dbSeed)) { | ||
return $next($service); | ||
} | ||
|
||
if (!$service->siteNewlyMade) { | ||
return $next($service); | ||
} | ||
|
||
return $this->attemptSeed($service, $next, $seeder); | ||
} | ||
|
||
public function attemptSeed(ForgeService $service, Closure $next) | ||
{ | ||
$this->information(sprintf('Seeding database.')); | ||
|
||
$content = $this->buildImportCommandContent($service); | ||
|
||
$site_command = $service->waitForSiteCommand( | ||
$service->forge->executeSiteCommand( | ||
$service->setting->server, | ||
$service->site->id, | ||
['command' => $content] | ||
) | ||
); | ||
|
||
if ($site_command->status === 'failed') { | ||
$this->fail(sprintf('---> Database seed failed with message: %s', $site_command->output)); | ||
return $next; | ||
|
||
} elseif ($site_command->status !== 'finished') { | ||
$this->fail('---> Database seed did not finish in time.'); | ||
return $next; | ||
} | ||
|
||
return $next($service); | ||
} | ||
|
||
public function buildImportCommandContent(ForgeService $service): string | ||
{ | ||
$seeder = ''; | ||
if (is_string($service->setting->dbSeed)) { | ||
$seeder = sprintf( | ||
'--class=%s', | ||
$service->setting->dbSeed | ||
); | ||
} | ||
|
||
return trim(sprintf( | ||
'%s artisan db:seed %s', | ||
$this->phpExecutable($service->site->phpVersion ?? 'php'), | ||
$seeder | ||
)); | ||
} | ||
|
||
/** | ||
* Forge's phpVersion strings don't exactly map to the executable. | ||
* For example php83 corresponds to the php8.3 executable. | ||
* This workaround assumes no minor versions above 9! | ||
*/ | ||
protected function phpExecutable(string $phpVersion): string | ||
{ | ||
return preg_replace_callback('/\d$/', fn ($matches) => '.' . $matches[0], $phpVersion); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The database property from $service only gets filled on the first run. One option is to refetch the env keys to get the credentials. The TextToArray action class might be useful here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, ok. I’ll try to resolve that soon!
On a related note, I discovered that Forge only waits for 2 minutes before marking a command as failed. So if this feature gets merged, we should note that this may not work in larger datasets that take longer to import.