thedevdojo/larasail

Add option to automatically create database

bobbyiliev opened this issue ยท 4 comments

Add an option to automatically create a new database, a database user and possibly update the database details in the .env file for a specific project

Hello @bobbyiliev! I would like to take this issue for the hacktoberfest event.

I want to clarify implementation: since the password would be placed inside .env file anyway, maybe it is worth removing mysql password temporary file right after setup is finished?

Hey @lexuzieel,

Yes feel free to take this one!

The MySQL password that is stored at the temporary file is the root MySQL password and the one that should be used at the .env file should be different for security reasons.

Here's an example function that I usually use:

function create_database_details() {
    password=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 14 | head -n 1)
    mysql -e "CREATE DATABASE some_database_name_here DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;"
    mysql -e "CREATE USER 'some_laravel_database_user_here'@'%' IDENTIFIED WITH mysql_native_password BY '$password';"
    mysql -e "GRANT ALL ON some_database_name_here.* TO 'some_laravel_database_user_here'@'%';"
}

It would need to be tweaked a bit so generate a db username and database name rather than the some_laravel_database_user_here and some_database_name_here.

Let me know how it goes!

@bobbyiliev during the development of this command I had a couple improvements and commands as a by-product:

  1. Launch help without supplying -h flag
  2. larasail enter - launch shell as larasail (basically su - larasail)
  3. larasail new <project-name> - that creates a new laravel project in /var/www directory and automatically sets up the host

If you wish, I can create a separate PR for those.

Great job on that @lexuzieel! ๐Ÿ™Œ Thank you for your contribution! I added just 1 comment to the PR.

Yes, that sounds like a really good idea, just one remark, maybe also allow people to specify --jet so that they could include Laravel Jetstream if they wanted to, so it could look something like this:

larasail new <project-name> --jet

Let me know what you think!