-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathrun-migrations-and-copy-database.sh
More file actions
66 lines (56 loc) · 2.18 KB
/
run-migrations-and-copy-database.sh
File metadata and controls
66 lines (56 loc) · 2.18 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
#!/bin/bash
DATA_FILE="players-sqlite3.db"
PROJECT_ROOT_PATH="src/Dotnet.Samples.AspNetCore.WebApi"
PROJECT_BASE_PATH="$PROJECT_ROOT_PATH/bin/Debug/net8.0"
SOURCE_FILE_PATH="$PROJECT_BASE_PATH/storage/$DATA_FILE"
TARGET_FILE_PATH="$PROJECT_ROOT_PATH/storage/$DATA_FILE"
# log prints a formatted log message prefixed by an emoji and timestamp, followed by the log level and message.
# Parameters: emoji — emoji or symbol to prefix the entry; level — severity or level label; message — the log text.
log() {
local emoji=$1
local level=$2
local message=$3
local timestamp
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo "$emoji [$timestamp] [$level] $message"
return 0
}
# Check if the EF Core CLI tool is installed
if ! command -v dotnet ef &> /dev/null; then
log "❌" "ERROR" "'dotnet ef' not found. Install it with 'dotnet tool install --global dotnet-ef'"
exit 1
fi
# Ensure clean placeholder database file exists
log "✅" "INFO" "Resetting placeholder database at '$TARGET_FILE_PATH'"
rm -f "$TARGET_FILE_PATH"
touch "$TARGET_FILE_PATH"
# Run the database migration
log "✅" "INFO" "Running EF Core database migration for project at '$PROJECT_ROOT_PATH'..."
dotnet ef database update --project "$PROJECT_ROOT_PATH"
if [[ $? -ne 0 ]]; then
log "❌" "ERROR" "Migration failed. See error above."
exit 1
fi
# Check and copy database
if [[ -f "$SOURCE_FILE_PATH" ]]; then
log "✅" "INFO" "Found database at '$SOURCE_FILE_PATH'"
log "✅" "INFO" "Copying to '$TARGET_FILE_PATH'..."
cp -f "$SOURCE_FILE_PATH" "$TARGET_FILE_PATH"
if [[ $? -eq 0 ]]; then
log "✅" "INFO" "Database successfully copied to '$TARGET_FILE_PATH'"
else
log "❌" "ERROR" "Failed to copy the database file."
exit 1
fi
else
log "⚠️" "WARNING" "Database file not found at '$SOURCE_FILE_PATH'."
log "⚠️" "WARNING" "Make sure the migration actually generated the file."
exit 1
fi
# Confirm destination file exists
if [[ -f "$TARGET_FILE_PATH" ]]; then
log "✅" "INFO" "Done. The database is now available at '$TARGET_FILE_PATH'"
else
log "⚠️" "WARNING" "Something went wrong. The destination file was not found."
exit 1
fi