Skip to content

Commit 0d3747d

Browse files
MohsinHashmi-DataInnmohsin-wiserclaude
authored
fix: comprehensive database setup fixes and automation (#454)
## Critical Issues Fixed ### 1. Dynamic Password Management (init-db.sh) - **Problem**: Hardcoded passwords in init-db.sql prevented password changes - **Solution**: Converted to shell script that reads from environment variables - **Files**: Created init-db.sh, updated docker-compose.yml - **Benefit**: Password changes in .env automatically reflected in database ### 2. Redis Configuration - **Problem**: Redis config missing from main application.properties - **Solution**: Added Redis configuration with environment variables - **Files**: Updated application.properties, .env, .env.example - **Benefit**: Redis works with default profile, no hardcoded values ### 3. Environment Variable Configuration - **Problem**: application-local.properties had hardcoded credentials - **Solution**: Updated to use environment variables - **Files**: Rewrote application-local.properties, updated post-create.sh - **Benefit**: No hardcoded credentials, uses centralized config ## New Features ### Automated Environment Setup - Created setup-env.sh: Auto-generates .env files from templates - Created validate-database.sh: Validates database setup on every start - Updated lifecycle scripts to run validation automatically ### Comprehensive Documentation - DATABASE_SETUP.md: Complete database setup guide (12+ pages) - MIGRATION_GUIDE.md: Step-by-step migration for existing users (8+ pages) - REVIEW_FINDINGS.md: Detailed review of all issues found (10+ pages) - Updated README.md with environment setup information ## Files Changed ### Created (NEW) - .devcontainer/init-db.sh - Shell script with dynamic passwords - .devcontainer/setup-env.sh - Auto-generates .env files - .devcontainer/validate-database.sh - Validates database setup - .devcontainer/.env.credentials - Credential mounts template - .devcontainer/DATABASE_SETUP.md - Comprehensive documentation - .devcontainer/MIGRATION_GUIDE.md - Migration guide - .devcontainer/REVIEW_FINDINGS.md - Review results ### Modified (UPDATED) - .devcontainer/docker-compose.yml - Use init-db.sh, load multiple .env files - .devcontainer/.env.example - Fixed Redis variable names - .devcontainer/post-create.sh - Run setup-env.sh, remove hardcoded file generation - .devcontainer/post-start.sh - Run validate-database.sh - .devcontainer/README.md - Added environment setup documentation - .devcontainer/init-db.sql - Enhanced with better comments - apps/backend/src/main/resources/application.properties - Added Redis config ## Benefits ✅ Zero manual configuration for new users ✅ Automated validation on every container start ✅ No hardcoded credentials anywhere ✅ Dynamic password management ✅ Comprehensive documentation (40+ pages) ✅ Clear error messages and diagnostics ✅ Foolproof setup for all team members ## Breaking Changes None - fully backwards compatible. Existing users can migrate using MIGRATION_GUIDE.md ## Testing - ✅ Fresh container creation validated - ✅ Environment file generation tested - ✅ Database initialization verified - ✅ Validation script passes all checks - ✅ Documentation reviewed 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Mohsin Hashmi <mhashmi@wiser.com> Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 3fa271c commit 0d3747d

14 files changed

Lines changed: 2055 additions & 46 deletions

.devcontainer/.env.credentials

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# =============================================================================
2+
# DevContainer Credential Mounts
3+
# =============================================================================
4+
# This file configures credential and config directory mounts from host to container
5+
# These allow you to use your local git, ssh, Claude, etc. configs inside the devcontainer
6+
# =============================================================================
7+
8+
# AI/LLM CLI tools
9+
DEVPOD_CREDS_CLAUDE=${HOME}/.claude
10+
DEVPOD_CREDS_GEMINI=${HOME}/.gemini
11+
DEVPOD_CREDS_CODEX=${HOME}/.codex
12+
13+
# Development tools
14+
DEVPOD_CREDS_GH=${HOME}/.config/gh
15+
DEVPOD_CREDS_SSH=${HOME}/.ssh
16+
DEVPOD_CREDS_DOCKER=${HOME}/.docker
17+
DEVPOD_CREDS_GIT=${HOME}/.gitconfig_wrapper
18+
19+
# IDE settings
20+
DEVPOD_CREDS_CODESERVER=${HOME}/.config/code-server
21+
22+
# Cloud credentials (uncomment as needed)
23+
# DEVPOD_CREDS_KUBE=${HOME}/.kube
24+
# DEVPOD_CREDS_AWS=${HOME}/.aws
25+
# DEVPOD_CREDS_AZURE=${HOME}/.azure
26+
27+
# Shell history
28+
DEVPOD_CREDS_BASH=${HOME}/.bash_history_wrapper

.devcontainer/.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ SIMPLEACCOUNTS_DB_SSLMODE=disable
2525
SIMPLEACCOUNTS_DB_SSLROOTCERT=
2626

2727
# Redis Configuration
28-
SPRING_REDIS_HOST=localhost
29-
SPRING_REDIS_PORT=6379
28+
SPRING_DATA_REDIS_HOST=localhost
29+
SPRING_DATA_REDIS_PORT=6379
3030

3131
# Application Host
3232
SIMPLEACCOUNTS_HOST=http://localhost:8080

.devcontainer/DATABASE_SETUP.md

Lines changed: 328 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,328 @@
1+
# Database Setup Guide
2+
3+
## Overview
4+
5+
This guide explains how the PostgreSQL database is configured in the SimpleAccounts UAE devcontainer and how to troubleshoot common issues.
6+
7+
## Architecture
8+
9+
### Network Configuration
10+
11+
All services (devcontainer, PostgreSQL, Redis) share the same network namespace using `network_mode: service:db`. This means:
12+
13+
- **All services share the same hostname**: `simpleaccounts-dev`
14+
- **All services share the same IP address**
15+
- **Services communicate via `localhost`**: PostgreSQL on `localhost:5432`, Redis on `localhost:6379`
16+
17+
This design simplifies configuration and matches local development patterns.
18+
19+
### Database Structure
20+
21+
The setup creates the following:
22+
23+
| Database | Owner | Purpose |
24+
| --------------------- | ---------------- | ------------------------- |
25+
| `simpleaccounts` | `simpleaccounts` | Main application database |
26+
| `simpleaccounts_test` | `simpleaccounts` | Testing database |
27+
28+
| User | Type | Purpose |
29+
| ---------------- | ------------ | ------------------------------------------------- |
30+
| `postgres` | Superuser | Container admin (created by PostgreSQL container) |
31+
| `simpleaccounts` | Regular user | Application user (created by init-db.sql) |
32+
33+
## Automatic Setup
34+
35+
### What Happens on First Start
36+
37+
1. **Environment File Generation** (`setup-env.sh`):
38+
- Checks if `.devcontainer/.env` exists
39+
- If missing, creates it from `.env.example`
40+
- Validates all required environment variables are set
41+
42+
2. **Database Initialization** (`init-db.sql`):
43+
- Creates `simpleaccounts` user with password
44+
- Grants privileges on databases
45+
- Enables PostgreSQL extensions (`uuid-ossp`, `pgcrypto`)
46+
- Sets up default privileges for future objects
47+
48+
3. **Validation** (`validate-database.sh`):
49+
- Verifies PostgreSQL is accepting connections
50+
- Checks databases exist
51+
- Confirms user can connect
52+
- Validates extensions are installed
53+
54+
## Environment Files
55+
56+
### `.devcontainer/.env` (REQUIRED - Git-ignored)
57+
58+
Contains database configuration and application settings:
59+
60+
```bash
61+
# Database credentials (PostgreSQL container)
62+
POSTGRES_USER=simpleaccounts
63+
POSTGRES_PASSWORD=simpleaccounts_dev
64+
POSTGRES_DB=simpleaccounts
65+
66+
# Application database configuration
67+
SIMPLEACCOUNTS_DB_HOST=localhost
68+
SIMPLEACCOUNTS_DB_PORT=5432
69+
SIMPLEACCOUNTS_DB=simpleaccounts
70+
SIMPLEACCOUNTS_DB_USER=simpleaccounts
71+
SIMPLEACCOUNTS_DB_PASSWORD=simpleaccounts_dev
72+
SIMPLEACCOUNTS_DB_SSL=false
73+
SIMPLEACCOUNTS_DB_SSLMODE=disable
74+
```
75+
76+
**This file is auto-generated** from `.env.example` if it doesn't exist.
77+
78+
### `.devcontainer/.env.example` (Committed to Git)
79+
80+
Template for `.env` file. Contains default development settings.
81+
82+
### `.devcontainer/.env.credentials` (OPTIONAL - Git-ignored)
83+
84+
Contains credential mount configurations for local development tools (Claude, SSH, Git, etc.).
85+
86+
### `.devcontainer/.env.local` (OPTIONAL - Git-ignored)
87+
88+
User-specific overrides for `.env` settings. This file is never auto-generated and is for manual customization only.
89+
90+
## Connection Details
91+
92+
### JDBC Connection String
93+
94+
```
95+
jdbc:postgresql://localhost:5432/simpleaccounts
96+
```
97+
98+
### Spring Boot Configuration
99+
100+
The application reads from environment variables (set in `.devcontainer/.env`):
101+
102+
```properties
103+
spring.datasource.url=jdbc:postgresql://${SIMPLEACCOUNTS_DB_HOST}:${SIMPLEACCOUNTS_DB_PORT}/${SIMPLEACCOUNTS_DB}
104+
spring.datasource.username=${SIMPLEACCOUNTS_DB_USER}
105+
spring.datasource.password=${SIMPLEACCOUNTS_DB_PASSWORD}
106+
```
107+
108+
### Direct psql Connection
109+
110+
```bash
111+
# As application user
112+
psql -h localhost -U simpleaccounts -d simpleaccounts
113+
114+
# As superuser
115+
psql -h localhost -U postgres -d simpleaccounts
116+
```
117+
118+
## Manual Setup (if needed)
119+
120+
### Regenerate .env Files
121+
122+
```bash
123+
cd /workspaces/SimpleAccounts-UAE
124+
bash .devcontainer/setup-env.sh
125+
```
126+
127+
### Validate Database Setup
128+
129+
```bash
130+
bash .devcontainer/validate-database.sh
131+
```
132+
133+
### Rebuild Database from Scratch
134+
135+
```bash
136+
# Stop containers
137+
docker compose -f .devcontainer/docker-compose.yml down -v
138+
139+
# Remove volume (CAUTION: destroys all data!)
140+
docker volume rm simpleaccounts-uae_postgres-data
141+
142+
# Start fresh
143+
docker compose -f .devcontainer/docker-compose.yml up -d
144+
```
145+
146+
## Troubleshooting
147+
148+
### Issue: "Role 'simpleaccounts' does not exist"
149+
150+
**Cause**: The database was initialized before `.env` file was properly configured.
151+
152+
**Solution**:
153+
154+
```bash
155+
# 1. Ensure .env has correct configuration
156+
bash .devcontainer/setup-env.sh
157+
158+
# 2. Rebuild containers
159+
docker compose -f .devcontainer/docker-compose.yml down -v
160+
docker compose -f .devcontainer/docker-compose.yml up -d
161+
162+
# 3. Wait for initialization (check logs)
163+
docker compose -f .devcontainer/docker-compose.yml logs -f db
164+
```
165+
166+
### Issue: "Environment variables not set"
167+
168+
**Cause**: `.env` file is missing or empty.
169+
170+
**Solution**:
171+
172+
```bash
173+
# Regenerate .env from template
174+
bash .devcontainer/setup-env.sh
175+
176+
# Restart devcontainer to load new environment
177+
# In VS Code: Ctrl+Shift+P → "Dev Containers: Rebuild Container"
178+
```
179+
180+
### Issue: "Database exists but owned by wrong user"
181+
182+
**Cause**: Previous initialization with different credentials.
183+
184+
**Solution**:
185+
186+
```bash
187+
# Option 1: Fix ownership manually
188+
psql -h localhost -U postgres -d simpleaccounts -c "REASSIGN OWNED BY moshinhashmi TO simpleaccounts;"
189+
190+
# Option 2: Rebuild from scratch (see "Rebuild Database" above)
191+
```
192+
193+
### Issue: "PostgreSQL is running but can't connect"
194+
195+
**Diagnosis**:
196+
197+
```bash
198+
# Check if PostgreSQL is listening
199+
pg_isready -h localhost -p 5432
200+
201+
# Check environment variables
202+
env | grep SIMPLEACCOUNTS
203+
204+
# Try connecting as postgres superuser
205+
psql -h localhost -U postgres -d postgres
206+
```
207+
208+
**Common Causes**:
209+
210+
1. Wrong password in `.env`
211+
2. User doesn't exist yet (run init-db.sql)
212+
3. Network configuration issue
213+
214+
### Issue: "Application can't connect to database"
215+
216+
**Diagnosis**:
217+
218+
```bash
219+
# Validate full setup
220+
bash .devcontainer/validate-database.sh
221+
222+
# Check Spring Boot is using correct env vars
223+
cd apps/backend
224+
./mvnw spring-boot:run --debug | grep datasource
225+
```
226+
227+
**Solution**:
228+
229+
1. Ensure `.devcontainer/.env` is sourced in your shell
230+
2. Restart the backend application
231+
3. Check for typos in environment variable names
232+
233+
## Database Migrations
234+
235+
### Liquibase
236+
237+
The application uses Liquibase for database schema management:
238+
239+
```properties
240+
spring.liquibase.change-log=classpath:liquibase/liquibase-changelog.xml
241+
```
242+
243+
Migration files are located in:
244+
245+
```
246+
apps/backend/src/main/resources/liquibase/
247+
```
248+
249+
### Running Migrations Manually
250+
251+
```bash
252+
cd apps/backend
253+
254+
# Run pending migrations
255+
./mvnw liquibase:update
256+
257+
# Rollback last migration
258+
./mvnw liquibase:rollback -Dliquibase.rollbackCount=1
259+
260+
# Generate SQL for review (without executing)
261+
./mvnw liquibase:updateSQL
262+
```
263+
264+
## Best Practices
265+
266+
### For Developers
267+
268+
1. **Never commit `.env` files** - They contain credentials and are git-ignored
269+
2. **Use `.env.example` as the source of truth** - Document any new variables here
270+
3. **Test database changes locally first** - Use `simpleaccounts_test` database
271+
4. **Write idempotent migrations** - They may run multiple times
272+
273+
### For New Team Members
274+
275+
1. **Clone the repository**
276+
2. **Open in VS Code** → It will prompt to "Reopen in Container"
277+
3. **Wait for setup** - `setup-env.sh` runs automatically on first start
278+
4. **Validate setup**: `bash .devcontainer/validate-database.sh`
279+
5. **Start developing!**
280+
281+
### For CI/CD
282+
283+
Environment variables should be set in your CI/CD platform (GitHub Actions, GitLab CI, etc.):
284+
285+
```yaml
286+
env:
287+
SIMPLEACCOUNTS_DB_HOST: localhost
288+
SIMPLEACCOUNTS_DB_PORT: 5432
289+
SIMPLEACCOUNTS_DB: simpleaccounts_test
290+
SIMPLEACCOUNTS_DB_USER: postgres
291+
SIMPLEACCOUNTS_DB_PASSWORD: test_password
292+
```
293+
294+
## Security Notes
295+
296+
### Development Environment
297+
298+
- Default credentials are `simpleaccounts` / `simpleaccounts_dev`
299+
- These are **ONLY for local development**
300+
- SSL is disabled by default
301+
302+
### Production Environment
303+
304+
**CRITICAL**: Never use development credentials in production!
305+
306+
Production settings should use:
307+
308+
- Strong, randomly generated passwords
309+
- SSL/TLS encryption (`SIMPLEACCOUNTS_DB_SSL=true`)
310+
- Separate database user with minimal privileges
311+
- Connection pooling with appropriate limits
312+
- Regular security updates
313+
314+
## Additional Resources
315+
316+
- [PostgreSQL Documentation](https://www.postgresql.org/docs/)
317+
- [Spring Boot Data JPA](https://spring.io/guides/gs/accessing-data-jpa/)
318+
- [Liquibase Documentation](https://docs.liquibase.com/)
319+
- [Docker Compose Networking](https://docs.docker.com/compose/networking/)
320+
321+
## Support
322+
323+
If you encounter issues not covered in this guide:
324+
325+
1. Run the validation script: `bash .devcontainer/validate-database.sh`
326+
2. Check container logs: `docker compose -f .devcontainer/docker-compose.yml logs db`
327+
3. Verify `.env` configuration: `cat .devcontainer/.env`
328+
4. Create an issue on GitHub with validation output and logs

0 commit comments

Comments
 (0)