Skip to content

Commit

Permalink
Merge pull request #89 from shawnhooper/customize-db-name
Browse files Browse the repository at this point in the history
Fix: Sluggify the database name
  • Loading branch information
mehrancodes committed Mar 18, 2024
2 parents 3ae0ef3 + fb51fdc commit a277776
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 12 deletions.
4 changes: 1 addition & 3 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->app->setLocale('en');

$this->loadTranslationsFrom(base_path('lang'), 'harbor');
//
}

public function register(): void
Expand Down
9 changes: 8 additions & 1 deletion app/Services/Forge/ForgeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,20 @@ public function getFormattedDomainName(): string
);
}

public function getStandardizedBranchName(): string
public function getSiteIsolationUsername(): string
{
return GenerateStandardizedBranchName::run(
$this->getFormattedBranchName()
);
}

public function getFormattedDatabaseName(): string
{
return $this->setting->dbName ?? GenerateStandardizedBranchName::run(
$this->getFormattedBranchName()
);
}

public function siteNginxTemplate(): string
{
return $this->forge->siteNginxFile($this->setting->server, $this->site->id);
Expand Down
11 changes: 9 additions & 2 deletions app/Services/Forge/ForgeSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class ForgeSetting
'site_isolation_required' => ['boolean'],
'job_scheduler_required' => ['boolean'],
'db_creation_required' => ['boolean'],
'db_name' => ['nullable', 'string'],
'db_name' => ['nullable', 'string', 'regex:/^[a-zA-Z0-9_]+$/'],
'auto_source_required' => ['boolean'],
'ssl_required' => ['boolean'],
'wait_on_ssl' => ['boolean'],
Expand All @@ -230,7 +230,14 @@ public function __construct()

private function init(array $configurations): void
{
$validator = Validator::make($configurations, $this->validationRules);
$validator = Validator::make(
$configurations,
$this->validationRules,
[
'db_name.regex' => 'The :attribute must only contain letters, numbers, and underscores.'
],
['db_name' => 'FORGE_DB_NAME']
);

throw_if($validator->fails(), ValidationException::class, $validator->errors()->all());

Expand Down
3 changes: 2 additions & 1 deletion app/Services/Forge/Pipeline/CreateDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace App\Services\Forge\Pipeline;

use App\Actions\GenerateStandardizedBranchName;
use App\Services\Forge\ForgeService;
use App\Traits\Outputifier;
use Closure;
Expand All @@ -28,8 +29,8 @@ public function __invoke(ForgeService $service, Closure $next)
return $next($service);
}

$dbName = $service->getFormattedDatabaseName();
$dbPassword = Str::random(16);
$dbName = $service->setting->dbName ?? $service->getStandardizedBranchName();

if (! $this->databaseExists($service, $dbName)) {
$this->information('Creating database.');
Expand Down
2 changes: 1 addition & 1 deletion app/Services/Forge/Pipeline/OrCreateNewSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private function gatherSiteData(ForgeService $service): array
$this->information('---> Enabling site isolation.');

$data['isolated'] = true;
$data['username'] = $service->getStandardizedBranchName();
$data['username'] = $service->getSiteIsolationUsername();
}

return $data;
Expand Down
2 changes: 1 addition & 1 deletion app/Services/Forge/Pipeline/RemoveDatabaseUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RemoveDatabaseUser
public function __invoke(ForgeService $service, Closure $next)
{
foreach ($service->forge->databaseUsers($service->setting->server) as $databaseUser) {
if ($databaseUser->name === $service->getStandardizedBranchName()) {
if ($databaseUser->name === $service->getFormattedDatabaseName()) {
$this->information('Removing database with user.');

foreach ($databaseUser->databases as $database) {
Expand Down
12 changes: 12 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@

'timezone' => 'UTC',

/*
|--------------------------------------------------------------------------
| Application Locale Configuration
|--------------------------------------------------------------------------
|
| The application locale determines the default locale that will be used
| by the translation service provider. You are free to set this value
| to any of the locales which will be supported by the application.
|
*/
'locale' => 'en',

/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
Expand Down
8 changes: 5 additions & 3 deletions lang/en/valiadtion.php → resources/lang/en/valiadtion.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@
*/

'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
'db_name' => [
'regex' => 'The :attribute must only contain letters, numbers, and underscores.',
],
],

Expand All @@ -179,6 +179,8 @@
|
*/

'attributes' => [],
'attributes' => [
'db_name' => 'FORGE_DB_NAME'
],

];

0 comments on commit a277776

Please sign in to comment.