@pradip1995/create-storefront-app 0.4.1 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,39 @@ set -euo pipefail
4
4
  DUMP_PATH="${DUMP_PATH:-/dump/dump.sql}"
5
5
  MIN_TABLES="${MIN_TABLES_FOR_DUMP:-1}"
6
6
 
7
+ table_count() {
8
+ psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -tAc \
9
+ "SELECT count(*) FROM information_schema.tables WHERE table_schema = 'public';" \
10
+ 2>/dev/null || echo "0"
11
+ }
12
+
13
+ restore_dump_if_empty() {
14
+ if [ -z "${POSTGRES_USER:-}" ] || [ -z "${POSTGRES_DB:-}" ]; then
15
+ return 0
16
+ fi
17
+
18
+ if ! pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB" >/dev/null 2>&1; then
19
+ return 0
20
+ fi
21
+
22
+ local count
23
+ count="$(table_count)"
24
+
25
+ if [ "${count:-0}" -ge "$MIN_TABLES" ]; then
26
+ return 0
27
+ fi
28
+
29
+ if [ ! -f "$DUMP_PATH" ] || [ ! -s "$DUMP_PATH" ]; then
30
+ echo "Database empty (${count:-0} tables) and no dump at ${DUMP_PATH} — run npm run migrate:backend"
31
+ return 0
32
+ fi
33
+
34
+ echo "Database empty (${count:-0} tables). Restoring from ${DUMP_PATH} ..."
35
+ psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -v ON_ERROR_STOP=1 -f "$DUMP_PATH"
36
+ count="$(table_count)"
37
+ echo "Database restore complete (${count} tables)."
38
+ }
39
+
7
40
  dump_database() {
8
41
  if [ -z "${POSTGRES_USER:-}" ] || [ -z "${POSTGRES_DB:-}" ]; then
9
42
  return 0
@@ -13,19 +46,15 @@ dump_database() {
13
46
  return 0
14
47
  fi
15
48
 
16
- local table_count
17
- table_count=$(
18
- psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -tAc \
19
- "SELECT count(*) FROM information_schema.tables WHERE table_schema = 'public';" \
20
- 2>/dev/null || echo "0"
21
- )
49
+ local count
50
+ count="$(table_count)"
22
51
 
23
- if [ "${table_count:-0}" -lt "$MIN_TABLES" ]; then
24
- echo "Skipping dump: public schema has ${table_count:-0} table(s)."
52
+ if [ "${count:-0}" -lt "$MIN_TABLES" ]; then
53
+ echo "Skipping dump: public schema has ${count:-0} table(s)."
25
54
  return 0
26
55
  fi
27
56
 
28
- echo "Saving database dump (${table_count} tables) to ${DUMP_PATH} ..."
57
+ echo "Saving database dump (${count} tables) to ${DUMP_PATH} ..."
29
58
  pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" -Fp > "${DUMP_PATH}.tmp"
30
59
  mv "${DUMP_PATH}.tmp" "${DUMP_PATH}"
31
60
  echo "Database dump saved."
@@ -46,4 +75,11 @@ trap shutdown_postgres SIGTERM SIGINT
46
75
 
47
76
  /usr/local/bin/docker-entrypoint.sh "$@" &
48
77
  child_pid=$!
78
+
79
+ until pg_isready -U "${POSTGRES_USER:-postgres}" -d "${POSTGRES_DB:-postgres}" >/dev/null 2>&1; do
80
+ sleep 1
81
+ done
82
+
83
+ restore_dump_if_empty
84
+
49
85
  wait "$child_pid"