Skip to content

Commit f59a0fa

Browse files
committed
chore(docker): update Docker configurations and add Makefile for database operations
- Adjusted the Docker Compose files to correct PostgreSQL port mapping and update the image reference for the tux-dev service. - Introduced a new tux-postgres service in the main Docker Compose file, including health checks and volume configurations for persistent data storage. - Added a Makefile to streamline database operations, providing commands for testing connections, managing migrations, and running various tests. - Updated dependencies in pyproject.toml to include alembic-utils and pytest-alembic for enhanced database migration testing capabilities. - Modified .gitignore to include examples directory for better project organization.
1 parent f603f25 commit f59a0fa

File tree

6 files changed

+334
-48
lines changed

6 files changed

+334
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,4 @@ reports/
189189
sqlmodel-refactor
190190
.database-archive
191191
data/
192+
examples/

Makefile

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# Tux Database Operations Makefile
2+
# Use this to test database operations without the CLI
3+
4+
.PHONY: help help-db db-connect db-current db-upgrade db-downgrade db-revision db-reset db-init test-unit test-integration test-e2e test-db test-alembic test-migrations test-models test-controllers test-service test-db-all test-coverage test-smoke test-clean
5+
6+
# Default target
7+
help:
8+
@echo "Tux Database Operations"
9+
@echo "======================="
10+
@echo ""
11+
@echo "Available targets:"
12+
@echo " help - Show this help message"
13+
@echo " help-db - Show database-specific help"
14+
@echo ""
15+
@echo "Database Operations:"
16+
@echo " db-connect - Test database connection"
17+
@echo " db-current - Show current migration version"
18+
@echo " db-upgrade - Upgrade database to latest migration"
19+
@echo " db-downgrade - Downgrade database by one migration"
20+
@echo " db-revision - Create new migration revision"
21+
@echo " db-reset - Reset database (WARNING: destroys data)"
22+
@echo " db-init - Initialize database schema"
23+
@echo ""
24+
@echo "Testing:"
25+
@echo " test-unit - Run unit tests"
26+
@echo " test-integration - Run integration tests"
27+
@echo " test-e2e - Run end-to-end tests"
28+
@echo " test-db - Run all database tests"
29+
@echo " test-db-all - Run comprehensive database test suite"
30+
@echo ""
31+
@echo "Environment variables:"
32+
@echo " MODE=dev|prod - Environment mode (default: dev)"
33+
@echo ""
34+
@echo "Examples:"
35+
@echo " make db-connect"
36+
@echo " make MODE=prod db-current"
37+
@echo " make db-upgrade"
38+
39+
# Environment setup
40+
MODE ?= dev
41+
PYTHON := uv run python
42+
43+
# Database connection test
44+
db-connect:
45+
@echo "🔍 Testing database connection..."
46+
@MODE=$(MODE) $(PYTHON) scripts/db_connect_test.py
47+
48+
# Show current migration
49+
db-current:
50+
@echo "📊 Getting current migration version..."
51+
@MODE=$(MODE) $(PYTHON) scripts/db_current.py
52+
53+
# Upgrade database
54+
db-upgrade:
55+
@echo "⬆️ Upgrading database to latest migration..."
56+
@MODE=$(MODE) $(PYTHON) scripts/db_upgrade.py
57+
58+
# Downgrade database
59+
db-downgrade:
60+
@echo "⬇️ Downgrading database by one migration..."
61+
@MODE=$(MODE) $(PYTHON) scripts/db_downgrade.py
62+
63+
# Create new migration
64+
db-revision:
65+
@echo "📝 Creating new migration revision..."
66+
@MODE=$(MODE) $(PYTHON) scripts/db_revision.py
67+
68+
# Initialize database schema
69+
db-init:
70+
@echo "🏗️ Initializing database schema..."
71+
@MODE=$(MODE) $(PYTHON) scripts/db_init.py
72+
73+
# Reset database (DANGER!)
74+
db-reset:
75+
@echo "⚠️ WARNING: This will reset the database and destroy all data!"
76+
@read -p "Are you sure? (type 'yes' to continue): " confirm && [ "$$confirm" = "yes" ] || (echo "Operation cancelled" && exit 1)
77+
@echo "🔄 Resetting database..."
78+
@MODE=$(MODE) $(PYTHON) scripts/db_reset.py
79+
80+
# ============================================================================
81+
# TESTING TARGETS
82+
# ============================================================================
83+
84+
# Run all database unit tests
85+
test-unit:
86+
@echo "🧪 Running database unit tests..."
87+
$(PYTHON) -m pytest tests/unit/ -v --tb=short
88+
89+
# Run database integration tests
90+
test-integration:
91+
@echo "🔗 Running database integration tests..."
92+
$(PYTHON) -m pytest --run-integration tests/integration/ -v --tb=short
93+
94+
# Run database end-to-end tests
95+
test-e2e:
96+
@echo "🌍 Running database E2E tests..."
97+
$(PYTHON) -m pytest --run-e2e tests/e2e/ -v --tb=short
98+
99+
# Run all database tests
100+
test-db: test-unit test-integration test-e2e
101+
@echo "✅ All database tests completed!"
102+
103+
# Run pytest-alembic tests
104+
test-alembic:
105+
@echo "🗃️ Running pytest-alembic tests..."
106+
$(PYTHON) -m pytest --test-alembic -v --tb=short
107+
108+
# Run migration-specific tests
109+
test-migrations:
110+
@echo "🔄 Running migration tests..."
111+
$(PYTHON) -m pytest tests/unit/test_database_migrations.py -v --tb=short
112+
113+
# Run model-specific tests
114+
test-models:
115+
@echo "📊 Running model tests..."
116+
$(PYTHON) -m pytest tests/unit/test_database_models.py -v --tb=short
117+
118+
# Run controller-specific tests
119+
test-controllers:
120+
@echo "🎛️ Running controller tests..."
121+
$(PYTHON) -m pytest tests/unit/test_database_controllers.py -v --tb=short
122+
123+
# Run database service tests
124+
test-service:
125+
@echo "🔧 Running database service tests..."
126+
$(PYTHON) -m pytest tests/unit/test_database_service.py -v --tb=short
127+
128+
# Comprehensive database test suite
129+
test-db-all: test-alembic test-migrations test-models test-controllers test-service test-integration test-e2e
130+
@echo "🎉 Complete database test suite passed!"
131+
132+
# Run database tests with coverage
133+
test-coverage:
134+
@echo "📊 Running database tests with coverage..."
135+
$(PYTHON) -m pytest --cov=tux.database --cov-report=html --cov-report=term tests/unit/ tests/integration/ tests/e2e/
136+
137+
# Quick smoke test for database functionality
138+
test-smoke:
139+
@echo "🚀 Running database smoke tests..."
140+
@make db-connect || (echo "❌ Database connection failed" && exit 1)
141+
@make db-current || (echo "❌ Database current check failed" && exit 1)
142+
@echo "✅ Database smoke tests passed!"
143+
144+
# Clean test artifacts
145+
test-clean:
146+
@echo "🧹 Cleaning test artifacts..."
147+
rm -rf .pytest_cache/
148+
rm -rf tests/**/__pycache__/
149+
rm -rf htmlcov/
150+
rm -f .coverage
151+
152+
# ============================================================================
153+
# DEVELOPMENT HELPERS
154+
# ============================================================================
155+
156+
# Show available database targets
157+
help-db:
158+
@echo "Database Management Targets:"
159+
@echo " db-connect - Test database connection"
160+
@echo " db-current - Show current migration version"
161+
@echo " db-upgrade - Upgrade database to latest migration"
162+
@echo " db-downgrade - Downgrade database by one migration"
163+
@echo " db-revision - Create new migration revision"
164+
@echo " db-init - Initialize database schema"
165+
@echo " db-reset - Reset database (DANGER!)"
166+
@echo ""
167+
@echo "Database Testing Targets:"
168+
@echo " test-unit - Run all unit tests"
169+
@echo " test-integration - Run integration tests"
170+
@echo " test-e2e - Run end-to-end tests"
171+
@echo " test-db - Run unit + integration + e2e tests"
172+
@echo " test-db-all - Run comprehensive database test suite"
173+
@echo " test-alembic - Run pytest-alembic tests"
174+
@echo " test-migrations - Run migration-specific tests"
175+
@echo " test-models - Run model-specific tests"
176+
@echo " test-controllers - Run controller-specific tests"
177+
@echo " test-service - Run database service tests"
178+
@echo " test-coverage - Run tests with coverage report"
179+
@echo " test-smoke - Quick smoke test (connection + current)"
180+
@echo " test-clean - Clean test artifacts"
181+
@echo ""
182+
@echo "Usage examples:"
183+
@echo " make db-connect"
184+
@echo " make MODE=prod db-current"
185+
@echo " make test-unit"
186+
@echo " make test-db"
187+
@echo " make test-alembic"
188+
@echo " make test-db-all"

