Skip to content

Commit

Permalink
reverse proxy shared db creation
Browse files Browse the repository at this point in the history
  • Loading branch information
fabio-ivona committed Dec 15, 2023
1 parent 837385a commit 85ecbf8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
57 changes: 57 additions & 0 deletions app/Containers/Commands/MakeDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php /** @noinspection LaravelFunctionsInspection */

/** @noinspection PhpUnhandledExceptionInspection */

namespace App\Containers\Commands;

use App\Services\DockerService;
use App\Services\TerminalService;
use LaravelZero\Framework\Commands\Command;

class MakeDatabase extends Command
{
protected $signature = 'make:database
{database_name?} : database name';
protected $description = 'Create a new database';

public function handle(DockerService $docker_service, TerminalService $terminal)
{
$this->title('New DB Creation');

$dbname = $this->argument('database_name') ?? $this->ask('Database Name:');

throw_if(empty($dbname), 'DB Name is required');

$dbuser = $this->ask('Database User Name', 'dbuser');
$dbpassword = $this->ask('Database User Password', 'dbpassword');

if (!$this->execute_mysql_command($docker_service, $terminal, "create database $dbname")) {
$this->error('Database creation failed');
return self::FAILURE;
}

if (!$this->execute_mysql_command($docker_service, $terminal, "create user '$dbuser'@'docker' identified by '$dbpassword'")) {
$this->error('User creation failed');
return self::FAILURE;
}

if (!$this->execute_mysql_command($docker_service, $terminal, "grant all privileges on '$dbname'.* to '$dbuser'@'docker' with grant option")) {
$this->error('User permission setup failed');
return self::FAILURE;
}

return self::SUCCESS;
}

private function execute_mysql_command(DockerService $docker_service, TerminalService $terminal_service, string $command): int
{
$password = env('MYSQL_ROOT_PASSWORD');

$command = [
"mysql -u root -p$password",
"-e \"$command\"",
];

return $docker_service->service('mysql')->run($terminal_service, $command) === 0;
}
}
9 changes: 9 additions & 0 deletions app/Containers/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,13 @@ public function __construct()
$this->disable_strict_mode();
}
}

public function commands(): array
{
return [

];
}


}

0 comments on commit 85ecbf8

Please sign in to comment.