Skip to content

Commit cf93e8d

Browse files
committed
mkdb.sh: resolve DB compose paths from script; add mariadb mkdb.sh
- PostgreSQL mkdb uses POSTGRESQL_DIR from script location (works from apps/ or experiments/) - Detect service dir via pwd vs POSTGRESQL_DIR; require app .env before appending backup URL - Add MariaDB mkdb.sh with same cwd pattern and BACKUP_DATABASE_URLS (mysql://) - Document examples in postgresql and mariadb READMEs Made-with: Cursor
1 parent 3b993a1 commit cf93e8d

4 files changed

Lines changed: 154 additions & 21 deletions

File tree

apps/mariadb/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ cp .env.example .env
1313
# Edit .env with your settings
1414
```
1515

16+
Set **`MARIADB_ROOT_PASSWORD`** in `apps/mariadb/.env`; `bin/mkdb.sh` uses it to create application databases.
17+
18+
### Creating an application database
19+
20+
From **`bin/mkdb.sh`**: creates `APPLICATION_user`, `APPLICATION_db`, and a random password; optionally appends **`BACKUP_DATABASE_URLS`** (MySQL URL) to the app’s `.env`.
21+
22+
**Change directory to the application** under `apps/` or `experiments/`, then run:
23+
24+
```bash
25+
# from apps/someapp
26+
../mariadb/bin/mkdb.sh someapp
27+
28+
# from experiments/someapp
29+
../../apps/mariadb/bin/mkdb.sh someapp
30+
```
31+
32+
If you run the script **from** `apps/mariadb`, it only prints the `BACKUP_DATABASE_URLS` line for you to copy into the app’s `.env`.
33+
1634
## Usage
1735

1836
### Starting the service

apps/mariadb/bin/mkdb.sh

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/bin/bash
2+
3+
if [ "$#" -ne 1 ]; then
4+
echo "Usage: $0 <application name>"
5+
exit 1
6+
fi
7+
8+
if ! command -v pwgen >/dev/null 2>&1; then
9+
echo "pwgen not found, needed to generate password"
10+
echo "install using 'apt install pwgen'"
11+
exit 1
12+
fi
13+
14+
# Resolve paths from this script so it works when run from any app under apps/ or experiments/
15+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
16+
MARIADB_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
17+
COMPOSE_FILE="$MARIADB_DIR/docker-compose.yml"
18+
19+
export USER=$1_user
20+
export DATABASE=$1_db
21+
export PASSWORD=$(pwgen 24 1)
22+
23+
echo "Will create this database:"
24+
echo
25+
echo "database: ${DATABASE}"
26+
echo "username: ${USER}"
27+
echo "password: ${PASSWORD}"
28+
echo
29+
echo
30+
31+
# shellcheck source=/dev/null
32+
source "$MARIADB_DIR/.env"
33+
34+
if [ -z "${MARIADB_ROOT_PASSWORD}" ]; then
35+
echo "Error: MARIADB_ROOT_PASSWORD is not set in $MARIADB_DIR/.env" >&2
36+
exit 1
37+
fi
38+
39+
echo "Using MariaDB root from $MARIADB_DIR/.env"
40+
echo
41+
42+
echo "Checking if MariaDB container is running..."
43+
if ! docker compose -f "$COMPOSE_FILE" ps mariadb | grep -q "Up"; then
44+
echo "Error: MariaDB container is not running"
45+
echo "Please start it with: docker compose -f $COMPOSE_FILE up -d mariadb"
46+
exit 1
47+
fi
48+
echo "✓ MariaDB container is running"
49+
echo
50+
51+
echo "1. creating user, database, and grants"
52+
echo " docker compose -f $COMPOSE_FILE exec -T mariadb mariadb -u root -p*** ..."
53+
if docker compose -f "$COMPOSE_FILE" exec -T mariadb mariadb -u root -p"${MARIADB_ROOT_PASSWORD}" << EOF
54+
CREATE USER IF NOT EXISTS '${USER}'@'%' IDENTIFIED BY '${PASSWORD}';
55+
CREATE DATABASE IF NOT EXISTS \`${DATABASE}\`;
56+
GRANT ALL PRIVILEGES ON \`${DATABASE}\`.* TO '${USER}'@'%';
57+
FLUSH PRIVILEGES;
58+
EOF
59+
then
60+
echo " ✓ User, database, and grants applied"
61+
else
62+
echo " ✗ Failed to create user/database or apply grants"
63+
exit 1
64+
fi
65+
66+
echo "2. adding database URL for backups to .env"
67+
68+
export BACKUP_DATABASE_URLS="BACKUP_DATABASE_URLS=mysql://${USER}:${PASSWORD}@mariadb:3306/${DATABASE}"
69+
70+
APP_CWD=$(pwd -P)
71+
if [ "$APP_CWD" = "$MARIADB_DIR" ]; then
72+
echo "You are in the MariaDB service directory. Add or replace BACKUP_DATABASE_URLS in your application's .env file:"
73+
echo "${BACKUP_DATABASE_URLS}"
74+
else
75+
if [ ! -f .env ]; then
76+
echo "No .env in $(pwd); create it from .env.example, then add:" >&2
77+
echo "${BACKUP_DATABASE_URLS}" >&2
78+
elif grep -qF BACKUP_DATABASE_URLS .env 2>/dev/null; then
79+
echo ".env already has BACKUP_DATABASE_URLS variable, not updating"
80+
echo "You should update it by hand with the new variable"
81+
else
82+
if echo "${BACKUP_DATABASE_URLS}" >> .env; then
83+
echo "Automatically added BACKUP_DATABASE_URLS to your .env file"
84+
else
85+
echo "Failed to write to .env file, please add BACKUP_DATABASE_URLS by hand"
86+
fi
87+
fi
88+
echo "${BACKUP_DATABASE_URLS}"
89+
fi
90+
91+
echo
92+
echo "Database setup complete!"
93+
echo "Connection details:"
94+
echo " Host: mariadb"
95+
echo " Port: 3306"
96+
echo " Database: ${DATABASE}"
97+
echo " Username: ${USER}"
98+
echo " Password: ${PASSWORD}"
99+
echo " Connection URL: mysql://${USER}:${PASSWORD}@mariadb:3306/${DATABASE}"

apps/postgresql/README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
# Postgresql
22

3-
Use the convenience script located in `bin/mkdb.sh` to create a database and its owner and the owner's password. While your current directory is the application's that needs Postgresql access run:
3+
Use the convenience script located in `bin/mkdb.sh` to create a database and its owner and the owner's password.
44

5-
```
6-
../postgresql/bin/mkdb.sh APPLICATION
5+
**Change directory to the application** that needs PostgreSQL (under `apps/` or `experiments/`), then run the script by path. The script resolves the PostgreSQL compose project from its own location, so the same invocation works from any app directory.
6+
7+
Examples:
8+
9+
```bash
10+
# from apps/someapp
11+
../postgresql/bin/mkdb.sh someapp
12+
13+
# from experiments/someapp
14+
../../apps/postgresql/bin/mkdb.sh someapp
715
```
816

9-
Where `APPLICATION` is the name of the application.
17+
Where `APPLICATION` is the name of the application (the argument to `mkdb.sh`).
1018

1119
This will:
1220
1. create a database named `APPLICATION_db`
1321
2. create a user which owns the database named `APPLICATION_user`
1422
3. create a strong password for the user
15-
4. if you run this from a directory other than postgresql's it will append a BACKUP_DATABASE URL to the .env file in that directory unless the .env file already has a BACKUP_DATABASE URL line
23+
4. if your current directory is **not** the `apps/postgresql` service directory, it will append a `BACKUP_DATABASE_URLS` line to `.env` in the current directory unless that file already contains `BACKUP_DATABASE_URLS`
1624
5. output the info for the database
1725

1826
Alternatively you can run commands to make the database and user yourself.

apps/postgresql/bin/mkdb.sh

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ if ! command -v pwgen >/dev/null 2>&1; then
1111
exit 1
1212
fi
1313

14+
# Resolve paths from this script so it works when run from any app under apps/ or experiments/
15+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
16+
POSTGRESQL_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
17+
COMPOSE_FILE="$POSTGRESQL_DIR/docker-compose.yml"
18+
1419
export USER=$1_user
1520
export DATABASE=$1_db
16-
export PASSWORD=`pwgen 24 1`
21+
export PASSWORD=$(pwgen 24 1)
1722

1823
echo "Will create this database:"
1924
echo
@@ -23,36 +28,35 @@ echo "password: ${PASSWORD}"
2328
echo
2429
echo
2530

26-
source ../postgresql/.env
27-
: ${POSTGRES_USER:=postgresql}
31+
# shellcheck source=/dev/null
32+
source "$POSTGRESQL_DIR/.env"
33+
: "${POSTGRES_USER:=postgresql}"
2834

2935
echo "Using PostgreSQL admin user: ${POSTGRES_USER}"
3036
echo
3137

3238
# Check if PostgreSQL container is running
3339
echo "Checking if PostgreSQL container is running..."
34-
if ! docker compose -f ../postgresql/docker-compose.yml ps postgresql | grep -q "Up"; then
40+
if ! docker compose -f "$COMPOSE_FILE" ps postgresql | grep -q "Up"; then
3541
echo "Error: PostgreSQL container is not running"
36-
echo "Please start it with: docker compose -f ../postgresql/docker-compose.yml up -d postgresql"
42+
echo "Please start it with: docker compose -f $COMPOSE_FILE up -d postgresql"
3743
exit 1
3844
fi
3945
echo "✓ PostgreSQL container is running"
4046
echo
4147

4248
echo "1. creating user"
43-
CMD="docker compose -f ../postgresql/docker-compose.yml exec postgresql createuser -U ${POSTGRES_USER} -w ${USER}"
44-
echo " ${CMD}"
45-
if $CMD; then
49+
echo " docker compose -f $COMPOSE_FILE exec postgresql createuser -U ${POSTGRES_USER} -w ${USER}"
50+
if docker compose -f "$COMPOSE_FILE" exec postgresql createuser -U "${POSTGRES_USER}" -w "${USER}"; then
4651
echo " ✓ User created successfully"
4752
else
4853
echo " ✗ Failed to create user"
4954
exit 1
5055
fi
5156

5257
echo "2. creating database owned by user"
53-
CMD="docker compose -f ../postgresql/docker-compose.yml exec postgresql createdb -U ${POSTGRES_USER} ${DATABASE} -O ${USER}"
54-
echo " ${CMD}"
55-
if $CMD; then
58+
echo " docker compose -f $COMPOSE_FILE exec postgresql createdb -U ${POSTGRES_USER} ${DATABASE} -O ${USER}"
59+
if docker compose -f "$COMPOSE_FILE" exec postgresql createdb -U "${POSTGRES_USER}" "${DATABASE}" -O "${USER}"; then
5660
echo " ✓ Database created successfully"
5761
else
5862
echo " ✗ Failed to create database"
@@ -61,7 +65,7 @@ fi
6165

6266
echo "3. setting user password"
6367
echo " Setting password for ${USER}..."
64-
if docker compose -f ../postgresql/docker-compose.yml exec -T postgresql psql -U ${POSTGRES_USER} -d postgres << EOF
68+
if docker compose -f "$COMPOSE_FILE" exec -T postgresql psql -U "${POSTGRES_USER}" -d postgres << EOF
6569
ALTER ROLE ${USER} WITH PASSWORD '${PASSWORD}';
6670
EOF
6771
then
@@ -75,15 +79,19 @@ echo "4. adding database URL for backups to .env"
7579

7680
export BACKUP_DATABASE_URLS="BACKUP_DATABASE_URLS=postgres://${USER}:${PASSWORD}@postgresql/${DATABASE}"
7781

78-
if pwd | grep -q postgresql ; then
79-
echo "You are in Postgresql's directory. You should add or replace BACKUP_DATABASE_URLS in your applications .env file"
82+
APP_CWD=$(pwd -P)
83+
if [ "$APP_CWD" = "$POSTGRESQL_DIR" ]; then
84+
echo "You are in the PostgreSQL service directory. Add or replace BACKUP_DATABASE_URLS in your application's .env file:"
8085
echo "${BACKUP_DATABASE_URLS}"
8186
else
82-
if grep -qF BACKUP_DATABASE_URLS .env; then
87+
if [ ! -f .env ]; then
88+
echo "No .env in $(pwd); create it from .env.example, then add:" >&2
89+
echo "${BACKUP_DATABASE_URLS}" >&2
90+
elif grep -qF BACKUP_DATABASE_URLS .env 2>/dev/null; then
8391
echo ".env already has BACKUP_DATABASE_URLS variable, not updating"
8492
echo "You should update it by hand with the new variable"
8593
else
86-
if echo "${BACKUP_DATABASE_URLS}" >> .env ; then
94+
if echo "${BACKUP_DATABASE_URLS}" >> .env; then
8795
echo "Automatically added BACKUP_DATABASE_URLS to your .env file"
8896
else
8997
echo "Failed to write to .env file, please add BACKUP_DATABASE_URLS by hand"

0 commit comments

Comments
 (0)