Performance: Migrations
Migrations affect the initial page load speed and backend response time by managing database schema changes efficiently.
await connection.runMigrations(); // run only pending migrationsawait connection.synchronize(true); // auto-sync schema on every app start| Pattern | Backend Delay | User Request Impact | LCP Impact | Verdict |
|---|---|---|---|---|
| Full schema sync on startup | High (500ms+) | Blocks startup | High | [X] Bad |
| Incremental migrations on startup | Low (under 100ms) | No block on requests | Low | [OK] Good |
| Migrations during user requests | High | Blocks user input | High | [X] Bad |
| Migrations before requests | Low | No block on user input | Low | [OK] Good |
migration:run applies pending migrations to the database.migration:create creates a new migration file, migration:generate generates migration from schema changes, and migration:revert undoes the last migration.up() is run?export class AddAgeColumn1678901234567 {
async up(queryRunner) {
await queryRunner.query(`ALTER TABLE users ADD COLUMN age int`);
}
async down(queryRunner) {
await queryRunner.query(`ALTER TABLE users DROP COLUMN age`);
}
}up() method runs SQL to add a column named 'age' of type integer to the 'users' table.npx typeorm migration:run. What is the likely problem?export class RemoveEmailColumn1678901234568 {
async up(queryRunner) {
await queryRunner.query(`ALTER TABLE users DROP email`);
}
async down(queryRunner) {
await queryRunner.query(`ALTER TABLE users ADD email varchar(255)`);
}
}ALTER TABLE users DROP email is incorrect SQL syntax for dropping a column.ALTER TABLE users DROP COLUMN email. Missing 'COLUMN' causes the error.up() and down() methods are correct?ALTER TABLE table_name RENAME COLUMN old_name TO new_name.async up(queryRunner) {
await queryRunner.query(`ALTER TABLE accounts RENAME COLUMN username TO user_name`);
}
async down(queryRunner) {
await queryRunner.query(`ALTER TABLE accounts RENAME COLUMN user_name TO username`);
} uses correct syntax for both up() and down() to rename and revert the column name. async up(queryRunner) {
await queryRunner.query(`ALTER TABLE accounts CHANGE username user_name varchar(255)`);
}
async down(queryRunner) {
await queryRunner.query(`ALTER TABLE accounts CHANGE user_name username varchar(255)`);
} uses MySQL-specific CHANGE syntax which may not be portable. async up(queryRunner) {
await queryRunner.query(`ALTER TABLE accounts DROP COLUMN username`);
await queryRunner.query(`ALTER TABLE accounts ADD COLUMN user_name varchar(255)`);
}
async down(queryRunner) {
await queryRunner.query(`ALTER TABLE accounts DROP COLUMN user_name`);
await queryRunner.query(`ALTER TABLE accounts ADD COLUMN username varchar(255)`);
} drops and adds columns, losing data. async up(queryRunner) {
await queryRunner.query(`RENAME COLUMN username TO user_name`);
}
async down(queryRunner) {
await queryRunner.query(`RENAME COLUMN user_name TO username`);
} misses the table name in the query, causing error.Pass the test and this pattern moves unknown