From 96d3aa767f297d242469319fb16f7f4161fcfd6d Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 3 Feb 2017 15:19:14 -0500 Subject: [PATCH] Model updates for Database Management in ACP --- .../Controllers/Admin/DatabaseController.php | 34 ++++++---------- app/Models/Database.php | 22 +++++++++- app/Models/DatabaseServer.php | 10 +++++ app/Models/Node.php | 39 ++++++++++++++++-- .../2017_02_03_140948_UpdateNodesTable.php | 40 +++++++++++++++++++ .../views/admin/databases/index.blade.php | 33 +++++++-------- resources/views/admin/databases/new.blade.php | 2 +- 7 files changed, 137 insertions(+), 43 deletions(-) create mode 100644 database/migrations/2017_02_03_140948_UpdateNodesTable.php diff --git a/app/Http/Controllers/Admin/DatabaseController.php b/app/Http/Controllers/Admin/DatabaseController.php index 4a7fc3631..6cfa73041 100644 --- a/app/Http/Controllers/Admin/DatabaseController.php +++ b/app/Http/Controllers/Admin/DatabaseController.php @@ -47,30 +47,18 @@ class DatabaseController extends Controller public function getIndex(Request $request) { return view('admin.databases.index', [ - 'databases' => Models\Database::select( - 'databases.*', - '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( + 'databases' => Models\Database::with('server')->paginate(50), + 'hosts' => Models\DatabaseServer::select( 'database_servers.*', - 'nodes.name as a_linkedNode', DB::raw('(SELECT COUNT(*) FROM `databases` WHERE `databases`.`db_server` = database_servers.id) as c_databases') - )->leftJoin('nodes', 'nodes.id', '=', 'database_servers.linked_node') - ->paginate(20), + )->with('node')->paginate(20), ]); } public function getNew(Request $request) { return view('admin.databases.new', [ - 'nodes' => Models\Node::select('nodes.id', 'nodes.name', 'locations.long as a_location') - ->join('locations', 'locations.id', '=', 'nodes.location') - ->get(), + 'nodes' => Models\Node::all()->load('location'), ]); } @@ -78,15 +66,17 @@ class DatabaseController extends Controller { try { $repo = new DatabaseRepository; - $repo->add($request->except([ - '_token', + $repo->add($request->only([ + 'name', + 'host', + 'port', + 'username', + 'password', + 'linked_node', ])); - Alert::success('Successfully added a new database server to the system.')->flash(); - return redirect()->route('admin.databases', [ - 'tab' => 'tab_dbservers', - ]); + return redirect()->route('admin.databases', ['tab' => 'tab_dbservers']); } catch (DisplayValidationException $ex) { return redirect()->route('admin.databases.new')->withErrors(json_decode($ex->getMessage()))->withInput(); } catch (\Exception $ex) { diff --git a/app/Models/Database.php b/app/Models/Database.php index 99e768c29..c316d6e0a 100644 --- a/app/Models/Database.php +++ b/app/Models/Database.php @@ -55,7 +55,27 @@ class Database extends Model * @var array */ protected $casts = [ - 'server' => 'integer', + 'server_id' => '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'); + } } diff --git a/app/Models/DatabaseServer.php b/app/Models/DatabaseServer.php index 74fd88694..7713f59bf 100644 --- a/app/Models/DatabaseServer.php +++ b/app/Models/DatabaseServer.php @@ -59,4 +59,14 @@ class DatabaseServer extends Model 'server_id' => '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'); + } } diff --git a/app/Models/Node.php b/app/Models/Node.php index 8e82ef935..0c4d8fb3c 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -53,7 +53,7 @@ class Node extends Model */ protected $casts = [ 'public' => 'integer', - 'location' => 'integer', + 'location_id' => 'integer', 'memory' => 'integer', 'disk' => 'integer', 'daemonListen' => 'integer', @@ -61,11 +61,34 @@ class Node extends Model ]; /** - * Fields that are not mass assignable. + * Fields that are mass assignable. * * @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 @@ -193,4 +216,14 @@ class Node extends Model 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'); + } } diff --git a/database/migrations/2017_02_03_140948_UpdateNodesTable.php b/database/migrations/2017_02_03_140948_UpdateNodesTable.php new file mode 100644 index 000000000..4408e612b --- /dev/null +++ b/database/migrations/2017_02_03_140948_UpdateNodesTable.php @@ -0,0 +1,40 @@ +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'); + }); + } +} diff --git a/resources/views/admin/databases/index.blade.php b/resources/views/admin/databases/index.blade.php index f45e70192..bb7be1e3f 100644 --- a/resources/views/admin/databases/index.blade.php +++ b/resources/views/admin/databases/index.blade.php @@ -52,20 +52,21 @@ - @foreach($databases as $db) + @foreach($databases as $database) - {{ $db->a_serverName }} - {{ $db->database }} - {{ $db->username }} ({{ $db->remote }}) - {{ $db->a_host }}:{{ $db->a_port }} - - + {{ $database->server->name }} + {{ $database->database }} + {{ $database->username }} ({{ $database->remote }}) + where('id', $database->db_server)->first(); ?> + {{ $host->host }}:{{ $host->port }} + + @endforeach
- {{ $databases->appends('tab', 'tab_databases')->render() }} + {{ $databases->appends(['tab' => 'tab_databases'])->links() }}
@@ -86,20 +87,20 @@ - @foreach($dbh as $db) + @foreach($hosts as $database) - {{ $db->name }} - {{ $db->host }}:{{ $db->port }} - {{ $db->username }} - {{ $db->c_databases }} - @if(is_null($db->a_linkedNode))unlinked@else{{ $db->a_linkedNode }}@endif - + {{ $database->name }} + {{ $database->host }}:{{ $database->port }} + {{ $database->username }} + {{ $database->c_databases }} + @if(is_null($database->node))unlinked@else{{ $database->node->name }}@endif + @endforeach
- {{ $dbh->appends('tab', 'tab_dbservers')->render() }} + {{ $hosts->appends('tab', 'tab_dbservers')->render() }}
diff --git a/resources/views/admin/databases/new.blade.php b/resources/views/admin/databases/new.blade.php index 24ff27007..c9625cd75 100644 --- a/resources/views/admin/databases/new.blade.php +++ b/resources/views/admin/databases/new.blade.php @@ -47,7 +47,7 @@

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.