Database Migrations¶
This document explains how to manage database schema changes using the project’s migration tools.
Generate a Migration: phantom-migration pull
¶
This command generates new migration files by detecting differences between your resource meta-definitions (phantom-api-backend/meta/*.json
) and the current state of the SQLite database (phantom.db
).
Usage Example:
Command | |
---|---|
Expected Behavior:
- The CLI (
phantom-api
) sends a request to your running backend. -
The backend:
-
Reads the meta-definitions in
phantom-api-backend/meta/
. - Compares them with the actual database structure.
- Generates a TypeScript migration file in
phantom-api-backend/migrations/
if changes are detected.
Example Generated File:
- The
up
function applies the changes. - The
down
function rolls them back.
Apply a Migration¶
This command applies a specific migration file to the database via the backend.
Usage Example:
Replace <fileName>
with the exact name of the migration file (e.g., 20231027103000_create_posts_table.ts
).
Concrete Example:
Command | |
---|---|
Best Practices¶
- Backend Running: Ensure the backend server is started and accessible when running these commands.
- Generate Before Applying: Always run
phantom-migration pull
after modifying the meta-definitions, before applying migrations. - Review Migrations: Check the contents of generated files before applying them, especially in production.
- Preserve Order: Apply migrations in the order they were generated to maintain database consistency.