Avnyr 9515c32016 docs: add migration system documentation and generated initial migrations
Added `README.md` to document the database migration system, including generation and usage instructions. Updated `generate-migrations.ts` to fix directory structure and streamline commands. Included initial migration files for schema setup using DrizzleORM.
2025-05-15 18:08:30 +02:00
..

Database Migrations

This directory contains the migration system for the database. The migrations are generated using DrizzleORM and are stored in the sql subdirectory.

Directory Structure

  • sql/ - Contains the generated SQL migration files
  • generate-migrations.ts - Script to generate migration files
  • migrate.ts - Script to run migrations
  • README.md - This file

How to Use

Generating Migrations

To generate migrations based on changes to the schema, run:

npm run db:generate:ts

This will generate SQL migration files in the sql directory.

Running Migrations

To run all pending migrations, run:

npm run db:migrate

Generating and Running Migrations in One Step

To generate and run migrations in one step, run:

npm run db:update

Integration with NestJS

The migrations are automatically run when the application starts. This is configured in the DatabaseService class in src/database/database.service.ts.

Migration Files

Migration files are SQL files that contain the SQL statements to create, alter, or drop database objects. They are named with a timestamp and a description, e.g. 0000_lively_tiger_shark.sql.

Configuration

The migration system is configured in drizzle.config.ts at the root of the project. This file specifies:

  • The schema file to use for generating migrations
  • The output directory for migration files
  • The database dialect and credentials