📜 ⬆️ ⬇️

Versioning the database structure in MySQL: MySQL Migration with PHP

When a project's database grows beyond three to five tables, while continuing to change constantly, the inconvenience of exchanging changes between developers is born. The problem is as old as the world, but I did not manage to find an instrument satisfying my requirements in November 2009.

My tool requirements are very simple:



')
Similar tools


Let's look at existing implementations. If I have missed something - write in the comments - I will be grateful, I will add to the article.


Having considered all these options, I decided to create my own tool that would satisfy all these requirements. Two projects influenced my decisions when building a bicycle writing tool: code.google.com/p/mysql-php-migrations and svn.limb-project.com/misc/migration

What we can do:



What we do not know how:

  1. Create ALTER-scripts - everything is stored inside classes.
  2. Roll dumps and ALTER scripts.
  3. Work with PDO - we need MySQLi.
  4. Run for a beer.


What we have?

Only one config file

config.ini
host=localhost
user=root
password=
db=mmpi_test
savedir=db ;
verbose=On
versiontable=db_version ; -


A small library of code.

Only one executable file: ./migration.php

Several teams:



It's simple. You can experiment on a test base and get to work.

System requirements:




What you need to know


The migrate command works with parameters that are recognized by the strtotime function. If no parameters are specified, the current time is taken. The name of the migration class, as well as the variable inside it stores the timestamp of its creation. The MySQL user should have the right to create a new database - the tool uses this when generating a new migration and when rolling in / away from the migrations, after work the script deletes the temporary database.

Work mechanism


When creating a migration: A temporary database is created, the schema.php is merged into it (there are initialization schema requests), then the migrations are sequentially merged into the most recent one. A snapshot array of each of the bases is removed, differences are identified, a new migration class is created. If you need any data manipulations during upgrade / downgrade, edit the class.
When applying migration : a list of migrations is read, the migration to which an upgrade / downgrade is needed is performed; all migrations from the current to the target one are sequentially performed.

Migration class : contains two arrays up and down, requests from which are sequentially executed when applying this migration in the corresponding direction.

Where to get?



hg clone bitbucket.org/idler/mmp

PS Well, here it is necessary to write that this is the alpha version! Please code for kicking, but not to death. Bugreport and funkvesty welcome. It works for me, but it does not mean that it will work for everyone.

PPS I'm thinking about the version for SQLite.

Source: https://habr.com/ru/post/90052/


All Articles