2016-02-08 18:03:02 -05:00
|
|
|
<?php
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Models;
|
2016-02-08 18:03:02 -05:00
|
|
|
|
2025-01-30 16:39:00 -05:00
|
|
|
use App\Contracts\Validatable;
|
|
|
|
|
use App\Traits\HasValidation;
|
2025-09-24 13:34:19 +02:00
|
|
|
use Carbon\CarbonImmutable;
|
2025-04-23 16:02:08 +02:00
|
|
|
use Illuminate\Database\Connection;
|
2025-09-24 13:34:19 +02:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2025-01-30 16:39:00 -05:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-12-06 20:24:30 -05:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2022-10-14 10:59:20 -06:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2025-04-23 16:02:08 +02:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2022-10-14 10:59:20 -06:00
|
|
|
|
2021-01-23 12:09:16 -08:00
|
|
|
/**
|
2021-01-27 20:52:11 -08:00
|
|
|
* @property int $id
|
|
|
|
|
* @property string $name
|
|
|
|
|
* @property string $host
|
|
|
|
|
* @property int $port
|
|
|
|
|
* @property string $username
|
|
|
|
|
* @property string $password
|
|
|
|
|
* @property int|null $max_databases
|
|
|
|
|
* @property int|null $node_id
|
2025-09-08 13:12:33 -04:00
|
|
|
* @property CarbonImmutable $created_at
|
|
|
|
|
* @property CarbonImmutable $updated_at
|
|
|
|
|
* @property Collection|Node[] $nodes
|
2024-12-12 18:26:37 +01:00
|
|
|
* @property int|null $nodes_count
|
2025-09-08 13:12:33 -04:00
|
|
|
* @property Collection|Database[] $databases
|
2024-12-12 18:26:37 +01:00
|
|
|
* @property int|null $databases_count
|
2021-01-23 12:09:16 -08:00
|
|
|
*/
|
2025-01-30 16:39:00 -05:00
|
|
|
class DatabaseHost extends Model implements Validatable
|
2016-02-08 18:03:02 -05:00
|
|
|
{
|
2025-01-30 16:39:00 -05:00
|
|
|
use HasFactory;
|
|
|
|
|
use HasValidation;
|
|
|
|
|
|
2018-01-25 21:26:06 -06:00
|
|
|
/**
|
|
|
|
|
* The resource name for this model when it is transformed into an
|
2024-11-06 09:09:10 +01:00
|
|
|
* API representation using fractal. Also used as name for api key permissions.
|
2018-01-25 21:26:06 -06:00
|
|
|
*/
|
2021-01-23 12:33:34 -08:00
|
|
|
public const RESOURCE_NAME = 'database_host';
|
2018-01-25 21:26:06 -06:00
|
|
|
|
2016-02-08 18:03:02 -05:00
|
|
|
/**
|
|
|
|
|
* The attributes excluded from the model's JSON form.
|
|
|
|
|
*/
|
|
|
|
|
protected $hidden = ['password'];
|
|
|
|
|
|
|
|
|
|
/**
|
2017-03-16 19:35:29 -04:00
|
|
|
* Fields that are mass assignable.
|
2016-02-08 18:03:02 -05:00
|
|
|
*/
|
2017-03-16 19:35:29 -04:00
|
|
|
protected $fillable = [
|
2024-12-06 20:24:30 -05:00
|
|
|
'name', 'host', 'port', 'username', 'password', 'max_databases',
|
2017-03-16 19:35:29 -04:00
|
|
|
];
|
2016-02-08 18:03:02 -05:00
|
|
|
|
2025-03-08 19:56:06 -05:00
|
|
|
/** @var array<array-key, string[]> */
|
2022-10-14 10:59:20 -06:00
|
|
|
public static array $validationRules = [
|
2025-03-08 19:56:06 -05:00
|
|
|
'name' => ['required', 'string', 'max:255'],
|
|
|
|
|
'host' => ['required', 'string'],
|
|
|
|
|
'port' => ['required', 'numeric', 'between:1,65535'],
|
|
|
|
|
'username' => ['required', 'string', 'max:32'],
|
|
|
|
|
'password' => ['nullable', 'string'],
|
|
|
|
|
'node_ids' => ['nullable', 'array'],
|
2025-11-10 12:25:58 +01:00
|
|
|
'node_ids.*' => ['required', 'integer', 'exists:nodes,id'],
|
2017-06-17 19:48:31 -05:00
|
|
|
];
|
2017-04-13 23:19:01 -04:00
|
|
|
|
2024-03-19 21:08:49 -04:00
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'id' => 'integer',
|
|
|
|
|
'max_databases' => 'integer',
|
2024-05-28 15:24:20 +02:00
|
|
|
'password' => 'encrypted',
|
2024-03-19 21:08:49 -04:00
|
|
|
'created_at' => 'immutable_datetime',
|
|
|
|
|
'updated_at' => 'immutable_datetime',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-06 20:24:30 -05:00
|
|
|
public function nodes(): BelongsToMany
|
2017-03-19 19:36:50 -04:00
|
|
|
{
|
2024-12-06 20:24:30 -05:00
|
|
|
return $this->belongsToMany(Node::class);
|
2017-03-19 19:36:50 -04:00
|
|
|
}
|
2017-02-09 17:43:54 -05:00
|
|
|
|
2017-03-19 19:36:50 -04:00
|
|
|
/**
|
2018-05-13 10:50:56 -04:00
|
|
|
* Gets the databases associated with this host.
|
2017-03-19 19:36:50 -04:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function databases(): HasMany
|
2017-03-19 19:36:50 -04:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(Database::class);
|
|
|
|
|
}
|
2025-04-23 16:02:08 +02:00
|
|
|
|
|
|
|
|
public function buildConnection(string $database = 'mysql', string $charset = 'utf8', string $collation = 'utf8_unicode_ci'): Connection
|
|
|
|
|
{
|
|
|
|
|
/** @var Connection $connection */
|
|
|
|
|
$connection = DB::build([
|
|
|
|
|
'driver' => 'mysql',
|
|
|
|
|
'host' => $this->host,
|
|
|
|
|
'port' => $this->port,
|
|
|
|
|
'database' => $database,
|
|
|
|
|
'username' => $this->username,
|
|
|
|
|
'password' => $this->password,
|
|
|
|
|
'charset' => $charset,
|
|
|
|
|
'collation' => $collation,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return $connection;
|
|
|
|
|
}
|
2016-02-08 18:03:02 -05:00
|
|
|
}
|