From d0a710a65095bc3145a92f13970937af625cb140 Mon Sep 17 00:00:00 2001 From: Avi Charlop Date: Sun, 12 Jan 2025 20:13:36 +0100 Subject: [PATCH] chore: upgrades to start-database.sh (#2038) --- .changeset/thin-hats-reflect.md | 5 +++++ cli/template/extras/start-database/mysql.sh | 17 ++++++++--------- cli/template/extras/start-database/postgres.sh | 18 +++++++++--------- 3 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 .changeset/thin-hats-reflect.md diff --git a/.changeset/thin-hats-reflect.md b/.changeset/thin-hats-reflect.md new file mode 100644 index 0000000000..386ff6715a --- /dev/null +++ b/.changeset/thin-hats-reflect.md @@ -0,0 +1,5 @@ +--- +"create-t3-app": patch +--- + +start-database.sh script gets DB_NAME and DB_CONTAINER_NAME based off of DATABASE_URL in .env diff --git a/cli/template/extras/start-database/mysql.sh b/cli/template/extras/start-database/mysql.sh index 731725c31c..65789a3c7b 100755 --- a/cli/template/extras/start-database/mysql.sh +++ b/cli/template/extras/start-database/mysql.sh @@ -9,7 +9,13 @@ # On Linux and macOS you can run this script directly - `./start-database.sh` -DB_CONTAINER_NAME="project1-mysql" +set -a +source .env + +DB_PASSWORD=$(echo "$DATABASE_URL" | awk -F':' '{print $3}' | awk -F'@' '{print $1}') +DB_PORT=$(echo "$DATABASE_URL" | awk -F':' '{print $4}' | awk -F'\/' '{print $1}') +DB_NAME=$(echo "$DATABASE_URL" | awk -F'/' '{print $4}') +DB_CONTAINER_NAME="$DB_NAME-postgres" if ! [ -x "$(command -v docker)" ]; then echo -e "Docker is not installed. Please install docker and try again.\nDocker install guide: https://docs.docker.com/engine/install/" @@ -32,13 +38,6 @@ if [ "$(docker ps -q -a -f name=$DB_CONTAINER_NAME)" ]; then exit 0 fi -# import env variables from .env -set -a -source .env - -DB_PASSWORD=$(echo "$DATABASE_URL" | awk -F':' '{print $3}' | awk -F'@' '{print $1}') -DB_PORT=$(echo "$DATABASE_URL" | awk -F':' '{print $4}' | awk -F'\/' '{print $1}') - if [ "$DB_PASSWORD" == "password" ]; then echo "You are using the default database password" read -p "Should we generate a random password for you? [y/N]: " -r REPLY @@ -54,6 +53,6 @@ fi docker run -d \ --name $DB_CONTAINER_NAME \ -e MYSQL_ROOT_PASSWORD="$DB_PASSWORD" \ - -e MYSQL_DATABASE=project1 \ + -e MYSQL_DATABASE="$DB_NAME" \ -p "$DB_PORT":3306 \ docker.io/mysql && echo "Database container '$DB_CONTAINER_NAME' was successfully created" diff --git a/cli/template/extras/start-database/postgres.sh b/cli/template/extras/start-database/postgres.sh index 378f19e23c..b162abdfe3 100755 --- a/cli/template/extras/start-database/postgres.sh +++ b/cli/template/extras/start-database/postgres.sh @@ -9,7 +9,14 @@ # On Linux and macOS you can run this script directly - `./start-database.sh` -DB_CONTAINER_NAME="project1-postgres" +# import env variables from .env +set -a +source .env + +DB_PASSWORD=$(echo "$DATABASE_URL" | awk -F':' '{print $3}' | awk -F'@' '{print $1}') +DB_PORT=$(echo "$DATABASE_URL" | awk -F':' '{print $4}' | awk -F'\/' '{print $1}') +DB_NAME=$(echo "$DATABASE_URL" | awk -F'/' '{print $4}') +DB_CONTAINER_NAME="$DB_NAME-postgres" if ! [ -x "$(command -v docker)" ]; then echo -e "Docker is not installed. Please install docker and try again.\nDocker install guide: https://docs.docker.com/engine/install/" @@ -32,13 +39,6 @@ if [ "$(docker ps -q -a -f name=$DB_CONTAINER_NAME)" ]; then exit 0 fi -# import env variables from .env -set -a -source .env - -DB_PASSWORD=$(echo "$DATABASE_URL" | awk -F':' '{print $3}' | awk -F'@' '{print $1}') -DB_PORT=$(echo "$DATABASE_URL" | awk -F':' '{print $4}' | awk -F'\/' '{print $1}') - if [ "$DB_PASSWORD" = "password" ]; then echo "You are using the default database password" read -p "Should we generate a random password for you? [y/N]: " -r REPLY @@ -55,6 +55,6 @@ docker run -d \ --name $DB_CONTAINER_NAME \ -e POSTGRES_USER="postgres" \ -e POSTGRES_PASSWORD="$DB_PASSWORD" \ - -e POSTGRES_DB=project1 \ + -e POSTGRES_DB="$DB_NAME" \ -p "$DB_PORT":5432 \ docker.io/postgres && echo "Database container '$DB_CONTAINER_NAME' was successfully created"