Skip to content

Commit 3bab347

Browse files
matrixiseclaude
andcommitted
feat: add SQLite database isolation by branch and documentation
Extend branch isolation to SQLite databases for local development and add comprehensive documentation for the branch isolation feature. Changes to SQLite isolation: - Modify dev.py to use branch-specific SQLite files (db-{branch}.sqlite3) - Add *.sqlite3 to .gitignore to exclude database files - Remove debug print statement from dev.py - Each branch now has isolated SQLite data without requiring worktrees Documentation improvements: - Add complete "Docker Branch Isolation" section to README.md - Document all branch:* commands (info, verify, volumes, clean) - Add examples for multi-branch development and branch switching - Add troubleshooting section for database configuration issues - Update DOCKER-BRANCHES.md to mention SQLite isolation - Add note in local setup about GIT_BRANCH export New Taskfile command: - task branch:verify - Comprehensive verification of branch configuration Shows git branch, expected database, .env file, and PostgreSQL databases This ensures complete data isolation between branches whether using Docker (PostgreSQL) or local development (SQLite), preventing data loss when switching branches. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 87b78f6 commit 3bab347

File tree

5 files changed

+102
-2
lines changed

5 files changed

+102
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ production.env
7373
# Database dumps and local databases
7474
*.dump
7575
*.duckdb
76+
*.sqlite3
7677

7778
# Local settings
7879
pythonie/pythonie/settings/pgdev.py

DOCKER-BRANCHES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The system automatically:
2020
Each branch gets:
2121
- ✅ Its own Docker image
2222
- ✅ Its own PostgreSQL database (isolated data)
23+
- ✅ Its own SQLite database for local dev: `pythonie/db-{branch}.sqlite3`
2324
- ✅ Its own Redis instance (isolated cache)
2425
- ✅ Its own Docker volumes (persistent storage)
2526
- ✅ Unique container names (no conflicts)

README.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Website for Python Ireland (python.ie / pycon.ie) community, built with Django 6
1212

1313
## Quick Start (Docker - Recommended)
1414

15+
**Note**: Each git branch automatically gets its own isolated database and Docker environment. See [Docker Branch Isolation](#docker-branch-isolation) below.
16+
1517
1. Build the Docker image:
1618
```bash
1719
task docker:build
@@ -20,7 +22,7 @@ Website for Python Ireland (python.ie / pycon.ie) community, built with Django 6
2022

2123
2. Start supporting services:
2224
```bash
23-
docker compose up -d postgres redis minio
25+
docker compose up -d postgres redis
2426
```
2527

2628
3. Run database migrations:
@@ -48,10 +50,79 @@ Website for Python Ireland (python.ie / pycon.ie) community, built with Django 6
4850
7. Visit http://127.0.0.1:8000/ to see the site with sample content
4951
8. Access Wagtail admin at http://127.0.0.1:8000/admin/
5052

53+
## Docker Branch Isolation
54+
55+
The project automatically isolates Docker environments by git branch, allowing you to work on multiple branches simultaneously without conflicts.
56+
57+
### How It Works
58+
59+
Each git branch gets:
60+
- **Separate Docker image**: `python.ie/website:{branch-name}`
61+
- **Isolated PostgreSQL database**: `pythonie_{branch-name}` (for docker-compose with pgdev settings)
62+
- **Isolated SQLite database**: `pythonie/db-{branch-name}.sqlite3` (for local dev)
63+
- **Dedicated volumes**: `pythonie-postgres-data-{branch}`, `pythonie-redis-data-{branch}`
64+
- **Unique containers**: `pythonie-web-{branch}`, `pythonie-postgres-{branch}`, `pythonie-redis-{branch}`
65+
66+
### Branch Commands
67+
68+
```bash
69+
# Check your current branch environment
70+
task branch:info
71+
72+
# Verify database configuration
73+
task branch:verify
74+
75+
# List all Docker volumes (all branches)
76+
task branch:volumes
77+
78+
# Clean up volumes for current branch (DESTRUCTIVE!)
79+
task branch:clean
80+
```
81+
82+
### Working on Multiple Branches
83+
84+
**Scenario**: Work on `main` and `feature/new-auth` simultaneously.
85+
86+
```bash
87+
# Terminal 1: Main branch on default ports
88+
git checkout main
89+
task run # Uses pythonie_main database on port 8000
90+
91+
# Terminal 2: Feature branch on custom ports
92+
git checkout feature/new-auth
93+
WEB_PORT=8001 PG_PORT=5433 task run # Uses pythonie_feature-new-auth on port 8001
94+
```
95+
96+
### Switching Branches
97+
98+
When you switch branches, the system automatically uses the correct environment:
99+
100+
```bash
101+
git checkout main
102+
task run # Automatically uses main database
103+
104+
git checkout feature/xyz
105+
task run # Automatically uses feature-xyz database (isolated from main)
106+
```
107+
108+
**Note**: Rebuild the Docker image after switching branches if dependencies changed:
109+
```bash
110+
git checkout feature/new-dependencies
111+
task docker:build
112+
task run
113+
```
114+
115+
For detailed documentation, see [DOCKER-BRANCHES.md](DOCKER-BRANCHES.md).
116+
51117
## Local Setup (Without Docker)
52118

53119
If you prefer to develop without Docker:
54120

121+
**Note**: When developing locally, each git branch automatically uses its own SQLite database (`db-{branch}.sqlite3`). Export `GIT_BRANCH` for automatic isolation:
122+
```bash
123+
export GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD | sed 's/[^a-zA-Z0-9._-]/-/g' | tr '[:upper:]' '[:lower:]')
124+
```
125+
55126
1. Fork the repository into your own personal GitHub account
56127
2. Clone your fork: `git clone git@github.com:YourGitHubName/website.git`
57128
3. Ensure you are running Python 3.13: `python -V` should output `Python 3.13.x`
@@ -134,6 +205,12 @@ task heroku:releases # Show deployment history
134205
task heroku:rollback # Rollback to previous release
135206
task heroku:maintenance:on # Enable maintenance mode
136207
task heroku:maintenance:off # Disable maintenance mode
208+
209+
# Branch Isolation (Docker)
210+
task branch:info # Show current branch and environment info
211+
task branch:verify # Verify database configuration
212+
task branch:volumes # List all Docker volumes (all branches)
213+
task branch:clean # Remove volumes for current branch (CAUTION!)
137214
```
138215

