aniart-solutions/bitrix-migrations
最新稳定版本:v1.2.0
Composer 安装命令:
composer require aniart-solutions/bitrix-migrations
包简介
Database migrations for Bitrix CMS
关键字:
README 文档
README
Bitrix-migrations
Database migrations for Bitrix CMS and more
Installation
-
composer require aniart-solutions/bitrix-migrations -
cp vendor/aniart-solutions/bitrix-migrations/migrator migrator- copy the executable file to a convenient location. -
Open the file and make sure that
$_SERVER['DOCUMENT_ROOT']is set correctly. Change settings if needed. -
php migrator install
This command will create a table in the database to store the names of executed migrations.
By default:
-
The table is called
migrations. -
composer.jsonandmigratorare located in the site root. -
Migration files will be created in the
./migrationsdirectory relative to the file copied in step 2.
If necessary, all of this can be changed in the copied migrator file.
- It is highly recommended to make
migratorand./migrationsinaccessible via HTTP through the web server. *
Usage
Workflow
The workflow is done through the console and is briefly described as follows:
- Create a migration file (or files) using
php migrator make migration_name
A migration file is a class with two methods: up() and down()
-
Implement the necessary database changes in the
up()method. Optionally, implement rollback of these changes in thedown()method. -
Apply available migrations -
php migrator migrate -
Add migration files to version control so they can be run on other machines.
Available Commands
You can get a list of available commands in the console - php migrator list
| Command | Description |
|---|---|
| `php migrator install` | Creates a table to store migrations. Run once. |
| `php migrator make migration_name` |
Creates a migration file
Options: `-d foo/bar` - specify a subdirectory in which the migration will be created |
| `php migrator migrate` | Applies all available migrations. Previously applied migrations are not applied. |
| `php migrator rollback` |
Rolls back the last migration (the `down()` method). After that, it can be applied again. Options: `--hard` - perform a hard rollback without calling the `down()` method `--delete` - delete the migration file after rollback. |
| `php migrator templates` | Shows a detailed table with all existing migration templates |
| `php migrator status` | Shows available migrations to run, as well as the last executed ones. |
| `php migrator archive` |
Moves all migrations to archive. By default, this is the `archive` directory, but it can be overridden in the config by specifying "dir_archive" Options: `-w 10` - do not archive the last N migrations |
Migration Templates
Since changing the Bitrix database structure through its API is extremely unpleasant, there is a migration template mechanism to facilitate this process, which works as follows:
When generating a migration file, you can specify its template: php migrator make migration_name -t add_iblock where add_iblock is the template name.
A class with a boilerplate from the template will be generated and you only need to specify the details (for example, the name and code of the infoblock)
You can add your own migration templates directly in the migrator file using TemplateCollection::registerTemplate()
Available templates:
| Name | Description | Aliases |
|---|---|---|
| `default` | Clean default template | |
| `add_iblock_type` | Adding an infoblock type | |
| `add_iblock` | Adding an infoblock | |
| `add_iblock_element_property` | Adding a property to an infoblock | `add_iblock_prop`, `add_iblock_element_prop`, `add_element_prop`, `add_element_property` |
| `add_uf` | Adding a UF property | |
| `query` | Custom database query via D7 API | |
| `add_table` | Creating a table via D7 API | `create_table` |
| `delete_table` | Deleting a table via D7 API | `drop_table` |
Automatic Migration Creation
Another killer feature is the automatic migration creation mode.
To enable it, add something like the following to init.php
AniartSolutions\BitrixMigrations\Autocreate\Manager::init($_SERVER["DOCUMENT_ROOT"].'/migrations');
The path to the directory similar to the config in the migrator file is passed to the Manager::init() method.
After that, when performing a number of actions in the admin panel, the following will happen:
-
A Bitrix event handler is triggered
-
A migration file is created as with
php migrator make -
The migration is marked as applied
-
A notification about the previous points is shown
Enabling this mode allows you to get rid of manually writing migrations for many cases. Nothing needs to be edited in automatically created migrations.
List of handled events:
| Event | Comments |
|---|---|
| Adding an infoblock | |
| Updating infoblock base fields | Due to the specifics of the Bitrix admin panel, this migration is often created when it's not needed, for example when adding a custom property to an infoblock. Nothing fatal, but you have to accept it. |
| Deleting an infoblock | |
| Adding a custom property to an infoblock | |
| Updating an infoblock custom property | Migration is created only if any of the property attributes were changed |
| Deleting an infoblock custom property | |
| Adding a UF property anywhere (infoblock section, user, highload block) | Unfortunately, Bitrix does not allow tracking changes to such a property - only addition and deletion |
| Deleting a UF property | |
| Adding a highload block | |
| Updating a highload block | Migration is created only if any of the highload block attributes were changed |
| Deleting a highload block | |
| Adding a user group | |
| Updating a user group | |
| Deleting a user group |
- Migrations use
OnBefore...events. If an error occurred during your change (for example, the site binding was not specified when adding an infoblock) and a notification was shown that the migration was created, you must manually rollback such a migration usingphp migrator rollback --hard --delete*
Migration Error Handling
To cancel a migration during its execution, it is enough to throw an exception - php throw new MigrationException('Error text here');
Neither the migration itself nor subsequent ones will be applied.
Usage Outside Bitrix
The package is created for use with Bitrix, but it can be easily used in other systems as well.
To do this, you need to in the migrator file:
-
Replace the Bitrix core connection with the core of another system.
-
Implement your own analogue of
AniartSolutions\BitrixMigrations\Repositories\BitrixDatabaseRepository;and use it. -
Optionally disable existing migration templates by creating your own.
统计信息
- 总下载量: 22
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-09