mirror of
https://github.com/quay/quay.git
synced 2026-01-26 06:21:37 +03:00
2.1 KiB
2.1 KiB
Database & Migrations
Database Stack
- PostgreSQL 12.1 - Primary database
- SQLAlchemy - ORM
- Alembic - Migrations
- Redis - Caching, sessions, build logs
Key Models
Located in data/model/:
user.py- User, FederatedLogin, Team, TeamMemberrepository.py- Repository, RepositoryPermission, Startag.py- Tag, TagManifest, ManifestLabelimage.py- Image, ImageStorage, DerivedStorageForImageorganization.py- Organization, OrganizationMemberbuild.py- RepositoryBuild, RepositoryBuildTriggernotification.py- Notification, RepositoryNotificationappspecifictoken.py- AppSpecificAuthTokenlog.py- LogEntry, LogEntry2, LogEntry3
Schema Changes
Creating a Migration
# Generate migration file
alembic revision -m "description_of_change"
# Edit the generated file in data/migrations/versions/
# Implement upgrade() and downgrade() functions
Applying Migrations
# Apply all pending migrations
alembic upgrade head
# Apply to specific revision
alembic upgrade <revision_id>
# Rollback one migration
alembic downgrade -1
Migration Best Practices
- Always implement both
upgrade()anddowngrade() - Use
op.batch_alter_table()for SQLite compatibility in tests - Test migrations in both directions
- Include data migrations if needed (not just schema)
Database Connection
from data.database import db_transaction
# Use context manager for transactions
with db_transaction() as db:
user = db.query(User).filter_by(username='admin').first()
Local Dev Database
- Host: localhost:5432
- User: quay
- Password: quay
- Database: quay
- Connection:
postgresql://quay:quay@quay-db/quay
Testing with Database
# Run tests with SQLite (default)
TEST=true PYTHONPATH="." pytest test/test_file.py -v
# Run tests with PostgreSQL
make test_postgres TESTS=test/test_file.py
Key Files
data/database.py- Database connection, session managementdata/model/__init__.py- Model importsdata/migrations/env.py- Alembic environmentdata/migrations/versions/- Migration files