In the context of databases and database management systems, “migrations” and “seeds” are two concepts often used during the development and maintenance of applications. They serve different purposes:
Migrations
Purpose: Migrations manage changes to the database schema, such as creating or modifying tables, indexes, constraints, and other structural elements.
Use Cases: Migrations are used when the database schema needs to evolve, for example, adding new tables, changing column data types, creating relationships between tables, or modifying existing constraints.
Tools: Web frameworks and ORM (Object-Relational Mapping) libraries like TypeORM in NestJS offer tools for managing database migrations. These tools help generate SQL scripts or execute commands to apply schema changes systematically and with version control.
Execution: Migrations are run once in a specific order. They update the database schema to a particular version or state.
Seeds
Purpose: Seeds populate the database with initial or sample data, such as records for reference tables, default user accounts, or necessary operational data.
Use Cases: Seeds are used to start the database with a known set of data, which helps in developing, testing, and deploying applications consistently.
Tools: Many web frameworks and database libraries offer tools for seeding data. These tools typically use code or configuration files to define the data to be inserted.
Execution: Seeds are generally run once during the initial setup of a new database or the first deployment of an application. They can also be used to reset the database to a known state for testing.
Key Differences
Purpose: Migrations are for managing schema changes; seeds are for inserting data.
Execution Frequency: Migrations are executed sequentially to update the schema to a specific version; seeds are usually executed once to initialize the database.
Content: Migrations handle structural changes like creating tables or altering columns; seeds involve data content like inserting rows.
Order: The order is crucial for migrations due to their impact on the database structure. For seeds, the order is less critical but should respect data dependencies.