Last active
September 2, 2022 17:27
-
-
Save Erth0/b44c03330a4ffd9de49bd6ee95e4b956 to your computer and use it in GitHub Desktop.
Creates a database if not exists based on laravel .env file and opens the database with the default application.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
createdb () { | |
[ ! -f .env ] && { echo "No .env file found."; exit 1; } | |
DB_CONNECTION=$(grep DB_CONNECTION .env | grep -v -e '^\s*#' | cut -d '=' -f 2-) | |
DB_HOST=$(grep DB_HOST .env | grep -v -e '^\s*#' | cut -d '=' -f 2-) | |
DB_PORT=$(grep DB_PORT .env | grep -v -e '^\s*#' | cut -d '=' -f 2-) | |
DB_DATABASE=$(grep DB_DATABASE .env | grep -v -e '^\s*#' | cut -d '=' -f 2-) | |
DB_USERNAME=$(grep DB_USERNAME .env | grep -v -e '^\s*#' | cut -d '=' -f 2-) | |
DB_PASSWORD=$(grep DB_PASSWORD .env | grep -v -e '^\s*#' | cut -d '=' -f 2-) | |
DB_URL="${DB_CONNECTION}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}" | |
if mysql -u $DB_USERNAME -p$DB_PASSWORD -e "use $DB_DATABASE"; | |
then | |
echo "Database \"${DB_DATABASE}\" already exists." | |
else | |
echo "Creating database \"${DB_DATABASE}\"." | |
mysql -u $DB_USERNAME -p$DB_PASSWORD -e "CREATE DATABASE IF NOT EXISTS ${DB_DATABASE};" | |
echo "Database \"${DB_DATABASE}\" created." | |
fi | |
echo "Opening ${DB_URL}" | |
open $DB_URL | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment