Skip to content

Feature/db comments #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Craft;
use craft\cache\DbCache;
use craft\cloud\db\Command;
use craft\cloud\fs\TmpFs;
use craft\cloud\Helper as CloudHelper;
use craft\cloud\queue\SqsQueue;
Expand Down Expand Up @@ -38,6 +39,8 @@ public function getConfig(): array
$config['components']['session'] = $this->getSession();
}

$config['components']['db'] = $this->getDb();
$config['components']['db2'] = $this->getDb();
$config['components']['cache'] = $this->getCache();
$config['components']['queue'] = $this->getQueue();
$config['components']['assetManager'] = $this->getAssetManager();
Expand All @@ -46,6 +49,20 @@ public function getConfig(): array
return $config;
}

private function getDb(): \Closure
{
return function() {
$config = [
'commandMap' => [
'pgsql' => Command::class,
'mysql' => Command::class,
],
] + App::dbConfig();

return Craft::createObject($config);
};
}

private function getSession(): \Closure
{
return function() {
Expand Down
2 changes: 2 additions & 0 deletions src/StaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public function purgeTags(string|StaticCacheTag ...$tags): void

Craft::info(new PsrMessage('Purging tags', [
'tags' => $tags,
'backtrace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),
]), __METHOD__);

if ($isWebResponse) {
Expand Down Expand Up @@ -300,6 +301,7 @@ public function purgeUrlPrefixes(string ...$urlPrefixes): void

Craft::info(new PsrMessage('Purging URL prefixes', [
'urlPrefixes' => $urlPrefixes->all(),
'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),
]), __METHOD__);

// TODO: make sure we don't go over max header size
Expand Down
43 changes: 43 additions & 0 deletions src/db/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace craft\cloud\db;

use Illuminate\Support\Collection;

class Command extends \craft\db\Command
{
public function setSql($sql): Command
{
$sql = $sql ? self::addComment($sql) : $sql;
return parent::setSql($sql);
}

public static function addComment(string $sql): string
{
$url = parse_url($_SERVER['REQUEST_URI'] ?? '');
$fields = Collection::make(PHP_SAPI === 'cli' ? [
'args' => implode(' ', $_SERVER['argv'] ?? []),
] : [
'method' => $_SERVER['REQUEST_METHOD'] ?? null,
'path' => $url['path'],
'query' => $url['query'] ?? null,
])->map(function($value, $key) {
$key = urlencode($key);

// allow un-encoded forward slashes and spaces
$value = str_replace(['%2F', '+'], ['/', ' '], urlencode($value));

$maxLength = 100;
$value = strlen($value) > $maxLength
? substr($value, 0, $maxLength) . '...[truncated]'
: $value;

return $value
? sprintf("%s='%s'", $key, $value)
: null;
})->filter();
$comment = $fields->join(',');

return "/*$comment*/ $sql";
}
}
Loading