Skip to content

Commit 96d3aa7

Browse files
committed
Model updates for Database Management in ACP
1 parent 9c2d34d commit 96d3aa7

File tree

7 files changed

+137
-43
lines changed

7 files changed

+137
-43
lines changed

app/Http/Controllers/Admin/DatabaseController.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,46 +47,36 @@ public function __construct()
4747
public function getIndex(Request $request)
4848
{
4949
return view('admin.databases.index', [
50-
'databases' => Models\Database::select(
51-
'databases.*',
52-
'database_servers.host as a_host',
53-
'database_servers.port as a_port',
54-
'servers.id as a_serverId',
55-
'servers.name as a_serverName'
56-
)->join('database_servers', 'database_servers.id', '=', 'databases.db_server')
57-
->join('servers', 'databases.server_id', '=', 'servers.id')
58-
->paginate(20),
59-
'dbh' => Models\DatabaseServer::select(
50+
'databases' => Models\Database::with('server')->paginate(50),
51+
'hosts' => Models\DatabaseServer::select(
6052
'database_servers.*',
61-
'nodes.name as a_linkedNode',
6253
DB::raw('(SELECT COUNT(*) FROM `databases` WHERE `databases`.`db_server` = database_servers.id) as c_databases')
63-
)->leftJoin('nodes', 'nodes.id', '=', 'database_servers.linked_node')
64-
->paginate(20),
54+
)->with('node')->paginate(20),
6555
]);
6656
}
6757

6858
public function getNew(Request $request)
6959
{
7060
return view('admin.databases.new', [
71-
'nodes' => Models\Node::select('nodes.id', 'nodes.name', 'locations.long as a_location')
72-
->join('locations', 'locations.id', '=', 'nodes.location')
73-
->get(),
61+
'nodes' => Models\Node::all()->load('location'),
7462
]);
7563
}
7664

