daursu/laravel-zero-downtime-migration
Composer 安装命令:
composer require daursu/laravel-zero-downtime-migration
包简介
Zero downtime migrations with Laravel and percona toolkit
README 文档
README
Zero downtime migrations with Laravel and gh-ost or pt-online-schema-change.
NOTE: works only with MySQL databases, including (Percona & MariaDB).
Installation
Compatible with Laravel 12. For Laravel 11, please use v2.1.0. For older version support, please use v1.0.
Prerequisites
If you are using gh-ost then make sure you download the binary from their releases page:
If you are using pt-online-schema-change then make sure you have percona-toolkit installed.
- On Mac you can install it using brew
brew install percona-toolkit. - On Debian/Ubuntu
apt-get install percona-toolkit.
Installation steps
- Run
composer require daursu/laravel-zero-downtime-migration - (Optional) Add the service provider to your
config/app.phpfile, if you are not using autoloading.
Daursu\ZeroDowntimeMigration\ServiceProvider::class,
- Update your
config/database.phpand add a new connection:
This package support pt-online-schema-change and gh-ost. Below are the configurations for each package:
gh-ost
'connections' => [ 'zero-downtime' => [ 'driver' => 'gh-ost', // This is your master write access database connection details 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), // Additional options, depending on your setup // all options available here: https://github.com/github/gh-ost/blob/master/doc/cheatsheet.md 'params' => [ '--max-load=Threads_running=25', '--critical-load=Threads_running=1000', '--chunk-size=1000', '--throttle-control-replicas=myreplica.1.com,myreplica.2.com', '--max-lag-millis=1500', '--verbose', '--switch-to-rbr', '--exact-rowcount', '--concurrent-rowcount', '--default-retries=120', ], ], ],
pt-online-schema-change
'connections' => [ 'zero-downtime' => [ 'driver' => 'pt-online-schema-change', // This is your master write access database connection details 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), // Additional options, depending on your setup // all options available here: https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html 'params' => [ '--nocheck-replication-filters', '--nocheck-unique-key-change', '--recursion-method=none', '--chunk-size=2000', ], ], ],
Usage
When writing a new migration, use the helper facade ZeroDowntimeSchema instead of Laravel's Schema,
and all your commands will run through gh-ost or pt-online-schema-change.
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; use Daursu\ZeroDowntimeMigration\ZeroDowntimeSchema; class AddPhoneNumberToUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { ZeroDowntimeSchema::table('users', function (Blueprint $table) { $table->string('phone_number')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { ZeroDowntimeSchema::table('users', function (Blueprint $table) { $table->dropColumn('phone-number'); }); } }
Run php artisan:migrate
Configuration
All the configuration is done inside config/database.php on the connection itself.
You can pass down custom flags to the raw pt-online-schema-change command.
Simply add the parameters you want inside the params array like so:
'params' => [ '--nocheck-replication-filters', '--nocheck-unique-key-change', '--recursion-method=none', '--chunk-size=2000', ]
You can find all the possible options here: https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html
Tests
The ZeroDowntimeSchema facades allows you disable running pt-online-schema-change during tests.
To do so, in your base test case TestCase.php under the setUp method add the following:
public function setUp() { // ... existing code ZeroDowntimeSchema::disable(); }
This will disable pt-online-schema-change and all the migrations using the helper facade will run
through the default laravel migrator.
Custom connection name
By default, the connection name used by ZeroDowntimeSchema helper is set to zero-downtime, however you can
override this if you called your connection something else in config/database.php.
To do so, in your AppServiceProvider.php add the following under the boot() method:
public function boot() { // ... existing code ZeroDowntimeSchema::$connection = 'your-custom-name'; }
Replication
If your database is running in a cluster with replication, then you need to
configure how pt-online-schema-changes finds your replica slaves.
Here's an example setup, but feel free to customize it to your own needs
'params' => [ '--nocheck-replication-filters', '--nocheck-unique-key-change', '--recursion-method=dsn=D=database_name,t=dsns', '--chunk-size=2000', ]
- Replace
database_namewith your database name. - Create a new table called
dsns
CREATE TABLE `dsns` ( `id` int(11) NOT NULL AUTO_INCREMENT, `parent_id` int(11) DEFAULT NULL, `dsn` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB;
- Add a new row for each replica you have, example
INSERT INTO `dsns` (`id`, `parent_id`, `dsn`) VALUES (1, NULL, 'h=my-replica-1.example.org,P=3306');
Upgrade to v1
There is one breaking change introduced in v1, that requires to modify
the configuration in database.php. The additional parameters array passed down to
pt-online-schema-change or gh-ost has been renamed from options to params.
This change was required as the name options conflicts with Laravel's database configuration
that is automatically passed down to PDO.
// Before 'options' => [ '--nocheck-replication-filters', '--nocheck-unique-key-change', '--recursion-method=none', '--chunk-size=2000', ] // After 'params' => [ '--nocheck-replication-filters', '--nocheck-unique-key-change', '--recursion-method=none', '--chunk-size=2000', ]
Gotchas
- This only works with MySQL, Percona & MariaDB
- Use this tool when you need to alter a table, not when creating or dropping tables.
daursu/laravel-zero-downtime-migration 适用场景与选型建议
daursu/laravel-zero-downtime-migration 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.06M 次下载、GitHub Stars 达 88, 最近一次更新时间为 2018 年 07 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 daursu/laravel-zero-downtime-migration 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 daursu/laravel-zero-downtime-migration 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.06M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 88
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-07-01