docker-compose.dev.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010
POSTGRES_PASSWORD: tuxpass
1111
POSTGRES_INITDB_ARGS: --encoding=UTF-8 --lc-collate=C --lc-ctype=C
1212
ports:
13-
- 5433:5432
13+
- 5432:5432
1414
volumes:
1515
- tux_dev_postgres_data:/var/lib/postgresql/data
1616
- ./scripts/init-db.sql:/docker-entrypoint-initdb.d/init-db.sql:ro
@@ -25,7 +25,7 @@ services:
2525
tux-dev:
2626
container_name: tux-dev
2727
hostname: tux-dev
28-
image: tux:${TUX_IMAGE_TAG:-dev}
28+
image: allthingslinux/tux:${TUX_IMAGE_TAG:-dev}
2929
build:
3030
context: .
3131
args:

docker-compose.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
services:
2+
tux-postgres:
3+
container_name: tux-postgres
4+
hostname: tux-postgres
5+
image: postgres:15-alpine
6+
restart: unless-stopped
7+
environment:
8+
POSTGRES_DB: tuxdb
9+
POSTGRES_USER: tuxuser
10+
POSTGRES_PASSWORD: tuxpass
11+
POSTGRES_INITDB_ARGS: --encoding=UTF-8 --lc-collate=C --lc-ctype=C
12+
ports:
13+
- 5432:5432
14+
volumes:
15+
- tux_postgres_data:/var/lib/postgresql/data
16+
- ./scripts/init-db.sql:/docker-entrypoint-initdb.d/init-db.sql:ro
17+
healthcheck:
18+
test:
19+
- CMD-SHELL
20+
- pg_isready -U tuxuser -d tuxdb
21+
interval: 10s
22+
timeout: 5s
23+
retries: 5
24+
start_period: 30s
225
tux:
326
container_name: tux
427
hostname: tux
@@ -23,6 +46,9 @@ services:
2346
environment:
2447
TUX_VERSION: ${VERSION}
2548
restart: unless-stopped
49+
depends_on:
50+
tux-postgres:
51+
condition: service_healthy
2652
healthcheck:
2753
test:
2854
- CMD
@@ -59,3 +85,5 @@ volumes:
5985
driver: local
6086
tux_user_home:
6187
driver: local
88+
tux_postgres_data:
89+
driver: local

