Repository navigation
Expand file tree
/
Copy pathstart-db.sh
More file actions
101 lines (83 loc) · 2.61 KB
/
Copy pathstart-db.sh
File metadata and controls
101 lines (83 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
# Script to start MSSQL Server Express in Docker and run EF Core migrations
set -e # Exit on error
echo "=========================================="
echo "Starting MSSQL Server Express in Docker"
echo "=========================================="
# Check if .env file exists
if [ ! -f api/.env ]; then
echo "ERROR: .env file not found!"
echo "Please create a .env file based on .env.example"
echo "Example: cp .env.example .env"
exit 1
fi
# Load environment variables from .env
set -a # automatically export all variables
source api/.env
set +a # stop automatically exporting
# Check if MSSQL_SA_PASSWORD is set
if [ -z "$MSSQL_SA_PASSWORD" ]; then
echo "ERROR: MSSQL_SA_PASSWORD not set in .env file!"
exit 1
fi
# Check if CONNECTION_STRING is set
if [ -z "$CONNECTION_STRING" ]; then
echo "ERROR: CONNECTION_STRING not set in .env file!"
exit 1
fi
# Start Docker containers
echo ""
echo "Starting Docker containers..."
docker-compose up -d
# Wait for SQL Server to be ready
echo ""
echo "Waiting for SQL Server to be ready..."
sleep 10
# Check if SQL Server is ready by checking the logs
MAX_RETRIES=30
RETRY_COUNT=0
until docker logs mssql-express 2>&1 | grep -q "Recovery is complete"; do
RETRY_COUNT=$((RETRY_COUNT + 1))
if [ $RETRY_COUNT -ge $MAX_RETRIES ]; then
echo "ERROR: SQL Server failed to start after $MAX_RETRIES attempts"
docker logs mssql-express --tail 20
exit 1
fi
echo "Waiting for SQL Server to be ready... (Attempt $RETRY_COUNT/$MAX_RETRIES)"
sleep 2
done
echo ""
echo "SQL Server is ready!"
# Drop and recreate the database
echo ""
echo "=========================================="
echo "Dropping and Recreating Database"
echo "=========================================="
cd api
echo "Dropping existing database if it exists..."
dotnet ef database drop --force
# Run EF Core migrations
echo ""
echo "=========================================="
echo "Running EF Core Migrations"
echo "=========================================="
# Check if migrations directory exists
if [ ! -d "Migrations" ]; then
echo "No migrations found. Creating initial migration..."
dotnet ef migrations add InitialCreate
fi
# Apply migrations to the database
echo ""
echo "Applying migrations to database..."
dotnet ef database update
echo ""
echo "=========================================="
echo "Database setup complete!"
echo "=========================================="
echo ""
echo "MSSQL Server is running on localhost:1433"
echo "Connection string available in .env file"
echo ""
echo "To stop the database, run:"
echo " docker-compose down"
echo ""