Database Overview
Macro uses PostgreSQL as its primary database with multiple logical databases for separation of concerns:- MacroDB - Main database for documents, users, communication, and email
- ContactsDB - User connections and contacts
Technology Stack
- Database: PostgreSQL
- ORM/Query Builder: SQLx with compile-time query validation
- Schema Management: Prisma (schema definition) + SQLx (migrations)
- Migration Tool:
sqlx-cli - Offline Development:
SQLX_OFFLINE=truemode for builds without DB connection
MacroDB Schema Management
Schema Definition
The schema for MacroDB is defined in the Prisma schema file:- Table structures
- Relationships between entities
- Column types and constraints
- Indexes for performance
Important: Column Naming
Prisma uses camelCase, but PostgreSQL stores them as camelCase too. When querying, you must cast column names to snake_case:schema.prisma for actual column names, not the Rust struct field names.
Migration Workflow
Making Schema Changes
When you need to modify the MacroDB schema:-
Edit the schema in
macro-api/database/schema.prisma -
Create migration from the
macro-api/databasefolder:
- Update SQLx metadata from
macro-api/cloud-storage:
- Prepare database queries in
macro-api/cloud-storage/macro_db_client:
.sqlx directory with query metadata for offline compilation.
Troubleshooting Migrations
If you encounter migration errors after runningjust setup_macrodb:
- Force drop and recreate in
macro-api/cloud-storage/macro_db_client:
- Recreate the database from
macro-api/cloud-storage:
After SQL Code Changes
Any time you modify SQL queries in Rust code, you must update the SQLx cache:.sqlx metadata stays in sync with your queries.
Database Client Pattern
Each logical database has its own client crate:macro_db_client- MacroDB accesscomms_db_client- Communications data (uses MacroDB)email_db_client- Email data (uses MacroDB)contacts_db_client- ContactsDB accessnotification_db_client- Notification data (uses MacroDB)properties_db_client- Properties data (uses MacroDB)
Database Client Structure
SQLx Query Patterns
Compile-Time Verified Queries
SQLx validates queries at compile time:Query Macro Types
query!()- Returns anonymous recordquery_as!()- Maps to a structquery_scalar!()- Returns single scalar value
Offline Development
SQLx supports offline mode for building without a database connection:.sqlx directory.
Updating Offline Cache
When you add or modify queries:.sqlx/query-*.json files with query metadata.
Database Setup Commands
Initialize All Databases
Frommacro-api/cloud-storage:
Individual Database Setup
Testing with Databases
Test Environment Setup
Test Database URLs
Tests use a consistent database URL defined injustfile:1-2:
Best Practices
Always Test After DB Changes
From CLAUDE.md:“Always run tests between changes that involve changes to db queries”
Migration Workflow Summary
- Edit
schema.prisma just create-migration <name>(frommacro-api/database)just setup_macrodb(frommacro-api/cloud-storage)just prepare_db(from db client crate, e.g.macro_db_client)- Test your changes:
cargo test -p macro_db_client
When Making DB Crate Changes
From CLAUDE.md:“When making changes to a db crate you should always update tests, and run prepare”
Index Strategy
When creating migrations, include appropriate indexes:- Composite indexes for frequently queried column combinations
- Foreign key indexes for relationship lookups
- Timestamp indexes for time-based ordering
- Unique indexes for constraint enforcement
Next Steps
- API Design - Learn about API patterns
- Testing - Database testing strategies
- Services - How services use database clients