Model updates for Database Management in ACP

This commit is contained in:
Dane Everitt 2017-02-03 15:19:14 -05:00
parent 9c2d34d6e6
commit 96d3aa767f
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
7 changed files with 137 additions and 43 deletions

View file

@ -47,30 +47,18 @@ class DatabaseController extends Controller
public function getIndex(Request $request) public function getIndex(Request $request)
{ {
return view('admin.databases.index', [ return view('admin.databases.index', [
'databases' => Models\Database::select( 'databases' => Models\Database::with('server')->paginate(50),
'databases.*', 'hosts' => Models\DatabaseServer::select(
'database_servers.host as a_host',
'database_servers.port as a_port',
'servers.id as a_serverId',
'servers.name as a_serverName'
)->join('database_servers', 'database_servers.id', '=', 'databases.db_server')
->join('servers', 'databases.server_id', '=', 'servers.id')
->paginate(20),
'dbh' => Models\DatabaseServer::select(
'database_servers.*', 'database_servers.*',
'nodes.name as a_linkedNode',
DB::raw('(SELECT COUNT(*) FROM `databases` WHERE `databases`.`db_server` = database_servers.id) as c_databases') DB::raw('(SELECT COUNT(*) FROM `databases` WHERE `databases`.`db_server` = database_servers.id) as c_databases')
)->leftJoin('nodes', 'nodes.id', '=', 'database_servers.linked_node') )->with('node')->paginate(20),
->paginate(20),
]); ]);
} }
public function getNew(Request $request) public function getNew(Request $request)
{ {
return view('admin.databases.new', [ return view('admin.databases.new', [
'nodes' => Models\Node::select('nodes.id', 'nodes.name', 'locations.long as a_location') 'nodes' => Models\Node::all()->load('location'),
->join('locations', 'locations.id', '=', 'nodes.location')
->get(),
]); ]);
} }
@ -78,15 +66,17 @@ class DatabaseController extends Controller
{ {
try { try {
$repo = new DatabaseRepository; $repo = new DatabaseRepository;
$repo->add($request->except([ $repo->add($request->only([
'_token', 'name',
'host',
'port',
'username',
'password',
'linked_node',
])); ]));
Alert::success('Successfully added a new database server to the system.')->flash(); Alert::success('Successfully added a new database server to the system.')->flash();
return redirect()->route('admin.databases', [ return redirect()->route('admin.databases', ['tab' => 'tab_dbservers']);
'tab' => 'tab_dbservers',
]);
} catch (DisplayValidationException $ex) { } catch (DisplayValidationException $ex) {
return redirect()->route('admin.databases.new')->withErrors(json_decode($ex->getMessage()))->withInput(); return redirect()->route('admin.databases.new')->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (\Exception $ex) { } catch (\Exception $ex) {

View file

@ -55,7 +55,27 @@ class Database extends Model
* @var array * @var array
*/ */
protected $casts = [ protected $casts = [
'server' => 'integer', 'server_id' => 'integer',
'db_server' => 'integer', 'db_server' => 'integer',
]; ];
/**
* Gets the host database server associated with a database.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function host()
{
return $this->hasOne(DatabaseServer::class, 'id', 'db_server');
}
/**
* Gets the server associated with a database.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function server()
{
return $this->hasOne(Server::class, 'id', 'server_id');
}
} }

View file

@ -59,4 +59,14 @@ class DatabaseServer extends Model
'server_id' => 'integer', 'server_id' => 'integer',
'db_server' => 'integer', 'db_server' => 'integer',
]; ];
/**
* Gets the node associated with a database host.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function node()
{
return $this->hasOne(Node::class, 'id', 'linked_node');
}
} }

View file

@ -53,7 +53,7 @@ class Node extends Model
*/ */
protected $casts = [ protected $casts = [
'public' => 'integer', 'public' => 'integer',
'location' => 'integer', 'location_id' => 'integer',
'memory' => 'integer', 'memory' => 'integer',
'disk' => 'integer', 'disk' => 'integer',
'daemonListen' => 'integer', 'daemonListen' => 'integer',
@ -61,11 +61,34 @@ class Node extends Model
]; ];
/** /**
* Fields that are not mass assignable. * Fields that are mass assignable.
* *
* @var array * @var array
*/ */
protected $guarded = ['id', 'created_at', 'updated_at']; protected $fillable = [
'uuid',
'uuidShort',
'node_id',
'name',
'suspended',
'owner_id',
'memory',
'swap',
'disk',
'io',
'cpu',
'oom_disabled',
'allocation_id',
'service_id',
'option_id',
'pack_id',
'startup',
'daemonSecret',
'image',
'username',
'sftp_password',
'installed',
];
/** /**
* @var array * @var array
@ -193,4 +216,14 @@ class Node extends Model
return json_encode($config, $json_options); return json_encode($config, $json_options);
} }
/**
* Gets the location associated with a node.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function location()
{
return $this->hasOne(Location::class, 'id', 'location_id');
}
} }

View file

@ -0,0 +1,40 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateNodesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('nodes', function (Blueprint $table) {
$table->dropForeign('nodes_location_foreign');
$table->dropIndex('nodes_location_foreign');
$table->renameColumn('location', 'location_id');
$table->foreign('location_id')->references('id')->on('locations');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('nodes', function (Blueprint $table) {
$table->dropForeign('nodes_location_id_foreign');
$table->dropIndex('nodes_location_id_foreign');
$table->renameColumn('location_id', 'location');
$table->foreign('location')->references('id')->on('locations');
});
}
}

View file

@ -52,20 +52,21 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@foreach($databases as $db) @foreach($databases as $database)
<tr> <tr>
<td>{{ $db->a_serverName }}</td> <td>{{ $database->server->name }}</td>
<td>{{ $db->database }}</td> <td>{{ $database->database }}</td>
<td>{{ $db->username }} ({{ $db->remote }})</td> <td>{{ $database->username }} ({{ $database->remote }})</td>
<td><code>{{ $db->a_host }}:{{ $db->a_port }}</code></td> <?php $host = $hosts->where('id', $database->db_server)->first(); ?>
<td class="text-center"><a href="/admin/servers/view/{{ $db->a_serverId }}?tab=tab_database"><i class="fa fa-search"></i></a></td> <td><code>{{ $host->host }}:{{ $host->port }}</code></td>
<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> <td class="text-center"><a href="/admin/servers/view/{{ $database->server->id }}?tab=tab_database"><i class="fa fa-search"></i></a></td>
<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>
</tr> </tr>
@endforeach @endforeach
</tbody> </tbody>
</table> </table>
<div class="col-md-12 text-center"> <div class="col-md-12 text-center">
{{ $databases->appends('tab', 'tab_databases')->render() }} {{ $databases->appends(['tab' => 'tab_databases'])->links() }}
</div> </div>
</div> </div>
</div> </div>
@ -86,20 +87,20 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@foreach($dbh as $db) @foreach($hosts as $database)
<tr> <tr>
<td>{{ $db->name }}</td> <td>{{ $database->name }}</td>
<td><code>{{ $db->host }}:{{ $db->port }}</code></td> <td><code>{{ $database->host }}:{{ $database->port }}</code></td>
<td>{{ $db->username }}</td> <td>{{ $database->username }}</td>
<td class="text-center">{{ $db->c_databases }}</td> <td class="text-center">{{ $database->c_databases }}</td>
<td>@if(is_null($db->a_linkedNode))<em>unlinked</em>@else{{ $db->a_linkedNode }}@endif</td> <td>@if(is_null($database->node))<em>unlinked</em>@else{{ $database->node->name }}@endif</td>
<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> <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>
</tr> </tr>
@endforeach @endforeach
</tbody> </tbody>
</table> </table>
<div class="col-md-12 text-center"> <div class="col-md-12 text-center">
{{ $dbh->appends('tab', 'tab_dbservers')->render() }} {{ $hosts->appends('tab', 'tab_dbservers')->render() }}
</div> </div>
</div> </div>
</div> </div>

View file

@ -47,7 +47,7 @@
<select name="linked_node" class="form-control"> <select name="linked_node" class="form-control">
<option>None</option> <option>None</option>
@foreach($nodes as $node) @foreach($nodes as $node)
<option value="{{ $node->id }}" @if((int) old('linked_node') === $node->id) selected="selected" @endif>{{ $node->name }} ({{ $node->a_location }})</option> <option value="{{ $node->id }}" @if((int) old('linked_node') === $node->id) selected="selected" @endif>{{ $node->name }} ({{ $node->location->short }})</option>
@endforeach @endforeach
</select> </select>
<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> <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>