Skip to content

Latest commit

 

History

History
30 lines (17 loc) · 1.99 KB

migrations.md

File metadata and controls

30 lines (17 loc) · 1.99 KB

What are migrations

Migrations are a mechanism for managing and versioning changes in the database schema in a controlled and repeatable way. They allow you to apply schema increments (such as creating, modifying, or deleting tables and columns) in an automated manner. Essentially, migrations are scripts that define the changes that need to be made to the database, ensuring that all environments (development, testing, production) are synchronized.

When to use migrations

You should use migrations whenever you need to make structural changes to the database in a controlled environment. This includes:

  • Creating new tables: When you need to add new entities to your database.
  • Modifying existing tables: Such as adding or removing columns, changing data types, or adding constraints.
  • Creating indexes: To improve query performance.
  • Seeding initial data: Inserting initial data required for the application to function.

Using migrations is especially important in collaborative projects or production environments where data consistency and integrity are crucial.

Best Practices

Here are some best practices for handling migrations:

  • Versioning Migrations: Name your migrations to reflect the date and the change being made. This helps maintain a clear and ordered history of changes.

  • Small and Frequent: Make small and frequent migrations. Large changes can be harder to debug and roll back in case of issues.

  • Rollback Scripts: Always write rollback scripts for your migrations. This allows you to undo changes in case of errors.

  • Testing Migrations: Test your migrations in a development or testing environment before applying them to production to ensure they do not cause issues.

  • Documentation: Document the changes made in each migration so that all team members understand what was changed and why.

  • Automated Tools: Use tools like the TypeORM CLI or custom tools to generate and apply migrations automatically, reducing the risk of manual errors.