From 94f670ac8bb5ab60b9918946e3b9b7d315a0eba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Viguier?= Date: Sat, 24 Jun 2023 16:25:25 +0200 Subject: [PATCH] Improve explain on db_logging (#1898) --- Makefile | 13 ++++++++-- app/Providers/AppServiceProvider.php | 25 ++++++++++++++++--- .../2018_08_15_102039_move_albums.php | 4 +-- phpunit.xml | 1 + tests/Feature/WebAuthTest.php | 1 + 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 2e3e9618f44..17ae0a26169 100644 --- a/Makefile +++ b/Makefile @@ -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) \ No newline at end of file diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 1486eeb3a78..d5019441dcd 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -38,6 +38,22 @@ class AppServiceProvider extends ServiceProvider { + /** + * Defines which queries to ignore when doing explain. + * + * @var array + */ + 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, @@ -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; } @@ -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; diff --git a/database/migrations/2018_08_15_102039_move_albums.php b/database/migrations/2018_08_15_102039_move_albums.php index dab8fd4ecbb..b7b3b6f60a9 100644 --- a/database/migrations/2018_08_15_102039_move_albums.php +++ b/database/migrations/2018_08_15_102039_move_albums.php @@ -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.'); } } diff --git a/phpunit.xml b/phpunit.xml index de73c381d0b..e0f105d6cfc 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -28,6 +28,7 @@ + diff --git a/tests/Feature/WebAuthTest.php b/tests/Feature/WebAuthTest.php index c8dc3c86a69..2efd76347f5 100644 --- a/tests/Feature/WebAuthTest.php +++ b/tests/Feature/WebAuthTest.php @@ -30,6 +30,7 @@ public function setUp(): void { parent::setUp(); $this->setUpRequiresEmptyWebAuthnCredentials(); + config(['app.url' => 'https://localhost']); } public function tearDown(): void