Skip to content

Commit b204973

Browse files
Ensure the database is available when doctrine database build runs
This consists of: * waiting for the port to be available at a customisable timeout (for when deploying the db and app at the same time) * not treating a database failure as an empty database, instead erroring out
1 parent dbca958 commit b204973

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

symfony/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ The following variables are supported
7171
Variable | Description | Expected values | Default
7272
--- | --- | --- | ----
7373
SYMFONY_DOCTRINE_MODE | Whether to use Doctrine migrations, schema update or nothing. Automatically detected based on installed composer packages by default | auto/migrations/schema/off | auto
74+
SYMFONY_DOCTRINE_WAIT_TIMEOUT | The maximum time to wait for the database to become available during the database build | time in seconds | 10
7475
SYMFONY_ENV | The Symfony env to use, when the app reads this variable | string | prod
7576
SYMFONY_FLEX | Whether the project uses the symfony/flex component and folder structure | true/false | autodetected based on whether in composer.lock
7677
SYMFONY_MAJOR_VERSION | The major version of Symfony that will be used | 2, 3 | auto-detected based on location of console script

symfony/usr/local/share/env/30-framework

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export PHP_OPCACHE_MAX_ACCELERATED_FILES=${PHP_OPCACHE_MAX_ACCELERATED_FILES:-20
55
export PHP_REALPATH_CACHE_SIZE=${PHP_REALPATH_CACHE_SIZE:-4096K}
66
export PHP_REALPATH_CACHE_TTL=${PHP_REALPATH_CACHE_TTL:-600}
77
export SYMFONY_DOCTRINE_MODE=auto
8+
export SYMFONY_DOCTRINE_WAIT_TIMEOUT="${SYMFONY_DOCTRINE_WAIT_TIMEOUT:-10}"
89
SYMFONY_WEB_APP_ENV_REWRITE="$(convert_to_boolean_string "${SYMFONY_WEB_APP_ENV_REWRITE:-false}")"
910
export SYMFONY_WEB_APP_ENV_REWRITE
1011

symfony/usr/local/share/symfony/symfony_functions.sh

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,15 @@ do_database_build() {
8181
return 0
8282
fi
8383

84-
# load the database if it doesn't exist
85-
set +e
86-
do_symfony_console doctrine:database:create >/dev/null
87-
local DATABASE_EXISTED=$?
88-
set -e
89-
90-
local QUERY_LINE_COUNT=0
91-
if [ "$DATABASE_EXISTED" -eq 1 ]; then
92-
QUERY_LINE_COUNT="$(do_symfony_console doctrine:query:sql 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = database()' | wc -l)"
93-
fi
84+
wait_for_remote_ports "${SYMFONY_DOCTRINE_WAIT_TIMEOUT}" "${DATABASE_HOST}:${DATABASE_PORT}"
85+
86+
# create the database if it doesn't exist
87+
do_symfony_console doctrine:database:create --if-not-exists
88+
89+
local QUERY_LINE_COUNT
90+
QUERY_LINE_COUNT="$(do_symfony_console doctrine:query:sql 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = database()' | wc -l)"
9491

95-
if [ "$DATABASE_EXISTED" -ne 1 ] || [ "$QUERY_LINE_COUNT" -lt 3 ]; then
92+
if [ "$QUERY_LINE_COUNT" -lt 3 ]; then
9693
do_database_install
9794
else
9895
do_database_update

0 commit comments

Comments
 (0)