Michael Rusch
Michael Rusch

Reputation: 2617

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?

Upvotes: 6

Views: 2424

Answers (2)

Bogdan
Bogdan

Reputation: 44526

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

Upvotes: 8

Can Celik
Can Celik

Reputation: 2087

You can set a custom connection without any password in the config file and pass the password via CLI command to run the migrations.

In database.php config

'connection_without_password' => [
            'driver'    => 'mysql',
            'host'      => 'hostname,
            'database'  => 'database',
            'username'  => '$username',
            'password'  => '$password',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

Schema class includes a connection name option.

Schema::connection('connection_without_password')->create('users', function ($table) {
    $table->increments('id');
});

Upvotes: 0

Related Questions