Database Migration Guide
This guide explains how to work with database migrations in the Meta Agent Platform using Alembic and SQLAlchemy.
Prerequisites
- PostgreSQL database running (via Docker or locally)
- Project dependencies installed
.envfile configured with database connection details
Database Connection Configuration
Ensure your .env file contains the following database configuration:
# Database settings
POSTGRES_SERVER=localhost
POSTGRES_USER=user
POSTGRES_PASSWORD=password
POSTGRES_DB=appdb
POSTGRES_PORT=5432
DATABASE_URL=postgresql://user:password@localhost:5432/appdb
Starting the Database
If you're using Docker, start the PostgreSQL database container:
Working with Migrations
Viewing Current Migration Status
To check the current state of migrations:
Creating a New Migration
To create a new migration script:
For example:
Applying Migrations
To apply all pending migrations:
To apply a specific number of migrations:
Reverting Migrations
To revert the most recent migration:
To revert to a specific migration version:
To revert all migrations:
Database Schema
Current Models
User Model
The User model is defined in backend/app/models/user.py and includes the following fields:
id: UUID primary keykeycloak_id: String, unique identifier from Keycloakemail: String, unique email addressusername: String, unique usernamefirst_name: String, optionallast_name: String, optionalis_active: Boolean, default Trueis_superuser: Boolean, default Falseprofile_data: JSON, optional additional user datacreated_at: DateTime, automatically set on creationupdated_at: DateTime, automatically updated
Adding New Models
To add a new model to the database:
- Create a new model file in
backend/app/models/or add to an existing file - Define your model class inheriting from
Base - Import the model in
backend/app/db/init_db.py - Generate a new migration with
uv run alembic revision --autogenerate -m "add new model" - Apply the migration with
uv run alembic upgrade head
Example model definition:
from sqlalchemy import Column, String, Boolean, DateTime, ForeignKey
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.sql import func
import uuid
from app.db.base_class import Base
class NewModel(Base):
__tablename__ = "new_model"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = Column(String, nullable=False)
description = Column(String, nullable=True)
user_id = Column(UUID(as_uuid=True), ForeignKey("user.id"), nullable=False)
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
Troubleshooting
Migration Conflicts
If you encounter conflicts during migration:
- Revert to a stable migration:
uv run alembic downgrade <stable_revision_id> - Fix the model definitions
- Create a new migration:
uv run alembic revision --autogenerate -m "fix conflicts" - Apply the new migration:
uv run alembic upgrade head
Database Connection Issues
If you encounter database connection issues:
- Verify the database is running:
docker ps(if using Docker) - Check your
.envfile for correct connection details - Try connecting directly to the database:
docker-compose exec db psql -U user -d appdb
Inspecting the Database
To inspect the current database schema:
To view a specific table: