Skip to content

Commit

Permalink
chore: upgrades to start-database.sh (#2038)
Browse files Browse the repository at this point in the history
  • Loading branch information
acharlop authored Jan 12, 2025
1 parent 6053b4d commit d0a710a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-hats-reflect.md
Original file line number Diff line number Diff line change
@@ -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
17 changes: 8 additions & 9 deletions cli/template/extras/start-database/mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand All @@ -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
Expand All @@ -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"
18 changes: 9 additions & 9 deletions cli/template/extras/start-database/postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand All @@ -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
Expand All @@ -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"

0 comments on commit d0a710a

Please sign in to comment.