aaronpk / Meetable

an event listing website
https://meetable.org
MIT License
125 stars 10 forks source link

Cannot run `php artisan migrate` on fresh install due to db access in MailConfigServiceProvider.php #179

Open abackstrom opened 2 months ago

abackstrom commented 2 months ago

I'm attempting to setup a local dev environment but the setup steps no longer work due to database access in MailConfigServiceProvider.php:

https://github.com/aaronpk/Meetable/blob/3b6a52c22a66fb790f7e698de28d8b63ddbe23d7/app/Providers/MailConfigServiceProvider.php#L26-L29

The provider attempts to access the database before it's set up, causing every artisan invocation to fail, including during composer install.

➜ luna Meetable git:(main =) php82 artisan

   Symfony\Component\HttpKernel\Exception\HttpException

  at vendor/laravel/framework/src/Illuminate/Foundation/Application.php:1147
    1143▕         if ($code == 404) {
    1144▕             throw new NotFoundHttpException($message);
    1145▕         }
    1146▕
  ➜ 1147▕         throw new HttpException($code, $message, null, $headers);
    1148▕     }
    1149▕
    1150▕     /**
    1151▕      * Register a terminating callback with the application.

      +1 vendor frames
  2   app/Exceptions/Handler.php:40
      abort()

      +3 vendor frames
  6   artisan:35
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

If we tell app/Exceptions/Handler.php to throw the original exception rather than raise an HttpException, we get:

➜ luna ^1 Meetable git:(main =) ✗ php82 artisan

   Illuminate\Database\QueryException

  SQLSTATE[HY000] [1049] Unknown database 'laravel' (SQL: select * from information_schema.tables where table_schema = laravel and table_name = settings and
table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

  i   Database name seems incorrect: You're using the default database name `laravel`. This database does not exist.

    Edit the `.env` file and use the correct database name in the `DB_DATABASE` key.
      https://laravel.com/docs/master/database#configuration

  1   [internal]:0
      Illuminate\Foundation\Application::Illuminate\Foundation\{closure}(Object(App\Providers\MailConfigServiceProvider))

      +2 vendor frames
  4   [internal]:0
      Illuminate\Foundation\Application::Illuminate\Foundation\{closure}(Object(App\Providers\MailConfigServiceProvider))

The trace for this exception is:

#0 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Database/Connection.php(720): Illuminate\Database\Connection->runQueryCallback('select * from i...', Array, Object(Closure))
#1 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Database/Connection.php(405): Illuminate\Database\Connection->run('select * from i...', Array, Object(Closure))
#2 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Database/Connection.php(392): Illuminate\Database\Connection->select('select * from i...', Array, false)
#3 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Database/Schema/MySqlBuilder.php(43): Illuminate\Database\Connection->selectFromWriteConnection('select * from i...', Array)
#4 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(338): Illuminate\Database\Schema\MySqlBuilder->hasTable('settings')
#5 /Users/annika/Herd/Meetable/app/Setting.php(19): Illuminate\Support\Facades\Facade::__callStatic('hasTable', Array)
#6 /Users/annika/Herd/Meetable/app/Providers/MailConfigServiceProvider.php(28): App\Setting::value('mail_from_addre...')
#7 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): App\Providers\MailConfigServiceProvider->boot()
#8 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#9 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#10 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#11 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Container/Container.php(661): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#12 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(939): Illuminate\Container\Container->call(Array)
#13 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(919): Illuminate\Foundation\Application->bootProvider(Object(App\Providers\MailConfigServiceProvider))
#14 [internal function]: Illuminate\Foundation\Application->Illuminate\Foundation\{closure}(Object(App\Providers\MailConfigServiceProvider), 27)
#15 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(917): array_walk(Array, Object(Closure))
#16 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php(17): Illuminate\Foundation\Application->boot()
#17 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(244): Illuminate\Foundation\Bootstrap\BootProviders->bootstrap(Object(Illuminate\Foundation\Application))
#18 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(383): Illuminate\Foundation\Application->bootstrapWith(Array)
#19 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(153): Illuminate\Foundation\Console\Kernel->bootstrap()
#20 /Users/annika/Herd/Meetable/artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 {main}#0 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(45): Illuminate\Foundation\Application->abort(550, '', Array)
#1 /Users/annika/Herd/Meetable/app/Exceptions/Handler.php(41): abort(550)
#2 /Users/annika/Herd/Meetable/vendor/nunomaduro/collision/src/Adapters/Laravel/ExceptionHandler.php(46): App\Exceptions\Handler->report(Object(Illuminate\Database\QueryException))
#3 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(454): NunoMaduro\Collision\Adapters\Laravel\ExceptionHandler->report(Object(Illuminate\Database\QueryException))
#4 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(157): Illuminate\Foundation\Console\Kernel->reportException(Object(Illuminate\Database\QueryException))
#5 /Users/annika/Herd/Meetable/artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#0 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(45): Illuminate\Foundation\Application->abort(550, '', Array)
#1 /Users/annika/Herd/Meetable/app/Exceptions/Handler.php(41): abort(550)
#2 /Users/annika/Herd/Meetable/vendor/nunomaduro/collision/src/Adapters/Laravel/ExceptionHandler.php(46): App\Exceptions\Handler->report(Object(Illuminate\Database\QueryException))
#3 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(454): NunoMaduro\Collision\Adapters\Laravel\ExceptionHandler->report(Object(Illuminate\Database\QueryException))
#4 /Users/annika/Herd/Meetable/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(157): Illuminate\Foundation\Console\Kernel->reportException(Object(Illuminate\Database\QueryException))
#5 /Users/annika/Herd/Meetable/artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#6 {main}
aaronpk commented 2 months ago

oh shoot, let me take a look...

aaronpk commented 2 months ago

Did you create the database first and add the config to .env? I just tried a fresh install and the migrate command seems to be working, although I get an error later on in the migrations.

What you found is the call to Setting::value which retrieves a setting from the database, however that function first checks if the settings table exists with a call to Schema::hasTable, and that function expects the database config to exist and be correct, even if the database table hasn't been created yet.

aaronpk commented 2 months ago

I just pushed a change that stops clobbering the exception when running in the console, so you should see the actual error message from the database now.

SQLSTATE[HY000] [1049] Unknown database 'laravel' means the database hasn't been created yet. I realized that step was not explicitly in the readme, so I also added that.