PostgreSQL Support (#4486)

Co-authored-by: Matthew Penner <matthew@pterodactyl.io>
This commit is contained in:
Lance Pioch 2022-11-25 15:29:04 -05:00 committed by GitHub
parent 21613fa602
commit 3bf5a71802
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
223 changed files with 912 additions and 1052 deletions

View file

@ -40,6 +40,11 @@ class ClientController extends ClientApiController
AllowedFilter::custom('*', new MultiFieldServerFilter()),
]);
$loweredBindings = collect($builder->getBindings())
->map(fn ($f, $key) => is_string($f) ? strtolower($f) : $f)
->all();
$builder->setBindings($loweredBindings);
$type = $request->input('type');
// Either return all the servers the user has access to because they are an admin `?type=admin` or
// just return all the servers the user has access to because they are the owner or a subuser of the

View file

@ -225,8 +225,8 @@ class Node extends Model
*/
public function isViable(int $memory, int $disk): bool
{
$memoryLimit = $this->memory * (1 + ($this->memory_overallocate / 100));
$diskLimit = $this->disk * (1 + ($this->disk_overallocate / 100));
$memoryLimit = $this->memory * (1.0 + ($this->memory_overallocate / 100.0));
$diskLimit = $this->disk * (1.0 + ($this->disk_overallocate / 100.0));
return ($this->sum_memory + $memory) <= $memoryLimit && ($this->sum_disk + $disk) <= $diskLimit;
}

View file

@ -76,7 +76,9 @@ use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification;
* @method static Builder|User whereUsername($value)
* @method static Builder|User whereUuid($value)
*
* @mixin \Eloquent
* @mixin \Barryvdh\LaravelIdeHelper\Eloquent
* @mixin \Illuminate\Database\Query\Builder
* @mixin \Illuminate\Database\Eloquent\Builder
*/
class User extends Model implements
AuthenticatableContract,

View file

@ -2,12 +2,12 @@
namespace Pterodactyl\Providers;
use View;
use Cache;
use Pterodactyl\Models;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\URL;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use Pterodactyl\Extensions\Themes\Theme;

View file

@ -14,13 +14,10 @@ class HashidsServiceProvider extends ServiceProvider
public function register()
{
$this->app->singleton(HashidsInterface::class, function () {
/** @var \Illuminate\Contracts\Config\Repository $config */
$config = $this->app['config'];
return new Hashids(
$config->get('hashids.salt', ''),
$config->get('hashids.length', 0),
$config->get('hashids.alphabet', 'abcdefghijkmlnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890')
config('hashids.salt', ''),
config('hashids.length', 0),
config('hashids.alphabet', 'abcdefghijkmlnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890')
);
});

View file

@ -41,7 +41,7 @@ use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register all of the repository bindings.
* Register all the repository bindings.
*/
public function register()
{

View file

@ -30,7 +30,7 @@ class RouteServiceProvider extends ServiceProvider
});
// This is needed to make use of the "resolveRouteBinding" functionality in the
// model. Without it you'll never trigger that logic flow thus resulting in a 404
// model. Without it, you'll never trigger that logic flow thus resulting in a 404
// error because we request databases with a HashID, and not with a normal ID.
Route::model('database', Database::class);

View file

@ -80,7 +80,7 @@ class SettingsServiceProvider extends ServiceProvider
if (in_array($key, self::$encrypted)) {
try {
$value = $encrypter->decrypt($value);
} catch (DecryptException $exception) {
} catch (DecryptException) {
}
}

View file

@ -40,14 +40,14 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
*/
protected function getDiscardableDedicatedAllocations(array $nodes = []): array
{
$query = Allocation::query()->selectRaw('CONCAT_WS("-", node_id, ip) as result');
$query = Allocation::query()->selectRaw('CONCAT_WS(\'-\', node_id, ip) as result');
if (!empty($nodes)) {
$query->whereIn('node_id', $nodes);
}
return $query->whereNotNull('server_id')
->groupByRaw('CONCAT(node_id, ip)')
->groupByRaw('result')
->get()
->pluck('result')
->toArray();
@ -89,7 +89,7 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
if (!empty($discard)) {
$query->whereNotIn(
$this->getBuilder()->raw('CONCAT_WS("-", node_id, ip)'),
$this->getBuilder()->raw('CONCAT_WS(\'-\', node_id, ip)'),
$discard
);
}

View file

@ -2,9 +2,12 @@
namespace Pterodactyl\Repositories\Eloquent;
use PDO;
use RuntimeException;
use Illuminate\Http\Request;
use Webmozart\Assert\Assert;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Pterodactyl\Repositories\Repository;
use Illuminate\Database\Eloquent\Builder;
@ -271,7 +274,17 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
return sprintf('(%s)', $grammar->parameterize($record));
})->implode(', ');
$statement = "insert ignore into $table ($columns) values $parameters";
$driver = DB::getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME);
switch ($driver) {
case 'mysql':
$statement = "insert ignore into $table ($columns) values $parameters";
break;
case 'pgsql':
$statement = "insert into $table ($columns) values $parameters on conflict do nothing";
break;
default:
throw new RuntimeException("Unsupported database driver \"$driver\" for insert ignore.");
}
return $this->getBuilder()->getConnection()->statement($statement, $bindings);
}

