Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passport migrations ignore --database option #1370

Closed
magoun opened this issue Oct 22, 2020 · 14 comments
Closed

Passport migrations ignore --database option #1370

magoun opened this issue Oct 22, 2020 · 14 comments
Labels

Comments

@magoun
Copy link

magoun commented Oct 22, 2020

  • Passport Version: 9.3.2
  • Laravel Version: 7.28.3
  • PHP Version: 7.4.11
  • Database Driver & Version: MySQL 5.7.31

Description:

The included passport migrations no longer consider the --database flag passed to migrate on the command line, instead reading the default database from the connection specified in config/passport.

Steps To Reproduce:

On a new Laravel project with at two database connections in config/database (mysql and testing), run php artisan migrate:fresh then run php artisan migrate:fresh --database testing.

The second migration will fail, reporting that the oauth_auth_codes_table already exists, as it is accessing the mysql connection instead of the testing connection.

I believe this behavior was introduced in 555e02c on May9th.

@driesvints
Copy link
Member

I'm investigating this one with the team. Thanks for reporting 👍

@driesvints
Copy link
Member

We're gonna look at how we're going let the --database flag take over precedence here.

@tecbeast42
Copy link

I can reproduce this problem in laravel 8.

@driesvints
Copy link
Member

Yeah still haven't gotten to this I'm afraid. Have been knee deep into PHP 8 updates. Appreciating help with it.

@CaptainBart
Copy link

Also running into this issue. Is there a workaround (except running the tests on the default database) available to avoid this issue?

@driesvints
Copy link
Member

Not atm.

@magoun
Copy link
Author

magoun commented Dec 15, 2020

One workaround is to manually change the database config item to your testing database before running tests. If you have a database connection set up in Laravel called 'testing', edit the below section in config/passport.php:

After running tests, be sure to uncomment the production database setting and comment out the 'testing' line.

/*
    |--------------------------------------------------------------------------
    | Passport Storage Driver
    |--------------------------------------------------------------------------
    |
    | This configuration value allows you to customize the storage options
    | for Passport, such as the database connection that should be used
    | by Passport's internal database models which store tokens, etc.
    |
    */

    'storage' => [
        'database' => [
            'connection' => 'testing',
            // 'connection' => env('DB_CONNECTION', 'mysql'),
        ],
    ],

@CaptainBart
Copy link

I do something similar, but I prefer to change my .env file, since it won't be comitted to git. Locally I change the .env DB_DATABASE setting to the test database when I want to create/run unit tests and change it back when I'm done testing. For the build pipeline I've created a dedicated test .env file that is copied/renamed before the tests are run.

@micc83
Copy link

micc83 commented Jan 21, 2021

You can fix the issue publishing the config/passport.php file and setting storage.database.connection to null so that Schema::connection($name = null) will return the default db set by the --database option.

@derekmd
Copy link

derekmd commented Feb 19, 2021

@driesvints: A sensible fix might be to change the out-of-the-box connection value to null to make apps fallback to config('database.default') unless configured otherwise?

'connection' => env('DB_CONNECTION', 'mysql'),

The above copy and pasted env('DB_CONNECTION', 'mysql') string from file config/database.php just causes the MigrateCommand option --database to be ignored.

An update to https://laravel.com/docs/passport can be done for the second case below since a userland config-based fix exists. #1255 essentially moved Passport database configuration into the migration files which overrides the CLI --database argument when a config value exist. But now apps can control the Passport models' destination database for the two use cases:

  1. Passport models use the application's default database connection

    When the passport models share the same database as the rest of the app, a null value in config('passport.storage.database.connection') allows the command php artisan migrate --database=... to behave as before.

    config/passport.php

    return [
        // ...
        'storage' => [
            'database' => [
                'connection' => null, // this defaults to config('database.default')
            ],
        ],
    ];
  2. Passport models are stored in a database different from the application's default connection

    Instead of calling php artisan migrate --database=..., configure a new environment variable and add it to each of the app's .env.* files. So in test environments they can share the same database, but staging/production, etc. can have split databases.

    config/passport.php

    return [
        // ...
        'storage' => [
            'database' => [
                'connection' => env('PASSPORT_DB_CONNECTION'),
            ],
        ],
    ];

    .env

    PASSPORT_DB_CONNECTION=prod_oauth
    

    .env.dusk

    PASSPORT_DB_CONNECTION=dusk_testing
    

    .env.testing

    PASSPORT_DB_CONNECTION=testing
    

@driesvints
Copy link
Member

Thanks @derekmd for those insights.

I've decided to revert #1255 to properly fix this. Please see my reasoning on the PR: #1412

@driesvints
Copy link
Member

We've reverted those changes for the next major release of Passport. At the moment we don't have a date yet for when that'll be released.

@Tylerian
Copy link

The revert doesn't help for cases where the passport config is exported. Replacing the passport database connection defaults to null on the config file fixes it tho.

@hasnain-nisan
Copy link

You can fix the issue publishing the config/passport.php file and setting storage.database.connection to null so that Schema::connection($name = null) will return the default db set by the --database option.

Helped me a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants