From 23f9af36515f8e482b13acf94f27a84a89216be5 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 8 Jul 2021 10:49:33 +1200 Subject: [PATCH] Use NullDatabase and DatabaselessKernel Depends on https://github.com/silverstripe/silverstripe-framework/pull/10016 --- src/Plugin.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Plugin.php b/src/Plugin.php index 31c65eb..6aa475f 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -6,10 +6,15 @@ use Composer\EventDispatcher\EventSubscriberInterface; use Composer\Script\Event; use Composer\IO\IOInterface; +use ReflectionClass; use SilverStripe\Core\CoreKernel; +use SilverStripe\Core\DatabaselessKernel; use SilverStripe\GraphQL\Schema\Exception\EmptySchemaException; use SilverStripe\GraphQL\Schema\Schema; use SilverStripe\GraphQL\Schema\SchemaBuilder; +use SilverStripe\ORM\Connect\MySQLDatabase; +use SilverStripe\ORM\Connect\NullDatabase; +use SilverStripe\ORM\DB; class Plugin implements PluginInterface, EventSubscriberInterface { @@ -57,10 +62,24 @@ public function generateSchema(Event $event) return; } + // Throw an exception when any logic in this execution is attempting to perform a query. + // GraphQL code generation can happen on environments which don't have a valid database connection, + // for example in CI when preparing a deployment package. + $db = new NullDatabase(); + $db->setQueryErrorMessage('Database query detected during GraphQL code generation: %s'); + $db->setErrorMessage('Database activity detected during GraphQL code generation.'); + DB::set_conn($db); + // Not using sake because it creates a HTTPApplication through cli-script.php. // This would trigger middlewares assuming a HTTP request execution // (rather than CLI), which then connect to the database that might not be available during this build. - $kernel = new CoreKernel(BASE_PATH, false); + $kernel = new DatabaselessKernel(BASE_PATH); + + // Not booting error handling since it assumes a (faked) HTTP execution context + // through SilverStripe\Logging\HTTPOutputHandler, and can in some contexts fail + // because HTTP variables aren't defined (see cli-script.php). + $kernel->setBootErrorHandling(false); + try { // Any composer update can introduce new config statements that require a manifest flush. // Since there is no way to pass flush=1 through composer commands, the safest way is to always perform the flush.