定制 aniart-solutions/bitrix-migrations 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aniart-solutions/bitrix-migrations

最新稳定版本:v1.2.0

Composer 安装命令:

composer require aniart-solutions/bitrix-migrations

包简介

Database migrations for Bitrix CMS

README 文档

README

Latest Stable Version Total Downloads License PHP Version

Bitrix-migrations

Database migrations for Bitrix CMS and more

Installation

  1. composer require aniart-solutions/bitrix-migrations

  2. cp vendor/aniart-solutions/bitrix-migrations/migrator migrator - copy the executable file to a convenient location.

  3. Open the file and make sure that $_SERVER['DOCUMENT_ROOT'] is set correctly. Change settings if needed.

  4. php migrator install

This command will create a table in the database to store the names of executed migrations.

By default:

  1. The table is called migrations.

  2. composer.json and migrator are located in the site root.

  3. Migration files will be created in the ./migrations directory 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 migrator and ./migrations inaccessible via HTTP through the web server. *

Usage

Workflow

The workflow is done through the console and is briefly described as follows:

  1. Create a migration file (or files) using php migrator make migration_name

A migration file is a class with two methods: up() and down()

  1. Implement the necessary database changes in the up() method. Optionally, implement rollback of these changes in the down() method.

  2. Apply available migrations - php migrator migrate

  3. 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

CommandDescription
`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:

NameDescriptionAliases
`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:

  1. A Bitrix event handler is triggered

  2. A migration file is created as with php migrator make

  3. The migration is marked as applied

  4. 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:

EventComments
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 using php 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:

  1. Replace the Bitrix core connection with the core of another system.

  2. Implement your own analogue of AniartSolutions\BitrixMigrations\Repositories\BitrixDatabaseRepository; and use it.

  3. Optionally disable existing migration templates by creating your own.

统计信息

  • 总下载量: 22
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固