Skip to content

Commit

Permalink
Merge pull request #167 from dshoreman/databases
Browse files Browse the repository at this point in the history
Add basic database listing

Adds a basic (but filterable) table with a list of databases so the Databases item in the main sidebar menu finally has a purpose.

To get the list we add a new Database class that wraps Doctrine's DBAL which can eventually also be used to list tables etc.

This also sets up the ground work for #146.
  • Loading branch information
dshoreman committed Oct 8, 2019
2 parents 0a9d4d8 + 895076c commit edf8b15
Show file tree
Hide file tree
Showing 11 changed files with 422 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ DB_PORT=3306
DB_DATABASE=servidor
DB_USERNAME=servidor
DB_PASSWORD=vagrant
DB_ADMIN_USER=servidor
DB_ADMIN_PASS=vagrant

BROADCAST_DRIVER=log
CACHE_DRIVER=file
Expand Down
44 changes: 44 additions & 0 deletions app/Database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Servidor;

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\MySqlSchemaManager;

class Database
{
/**
* @var Connection
*/
private $connection;

public function dbal(): MySqlSchemaManager
{
if (!$this->connection) {
$this->connect();
}

return $this->connection->getSchemaManager();
}

private function connect(): Connection
{
if (!$this->connection) {
$this->connection = DriverManager::getConnection([
'user' => config('database.dbal.user'),
'password' => config('database.dbal.password'),
'host' => config('database.connections.mysql.host'),
'driver' => 'pdo_mysql',
], new Configuration());
}

return $this->connection;
}

public function listDatabases(): array
{
return $this->dbal()->listDatabases();
}
}
20 changes: 20 additions & 0 deletions app/Http/Controllers/DatabaseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Servidor\Http\Controllers;

use Servidor\Database;

class DatabaseController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$db = new Database();

return $db->listDatabases();
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"type": "project",
"require": {
"php": "^7.3.0",
"doctrine/dbal": "^2.9",
"fideloper/proxy": "^4.0",
"laravel/framework": "5.7.*",
"laravel/passport": "^7.0",
Expand Down
233 changes: 232 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 12 additions & 32 deletions config/database.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

$default = (object) [
'username' => env('DB_USERNAME', 'servidor'),
'password' => env('DB_PASSWORD', 'vagrant'),
];

return [
/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -31,50 +36,25 @@
*/

'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],

'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'database' => env('DB_DATABASE', 'servidor'),
'username' => $default->username,
'password' => $default->password,
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],

'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],

'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
'dbal' => [
'user' => env('DB_ADMIN_USER', $default->username),
'password' => env('DB_ADMIN_PASS', $default->password),
],

/*
Expand Down

0 comments on commit edf8b15

Please sign in to comment.