├── .github └── workflows │ ├── ci.yml │ └── tests.yml ├── .php_cs ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── online-migrator.php ├── phpunit.xml.github └── src ├── CombineIncompatible.php ├── InnodbOnlineDdl.php ├── OnlineIncompatible.php ├── OnlineIncompatibleWithDown.php ├── OnlineIncompatibleWithUp.php ├── OnlineMigrator.php ├── OnlineMigratorServiceProvider.php ├── PtOnlineSchemaChange.php └── Strategy ├── InnodbOnlineDdl.php ├── PtOnlineSchemaChange.php └── StrategyInterface.php /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: tests 2 | 3 | on: 4 | push: 5 | 6 | jobs: 7 | styling: 8 | name: PHP-CS-Fixer 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Checkout branch 12 | if: github.ref != 'refs/heads/master' 13 | uses: actions/checkout@v2 14 | - name: Cache .php_cs.cache 15 | if: github.ref != 'refs/heads/master' 16 | uses: actions/cache@v2 17 | with: 18 | path: ./.php_cs.cache 19 | key: php-cs-cache 20 | - name: Run php-cs-fixer 21 | if: github.ref != 'refs/heads/master' 22 | uses: docker://oskarstark/php-cs-fixer-ga 23 | - name: Apply fixes 24 | if: github.ref != 'refs/heads/master' 25 | uses: stefanzweifel/git-auto-commit-action@v4.0.0 26 | with: 27 | branch: ${{ github.head_ref }} 28 | commit_message: Automatic application of php-cs-fixer changes 29 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: tests 2 | 3 | on: 4 | push: 5 | 6 | jobs: 7 | tests: 8 | runs-on: ubuntu-20.04 9 | 10 | strategy: 11 | fail-fast: true 12 | matrix: 13 | php: [7.3, 7.4] 14 | stability: [prefer-lowest, prefer-stable] 15 | 16 | name: PHP ${{ matrix.php }} - ${{ matrix.stability }} 17 | 18 | steps: 19 | - name: Checkout code 20 | uses: actions/checkout@v2 21 | - name: Cache dependencies 22 | uses: actions/cache@v2 23 | with: 24 | path: ~/.composer/cache/files 25 | key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} 26 | - name: Download Percona Repo 27 | run: wget --quiet https://repo.percona.com/apt/percona-release_latest.generic_all.deb 28 | - name: Install Percona Repo 29 | run: sudo dpkg -i percona-release_latest.generic_all.deb 30 | - name: Install Percona Toolkit 31 | run: sudo apt-get install -y -qq percona-toolkit 32 | - name: Workaround outdated PTOSC validation 33 | run: sudo sed -i -r -e 's/^([ \t]*if \()(\$vp->cmp\()/\1 0 \&\& \2/g' /usr/bin/pt-online-schema-change 34 | - name: Start Mysql 35 | run: sudo systemctl start mysql 36 | - name: Create Mysql DB 37 | run: mysql --user=root --password=root --host=127.0.0.1 -e 'CREATE DATABASE IF NOT EXISTS online_migrator_ci;' 38 | - name: Workaround PHP 7.3 not supporting newer authentication 39 | run: mysql --user=root --password=root --host=127.0.0.1 -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'; FLUSH PRIVILEGES;" 40 | - name: Setup PHP 41 | uses: shivammathur/setup-php@v2 42 | with: 43 | php-version: ${{ matrix.php }} 44 | extensions: pdo, pdo_mysql 45 | coverage: none 46 | - name: Composer update 47 | run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress 48 | - name: Execute tests 49 | run: vendor/bin/phpunit --testdox --configuration phpunit.xml.github 50 | -------------------------------------------------------------------------------- /.php_cs: -------------------------------------------------------------------------------- 1 | [ 8 | 'syntax' => 'short', 9 | ], 10 | 'align_multiline_comment' => [ 11 | 'comment_type' => 'phpdocs_only', 12 | ], 13 | 'binary_operator_spaces' => [ 14 | 'align_double_arrow' => true, 15 | 'align_equals' => false, 16 | ], 17 | 'blank_line_after_namespace' => true, 18 | 'blank_line_after_opening_tag' => true, 19 | 'blank_line_before_statement' => [ 20 | 'statements' => [ 21 | 'case', 22 | 'continue', 23 | 'declare', 24 | 'default', 25 | 'die', 26 | 'do', 27 | 'exit', 28 | 'for', 29 | 'foreach', 30 | 'goto', 31 | 'if', 32 | 'return', 33 | 'switch', 34 | 'throw', 35 | 'try', 36 | 'while', 37 | 'yield', 38 | ], 39 | ], 40 | 'braces' => true, 41 | 'cast_spaces' => true, 42 | 'class_definition' => true, 43 | 'concat_space' => [ 44 | 'spacing' => 'one', 45 | ], 46 | 'declare_equal_normalize' => true, 47 | 'elseif' => true, 48 | 'encoding' => true, 49 | 'full_opening_tag' => true, 50 | 'function_declaration' => true, 51 | 'function_typehint_space' => true, 52 | 'hash_to_slash_comment' => true, 53 | 'heredoc_to_nowdoc' => true, 54 | 'include' => true, 55 | 'increment_style' => [ 56 | 'style' => 'post', 57 | ], 58 | 'indentation_type' => true, 59 | 'line_ending' => true, 60 | 'lowercase_cast' => true, 61 | 'lowercase_constants' => true, 62 | 'lowercase_keywords' => true, 63 | 'magic_constant_casing' => true, 64 | 'method_argument_space' => true, 65 | 'method_separation' => true, 66 | 'multiline_comment_opening_closing' => true, 67 | 'native_function_casing' => true, 68 | 'new_with_braces' => true, 69 | 'no_alias_functions' => true, 70 | 'no_blank_lines_after_class_opening' => true, 71 | 'no_blank_lines_after_phpdoc' => true, 72 | 'no_closing_tag' => true, 73 | 'no_empty_phpdoc' => true, 74 | 'no_empty_statement' => true, 75 | 'no_extra_consecutive_blank_lines' => true, 76 | 'no_leading_import_slash' => true, 77 | 'no_leading_namespace_whitespace' => true, 78 | 'no_mixed_echo_print' => [ 79 | 'use' => 'echo', 80 | ], 81 | 'no_multiline_whitespace_around_double_arrow' => true, 82 | 'no_multiline_whitespace_before_semicolons' => true, 83 | 'no_short_bool_cast' => true, 84 | 'no_singleline_whitespace_before_semicolons' => true, 85 | 'no_spaces_after_function_name' => true, 86 | 'no_spaces_around_offset' => true, 87 | 'no_spaces_inside_parenthesis' => true, 88 | 'no_trailing_comma_in_list_call' => true, 89 | 'no_trailing_comma_in_singleline_array' => true, 90 | 'no_trailing_whitespace' => true, 91 | 'no_trailing_whitespace_in_comment' => true, 92 | 'no_unneeded_control_parentheses' => true, 93 | 'no_unreachable_default_argument_value' => true, 94 | 'no_unused_imports' => true, 95 | 'no_useless_return' => true, 96 | 'no_whitespace_before_comma_in_array' => true, 97 | 'no_whitespace_in_blank_line' => true, 98 | 'normalize_index_brace' => true, 99 | 'not_operator_with_successor_space' => true, 100 | 'object_operator_without_whitespace' => true, 101 | 'ordered_imports' => true, 102 | 'phpdoc_align' => true, 103 | 'phpdoc_indent' => true, 104 | 'phpdoc_inline_tag' => true, 105 | 'phpdoc_no_access' => true, 106 | 'phpdoc_no_package' => true, 107 | 'phpdoc_no_useless_inheritdoc' => true, 108 | 'phpdoc_order' => true, 109 | 'phpdoc_scalar' => true, 110 | 'phpdoc_separation' => true, 111 | 'phpdoc_single_line_var_spacing' => true, 112 | 'phpdoc_summary' => true, 113 | 'phpdoc_to_comment' => false, 114 | 'phpdoc_trim' => true, 115 | 'phpdoc_types' => true, 116 | 'phpdoc_var_without_name' => true, 117 | 'psr4' => true, 118 | 'self_accessor' => true, 119 | 'short_scalar_cast' => true, 120 | 'simplified_null_return' => false, 121 | 'single_blank_line_at_eof' => true, 122 | 'single_blank_line_before_namespace' => true, 123 | 'single_class_element_per_statement' => true, 124 | 'single_import_per_statement' => true, 125 | 'single_line_after_imports' => true, 126 | 'single_quote' => false, 127 | 'list_syntax' => [ 128 | 'syntax' => 'short', 129 | ], 130 | 'space_after_semicolon' => true, 131 | 'standardize_not_equals' => true, 132 | 'switch_case_semicolon_to_colon' => true, 133 | 'switch_case_space' => true, 134 | 'ternary_operator_spaces' => true, 135 | 'trailing_comma_in_multiline_array' => true, 136 | 'trim_array_spaces' => true, 137 | 'unary_operator_spaces' => true, 138 | 'visibility_required' => [ 139 | 'elements' => [ 140 | 'property', 141 | 'method', 142 | ], 143 | ], 144 | 'whitespace_after_comma_in_array' => true, 145 | ]; 146 | 147 | return Config::create() 148 | ->setRules($rules) 149 | ->setUsingCache(true) 150 | ->setRiskyAllowed(true) 151 | ->setFinder( 152 | Finder::create() 153 | ->in(__DIR__) 154 | ->exclude([ 155 | 'apps', 156 | 'lib', 157 | 'vendor', 158 | 'node_modules', 159 | 'phpdocker', 160 | 'storage', 161 | 'tests/cache', 162 | 'public/client', 163 | ]) 164 | ->notName('*.md') 165 | ->notName('*.xml') 166 | ->notName('*.yml') 167 | ->notName('*.blade.php') 168 | ); 169 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 ORIS Intelligence, LLC 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Online Migrator 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/orisintel/laravel-online-migrator.svg?style=flat-square)](https://packagist.org/packages/orisintel/laravel-online-migrator) 4 | [![Build Status](https://img.shields.io/travis/orisintel/laravel-online-migrator/master.svg?style=flat-square)](https://travis-ci.org/orisintel/laravel-online-migrator) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/orisintel/laravel-online-migrator.svg?style=flat-square)](https://packagist.org/packages/orisintel/laravel-online-migrator) 6 | 7 | This package minimizes disruptions when applying Laravel's database migrations 8 | using tools like [Percona Online Schema Change](https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html) 9 | or [InnoDB Online DDL](https://dev.mysql.com/doc/refman/5.6/en/innodb-online-ddl.html). 10 | For example, one can write (mostly) standard Laravel migration files then run 11 | "php artisan migrate". Database changes will be automatically converted into 12 | PTOSC commands or online DDL queries. 13 | 14 | ## Installation 15 | 16 | You can install the package via composer: 17 | 18 | ``` bash 19 | composer require orisintel/laravel-online-migrator 20 | ``` 21 | 22 | The `pt-online-schema-change` command from Percona's toolkit must be in the path 23 | where Artisan will be run, unless InnoDB Online DDL is being used exclusively. 24 | 25 | ### Configuration 26 | 27 | Publish the configuration file: 28 | ``` bash 29 | php artisan vendor:publish --provider='OrisIntel\OnlineMigrator\OnlineMigratorServiceProvider' 30 | ``` 31 | 32 | If not using discovery then add the provider to `config/app.php`: 33 | ``` php 34 | 'providers' => [ 35 | // ... 36 | OrisIntel\OnlineMigrator\OnlineMigratorServiceProvider::class, 37 | ``` 38 | 39 | If changing tables with `enum` columns consider working around "Unknown database 40 | type enum requested" by tweaking `config/online-migrator.php`: 41 | ``` php 42 | 'doctrine-enum-mapping' => env('DOCTRINE_ENUM_MAPPING', 'string'), 43 | ``` 44 | or `.env` with `DOCTRINE_ENUM_MAPPING=string` 45 | 46 | 47 | ## Usage 48 | 49 | Run Artisan's migrate to apply migrations online*: 50 | ``` bash 51 | php artisan migrate 52 | ``` 53 | \*Limitations are documented below. 54 | 55 | Preview what changes it would make: 56 | ``` bash 57 | php artisan migrate --pretend 58 | ``` 59 | 60 | Add PTOSC options using environment variables: 61 | ``` bash 62 | PTOSC_OPTIONS='--recursion-method=none' php artisan migrate 63 | ``` 64 | 65 | Flag migrations known to be incompatible with this tool using the built-in trait: 66 | ``` php 67 | class MyMigration extends Migration 68 | { 69 | use \OrisIntel\OnlineMigrator\OnlineIncompatible 70 | ``` 71 | 72 | Use a different strategy for a single migration: 73 | ``` php 74 | class MyMigration extends Migration 75 | { 76 | use \OrisIntel\OnlineMigrator\InnodbOnlineDdl 77 | ``` 78 | 79 | Do not combine queries for a migration when using PTOSC: 80 | ``` php 81 | class MyMigration extends Migration 82 | { 83 | use \OrisIntel\OnlineMigrator\CombineIncompatible 84 | ``` 85 | 86 | Adding foreign key with index to existing table: 87 | ``` php 88 | class MyColumnWithFkMigration extends Migration 89 | { 90 | public function up() 91 | { 92 | Schema::table('my_table', function ($table) { 93 | $table->integer('my_fk_id')->index(); 94 | }); 95 | 96 | Schema::table('my_table', function ($table) { 97 | $table->foreign('my_fk_id')->references('id')->on('my_table2'); 98 | ``` 99 | 100 | ## Limitations 101 | - Only supports Mysql, specifically those versions supported by PTOSC v3 102 | - With PTOSC 103 | - Adding unique indexes may cause data loss unless tables are manually checked 104 | beforehand [because of how PTOSC works](https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html#id7) 105 | - Adding not-null columns [requires a default](https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html#cmdoption-pt-online-schema-change-alter) 106 | - Renaming a column or dropping a primary key [have additional risks](https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html#id1) 107 | - Foreign key creation should be done separately from column creation or 108 | duplicate indexes may be created with slightly different naming 109 | - Close the `Schema::create()` call and make a separate `Schema::table()` 110 | call for all FKs in the migration 111 | - With InnoDB Online DDL 112 | - See the [MySQL Online DDL documentation](https://dev.mysql.com/doc/refman/5.6/en/innodb-create-index-overview.html) 113 | - May be [problematic on AWS Aurora](https://medium.com/@soomiq/why-you-should-not-use-mysql-5-6-online-ddl-on-aws-aurora-40985d5e90f5) 114 | - Stateful migrations, like those selecting _then_ saving rows, 115 | will instead need to do one of the following: 116 | - Use non-selecting queries like `MyModel::where(...)->update(...)` 117 | - Pipe the raw SQL like `\DB::statement('UPDATE ... SET ... WHERE ...');` 118 | - Use the `OnlineMigratorIncompatible` trait to mark the migration as 119 | incompatible 120 | 121 | ### Testing 122 | 123 | ``` bash 124 | composer test 125 | ``` 126 | 127 | Output is verbose because `passthru` is used to help debug production problems. 128 | Executing as `phpunit --testdox` may be more readable until the verbosity can be 129 | tamed. 130 | 131 | ## Contributing 132 | 133 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 134 | 135 | ## Security 136 | 137 | If you discover any security related issues, please email 138 | opensource@pricespider.com instead of using the issue tracker. 139 | 140 | ## Credits 141 | 142 | - [Paul R. Rogers](https://github.com/paulrrogers) 143 | - [All Contributors](../../contributors) 144 | - [Percona Team](https://www.percona.com/about-percona/team) for `pt-online-schema-change` 145 | 146 | ## License 147 | 148 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 149 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "orisintel/laravel-online-migrator", 3 | "description": "Apply Laravel's database migrations with minimal disruptions using tools like Percona Online Schema Change", 4 | "keywords": [ 5 | "orisintel", 6 | "laravel-online-migrator", 7 | "laravel", 8 | "migration" 9 | ], 10 | "homepage": "https://github.com/orisintel/laravel-online-migrator", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Paul R. Rogers", 15 | "role": "Developer" 16 | }, 17 | { 18 | "name": "ORIS Intelligence", 19 | "email": "opensource@pricespider.com", 20 | "homepage": "https://orisintel.com", 21 | "role": "Organization" 22 | } 23 | ], 24 | "require": { 25 | "php": "^7.3", 26 | "laravel/framework": "^8.0" 27 | }, 28 | "require-dev": { 29 | "doctrine/dbal": "^2.8", 30 | "larapack/dd": "^1.0", 31 | "mockery/mockery": "~1.0", 32 | "orchestra/testbench": "^6.0", 33 | "phpunit/phpunit": "^8.0|^9.0" 34 | }, 35 | "autoload": { 36 | "psr-4": { 37 | "OrisIntel\\OnlineMigrator\\": "src" 38 | } 39 | }, 40 | "autoload-dev": { 41 | "psr-4": { 42 | "OrisIntel\\OnlineMigrator\\Tests\\": "tests" 43 | } 44 | }, 45 | "scripts": { 46 | "test": "vendor/bin/phpunit", 47 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 48 | 49 | }, 50 | "config": { 51 | "sort-packages": true 52 | }, 53 | "extra": { 54 | "laravel": { 55 | "providers": [ 56 | "OrisIntel\\OnlineMigrator\\OnlineMigratorServiceProvider" 57 | ] 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /config/online-migrator.php: -------------------------------------------------------------------------------- 1 | env('ONLINE_MIGRATOR'), 18 | 19 | /* 20 | |-------------------------------------------------------------------------- 21 | | On-line Strategy 22 | |-------------------------------------------------------------------------- 23 | | 24 | | Which tool or technique to use when trying to minimize disruptions during 25 | | migrations. Individual migrations may override this with traits. 26 | | 27 | | Currently there are only two values: 28 | | pt-online-schema-change 29 | | innodb-online-ddl 30 | | 31 | */ 32 | 33 | 'strategy' => env('ONLINE_MIGRATOR_STRATEGY', 'pt-online-schema-change'), 34 | 35 | /* 36 | |-------------------------------------------------------------------------- 37 | | Percona Online Schema Change Options 38 | |-------------------------------------------------------------------------- 39 | | 40 | | Accepts a comma-separated list of options for pt-online-schema-change. 41 | | 42 | | These options are always included unless overwritten with different 43 | | values: 44 | | --alter-foreign-keys-method=auto 45 | | --no-check-alter 46 | | --no-check-unique-key-change 47 | | 48 | */ 49 | 50 | 'ptosc-options' => env('PTOSC_OPTIONS'), 51 | 52 | /* 53 | |-------------------------------------------------------------------------- 54 | | Percona Online Schema Change - Fold Table Case 55 | |-------------------------------------------------------------------------- 56 | | 57 | | Whether to force table case to 'upper' or 'lower'. Mysql can silently 58 | | coerce case but PTOSC cannot. 59 | | 60 | */ 61 | 62 | 'ptosc-fold-table-case' => env('PTOSC_FOLD_TABLE_CASE'), 63 | 64 | /* 65 | |-------------------------------------------------------------------------- 66 | | Register Doctrine Enum Type 67 | |-------------------------------------------------------------------------- 68 | | 69 | | Works around altering non-enum columns blocked by quirk in Doctrine by 70 | | mapping enums to 'string'. Using 'string' does not allow changing enums 71 | | themselves with Eloquent like `->enum(...)->change()`. Can pass along any 72 | | truthy type name in case there are better alternatives. 73 | | 74 | */ 75 | 'doctrine-enum-mapping' => env('DOCTRINE_ENUM_MAPPING'), 76 | ]; 77 | -------------------------------------------------------------------------------- /phpunit.xml.github: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | tests 15 | 16 | 17 | 18 | 19 | src/ 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/CombineIncompatible.php: -------------------------------------------------------------------------------- 1 | output)) { 18 | $this->output = new ConsoleOutput(); 19 | } 20 | 21 | return $this->output; 22 | } 23 | 24 | /** 25 | * Get all of the queries that would be run for a migration. 26 | * 27 | * @param object $migration 28 | * @param string $method 29 | * 30 | * @return array 31 | */ 32 | protected function getQueries($migration, $method) 33 | { 34 | // BEGIN: Copied from parent. 35 | $db = $this->resolveConnection( 36 | $connection = $migration->getConnection() 37 | ); 38 | // END: Copied from parent. 39 | 40 | // This should allow "--pretend" to work when changing any columns on 41 | // tables with enums. 42 | $this->registerEnumMapping($db); 43 | 44 | if (! self::isOnlineAppropriate($migration, $method, $db->getDatabaseName())) { 45 | return parent::getQueries($migration, $method); 46 | } 47 | 48 | if ('mysql' !== $db->getDriverName()) { 49 | throw new \InvalidArgumentException( 50 | 'Database driver unsupported: ' . var_export($db->getDriverName(), 1)); 51 | } 52 | 53 | // BEGIN: Copied from parent. 54 | $queries = $db->pretend(function () use ($migration, $method) { 55 | if (method_exists($migration, $method)) { 56 | $migration->{$method}(); 57 | } 58 | }); 59 | // END: Copied from parent. 60 | 61 | $strategy = self::getStrategy($migration); 62 | 63 | return $strategy::getQueriesAndCommands($queries, $db, $migration->combineIncompatible ?? false); 64 | } 65 | 66 | /** 67 | * Run a migration inside a transaction if the database supports it. 68 | * 69 | * @param object $migration 70 | * @param string $method 71 | * 72 | * @return void 73 | */ 74 | protected function runMigration($migration, $method) 75 | { 76 | // BEGIN: Copied from parent. 77 | $connection = $this->resolveConnection( 78 | $migration->getConnection() 79 | ); 80 | // END: Copied from parent. 81 | 82 | // Map enum even when not using Online Migrator to workaround Doctrine 83 | // blocking changes to any columns with tables containing enums. 84 | $this->registerEnumMapping($connection); 85 | 86 | if (! self::isOnlineAppropriate($migration, $method, $connection->getDatabaseName())) { 87 | parent::runMigration($migration, $method); 88 | 89 | return; 90 | } 91 | 92 | if (5.7 >= floatval(app()->version())) { 93 | // HACK: Output immediately instead of waiting for runner to flush notes since 94 | // it can be slow and output is useful while in progress. 95 | foreach ($this->notes as $note) { 96 | $this->getOutput()->writeln($note); 97 | } 98 | $this->notes = []; 99 | } 100 | 101 | // Instead of running the migration's callback unchanged, we need to 102 | // use the possibly reformatted query as a command. 103 | $queries = $this->getQueries($migration, $method); 104 | 105 | // CONSIDER: Trying to detect, and error out, when PHP code would have 106 | // non-DB changes since actual migration's PHP code won't be executed 107 | // directly (for ex. queue, cache, signal changes). 108 | 109 | // CONSIDER: Trying to run non-alters inside DB transaction. 110 | // CONSIDER: Emitting warning if $this->getSchemaGrammar($connection)->supportsSchemaTransactions(). 111 | 112 | $strategy = self::getStrategy($migration); 113 | 114 | foreach ($queries as &$query) { 115 | $strategy::runQueryOrCommand($query, $connection); 116 | } 117 | } 118 | 119 | private function getStrategy($migration) : string 120 | { 121 | return Str::start( 122 | Str::studly( 123 | $migration->onlineStrategy 124 | ?? config('online-migrator.strategy', 'pt-online-schema-change') 125 | ), 126 | '\\OrisIntel\\OnlineMigrator\\Strategy\\' 127 | ); 128 | } 129 | 130 | private function registerEnumMapping(Connection $db) : void 131 | { 132 | // CONSIDER: Moving to separate package since this isn't directly 133 | // related to online migrations. 134 | $enum_mapping = config('online-migrator.doctrine-enum-mapping'); 135 | 136 | if ($enum_mapping) { 137 | $db->getDoctrineSchemaManager() 138 | ->getDatabasePlatform() 139 | ->registerDoctrineTypeMapping('enum', $enum_mapping); 140 | } 141 | } 142 | 143 | /** 144 | * @param Migration $migration 145 | * @param string $method 146 | * @param null|string $db_name 147 | * 148 | * @return bool 149 | */ 150 | private static function isOnlineAppropriate(Migration &$migration, string &$method, ?string $db_name = '') : bool 151 | { 152 | // Traits on migrations themselves may rule out using this migrator. 153 | $is_online_compatible = (empty($migration->onlineIncompatibleMethods) 154 | || ! in_array($method, $migration->onlineIncompatibleMethods)); 155 | 156 | if (! $is_online_compatible) { 157 | return false; 158 | } 159 | 160 | // Allow overriding general config and `.env` when specified explicitly 161 | // from the CLI or phpunit.xml. 162 | $override = getenv('ONLINE_MIGRATOR'); // Don't use `env()`, it reads `.env`. 163 | 164 | if (0 < strlen($override)) { 165 | return $override ? true : false; 166 | } 167 | 168 | // HACK: Work around slow and sometimes absent PTOSC by using original 169 | // queries when migrating "test" database(s). 170 | // CONSIDER: Instead leveraging configurable blacklist or per-DB option. 171 | $is_test_database = (false !== stripos($db_name, 'test')); 172 | 173 | if ($is_test_database) { 174 | return false; 175 | } 176 | 177 | // Default to enabled whenever package installed yet not explicitly 178 | // enabled. 179 | $config_enabled = config('online-migrator.enabled'); 180 | 181 | return 0 === strlen($config_enabled) || $config_enabled ? true : false; 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /src/OnlineMigratorServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 12 | $this->publishes([ 13 | __DIR__ . '/../config/online-migrator.php' => config_path('online-migrator.php'), 14 | ], 'config'); 15 | } 16 | } 17 | 18 | public function register() 19 | { 20 | $this->mergeConfigFrom(__DIR__ . '/../config/online-migrator.php', 'online-migrator'); 21 | 22 | parent::register(); 23 | } 24 | 25 | /** 26 | * Register the migrator service. 27 | * 28 | * @return void 29 | */ 30 | public function registerMigrator() 31 | { 32 | // Provides hook to send SQL DDL changes through pt-online-schema-change in CLI 33 | $this->app->singleton('migrator', function ($app) { 34 | return new OnlineMigrator($app['migration.repository'], $app['db'], $app['files']); 35 | }); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/PtOnlineSchemaChange.php: -------------------------------------------------------------------------------- 1 | table('information_schema.tables') 63 | ->where('table_name', $table_name) 64 | ->value('engine'); 65 | // CONSIDER: Blacklisting known-to-be unsupported instead, or making 66 | // whitelist configurable, since others may have similar online DDL. 67 | if ('InnoDB' !== $engine) { 68 | // CONSIDER: Using different strategy, like PTOSC. 69 | return $query_or_command_str; 70 | } 71 | 72 | $separator = ! empty($alter_parts) ? ', ' : ' '; 73 | 74 | // CONSIDER: Making algorithm and lock configurable generally and 75 | // per migration. 76 | // CONSIDER: Falling back to 'COPY' if 'INPLACE' is stopped. 77 | $algorithm = null; 78 | 79 | if (! preg_match('/[\s,]\s*ALGORITHM\s*=\s*([^\s]+)/imu', $query_or_command_str, $algo_parts)) { 80 | $algorithm = static::isInplaceCompatible($query_or_command_str, $connection) 81 | ? 'INPLACE' : 'COPY'; 82 | 83 | $query_or_command_str .= $separator . 'ALGORITHM=' . $algorithm; 84 | } else { 85 | $algorithm = strtoupper(trim($algo_parts[1])); 86 | } 87 | 88 | if (! preg_match('/[\s,]\s*LOCK\s*=/iu', $query_or_command_str)) { 89 | $has_auto_increment = preg_match('/\bAUTO_INCREMENT\b/imu', $alter_parts[2] ?? ''); 90 | // CONSIDER: Supporting non-alter statements like "CREATE (FULLTEXT) INDEX". 91 | $has_fulltext = preg_match('/\A\s*ADD\s+FULLTEXT\b/imu', $alter_parts[2] ?? '') 92 | || 0 === stripos($create_parts[2] ?? '', 'FULLTEXT'); 93 | $lock = 'COPY' === $algorithm || $has_auto_increment || $has_fulltext 94 | ? 'SHARED' : 'NONE'; 95 | $query_or_command_str .= $separator . 'LOCK=' . $lock; 96 | } 97 | } 98 | 99 | return $query_or_command_str; 100 | } 101 | 102 | private static function isInplaceCompatible(string $query_str, Connection $connection) : bool 103 | { 104 | // Migration authors may disable FKs to allow in-place altering. 105 | // CONSIDER: Automatically setting foreign_key_checks=OFF, then back ON. 106 | if (preg_match('/\bFOREIGN\s+KEY\b/imu', $query_str)) { 107 | $foreign_key_checks = $connection->select('SELECT @@FOREIGN_KEY_CHECKS')[0]->{'@@FOREIGN_KEY_CHECKS'}; 108 | 109 | if (1 === $foreign_key_checks) { 110 | return false; 111 | } 112 | } 113 | 114 | return preg_match( 115 | '/\b(' . implode('|', static::INPLACE_INCOMPATIBLE) . ')\b/imu', 116 | $query_str 117 | ) ? false : true; 118 | } 119 | 120 | /** 121 | * Execute query or on-line command. 122 | * 123 | * @param array $query like [ 'query' => string, 'bindings' => array ] 124 | * @param Connection $connection already established with database 125 | * 126 | * @throws \UnexpectedValueException 127 | * 128 | * @return void 129 | */ 130 | public static function runQueryOrCommand(array &$query, Connection $connection) : void 131 | { 132 | // Always run unchanged query since this strategy does not need to 133 | // execute commands of other tools. 134 | // CONSIDER: Moving this simple implementation to skeletal base class. 135 | $connection->affectingStatement($query['query'], $query['bindings']); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /src/Strategy/PtOnlineSchemaChange.php: -------------------------------------------------------------------------------- 1 | string, 'changes' => array]. */ 27 | $combining = []; 28 | 29 | $queries_commands = []; 30 | 31 | foreach ($queries as &$query) { 32 | if ( 33 | ! $combineIncompatible 34 | && $combinable = self::getCombinableParts($query) 35 | ) { 36 | // First adjacent combinable. 37 | if (empty($combining)) { 38 | $combining = $combinable; 39 | 40 | continue; 41 | } 42 | 43 | // Same table, so combine changes into comma-separated string. 44 | if ($combining['table_name'] === $combinable['table_name']) { 45 | $combining['changes'] = 46 | (! empty($combining['changes']) ? $combining['changes'] . ', ' : '') 47 | . $combinable['changes']; 48 | 49 | continue; 50 | } 51 | 52 | // Different table, so store previous combinables and reset. 53 | $queries_commands[] = self::getCombinedWithBindings($combining, $connection); 54 | $combining = $combinable; 55 | 56 | continue; 57 | } 58 | 59 | // Not combinable, so store any previous combinables. 60 | if (! empty($combining)) { 61 | $queries_commands[] = self::getCombinedWithBindings($combining, $connection); 62 | $combining = []; 63 | } 64 | 65 | $query['query'] = self::getQueryOrCommand($query, $connection); 66 | $queries_commands[] = $query; 67 | } 68 | 69 | // Store residual combinables so they aren't lost. 70 | if (! empty($combining)) { 71 | $queries_commands[] = self::getCombinedWithBindings($combining, $connection); 72 | } 73 | 74 | return $queries_commands; 75 | } 76 | 77 | /** 78 | * @param array $combining like ['table_name' => string, 'changes' => string] 79 | * @param Connection $connection 80 | * 81 | * @return array like ['query' => string, 'binding' => array, 'time' => float]. 82 | */ 83 | private static function getCombinedWithBindings(array $combining, Connection $connection) : array 84 | { 85 | $query_bindings_time = [ 86 | 'query' => "ALTER TABLE $combining[escape]$combining[table_name]$combining[escape] $combining[changes]", 87 | 'bindings' => [], 88 | 'time' => 0.0, 89 | ]; 90 | $query_bindings_time['query'] = self::getQueryOrCommand($query_bindings_time, $connection); 91 | 92 | return $query_bindings_time; 93 | } 94 | 95 | /** 96 | * @return array like 'table_name' => string, 'changes' => string]. 97 | */ 98 | public static function getCombinableParts(array $query) : array 99 | { 100 | // CONSIDER: Combining if all named or all unnamed. 101 | if (! empty($query['bindings'])) { 102 | return []; 103 | } 104 | 105 | $parts = self::getOnlineableParts($query['query']); 106 | 107 | // CONSIDER: Supporting combinable partition options. 108 | if (preg_match('/\A\s*(ADD|ALTER|DROP|CHANGE)\b/imu', $parts['changes'] ?? '')) { 109 | return $parts; 110 | } 111 | 112 | return []; 113 | } 114 | 115 | /** 116 | * @param string $query_string 117 | * 118 | * @return array like ['table_name' => ?string, 'changes' => ?string]. 119 | */ 120 | private static function getOnlineableParts(string $query_string) : array 121 | { 122 | $table_name = null; 123 | $changes = null; 124 | $escape = null; 125 | 126 | $alter_re = '/\A\s*ALTER\s+TABLE\s+([`"]?[^\s`"]+[`"]?)\s*/imu'; 127 | $create_re = '/\A\s*CREATE\s+' 128 | . '((UNIQUE|FULLTEXT|SPATIAL)\s+)?' 129 | . 'INDEX\s+[`"]?([^`"\s]+)[`"]?\s+ON\s+([`"]?[^`"\s]+[`"]?)\s+?/imu'; 130 | $drop_re = '/\A\s*DROP\s+' 131 | . 'INDEX\s+[`"]?([^`"\s]+)[`"]?\s+ON\s+([`"]?[^`"\s]+[`"]?)\s*?/imu'; 132 | 133 | if (preg_match($alter_re, $query_string, $alter_parts)) { 134 | $table_name = $alter_parts[1]; 135 | // Changing query so pretendToRun output will match command. 136 | // CONSIDER: Separate index and overriding pretendToRun instead. 137 | $changes = preg_replace($alter_re, '', $query_string); 138 | 139 | // Alter-table-rename-to-as is not supported by PTOSC. 140 | if (preg_match('/\A\s*RENAME(\s+(TO|AS))?\s+[^\s]+\s*(;|\z)/imu', $changes)) { 141 | return []; 142 | } 143 | } elseif (preg_match($create_re, $query_string, $create_parts)) { 144 | $index_name = $create_parts[3]; 145 | $table_name = $create_parts[4]; 146 | $changes = "ADD $create_parts[2] INDEX $index_name " 147 | . preg_replace($create_re, '', $query_string); 148 | } elseif (preg_match($drop_re, $query_string, $drop_parts)) { 149 | $index_name = $drop_parts[1]; 150 | $table_name = $drop_parts[2]; 151 | $changes = "DROP INDEX $index_name " 152 | . preg_replace($drop_re, '', $query_string); 153 | } else { 154 | // Query not supported by PTOSC. 155 | return []; 156 | } 157 | 158 | $escape = preg_match('/^([`"])/u', $table_name, $m) ? $m[1] : null; 159 | $table_name = trim($table_name, '`"'); 160 | 161 | // HACK: Workaround PTOSC quirk with escaping and defaults. 162 | $changes = str_replace( 163 | ["default '0'", "default '1'"], 164 | ['default 0', 'default 1'], $changes); 165 | 166 | // Dropping FKs with PTOSC requires prefixing constraint name with 167 | // '_'; adding another if it already starts with '_'. 168 | $changes = preg_replace('/(\bDROP\s+FOREIGN\s+KEY\s+[`"]?)([^`"\s]+)/imu', '\01_\02', $changes); 169 | 170 | return [ 171 | 'table_name' => $table_name, 172 | 'changes' => $changes, 173 | 'escape' => $escape, 174 | ]; 175 | } 176 | 177 | /** 178 | * Get query or command, converting "ALTER TABLE " statements to on-line commands/queries. 179 | * 180 | * @param array $query 181 | * @param array $connection 182 | * 183 | * @return string 184 | */ 185 | public static function getQueryOrCommand(array &$query, Connection $connection) : string 186 | { 187 | // CONSIDER: Executing --dry-run (only during pretend?) first to validate all will work. 188 | 189 | $onlineable = self::getOnlineableParts($query['query']); 190 | 191 | // Leave unchanged when not supported by PTOSC. 192 | if ( 193 | empty($onlineable['table_name']) 194 | || empty($onlineable['changes']) 195 | ) { 196 | return $query['query']; 197 | } 198 | 199 | $table_name_folded = $onlineable['table_name']; 200 | 201 | switch (mb_strtolower(config('online-migrator.ptosc-fold-table-case'))) { 202 | case 'upper': 203 | $table_name_folded = mb_strtoupper($table_name_folded); 204 | break; 205 | 206 | case 'lower': 207 | $table_name_folded = mb_strtolower($table_name_folded); 208 | break; 209 | } 210 | 211 | // Keeping defaults here so overriding one does not discard all, as 212 | // would happen if left to `config/online-migrator.php`. 213 | $ptosc_defaults = [ 214 | '--alter-foreign-keys-method=auto', 215 | '--no-check-alter', // ASSUMES: Users accept risks w/RENAME. 216 | // ASSUMES: All are known to be unique. 217 | // CONSIDER: Extracting/re-creating automatic uniqueness checks 218 | // and running them here in PHP beforehand. 219 | '--no-check-unique-key-change', 220 | ]; 221 | $ptosc_options_str = self::getOptionsForShell( 222 | config('online-migrator.ptosc-options'), $ptosc_defaults); 223 | 224 | if (false !== strpos($ptosc_options_str, '--dry-run')) { 225 | throw new \InvalidArgumentException( 226 | 'Cannot run PTOSC with --dry-run because it would incompletely change the database. Remove from PTOSC_OPTIONS.'); 227 | } 228 | 229 | $db_config = $connection->getConfig(); 230 | $command = 'pt-online-schema-change --alter ' 231 | . escapeshellarg($onlineable['changes']) 232 | . ' D=' . escapeshellarg($db_config['database'] . ',t=' . $table_name_folded) 233 | . ' --host ' . escapeshellarg($db_config['host']) 234 | . ' --port ' . escapeshellarg($db_config['port']) 235 | . ' --user ' . escapeshellarg($db_config['username']) 236 | // CONSIDER: Redacting password during pretend 237 | . ' --password ' . escapeshellarg($db_config['password']) 238 | . $ptosc_options_str 239 | . ' --execute' 240 | . ' 2>&1'; 241 | 242 | return $command; 243 | } 244 | 245 | /** 246 | * Get options from env. since artisan migrate has fixed arguments. 247 | * 248 | * ASSUMES: Shell command(s) use '--no-' to invert options when checking for defaults. 249 | * 250 | * @param string $option_csv 251 | * @param array $defaults 252 | * 253 | * @return string 254 | */ 255 | public static function getOptionsForShell(?string $option_csv, array $defaults = []) : string 256 | { 257 | $return = ''; 258 | 259 | $options = []; 260 | 261 | foreach ($defaults as $raw_default) { // CONSIDER: Accepting value 262 | if (false === strpos($raw_default, '--', 0)) { 263 | throw new \InvalidArgumentException( 264 | 'Only double dashed (full) options supported ' 265 | . var_export($raw_default, 1)); 266 | } 267 | $default_root = preg_replace('/(^--(no-?)?|=.*)/', '', $raw_default); 268 | $options[$default_root] = $raw_default; 269 | } 270 | 271 | if (! empty($option_csv)) { 272 | // CONSIDER: Formatting CLI options in config as native arrays 273 | // instead of CSV. 274 | $raw_options = preg_split('/[, ]+(?=--)/', $option_csv); 275 | 276 | foreach ($raw_options as $raw_option) { 277 | if (false === strpos($raw_option, '--', 0)) { 278 | throw new \InvalidArgumentException( 279 | 'Only double dashed (full) options supported ' 280 | . var_export($raw_option, 1)); 281 | } 282 | $option_root = preg_replace('/(^--(no-?)?|[= ].*)?/', '', $raw_option); 283 | $options[$option_root] = $raw_option; 284 | } 285 | } 286 | 287 | foreach ($options as $raw_default) { 288 | $return .= ' ' . $raw_default; // TODO: Escape 289 | } 290 | 291 | return $return; 292 | } 293 | 294 | /** 295 | * Execute query or on-line command. 296 | * 297 | * @param array $query like [ 'query' => string, 'bindings' => array ] 298 | * @param Connection $connection already established with database 299 | * 300 | * @throws \UnexpectedValueException 301 | * 302 | * @return void 303 | */ 304 | public static function runQueryOrCommand(array &$query, Connection $connection) : void 305 | { 306 | // CONSIDER: Using unmodified migration code when small and not 307 | // currently locked table. 308 | // CONSIDER: Non-PTOSC specific prefix like "-- COMMAND:". 309 | if (0 === strpos($query['query'], 'pt-online-schema-change')) { 310 | $return_var = null; 311 | // CONSIDER: Converting migration verbosity switches into PTOSC 312 | // verbosity switches. 313 | $command = $query['query']; 314 | // Pass-through output instead of capturing since delay until end of 315 | // command may be infinite during prompt or too late to correct. 316 | passthru($command, $return_var); 317 | 318 | if (0 !== $return_var) { 319 | throw new \UnexpectedValueException('Exited with error code ' 320 | . var_export($return_var, 1) . ', command:' . PHP_EOL 321 | . $query['query'], 322 | intval($return_var)); 323 | } 324 | } else { 325 | // Run unchanged query 326 | $connection->affectingStatement($query['query'], $query['bindings']); 327 | } 328 | } 329 | } 330 | -------------------------------------------------------------------------------- /src/Strategy/StrategyInterface.php: -------------------------------------------------------------------------------- 1 | string, 'bindings' => array ] 24 | * @param Connection $connection already established with database 25 | * 26 | * @throws \UnexpectedValueException 27 | * 28 | * @return void 29 | */ 30 | public static function runQueryOrCommand(array &$query, Connection $connection) : void; 31 | } 32 | --------------------------------------------------------------------------------