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: e3840764acb3088233bf6552d74721abb00518a6
> <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 -->
This commit is contained in:
Abhijeet 2024-10-08 07:53:58 +05:30 committed by GitHub
parent d15eea3e1e
commit 22ccab07d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,6 +6,7 @@ tlog "Running as: $(id)"
stacks_path=/appsmith-stacks stacks_path=/appsmith-stacks
export APPSMITH_PG_DATABASE="appsmith"
export SUPERVISORD_CONF_TARGET="$TMP/supervisor-conf.d/" # export for use in supervisord.conf export SUPERVISORD_CONF_TARGET="$TMP/supervisor-conf.d/" # export for use in supervisord.conf
export MONGODB_TMP_KEY_PATH="$TMP/mongodb-key" # export for use in supervisor process mongodb.conf export MONGODB_TMP_KEY_PATH="$TMP/mongodb-key" # export for use in supervisor process mongodb.conf
@ -432,6 +433,7 @@ init_postgres() {
tlog "Initializing local Postgres data folder" tlog "Initializing local Postgres data folder"
su postgres -c "env PATH='$PATH' initdb -D $POSTGRES_DB_PATH" su postgres -c "env PATH='$PATH' initdb -D $POSTGRES_DB_PATH"
fi fi
create_appsmith_pg_db "$POSTGRES_DB_PATH"
else else
runEmbeddedPostgres=0 runEmbeddedPostgres=0
fi fi
@ -453,6 +455,33 @@ safe_init_postgres() {
fi fi
} }
# Method to create a appsmith database in the postgres
# Args:
# POSTGRES_DB_PATH (string): Path to the postgres data directory
# Returns:
# None
# Example:
# create_appsmith_pg_db "/appsmith-stacks/data/postgres/main"
create_appsmith_pg_db() {
POSTGRES_DB_PATH=$1
# Start the postgres , wait for it to be ready and create a appsmith db
su postgres -c "env PATH='$PATH' pg_ctl -D $POSTGRES_DB_PATH -l $POSTGRES_DB_PATH/logfile start"
echo "Waiting for Postgres to start"
until su postgres -c "env PATH='$PATH' pg_isready -d postgres"; do
tlog "Waiting for Postgres to be ready..."
sleep 1
done
# Check if the appsmith DB is present
DB_EXISTS=$(su postgres -c "env PATH='$PATH' psql -tAc \"SELECT 1 FROM pg_database WHERE datname='${APPSMITH_PG_DATABASE}'\"")
if [[ "$DB_EXISTS" != "1" ]]; then
su postgres -c "env PATH='$PATH' psql -c \"CREATE DATABASE ${APPSMITH_PG_DATABASE}\""
else
echo "Database ${APPSMITH_PG_DATABASE} already exists."
fi
su postgres -c "env PATH='$PATH' pg_ctl -D $POSTGRES_DB_PATH stop"
}
setup_caddy() { setup_caddy() {
if [[ "$APPSMITH_RATE_LIMIT" == "disabled" ]]; then if [[ "$APPSMITH_RATE_LIMIT" == "disabled" ]]; then
export _APPSMITH_CADDY="/opt/caddy/caddy_vanilla" export _APPSMITH_CADDY="/opt/caddy/caddy_vanilla"