Skip to content

Commit

Permalink
Improve explain on db_logging (#1898)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Jun 24, 2023
1 parent 7103c0b commit 94f670a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,16 @@ release_major: gen_major
TESTS_PHP := $(shell find tests/Feature -name "*Test.php" -printf "%f\n")
TEST_DONE := $(addprefix build/,$(TESTS_PHP:.php=.done))

build/%.done: tests/Feature/%.php
XDEBUG_MODE=coverage vendor/bin/phpunit --filter $* && touch build/$*.done
build:
mkdir build

build/Base%.done:
touch build/Base$*.done

build/%UnitTest.done:
touch build/$*UnitTest.done

build/%.done: tests/Feature/%.php build
vendor/bin/phpunit --no-coverage --filter $* && touch build/$*.done

all_tests: $(TEST_DONE)
25 changes: 22 additions & 3 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@

class AppServiceProvider extends ServiceProvider
{
/**
* Defines which queries to ignore when doing explain.
*
* @var array<int,string>
*/
private array $ignore_log_SQL =
[
'information_schema', // Not interesting

// We do not want infinite loops
'EXPLAIN',

// Way too noisy
'configs',
];

public array $singletons =
[
SymLinkFunctions::class => SymLinkFunctions::class,
Expand Down Expand Up @@ -151,8 +167,7 @@ private function logSQL(QueryExecuted $query): void
{
// Quick exit
if (
Str::contains(request()->getRequestUri(), 'logs', true) ||
Str::contains($query->sql, ['information_schema', 'EXPLAIN', 'configs'])
Str::contains(request()->getRequestUri(), 'logs', true)
) {
return;
}
Expand All @@ -161,7 +176,11 @@ private function logSQL(QueryExecuted $query): void
$msg = '(' . $query->time . 'ms) ' . $query->sql . ' [' . implode(', ', $query->bindings) . ']';

// For pgsql and sqlite we log the query and exit early
if (config('database.default', 'mysql') !== 'mysql' || config('database.explain', false) === false) {
if (config('database.default', 'mysql') !== 'mysql' ||
config('database.explain', false) === false ||
!Str::contains($query->sql, 'select') ||
Str::contains($query->sql, $this->ignore_log_SQL)
) {
Log::debug($msg);

return;
Expand Down
4 changes: 2 additions & 2 deletions database/migrations/2018_08_15_102039_move_albums.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public function up(): void
]);
}
} else {
Log::notice(__FUNCTION__ . ':' . __LINE__ . ' ' . env('DB_OLD_LYCHEE_PREFIX', '') . 'lychee_albums does not exist!');
Log::notice(__METHOD__ . ':' . __LINE__ . ' ' . env('DB_OLD_LYCHEE_PREFIX', '') . 'lychee_albums does not exist!');
}
} else {
Log::notice(__FUNCTION__ . ':' . __LINE__ . ' albums is not empty.');
Log::notice(__METHOD__ . ':' . __LINE__ . ' albums is not empty.');
}
}

Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_LOG_SQL" value="true"/>
<env name="DB_LOG_SQL_EXPLAIN" value="true"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/WebAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function setUp(): void
{
parent::setUp();
$this->setUpRequiresEmptyWebAuthnCredentials();
config(['app.url' => 'https://localhost']);
}

public function tearDown(): void
Expand Down

0 comments on commit 94f670a

Please sign in to comment.