Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prompt for database password when running Laravel 5 migrations

Laravel migrations need to be run with a particularly high level of privilege, as they perform DDL operations. I would like to run migrations as a named user without storing the password anywhere. This will require prompting the user for their password when they run the migration from the command-line. I cannot find any way to achieve this via connection configuration.

Is there a way to achieve this via the connection configuration?

If not, is there a way to perform the migrations with a thin layer of custom code over the top to establish the database connection in a custom manner? e.g. write a script/artisan command that does the prompting, connects to the DB, and then delegates the rest to Laravel's existing migration code?

like image 972
Michael Rusch Avatar asked Mar 08 '16 19:03

Michael Rusch


People also ask

How can check mail and password from database in Laravel?

First, you'll need to find the User who is logging in based on email address or username or however you identify them, for example: $user = User::where('email', '=', '[email protected]')->first(); Then, you'll need to CHECK the hashed password, like so: Hash::check('INPUT PASSWORD', $user->password);

How does Laravel keep track of migrations?

Each migration file name contains a timestamp, which allows Laravel to determine the order of the migrations. If you would like to specify a custom output path for the generated migration, you may use the --path option when executing the make:migration command.

Which Artisan command is used to migrate a database?

To create a new migration, you can run the make:migration Artisan command and that will bootstrap a new class on your Laravel application, in the database/migrations folder.


1 Answers

If your requirement is to just ask the user for database credentials in order to run the migrations, then this can very easily be achieved by wrapping the artisan migrate command within another command that asks for appropriate credentials. So the steps below should suffice:

1. Create the new command:

artisan make:console MigratePrivilegedCommand --command="migrate:privileged"

2. Add the necessary code to handle user input and run the migrations in your new command class:

class MigratePrivilegedCommand extends Command
{
    protected $signature = 'migrate:privileged';
    protected $description = 'Run migrations as a priviledged database user.';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        // Set the username and password for the
        // database connection from user input
        config([
            'database.connections.mysql.username' => $this->ask('What is your username?'),
            'database.connections.mysql.password' => $this->secret('What is your password?')
        ]);

        // Call the migrate command and you're done    
        $this->call('migrate');
    }
}

3. Register the new command in App\Console\Kernel.php:

protected $commands = [
    ...
    \App\Console\Commands\MigratePrivilegedCommand::class,
];

And now you can run migrations that need privileged database credentials with this:

php artisan migrate:privileged
like image 114
Bogdan Avatar answered Sep 28 '22 07:09

Bogdan