139216
### Direct Django Commands
@@ -257,6 +334,12 @@ This project uses several tools to streamline development:
257334
- Rebuild Docker image: `task docker:build`
258335
- Reinstall dependencies: `pip install -r requirements.txt`
259336

337+
### Wrong Database Being Used
338+
- Check current branch: `git branch`
339+
- Verify environment: `task branch:info`
340+
- Check database configuration: `task branch:verify`
341+
- Ensure .env file is up to date: `task env:write`
342+
260343
## Contributing
261344

262345
1. Fork the repository into your own GitHub account

Taskfile.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,16 @@ tasks:
302302
- docker volume rm pythonie-postgres-data-{{.GIT_BRANCH}} 2>/dev/null || echo "Postgres volume not found"
303303
- docker volume rm pythonie-redis-data-{{.GIT_BRANCH}} 2>/dev/null || echo "Redis volume not found"
304304
- echo "Volumes cleaned for branch {{.GIT_BRANCH}}"
305+
306+
branch:verify:
307+
desc: Verify database configuration for current branch
308+
cmds:
309+
- echo "=== Branch Configuration ==="
310+
- echo "Git branch → {{.GIT_BRANCH}}"
311+
- echo "Expected database → pythonie_{{.GIT_BRANCH}}"
312+
- echo ""
313+
- echo "=== Docker Environment ==="
314+
- cat .env 2>/dev/null || echo ".env file not found (run any docker compose task to generate)"
315+
- echo ""
316+
- echo "=== PostgreSQL Databases ==="
317+
- docker exec pythonie-postgres-{{.GIT_BRANCH}} psql -U postgres -c "\l" 2>/dev/null | grep pythonie || echo "PostgreSQL container not running"

pythonie/pythonie/settings/dev.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
WAGTAILADMIN_BASE_URL = "http://localhost:8000"
1717

1818
# SQLite (simplest install)
19+
# Database name includes git branch for isolation between branches
20+
GIT_BRANCH = os.environ.get("GIT_BRANCH", "dev")
1921
DATABASES = {
2022
"default": {
2123
"ENGINE": "django.db.backends.sqlite3",
22-
"NAME": join(PROJECT_ROOT, "db.sqlite3"),
24+
"NAME": join(PROJECT_ROOT, f"db-{GIT_BRANCH}.sqlite3"),
2325
}
2426
}
2527

0 commit comments

Comments
 (0)