...
To set up database migration in your project, follow the following steps:
Run
~6composer require nostromo/module-database-migration
:dev-nostromo-8
Activate migrations by hooking the PHP code
DatabaseMigrationController::runUpdate();
where you want it. Two good examples are shown below.Put your migration files into ./src/migrations/ (
Environment::get()->migrationDir
). Migration files are simple SQL files which hold semicolon separated SQL statements. Example below.
Consistency
Migration file names
...
New migration files therefore need to have a lower sorting than old ones. Else they will be ignored. For example, a meaningful file name scheme would be:
20190403_clear_api_logs.sql
20190404_added_indexes.sql
20190406_my_new_sql_file.sql
The database migration will keep track of which migration files were executed, and how many SQL statements of each file were executed. The statistics are saved to the database itself into __database_migration
.
...
The behavior described above brings a problem when you are working with several branches and add migration files in parallel. If you have already run the last migration update of your branch, the migration file that was created first in another branch will be ignored. Like so:
20190403_clear_api_logs.sql
20190404_added_indexes.sql
20190405_merged_from_other_branch.sql20190406_file_from_my_branch.sql
You will not be warned about this. However, if you did not run the migration update, there will no problem.
...
In version 6.5.6, new SQL comment based commands were added, so that it is possible to write different SQL statements for each database management system. Available statements are:
--switch engine;
--case ______;
--end;
It is very important to put semicolons behind each statement. Else they will act as regular comments.
...