-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathmigration-cleanup.sh
More file actions
77 lines (63 loc) · 2.33 KB
/
migration-cleanup.sh
File metadata and controls
77 lines (63 loc) · 2.33 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
#!/bin/bash
set -e
# This script can help automate migration conflicts and local environment
# It stashes your changes, pulls / checks out main branch makes a clean db,
# rechecks out your branch / stash, detects any migrations that haven't run
# (and deletes them because these are probably just conflicting) and then regenerates them
YELLOW='\033[1;33m'
NC='\033[0m'
# Remember current branch
ORIGINAL_BRANCH=$(git rev-parse --abbrev-ref HEAD)
echo "Current branch: $ORIGINAL_BRANCH"
STASHED=false
# Stop current env if running
echo "Shutting down env..."
docker-compose down
# Check for uncommitted changes
if ! git diff-index --quiet HEAD -- || [ -n "$(git ls-files --others --exclude-standard)" ]; then
echo
echo -e "${YELLOW}You have uncommitted changes on this branch ($ORIGINAL_BRANCH).${NC}"
echo "These changes will be temporarily stashed during this workflow."
read -p "Do you want to continue with the stash workflow? [y/N]: " confirm
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
echo "Aborting."
exit 1
fi
echo "Stashing changes (including untracked files)..."
git stash --include-untracked || echo "Nothing to stash."
STASHED=true
else
echo "No uncommitted changes detected."
fi
# Checkout main branch and pull latest changes
echo "Checking out 'main' branch..."
git checkout main
echo "Pulling latest changes..."
git pull origin main
# Rebuild database from committed migrations
echo "Rebuilding DB from migrations..."
inv new-db
# Switch back to the original branch
echo "Switching back to $ORIGINAL_BRANCH..."
git checkout "$ORIGINAL_BRANCH"
# Reapply stashed changes if any
if $STASHED && git stash list | grep -q "WIP on $ORIGINAL_BRANCH"; then
echo "Reapplying stashed changes..."
git stash pop || echo "Nothing to pop or stash already applied."
fi
# Delete unapplied migration files
echo "Removing unapplied migration files..."
inv manage "showmigrations --plan" | grep '\[ \]' | awk '{print $3}' | tr -d '\r' | while read migration; do
app=$(echo "$migration" | cut -d. -f1)
mig=$(echo "$migration" | cut -d. -f2)
file="foundation_cms/${app}/migrations/${mig}.py"
echo "$file"
if [ -f "$file" ]; then
echo " Deleting $file"
rm "$file"
fi
done
# Generate new migrations
echo "Generating fresh migrations..."
inv makemigrations
echo "Migration cleanup and regeneration complete. Feel free to migrate!"