7765
public function postNew(Request $request)
7866
{
7967
try {
8068
$repo = new DatabaseRepository;
81-
$repo->add($request->except([
82-
'_token',
69+
$repo->add($request->only([
70+
'name',
71+
'host',
72+
'port',
73+
'username',
74+
'password',
75+
'linked_node',
8376
]));
84-
8577
Alert::success('Successfully added a new database server to the system.')->flash();
8678

87-
return redirect()->route('admin.databases', [
88-
'tab' => 'tab_dbservers',
89-
]);
79+
return redirect()->route('admin.databases', ['tab' => 'tab_dbservers']);
9080
} catch (DisplayValidationException $ex) {
9181
return redirect()->route('admin.databases.new')->withErrors(json_decode($ex->getMessage()))->withInput();
9282
} catch (\Exception $ex) {

app/Models/Database.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,27 @@ class Database extends Model
5555
* @var array
5656
*/
5757
protected $casts = [
58-
'server' => 'integer',
58+
'server_id' => 'integer',
5959
'db_server' => 'integer',
6060
];
61+
62+
/**
63+
* Gets the host database server associated with a database.
64+
*
65+
* @return \Illuminate\Database\Eloquent\Relations\HasOne
66+
*/
67+
public function host()
68+
{
69+
return $this->hasOne(DatabaseServer::class, 'id', 'db_server');
70+
}
71+
72+
/**
73+
* Gets the server associated with a database.
74+
*
75+
* @return \Illuminate\Database\Eloquent\Relations\HasOne
76+
*/
77+
public function server()
78+
{
79+
return $this->hasOne(Server::class, 'id', 'server_id');
80+
}
6181
}

app/Models/DatabaseServer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,14 @@ class DatabaseServer extends Model
5959
'server_id' => 'integer',
6060
'db_server' => 'integer',
6161
];
62+
63+
/**
64+
* Gets the node associated with a database host.
65+
*
66+
* @return \Illuminate\Database\Eloquent\Relations\HasOne
67+
*/
68+
public function node()
69+
{
70+
return $this->hasOne(Node::class, 'id', 'linked_node');
71+
}
6272
}

app/Models/Node.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,42 @@ class Node extends Model
5353
*/
5454
protected $casts = [
5555
'public' => 'integer',
56-
'location' => 'integer',
56+
'location_id' => 'integer',
5757
'memory' => 'integer',
5858
'disk' => 'integer',
5959
'daemonListen' => 'integer',
6060
'daemonSFTP' => 'integer',
6161
];
6262

6363
/**
64-
* Fields that are not mass assignable.
64+
* Fields that are mass assignable.
6565
*
6666
* @var array
6767
*/
68-
protected $guarded = ['id', 'created_at', 'updated_at'];
68+
protected $fillable = [
69+
'uuid',
70+
'uuidShort',
71+
'node_id',
72+
'name',
73+
'suspended',
74+
'owner_id',
75+
'memory',
76+
'swap',
77+
'disk',
78+
'io',
79+
'cpu',
80+
'oom_disabled',
81+
'allocation_id',
82+
'service_id',
83+
'option_id',
84+
'pack_id',
85+
'startup',
86+
'daemonSecret',
87+
'image',
88+
'username',
89+
'sftp_password',
90+
'installed',
91+
];
6992

7093
/**
7194
* @var array
@@ -193,4 +216,14 @@ public function getConfigurationAsJson($pretty = false)
193216

194217
return json_encode($config, $json_options);
195218
}
219+
220+
/**
221+
* Gets the location associated with a node.
222+
*
223+
* @return \Illuminate\Database\Eloquent\Relations\HasOne
224+
*/
225+
public function location()
226+
{
227+
return $this->hasOne(Location::class, 'id', 'location_id');
228+
}
196229
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class UpdateNodesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('nodes', function (Blueprint $table) {
17+
$table->dropForeign('nodes_location_foreign');
18+
$table->dropIndex('nodes_location_foreign');
19+
20+
$table->renameColumn('location', 'location_id');
21+
$table->foreign('location_id')->references('id')->on('locations');
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::table('nodes', function (Blueprint $table) {
33+
$table->dropForeign('nodes_location_id_foreign');
34+
$table->dropIndex('nodes_location_id_foreign');
35+
36+
$table->renameColumn('location_id', 'location');
37+
$table->foreign('location')->references('id')->on('locations');
38+
});
39+
}
40+
}

resources/views/admin/databases/index.blade.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,21 @@
5252
</tr>
5353
</thead>
5454
<tbody>
55-
@foreach($databases as $db)
55+
@foreach($databases as $database)
5656
<tr>
57-
<td>{{ $db->a_serverName }}</td>
58-
<td>{{ $db->database }}</td>
59-
<td>{{ $db->username }} ({{ $db->remote }})</td>
60-
<td><code>{{ $db->a_host }}:{{ $db->a_port }}</code></td>
61-
<td class="text-center"><a href="/admin/servers/view/{{ $db->a_serverId }}?tab=tab_database"><i class="fa fa-search"></i></a></td>
62-
<td class="text-center"><a href="#" data-action="delete" data-type="delete" data-attr="{{ $db->id }}" class="text-danger"><i class="fa fa-trash-o"></i></a></td>
57+
<td>{{ $database->server->name }}</td>
58+
<td>{{ $database->database }}</td>
59+
<td>{{ $database->username }} ({{ $database->remote }})</td>
60+
<?php $host = $hosts->where('id', $database->db_server)->first(); ?>
61+
<td><code>{{ $host->host }}:{{ $host->port }}</code></td>
62+
<td class="text-center"><a href="/admin/servers/view/{{ $database->server->id }}?tab=tab_database"><i class="fa fa-search"></i></a></td>
63+
<td class="text-center"><a href="#" data-action="delete" data-type="delete" data-attr="{{ $database->id }}" class="text-danger"><i class="fa fa-trash-o"></i></a></td>
6364
</tr>
6465
@endforeach
6566
</tbody>
6667
</table>
6768
<div class="col-md-12 text-center">
68-
{{ $databases->appends('tab', 'tab_databases')->render() }}
69+
{{ $databases->appends(['tab' => 'tab_databases'])->links() }}
6970
</div>
7071
</div>
7172
</div>
@@ -86,20 +87,20 @@
8687
</tr>
8788
</thead>
8889
<tbody>
89-
@foreach($dbh as $db)
90+
@foreach($hosts as $database)
9091
<tr>
91-
<td>{{ $db->name }}</td>
92-
<td><code>{{ $db->host }}:{{ $db->port }}</code></td>
93-
<td>{{ $db->username }}</td>
94-
<td class="text-center">{{ $db->c_databases }}</td>
95-
<td>@if(is_null($db->a_linkedNode))<em>unlinked</em>@else{{ $db->a_linkedNode }}@endif</td>
96-
<td class="text-center"><a href="#" class="text-danger" data-action="delete" data-type="delete-server" data-attr="{{ $db->id }}"><i class="fa fa-trash-o"></i></a></td>
92+
<td>{{ $database->name }}</td>
93+
<td><code>{{ $database->host }}:{{ $database->port }}</code></td>
94+
<td>{{ $database->username }}</td>
95+
<td class="text-center">{{ $database->c_databases }}</td>
96+
<td>@if(is_null($database->node))<em>unlinked</em>@else{{ $database->node->name }}@endif</td>
97+
<td class="text-center"><a href="#" class="text-danger" data-action="delete" data-type="delete-server" data-attr="{{ $database->id }}"><i class="fa fa-trash-o"></i></a></td>
9798
</tr>
9899
@endforeach
99100
</tbody>
100101
</table>
101102
<div class="col-md-12 text-center">
102-
{{ $dbh->appends('tab', 'tab_dbservers')->render() }}
103+
{{ $hosts->appends('tab', 'tab_dbservers')->render() }}
103104
</div>
104105
</div>
105106
</div>

resources/views/admin/databases/new.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<select name="linked_node" class="form-control">
4848
<option>None</option>
4949
@foreach($nodes as $node)
50-
<option value="{{ $node->id }}" @if((int) old('linked_node') === $node->id) selected="selected" @endif>{{ $node->name }} ({{ $node->a_location }})</option>
50+
<option value="{{ $node->id }}" @if((int) old('linked_node') === $node->id) selected="selected" @endif>{{ $node->name }} ({{ $node->location->short }})</option>
5151
@endforeach
5252
</select>
5353
<p class="text-muted"><small>A linked node implies that this Database Server is running on that node and it will be auto-selected when adding a database to servers on that node.</small></p>

0 commit comments

Comments
 (0)