The Social Media Backend uses Alembic for database schema migrations, giving you version-controlled, reversible changes to your PostgreSQL schema. Every structural change to the database — creating tables, adding columns, defining foreign keys — is captured in a numbered migration file that can be applied forward or rolled back independently.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/pvnm4/Social-Media-Backend/llms.txt
Use this file to discover all available pages before exploring further.
Migration history
The project ships with 7 migration files inalembic/versions/, applied in the following order:
108e0432cce3 — Create posts table
Creates the initial
posts table with two columns: a primary key id (INTEGER NOT NULL) and a title column (VARCHAR NOT NULL).3f67e4df37c9 — Add content column to posts table
Adds a
content column (VARCHAR NOT NULL) to the existing posts table.e92365d5c852 — Add users table
Creates the
users table with id (primary key), email (unique, NOT NULL), password (NOT NULL), and created_at (timezone-aware timestamp, defaults to now()).4ddcfb76cfd6 — Add foreign key to posts table
Adds an
owner_id column (INTEGER NOT NULL) to posts and creates a foreign key constraint post_users_fk that references users.id with ON DELETE CASCADE.0a1bfb2e9c2d — Add last few columns to posts table
Adds
published (BOOLEAN NOT NULL, server default True) and created_at (TIMESTAMP WITH TIME ZONE NOT NULL, server default NOW()) to the posts table.b1c66d590ccf — Generate votes table
Creates the
votes table with a composite primary key of user_id and post_id. Both columns are foreign keys with ON DELETE CASCADE — deleting a user or post automatically removes their associated votes.Common commands
Use these Alembic commands from the project root (the directory containingalembic.ini):
Creating new migrations
When you need to change the database schema, follow this workflow:-
Modify
app/models.py— add, remove, or alter SQLAlchemy model columns or tables. -
Generate the migration file:
Alembic compares
Base.metadata(your current models) against the live database schema and writes a new file inalembic/versions/withupgrade()anddowngrade()functions. -
Review the generated file — open the new revision in
alembic/versions/and verify that theupgrade()anddowngrade()functions match your intentions exactly. -
Apply the migration:
How Alembic connects to the database
Alembic uses the sameDATABASE_* environment variables as the application. The alembic/env.py file reads those variables via the settings object from app/config.py and constructs the SQLAlchemy connection URL at startup:
app/config.py uses pydantic-settings with env_file = ".env", so Alembic automatically picks up credentials from your .env file — no separate Alembic configuration is needed:
.env file you use to run the FastAPI server is also used when you run any alembic command, keeping your local development configuration in one place.
When running migrations in a production or CI environment where a
.env file is not present, ensure the DATABASE_HOSTNAME, DATABASE_PORT, DATABASE_PASSWORD, DATABASE_USERNAME, and DATABASE_NAME variables are exported as real environment variables before invoking alembic.