You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
5 Hrs
26 Min
21 Seg
Curso de SQL y MySQL

Curso de SQL y MySQL

Genaro Bernardino

Genaro Bernardino

Domina las Migrations: Evoluciona y Gestiona tu Base de Datos con Eficiencia

18/19
Resources

Database management through migrations is a fundamental tool for any developer working with scalable projects. This structured approach allows us to maintain the integrity of our databases as they evolve with our applications, facilitating teamwork and minimizing errors during development.

What are migrations and why are they important?

Migrations are a structured and programmatic way of keeping the structure of our databases up to date. Although they are closely related to MySQL, they are actually part of programming frameworks external to the database.

Prior to migrations, the typical process for updating databases consisted of:

  1. Maintaining a SQL file with the complete schema.
  2. Create "alters" files for each version.
  3. Manually run these files in order.

This process was prone to errors, especially when:

  • New developers were added to the project.
  • Needed to migrate the structure to test servers.
  • Working in large teams with parallel development.

Migrations solve these problems by providing a series of orderly steps to evolve the database from the first tables to the current structure, including:

  • Creating new tables
  • Modification of column names
  • Changes in data types
  • Deleting obsolete tables

How do migrations work in practice?

A crucial feature of migrations is that each script contains:

  1. Instructions to move forward (up): Implement the desired changes in the structure.
  2. Down instructions: Reverse the changes to leave the database as it was before.

For example, if a migration creates a table, the way back must delete that table. This allows to undo changes that did not work correctly.

// Example of migration in G2 (PHP)public function saveUp(){ $this->createTable('brands', [ ' brand_name' => $this->string(50)->notNull()->unique(), ' founder' => $this->string(40)->defaultValue('so-and-so'), ' employees' => $this->integer()->notNull()->defaultValue(1) ]);
}
public function saveDown(){ $this->dropTable('brands');}

It is important to understand that when reversing migrations we may lose data. For example, if we delete a column and then want to revert that change, the column will be recreated but the original data will be lost. This is why a culture of backups is essential.

How are migrations recorded and controlled?

An essential component of the migration system is a central table (usually called "migrations") that records:

  • Which migrations have been applied
  • When each migration was executed (with timestamp)

This table is the source of truth about the current state of the database structure. If a migration is not recorded there, it means that it has not been executed correctly.

Best practices for migrations:

  1. Encapsulate related operations: Each migration should contain as few related operations as possible.
  2. Descriptive names: The name of the migration should clearly explain what it does.
  3. Separate by context: If you are going to modify different tables, it is better to create separate migrations.
  4. Transactions: Use transactions whenever possible to ensure integrity.

Migrations are especially useful in CI/CD (Continuous Integration/Continuous Deployment) environments, where they can run automatically with each deployment, keeping all database instances synchronized.

How are migrations implemented in different frameworks?

Although the example shown uses G2 (a PHP framework), the concept is similar in most frameworks:

  • Django: Creates migrations automatically when you modify the model, although you can edit them manually.
  • Ruby on Rails: Offers a robust migration system with a clear syntax.
  • Node.js: Frameworks such as Sequelize or Knex provide migration tools.

The main difference between implementations is usually in whether migrations are generated automatically or written manually, and in the specific syntax for defining changes.

The advantage of systems like G2 is that they give you full control over the migration, allowing you to design exactly what will be executed, which may be preferable for developers who want to maintain precise control over their databases.

Migrations represent a paradigm shift in database management that, while it can take time to master, becomes an indispensable tool for any serious project. Their ability to keep data structures synchronized between different environments and developers makes them invaluable in modern software development.

Have you implemented migrations in your projects? What framework do you use to manage the evolution of your databases? Share your experience in the comments.

Contributions 0

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?