pyproject.toml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ dependencies = [
4444
"aiosqlite>=0.20.0",
4545
"redis>=5.0.0",
4646
"psycopg2-binary>=2.9.10",
47+
"alembic-utils>=0.8.8",
4748
]
4849

4950
[project.urls]
@@ -75,6 +76,14 @@ test = [
7576
"pytest-timeout>=2.3.1,<3",
7677
"pytest-html>=4.1.1,<5",
7778
"pytest-benchmark>=5.1.0,<6",
79+
"pytest-alembic>=0.12.0,<0.13",
80+
# https://pypi.org/project/pytest-clean-database/
81+
# https://pypi.org/project/pytest-click/
82+
# https://pypi.org/project/pytest-codecov/
83+
# https://pypi.org/project/pytest-databases/
84+
# https://pypi.org/project/pytest-postgresql/
85+
# https://pypi.org/project/pytest-sqlalchemy/
86+
# https://pypi.org/project/pytest-sqlguard/
7887
]
7988
docs = [
8089
"mkdocs-material>=9.5.30,<10",
@@ -258,4 +267,9 @@ file_template = "%%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(r
258267
timezone = "UTC"
259268

260269
[tool.alembic.sqlalchemy]
261-
url = ""
270+
url = "sqlite:///test.db"
271+
272+
# pytest-alembic configuration
273+
[tool.pytest-alembic]
274+
script_location = "src/tux/database/migrations"
275+
version_locations = ["src/tux/database/migrations/versions"]

0 commit comments

Comments
 (0)