Skip to content

Commit 22ccab0

Browse files
authored
chore: Create appsmith postgres DB to be used by internal processes (#36670)
## Description With the migration to postgres as Appsmith's persistent database we wanted to have central DB to be consumed by internal services. Currently all the services try to create the same DB and may end up in a race condition, so we are moving this task to `entrypoint.sh`. Steps: 1. Initialise data directory for pg 2. Run the upgrade migration if necessary 3. Start the pg server 4. Create `appsmith` db with postgres user 5. Stop the pg server /test Sanity ### 🔍 Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/11212147481> > Commit: e384076 > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=11212147481&attempt=1" target="_blank">Cypress dashboard</a>. > Tags: `@tag.Sanity` > Spec: > <hr>Mon, 07 Oct 2024 09:31:39 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Improved database initialization logic for the Appsmith application. - Automatically creates the Appsmith database if it does not already exist during startup. - **Bug Fixes** - Enhanced robustness of the database setup process with existence checks before creation. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent d15eea3 commit 22ccab0

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

deploy/docker/fs/opt/appsmith/entrypoint.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ tlog "Running as: $(id)"
66

77
stacks_path=/appsmith-stacks
88

9+
export APPSMITH_PG_DATABASE="appsmith"
910
export SUPERVISORD_CONF_TARGET="$TMP/supervisor-conf.d/" # export for use in supervisord.conf
1011
export MONGODB_TMP_KEY_PATH="$TMP/mongodb-key" # export for use in supervisor process mongodb.conf
1112

@@ -432,6 +433,7 @@ init_postgres() {
432433
tlog "Initializing local Postgres data folder"
433434
su postgres -c "env PATH='$PATH' initdb -D $POSTGRES_DB_PATH"
434435
fi
436+
create_appsmith_pg_db "$POSTGRES_DB_PATH"
435437
else
436438
runEmbeddedPostgres=0
437439
fi
@@ -453,6 +455,33 @@ safe_init_postgres() {
453455
fi
454456
}
455457

458+
# Method to create a appsmith database in the postgres
459+
# Args:
460+
# POSTGRES_DB_PATH (string): Path to the postgres data directory
461+
# Returns:
462+
# None
463+
# Example:
464+
# create_appsmith_pg_db "/appsmith-stacks/data/postgres/main"
465+
create_appsmith_pg_db() {
466+
POSTGRES_DB_PATH=$1
467+
# Start the postgres , wait for it to be ready and create a appsmith db
468+
su postgres -c "env PATH='$PATH' pg_ctl -D $POSTGRES_DB_PATH -l $POSTGRES_DB_PATH/logfile start"
469+
echo "Waiting for Postgres to start"
470+
until su postgres -c "env PATH='$PATH' pg_isready -d postgres"; do
471+
tlog "Waiting for Postgres to be ready..."
472+
sleep 1
473+
done
474+
# Check if the appsmith DB is present
475+
DB_EXISTS=$(su postgres -c "env PATH='$PATH' psql -tAc \"SELECT 1 FROM pg_database WHERE datname='${APPSMITH_PG_DATABASE}'\"")
476+
477+
if [[ "$DB_EXISTS" != "1" ]]; then
478+
su postgres -c "env PATH='$PATH' psql -c \"CREATE DATABASE ${APPSMITH_PG_DATABASE}\""
479+
else
480+
echo "Database ${APPSMITH_PG_DATABASE} already exists."
481+
fi
482+
su postgres -c "env PATH='$PATH' pg_ctl -D $POSTGRES_DB_PATH stop"
483+
}
484+
456485
setup_caddy() {
457486
if [[ "$APPSMITH_RATE_LIMIT" == "disabled" ]]; then
458487
export _APPSMITH_CADDY="/opt/caddy/caddy_vanilla"

0 commit comments

Comments
 (0)