Skip to content

Commit

Permalink
feat: Add a command to init dotCMS with a custom database (#28678) (#…
Browse files Browse the repository at this point in the history
…29139)

### Proposed Changes
This Pull Request introduces the functionality to initialize the
database using an SQL dump file specified by the DB_LOAD_DUMP_SQL
environment variable. Key enhancements include:

* Environment Variable Addition: `DB_LOAD_DUMP_SQL` can now be set to
the path of an SQL dump file, which will be imported into the database
during container initialization.
* Dependency Management: The loading of the SQL dump depends on the
database connection details (`DB_HOST`, `DB_USERNAME`, `DB_PASSWORD`,
`DB_NAME`) being correctly set.
* Custom Starter Override: If a custom starter URL
(`CUSTOM_STARTER_URL`) is specified, it will take precedence over the
`DB_LOAD_DUMP_SQL` variable, bypassing the SQL dump import.
* Script Integration: Updates to the `50-load-dump-sql.sh` script ensure
that the database dump is imported if the conditions are met, with
appropriate logging for success or failure.
* Docker Compose Configuration: Documentation has been provided on how
to set the `DB_LOAD_DUMP_SQL` variable in the `docker-compose.yml` file.

This feature enhances the flexibility and ease of initializing the
database with pre-existing data, improving setup efficiency for
development and testing environments.

Usage:

On the docker-compose file you must declare the following env variables.

```
    environment:
      - DB_HOST=db.dotcms.site
      - DB_USERNAME=dotcmsdbuser
      - DB_PASSWORD=password
      - DB_NAME=dotcms
      - DB_LOAD_DUMP_SQL=/path/to/your/dumpfile.sql
    volumes:
      - ./path/to/dumpfile.sql:/path/to/your/dumpfile.sql
```

### Additional Info
Related to #28678 (Doc Add a command to init dotCMS with a custom dat).

### Screenshots
Original             |  Updated
:-------------------------:|:-------------------------:
** original screenshot **  |  ** updated screenshot **

---------

Co-authored-by: Daniel Colina <[email protected]>
  • Loading branch information
dcolina and dcolina authored Jul 5, 2024
1 parent 0c0667c commit 6c222d0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ services:
volumes:
- dbdata:/var/lib/postgresql/data
networks:
- db_net
- db_net
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ export DB_DRIVER=${DB_DRIVER:-"org.postgresql.Driver"}
export DB_BASE_URL=${DB_BASE_URL:-"jdbc:postgresql://db.dotcms.site/dotcms"}
export DB_USERNAME=${DB_USERNAME:-"dotcmsdbuser"}
export DB_PASSWORD=${DB_PASSWORD:-"password"}
export DB_HOST=${DB_HOST:-"db.dotcms.site"}
export DB_NAME=${DB_NAME:-"dotcms"}
export DB_MAX_WAIT=${DB_MAX_WAIT:-"180000"}
export DB_MAX_TOTAL=${DB_MAX_TOTAL:-"200"}
export DB_CONNECTION_TIMEOUT=${DB_CONNECTION_TIMEOUT:-"5000"}
export DB_MIN_IDLE=${DB_MIN_IDLE:-"10"}
export DB_VALIDATION_QUERY=${DB_VALIDATION_QUERY:-""}
export DB_LEAK_DETECTION_THRESHOLD=${DB_LEAK_DETECTION_THRESHOLD:-"300000"}
export DB_DEFAULT_TRANSACTION_ISOLATION=${DB_DEFAULT_TRANSACTION_ISOLATION:-""}
export DB_LOAD_DUMP_SQL=${DB_LOAD_DUMP_SQL:-""}

## Elasticsearch config
# ES Auth Type = BASIC|JWT
Expand Down
16 changes: 16 additions & 0 deletions dotCMS/src/main/docker/original/ROOT/srv/50-load-dump-sql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
set -e

# Check if the DB_LOAD_DUMP environment variable is set and the file exists
if [[ -n "${DB_LOAD_DUMP_SQL}" && -f "${DB_LOAD_DUMP_SQL}" && -z ${CUSTOM_STARTER_URL} ]]; then
echo "Importing database dump from ${DB_LOAD_DUMP_SQL}..."
sleep 10
export PGPASSWORD=${DB_PASSWORD}
/usr/bin/psql -h "${DB_HOST}" -U "${DB_USERNAME}" -d "${DB_NAME}" -f "${DB_LOAD_DUMP_SQL}"

echo "Dump successfully imported."
elif [[ -n ${DOT_STARTER_DATA_LOAD} ]]; then
echo "Importing data from starter ${CUSTOM_STARTER_URL}..."
else
echo "Dump file not found [${DB_LOAD_DUMP_SQL}]"
fi
1 change: 1 addition & 0 deletions dotCMS/src/main/docker/original/ROOT/srv/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ source /srv/00-config-defaults.sh
source /srv/20-copy-overriden-files.sh
source /srv/30-override-config-props.sh
source /srv/40-custom-starter-zip.sh
source /srv/50-load-dump-sql.sh


[[ -n "${WAIT_FOR_DEPS}" ]] && echo "Waiting ${WAIT_FOR_DEPS} seconds for DotCMS dependencies to load..." && sleep ${WAIT_FOR_DEPS}
Expand Down

0 comments on commit 6c222d0

Please sign in to comment.