Everything was working fine until I upgraded from from Laravel 5.3 to Laravel 5.4 I start getting this error:
>> testing.ALERT: Could not register permissions because SQLSTATE[HY000] General error: 1 no such table: permissions (SQL: select * from "permissions")
#0 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Database/Connection.php(607): Illuminate\Database\Connection->runQueryCallback('select * from "...', Array, Object(Closure))
#1 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Database/Connection.php(326): Illuminate\Database\Connection->run('select * from "...', Array, Object(Closure))
#2 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1718): Illuminate\Database\Connection->select('select * from "...', Array, true)
#3 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1703): Illuminate\Database\Query\Builder->runSelect()
#4 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(493): Illuminate\Database\Query\Builder->get(Array)
#5 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(477): Illuminate\Database\Eloquent\Builder->getModels(Array)
#6 /Users/mz/Codes/Projects/POMS/backend/api/vendor/spatie/laravel-permission/src/PermissionRegistrar.php(79): Illuminate\Database\Eloquent\Builder->get()
#7 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Cache/Repository.php(323): Spatie\Permission\PermissionRegistrar->Spatie\Permission\{closure}()
#8 /Users/mz/Codes/Projects/POMS/backend/api/vendor/spatie/laravel-permission/src/PermissionRegistrar.php(80): Illuminate\Cache\Repository->rememberForever('spatie.permissi...', Object(Closure))
#9 /Users/mz/Codes/Projects/POMS/backend/api/vendor/spatie/laravel-permission/src/PermissionRegistrar.php(46): Spatie\Permission\PermissionRegistrar->getPermissions()
#10 /Users/mz/Codes/Projects/POMS/backend/api/vendor/spatie/laravel-permission/src/PermissionServiceProvider.php(37): Spatie\Permission\PermissionRegistrar->registerPermissions()
#11 [internal function]: Spatie\Permission\PermissionServiceProvider->boot(Object(Spatie\Permission\PermissionRegistrar))
#12 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(28): call_user_func_array(Array, Array)
#13 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Support/helpers.php(912): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#14 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(86): value(Object(Closure))
#15 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(30): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#16 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Container/Container.php(524): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#17 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(762): Illuminate\Container\Container->call(Array)
#18 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(745): Illuminate\Foundation\Application->bootProvider(Object(Spatie\Permission\PermissionServiceProvider))
#19 [internal function]: Illuminate\Foundation\Application->Illuminate\Foundation\{closure}(Object(Spatie\Permission\PermissionServiceProvider), 44)
#20 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(746): array_walk(Array, Object(Closure))
#21 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php(17): Illuminate\Foundation\Application->boot()
#22 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(208): Illuminate\Foundation\Bootstrap\BootProviders->bootstrap(Object(Illuminate\Foundation\Application))
#23 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(270): Illuminate\Foundation\Application->bootstrapWith(Array)
#24 /Users/mz/Codes/Projects/POMS/backend/api/app/Port/Test/PHPUnit/Abstracts/TestCase.php(67): Illuminate\Foundation\Console\Kernel->bootstrap()
#25 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/browser-kit-testing/src/TestCase.php(95): App\Port\Test\PHPUnit\Abstracts\TestCase->createApplication()
#26 /Users/mz/Codes/Projects/POMS/backend/api/vendor/laravel/browser-kit-testing/src/TestCase.php(70): Laravel\BrowserKitTesting\TestCase->refreshApplication()
#27 /Users/mz/Codes/Projects/POMS/backend/api/app/Port/Test/PHPUnit/Abstracts/TestCase.php(34): Laravel\BrowserKitTesting\TestCase->setUp()
#28 /Users/mz/Codes/Projects/POMS/backend/api/vendor/phpunit/phpunit/src/Framework/TestCase.php(967): App\Port\Test\PHPUnit\Abstracts\TestCase->setUp()
#29 /Users/mz/Codes/Projects/POMS/backend/api/vendor/phpunit/phpunit/src/Framework/TestResult.php(709): PHPUnit_Framework_TestCase->runBare()
#30 /Users/mz/Codes/Projects/POMS/backend/api/vendor/phpunit/phpunit/src/Framework/TestCase.php(926): PHPUnit_Framework_TestResult->run(Object(App\Containers\Order\UI\API\Tests\Functional\ListAllStoreOrdersTest))
#31 /Users/mz/Codes/Projects/POMS/backend/api/vendor/phpunit/phpunit/src/Framework/TestSuite.php(728): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#32 /Users/mz/Codes/Projects/POMS/backend/api/vendor/phpunit/phpunit/src/Framework/TestSuite.php(728): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#33 /Users/mz/Codes/Projects/POMS/backend/api/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(521): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#34 /Users/mz/Codes/Projects/POMS/backend/api/vendor/phpunit/phpunit/src/TextUI/Command.php(188): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array, true)
#35 /Users/mz/Codes/Projects/POMS/backend/api/vendor/phpunit/phpunit/src/TextUI/Command.php(118): PHPUnit_TextUI_Command->run(Array, true)
#36 /Users/mz/Codes/Projects/POMS/backend/api/vendor/phpunit/phpunit/phpunit(52): PHPUnit_TextUI_Command::main()
#37 {main}
Using version: "spatie/laravel-permission": "1.8.0"
Configs:
config/database.php:
'testing_sqlite_in_memory' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
phpunit.xml:
<env name="DB_CONNECTION" value="testing_sqlite_in_memory"/>
--
Thanks Freek for this awesome package :)
Hi @Mahmoudz,
Are you sure you performed the necessary migrations before your test? As the error reads; the permission can't be created because the permissions table doesn't exist...
If you're sure the migrations have ran and the permissions table exists when the test is running please provide a simplified example that triggers the error. This way we can replicate the error as well.
Thanks!
Hi @AlexVanderbist, thanks for your reply.
I forget to mention that all my test are passing. But still getting the above alert logged before every single test. Only in the testing environment.
Also that's not related to upgrading Laravel, I just remembered that I used to have the logging line commented out, because I didn't know why I'm getting this in my Log while everything seems working fine. But now after updating my dependencies obviously the line is not commented out anymore.
Yes I am migrating the DB on the right time. The problem is internal in the package I guess! When registering the PermissionServiceProvider, the boot function calls registerPermissions which calls getPermissions() and this executes app(Permission::class)->with('roles')->get(); which executes this query select * from "permissions" in order to cache them..
But what I'm not getting is why executing a query during the service providers registration when migrations are not prepared!! maybe if the config file has enable_cache = false feature where we can turn it off could be a quick work around!
The exception that got logged used to be much shorter: Could not register permissions
It was easy to ignore then. Now that its a full stack trace for every test. It makes the log pretty awful to read during TDD.
Could this be changed to not Log the error? Maybe throw a custom Exception so whoever is using the package can handle it instead of sending it straight to logs. Is something like this possible?
https://github.com/scottgrayson/laravel-permission/commit/bcfb46acd160661a08b7673291c8a3f1d873ab06
Right, I can see how this is annoying when testing. Personally I like the enable_cache flag approach but I'll check with @freekmurze on monday. Thanks!
All permission are being read primarly to register them at Laravel's gate.
I get that the long log message can be a bit annoying. Maybe we can introduce a setting in the config file that enables/disables that logging of the exception.
Thoughts?
A config option would solve the issue for me
@AlexVanderbist can you create a fully tested PR that add the config option. Default can be logging enabled. To ensure backwards compatibility assume true when the option does not exist. Thanks!
Thanks for the PR, @Mahmoudz ! I've just added some tests to it in a new PR.
Most helpful comment
All permission are being read primarly to register them at Laravel's gate.
I get that the long log message can be a bit annoying. Maybe we can introduce a setting in the config file that enables/disables that logging of the exception.
Thoughts?