Skip to content

Commit cfb7f8f

Browse files
committed
feat: add a db import script
1 parent 5ebef36 commit cfb7f8f

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

bin/db/import.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/bash
2+
3+
# Import a fresh database into Magento
4+
# WARNING: This will DROP and recreate the database!
5+
# Supports .sql and .sql.gz files
6+
7+
set -e
8+
9+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
10+
source "$SCRIPT_DIR/lib/detect-devcontainer.sh"
11+
12+
# Check for input file
13+
if [ -z "$1" ]; then
14+
echo "Usage: $0 <file.sql|file.sql.gz>" >&2
15+
echo "" >&2
16+
echo "Examples:" >&2
17+
echo " $0 dump.sql" >&2
18+
echo " $0 dump.sql.gz" >&2
19+
exit 1
20+
fi
21+
22+
SQL_FILE="$1"
23+
24+
if [ ! -f "$SQL_FILE" ]; then
25+
echo "Error: File not found: $SQL_FILE" >&2
26+
exit 1
27+
fi
28+
29+
load_devcontainer_config || exit 1
30+
31+
# Database configuration (matches docker-compose service settings)
32+
DB_HOST="${DB_HOST:-db}"
33+
DB_NAME="${DB_NAME:-magento}"
34+
DB_USER="${DB_USER:-magento}"
35+
DB_PASSWORD="${DB_PASSWORD:-magento}"
36+
37+
echo "Importing fresh database:" >&2
38+
echo " Host: $DB_HOST" >&2
39+
echo " Database: $DB_NAME" >&2
40+
echo " User: $DB_USER" >&2
41+
echo " File: $SQL_FILE" >&2
42+
echo "" >&2
43+
echo "WARNING: This will DROP the existing database!" >&2
44+
read -p "Continue? [y/N]: " confirm </dev/tty
45+
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
46+
echo "Aborted." >&2
47+
exit 1
48+
fi
49+
echo "" >&2
50+
51+
# Drop and recreate the database
52+
echo "Dropping and recreating database '$DB_NAME'..." >&2
53+
mysql -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASSWORD" -e "DROP DATABASE IF EXISTS \`$DB_NAME\`; CREATE DATABASE \`$DB_NAME\`;"
54+
55+
# Determine if file is gzipped
56+
if [[ "$SQL_FILE" == *.gz ]]; then
57+
echo "Detected gzipped file, decompressing on the fly..." >&2
58+
gunzip -c "$SQL_FILE" | mysql -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME"
59+
else
60+
mysql -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" < "$SQL_FILE"
61+
fi
62+
63+
echo "" >&2
64+
echo "Import completed successfully." >&2

0 commit comments

Comments
 (0)