View file

@ -22,7 +22,7 @@ class NodeRepository extends EloquentRepository implements NodeRepositoryInterfa
public function getUsageStats(Node $node): array
{
$stats = $this->getBuilder()
->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk')
->selectRaw('COALESCE(SUM(servers.memory), 0) as sum_memory, COALESCE(SUM(servers.disk), 0) as sum_disk')
->join('servers', 'servers.node_id', '=', 'nodes.id')
->where('node_id', '=', $node->id)
->first();
@ -54,7 +54,7 @@ class NodeRepository extends EloquentRepository implements NodeRepositoryInterfa
public function getUsageStatsRaw(Node $node): array
{
$stats = $this->getBuilder()->select(
$this->getBuilder()->raw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk')
$this->getBuilder()->raw('COALESCE(SUM(servers.memory), 0) as sum_memory, COALESCE(SUM(servers.disk), 0) as sum_disk')
)->join('servers', 'servers.node_id', '=', 'nodes.id')->where('node_id', $node->id)->first();
return collect(['disk' => $stats->sum_disk, 'memory' => $stats->sum_memory])->mapWithKeys(function ($value, $key) use ($node) {
@ -143,7 +143,7 @@ class NodeRepository extends EloquentRepository implements NodeRepositoryInterfa
{
$instance = $this->getBuilder()
->select(['nodes.id', 'nodes.fqdn', 'nodes.scheme', 'nodes.daemon_token', 'nodes.daemonListen', 'nodes.memory', 'nodes.disk', 'nodes.memory_overallocate', 'nodes.disk_overallocate'])
->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory, IFNULL(SUM(servers.disk), 0) as sum_disk')
->selectRaw('COALESCE(SUM(servers.memory), 0) as sum_memory, COALESCE(SUM(servers.disk), 0) as sum_disk')
->leftJoin('servers', 'servers.node_id', '=', 'nodes.id')
->where('nodes.id', $node_id);

View file

@ -72,8 +72,8 @@ class FindViableNodesService
Assert::integer($this->memory, 'Memory usage must be an int, got %s');
$query = Node::query()->select('nodes.*')
->selectRaw('IFNULL(SUM(servers.memory), 0) as sum_memory')
->selectRaw('IFNULL(SUM(servers.disk), 0) as sum_disk')
->selectRaw('COALESCE(SUM(servers.memory), 0) as sum_memory')
->selectRaw('COALESCE(SUM(servers.disk), 0) as sum_disk')
->leftJoin('servers', 'servers.node_id', '=', 'nodes.id')
->where('nodes.public', 1);
@ -82,8 +82,8 @@ class FindViableNodesService
}
$results = $query->groupBy('nodes.id')
->havingRaw('(IFNULL(SUM(servers.memory), 0) + ?) <= (nodes.memory * (1 + (nodes.memory_overallocate / 100)))', [$this->memory])
->havingRaw('(IFNULL(SUM(servers.disk), 0) + ?) <= (nodes.disk * (1 + (nodes.disk_overallocate / 100)))', [$this->disk]);
->havingRaw('(COALESCE(SUM(servers.memory), 0) + ?) <= (nodes.memory * (1.0 + (nodes.memory_overallocate / 100.0)))', [$this->memory])
->havingRaw('(COALESCE(SUM(servers.disk), 0) + ?) <= (nodes.disk * (1.0 + (nodes.disk_overallocate / 100.0)))', [$this->disk]);
if (!is_null($page)) {
$results = $results->paginate($perPage ?? 50, ['*'], 'page', $page);