init
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
linters:
|
||||
phpcs:
|
||||
standard: CakePHP3
|
||||
fixer: true
|
||||
|
||||
files:
|
||||
ignore:
|
||||
- 'vendor/*'
|
||||
|
||||
fixers:
|
||||
enable: true
|
||||
workflow: commit
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
(The MIT license)
|
||||
|
||||
Copyright (c) 2012-2017 Rob Morgan
|
||||
Copyright (c) 2017-present, Cake Software Foundation, Inc. (https://cakefoundation.org)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
# [Phinx](https://phinx.org): Simple PHP Database Migrations
|
||||
|
||||
[](https://github.com/cakephp/phinx/actions/workflows/ci.yml)
|
||||
[](https://codecov.io/gh/cakephp/phinx)
|
||||

|
||||
[](https://php.net/)
|
||||

|
||||
|
||||
## Intro
|
||||
|
||||
Phinx makes it ridiculously easy to manage the database migrations for your PHP app. In less than 5 minutes, you can install Phinx and create your first database migration. Phinx is just about migrations without all the bloat of a database ORM system or framework.
|
||||
|
||||
**Check out [book.cakephp.org/phinx](https://book.cakephp.org/phinx) for the comprehensive documentation.**
|
||||
|
||||

|
||||
|
||||
### Features
|
||||
|
||||
* Write database migrations using database agnostic PHP code.
|
||||
* Migrate up and down.
|
||||
* Migrate on deployment.
|
||||
* Seed data after database creation.
|
||||
* Get going in less than 5 minutes.
|
||||
* Stop worrying about the state of your database.
|
||||
* Take advantage of SCM features such as branching.
|
||||
* Integrate with any app.
|
||||
|
||||
### Supported Adapters
|
||||
|
||||
Phinx natively supports the following database adapters:
|
||||
|
||||
* MySQL
|
||||
* PostgreSQL
|
||||
* SQLite
|
||||
* Microsoft SQL Server
|
||||
|
||||
## Install & Run
|
||||
|
||||
See [version and branch overview](https://github.com/cakephp/phinx/wiki#version-and-branch-overview) for branch and PHP compatibility.
|
||||
|
||||
### Composer
|
||||
|
||||
The fastest way to install Phinx is to add it to your project using Composer (https://getcomposer.org/).
|
||||
|
||||
1. Install Composer:
|
||||
|
||||
```
|
||||
curl -sS https://getcomposer.org/installer | php
|
||||
```
|
||||
|
||||
1. Require Phinx as a dependency using Composer:
|
||||
|
||||
```
|
||||
php composer.phar require robmorgan/phinx
|
||||
```
|
||||
|
||||
1. Install Phinx:
|
||||
|
||||
```
|
||||
php composer.phar install
|
||||
```
|
||||
|
||||
1. Execute Phinx:
|
||||
|
||||
```
|
||||
php vendor/bin/phinx
|
||||
```
|
||||
|
||||
### As a Phar
|
||||
|
||||
You can also use the Box application to build Phinx as a Phar archive (https://box-project.github.io/box2/).
|
||||
|
||||
1. Clone Phinx from GitHub
|
||||
|
||||
```
|
||||
git clone https://github.com/cakephp/phinx.git
|
||||
cd phinx
|
||||
```
|
||||
|
||||
1. Install Composer
|
||||
|
||||
```
|
||||
curl -s https://getcomposer.org/installer | php
|
||||
```
|
||||
|
||||
1. Install the Phinx dependencies
|
||||
|
||||
```
|
||||
php composer.phar install
|
||||
```
|
||||
|
||||
1. Install Box:
|
||||
|
||||
```
|
||||
curl -LSs https://box-project.github.io/box2/installer.php | php
|
||||
```
|
||||
|
||||
1. Create a Phar archive
|
||||
|
||||
```
|
||||
php box.phar build
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
Check out https://book.cakephp.org/phinx for the comprehensive documentation.
|
||||
|
||||
Other translations include:
|
||||
|
||||
* [Chinese](https://tsy12321.gitbooks.io/phinx-doc/) (Maintained by [@tsy12321](https://github.com/tsy12321/phinx-doc))
|
||||
|
||||
## Contributing
|
||||
|
||||
Please read the [CONTRIBUTING](CONTRIBUTING.md) document.
|
||||
|
||||
## News & Updates
|
||||
|
||||
Follow [@CakePHP](https://twitter.com/cakephp) on Twitter to stay up to date.
|
||||
|
||||
## Limitations
|
||||
|
||||
### PostgreSQL
|
||||
|
||||
- Not able to set a unique constraint on a table (<https://github.com/cakephp/phinx/issues/1026>).
|
||||
|
||||
|
||||
## Misc
|
||||
|
||||
### Version History
|
||||
|
||||
Please read the [release notes](https://github.com/cakephp/phinx/releases).
|
||||
|
||||
### License
|
||||
|
||||
(The MIT license)
|
||||
|
||||
Copyright (c) 2017 Rob Morgan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/* Phinx
|
||||
*
|
||||
* (The MIT license)
|
||||
* Copyright (c) 2014 Rob Morgan
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated * documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
$autoloader = require __DIR__ . '/../src/composer_autoloader.php';
|
||||
|
||||
if (!$autoloader()) {
|
||||
die(
|
||||
'You need to set up the project dependencies using the following commands:' . PHP_EOL .
|
||||
'curl -sS https://getcomposer.org/installer | php' . PHP_EOL .
|
||||
'php composer.phar install' . PHP_EOL
|
||||
);
|
||||
}
|
||||
|
||||
return new Phinx\Console\PhinxApplication();
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/* Phinx
|
||||
*
|
||||
* (The MIT license)
|
||||
* Copyright (c) 2014 Rob Morgan
|
||||
* Copyright (c) 2014 Woody Gilk
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated * documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
// This script can be run as a router with the built in PHP web server:
|
||||
//
|
||||
// php -S localhost:8000 app/web.php
|
||||
//
|
||||
// Or can be run from any other web server with:
|
||||
//
|
||||
// require 'phinx/app/web.php';
|
||||
//
|
||||
// This script uses the following query string arguments:
|
||||
//
|
||||
// - (string) "e" environment name
|
||||
// - (string) "t" target version
|
||||
// - (boolean) "debug" enable debugging?
|
||||
|
||||
// Get the phinx console application and inject it into TextWrapper.
|
||||
$app = require __DIR__ . '/phinx.php';
|
||||
$wrap = new Phinx\Wrapper\TextWrapper($app);
|
||||
|
||||
// Mapping of route names to commands.
|
||||
$routes = [
|
||||
'status' => 'getStatus',
|
||||
'migrate' => 'getMigrate',
|
||||
'rollback' => 'getRollback',
|
||||
];
|
||||
|
||||
// Extract the requested command from the URL, default to "status".
|
||||
$command = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
|
||||
if (!$command) {
|
||||
$command = 'status';
|
||||
}
|
||||
|
||||
// Verify that the command exists, or list available commands.
|
||||
if (!isset($routes[$command])) {
|
||||
$commands = implode(', ', array_keys($routes));
|
||||
header('Content-Type: text/plain', true, 404);
|
||||
die("Command not found! Valid commands are: {$commands}.");
|
||||
}
|
||||
|
||||
// Get the environment and target version parameters.
|
||||
$env = $_GET['e'] ?? null;
|
||||
$target = $_GET['t'] ?? null;
|
||||
|
||||
// Check if debugging is enabled.
|
||||
$debug = !empty($_GET['debug']) && filter_var($_GET['debug'], FILTER_VALIDATE_BOOLEAN);
|
||||
|
||||
// Execute the command and determine if it was successful.
|
||||
$output = call_user_func([$wrap, $routes[$command]], $env, $target);
|
||||
$error = $wrap->getExitCode() > 0;
|
||||
|
||||
// Finally, display the output of the command.
|
||||
header('Content-Type: text/plain', true, $error ? 500 : 200);
|
||||
if ($debug) {
|
||||
// Show what command was executed based on request parameters.
|
||||
$args = implode(', ', [var_export($env, true), var_export($target, true)]);
|
||||
echo "DEBUG: $command($args)" . PHP_EOL . PHP_EOL;
|
||||
}
|
||||
echo $output;
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
/* Phinx
|
||||
*
|
||||
* (The MIT license)
|
||||
* Copyright (c) 2014 Rob Morgan
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated * documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
$app = require __DIR__ . '/../app/phinx.php';
|
||||
$app->run();
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
@echo off
|
||||
|
||||
rem This script will do the following:
|
||||
rem - check for PHP_COMMAND env, if found, use it.
|
||||
rem - if not found detect php, if found use it, otherwise err and terminate
|
||||
|
||||
if "%OS%"=="Windows_NT" @setlocal
|
||||
|
||||
rem %~dp0 is expanded pathname of the current script under NT
|
||||
set DEFAULT_PHINX_HOME=%~dp0..
|
||||
|
||||
goto init
|
||||
goto cleanup
|
||||
|
||||
:init
|
||||
|
||||
if "%PHINX_HOME%" == "" set PHINX_HOME=%DEFAULT_PHINX_HOME%
|
||||
set DEFAULT_PHINX_HOME=
|
||||
|
||||
if "%PHP_COMMAND%" == "" goto no_phpcommand
|
||||
|
||||
goto run
|
||||
goto cleanup
|
||||
|
||||
:run
|
||||
"%PHP_COMMAND%" -d html_errors=off -qC "%PHINX_HOME%\bin\phinx" %*
|
||||
goto cleanup
|
||||
|
||||
:no_phpcommand
|
||||
rem PHP_COMMAND environment variable not found, assuming php.exe is on path.
|
||||
set PHP_COMMAND=php.exe
|
||||
goto init
|
||||
|
||||
:err_home
|
||||
echo ERROR: Environment var PHINX_HOME not set. Please point this
|
||||
echo variable to your local phinx installation!
|
||||
goto cleanup
|
||||
|
||||
:cleanup
|
||||
if "%OS%"=="Windows_NT" @endlocal
|
||||
rem pause
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
{
|
||||
"name": "robmorgan/phinx",
|
||||
"type": "library",
|
||||
"description": "Phinx makes it ridiculously easy to manage the database migrations for your PHP app.",
|
||||
"keywords": [
|
||||
"phinx",
|
||||
"migrations",
|
||||
"database",
|
||||
"db",
|
||||
"database migrations"
|
||||
],
|
||||
"homepage": "https://phinx.org",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Rob Morgan",
|
||||
"email": "robbym@gmail.com",
|
||||
"homepage": "https://robmorgan.id.au",
|
||||
"role": "Lead Developer"
|
||||
},
|
||||
{
|
||||
"name": "Woody Gilk",
|
||||
"email": "woody.gilk@gmail.com",
|
||||
"homepage": "https://shadowhand.me",
|
||||
"role": "Developer"
|
||||
},
|
||||
{
|
||||
"name": "Richard Quadling",
|
||||
"email": "rquadling@gmail.com",
|
||||
"role": "Developer"
|
||||
},
|
||||
{
|
||||
"name": "CakePHP Community",
|
||||
"role": "Developer",
|
||||
"homepage": "https://github.com/cakephp/phinx/graphs/contributors"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php-64bit": ">=8.1",
|
||||
"composer-runtime-api": "^2.0",
|
||||
"cakephp/database": "^5.0.2",
|
||||
"psr/container": "^1.1|^2.0",
|
||||
"symfony/config": "^4.0|^5.0|^6.0|^7.0",
|
||||
"symfony/console": "^6.0|^7.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"ext-json": "*",
|
||||
"ext-pdo": "*",
|
||||
"cakephp/cakephp-codesniffer": "^5.0",
|
||||
"cakephp/i18n": "^5.0",
|
||||
"phpunit/phpunit": "^9.5.19",
|
||||
"symfony/yaml": "^4.0|^5.0|^6.0|^7.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Phinx\\": "src/Phinx/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Test\\Phinx\\": "tests/Phinx/"
|
||||
}
|
||||
},
|
||||
"suggest": {
|
||||
"ext-json": "Install if using JSON configuration format",
|
||||
"ext-pdo": "PDO extension is needed",
|
||||
"symfony/yaml": "Install if using YAML configuration format"
|
||||
},
|
||||
"scripts": {
|
||||
"check": [
|
||||
"@test",
|
||||
"@cs-check"
|
||||
],
|
||||
"cs-check": "phpcs",
|
||||
"cs-fix": "phpcbf",
|
||||
"stan": "phpstan analyse",
|
||||
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:~1.9.0 && mv composer.backup composer.json",
|
||||
"lowest": "validate-prefer-lowest",
|
||||
"lowest-setup": "composer update --prefer-lowest --prefer-stable --prefer-dist --no-interaction && cp composer.json composer.backup && composer require --dev dereuromark/composer-prefer-lowest && mv composer.backup composer.json",
|
||||
"test": "phpunit --colors=always"
|
||||
},
|
||||
"bin": [
|
||||
"bin/phinx"
|
||||
],
|
||||
"minimum-stability": "dev",
|
||||
"prefer-stable": true,
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
"dealerdirect/phpcodesniffer-composer-installer": true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"paths": {
|
||||
"migrations": "%%PHINX_CONFIG_DIR%%/db/migrations",
|
||||
"seeds": "%%PHINX_CONFIG_DIR%%/db/seeds"
|
||||
},
|
||||
"environments": {
|
||||
"default_migration_table": "phinxlog",
|
||||
"default_environment": "development",
|
||||
"production": {
|
||||
"adapter": "mysql",
|
||||
"host": "localhost",
|
||||
"name": "production_db",
|
||||
"user": "root",
|
||||
"pass": "",
|
||||
"port": 3306,
|
||||
"charset": "utf8"
|
||||
},
|
||||
"development": {
|
||||
"adapter": "mysql",
|
||||
"host": "localhost",
|
||||
"name": "development_db",
|
||||
"user": "root",
|
||||
"pass": "",
|
||||
"port": 3306,
|
||||
"charset": "utf8"
|
||||
},
|
||||
"testing": {
|
||||
"adapter": "mysql",
|
||||
"host": "localhost",
|
||||
"name": "testing_db",
|
||||
"user": "root",
|
||||
"pass": "",
|
||||
"port": 3306,
|
||||
"charset": "utf8"
|
||||
}
|
||||
},
|
||||
"version_order": "creation"
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
return
|
||||
[
|
||||
'paths' => [
|
||||
'migrations' => '%%PHINX_CONFIG_DIR%%/db/migrations',
|
||||
'seeds' => '%%PHINX_CONFIG_DIR%%/db/seeds'
|
||||
],
|
||||
'environments' => [
|
||||
'default_migration_table' => 'phinxlog',
|
||||
'default_environment' => 'development',
|
||||
'production' => [
|
||||
'adapter' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'name' => 'production_db',
|
||||
'user' => 'root',
|
||||
'pass' => '',
|
||||
'port' => '3306',
|
||||
'charset' => 'utf8',
|
||||
],
|
||||
'development' => [
|
||||
'adapter' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'name' => 'development_db',
|
||||
'user' => 'root',
|
||||
'pass' => '',
|
||||
'port' => '3306',
|
||||
'charset' => 'utf8',
|
||||
],
|
||||
'testing' => [
|
||||
'adapter' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'name' => 'testing_db',
|
||||
'user' => 'root',
|
||||
'pass' => '',
|
||||
'port' => '3306',
|
||||
'charset' => 'utf8',
|
||||
]
|
||||
],
|
||||
'version_order' => 'creation'
|
||||
];
|
||||
@@ -0,0 +1,35 @@
|
||||
paths:
|
||||
migrations: '%%PHINX_CONFIG_DIR%%/db/migrations'
|
||||
seeds: '%%PHINX_CONFIG_DIR%%/db/seeds'
|
||||
|
||||
environments:
|
||||
default_migration_table: phinxlog
|
||||
default_environment: development
|
||||
production:
|
||||
adapter: mysql
|
||||
host: localhost
|
||||
name: production_db
|
||||
user: root
|
||||
pass: ''
|
||||
port: 3306
|
||||
charset: utf8
|
||||
|
||||
development:
|
||||
adapter: mysql
|
||||
host: localhost
|
||||
name: development_db
|
||||
user: root
|
||||
pass: ''
|
||||
port: 3306
|
||||
charset: utf8
|
||||
|
||||
testing:
|
||||
adapter: mysql
|
||||
host: localhost
|
||||
name: testing_db
|
||||
user: root
|
||||
pass: ''
|
||||
port: 3306
|
||||
charset: utf8
|
||||
|
||||
version_order: creation
|
||||
@@ -0,0 +1,43 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Phinx documentation build configuration file, created by
|
||||
# sphinx-quickstart on Thu Jun 14 17:39:42 2012.
|
||||
#
|
||||
|
||||
# Import the base theme configuration
|
||||
from cakephpsphinx.config.all import *
|
||||
|
||||
# The full version, including alpha/beta/rc tags.
|
||||
release = '0.16.x'
|
||||
|
||||
# The search index version.
|
||||
search_version = 'phinx-0'
|
||||
|
||||
# The marketing display name for the book.
|
||||
version_name = ''
|
||||
|
||||
# Project name shown in the black header bar
|
||||
project = 'Phinx'
|
||||
|
||||
# Other versions that display in the version picker menu.
|
||||
version_list = [
|
||||
{'name': '0.16', 'number': '/phinx/0', 'title': '0.16', 'current': True},
|
||||
]
|
||||
|
||||
# Languages available.
|
||||
languages = ['en']
|
||||
|
||||
# The GitHub branch name for this version of the docs
|
||||
# for edit links to point at.
|
||||
branch = '0.x'
|
||||
|
||||
# Current version being built
|
||||
version = '0.16'
|
||||
|
||||
show_root_link = True
|
||||
|
||||
repository = 'cakephp/phinx'
|
||||
|
||||
source_path = 'docs/'
|
||||
|
||||
hide_page_contents = ('search', '404', 'contents')
|
||||
+416
@@ -0,0 +1,416 @@
|
||||
.. index::
|
||||
single: Commands
|
||||
|
||||
Commands
|
||||
========
|
||||
|
||||
Phinx is run using a number of commands.
|
||||
|
||||
The Breakpoint Command
|
||||
----------------------
|
||||
|
||||
The Breakpoint command is used to set breakpoints, allowing you to limit
|
||||
rollbacks. You can toggle the breakpoint of the most recent migration by
|
||||
not supplying any parameters.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx breakpoint -e development
|
||||
|
||||
To toggle a breakpoint on a specific version then use the ``--target``
|
||||
parameter or ``-t`` for short.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx breakpoint -e development -t 20120103083322
|
||||
|
||||
You can remove all the breakpoints by using the ``--remove-all`` parameter
|
||||
or ``-r`` for short.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx breakpoint -e development -r
|
||||
|
||||
You can set or unset (rather than just toggle) the breakpoint on the most
|
||||
recent migration (or on a specific migration when combined with the
|
||||
``--target`` or ``-t`` parameter) by using ``-set`` or ``--unset``.
|
||||
|
||||
Breakpoints are visible when you run the ``status`` command.
|
||||
|
||||
The Create Command
|
||||
------------------
|
||||
|
||||
The Create command is used to create a new migration file. It requires one
|
||||
argument: the name of the migration. The migration name should be specified in
|
||||
CamelCase format.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx create MyNewMigration
|
||||
|
||||
Open the new migration file in your text editor to add your database
|
||||
transformations. Phinx creates migration files using the path specified in your
|
||||
phinx configuration file. Please see the :doc:`Configuration <configuration>` chapter
|
||||
for more information.
|
||||
|
||||
You are able to override the template file used by Phinx by supplying an
|
||||
alternative template filename.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx create MyNewMigration --template="<file>"
|
||||
|
||||
You can also supply a template generating class. This class must implement the
|
||||
interface ``Phinx\Migration\CreationInterface``.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx create MyNewMigration --class="<class>"
|
||||
|
||||
In addition to providing the template for the migration, the class can also define
|
||||
a callback that will be called once the migration file has been generated from the
|
||||
template.
|
||||
|
||||
You cannot use ``--template`` and ``--class`` together.
|
||||
|
||||
The Init Command
|
||||
----------------
|
||||
|
||||
The Init command (short for initialize) is used to prepare your project for
|
||||
Phinx. This command generates the phinx configuration file in the root of your
|
||||
project directory. By default, this file will be named ``phinx.php``.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx init
|
||||
|
||||
Optionally you can specify a custom location for Phinx's config file:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx init ./custom/location/
|
||||
|
||||
You can also specify a custom file name:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx init custom-config.yml
|
||||
|
||||
As well as a different format from php, yml, and json. For example, to create yml file:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx init --format yml
|
||||
|
||||
Open this file in your text editor to setup your project configuration. Please
|
||||
see the :doc:`Configuration <configuration>` chapter for more information.
|
||||
|
||||
The Migrate Command
|
||||
-------------------
|
||||
|
||||
The Migrate command runs all of the available migrations, optionally up to a
|
||||
specific version.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx migrate -e development
|
||||
|
||||
To migrate to a specific version then use the ``--target`` parameter or ``-t``
|
||||
for short.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx migrate -e development -t 20110103081132
|
||||
|
||||
Use ``--dry-run`` to print the queries to standard output without executing them
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx migrate --dry-run
|
||||
|
||||
The Rollback Command
|
||||
--------------------
|
||||
|
||||
The Rollback command is used to undo previous migrations executed by Phinx. It
|
||||
is the opposite of the Migrate command.
|
||||
|
||||
You can rollback to the previous migration by using the ``rollback`` command
|
||||
with no arguments.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx rollback -e development
|
||||
|
||||
To rollback all migrations to a specific version then use the ``--target``
|
||||
parameter or ``-t`` for short.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx rollback -e development -t 20120103083322
|
||||
|
||||
Specifying 0 as the target version will revert all migrations.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx rollback -e development -t 0
|
||||
|
||||
To rollback all migrations to a specific date then use the ``--date``
|
||||
parameter or ``-d`` for short.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx rollback -e development -d 2012
|
||||
$ phinx rollback -e development -d 201201
|
||||
$ phinx rollback -e development -d 20120103
|
||||
$ phinx rollback -e development -d 2012010312
|
||||
$ phinx rollback -e development -d 201201031205
|
||||
$ phinx rollback -e development -d 20120103120530
|
||||
|
||||
If a breakpoint is set, blocking further rollbacks, you can override the
|
||||
breakpoint using the ``--force`` parameter or ``-f`` for short.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx rollback -e development -t 0 -f
|
||||
|
||||
Use ``--dry-run`` to print the queries to standard output without executing them
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx rollback --dry-run
|
||||
|
||||
.. note::
|
||||
|
||||
When rolling back, Phinx orders the executed migrations using
|
||||
the order specified in the ``version_order`` option of your
|
||||
phinx configuration file.
|
||||
Please see the :doc:`Configuration <configuration>` chapter for more information.
|
||||
|
||||
The Status Command
|
||||
------------------
|
||||
|
||||
The Status command prints a list of all migrations, along with their current
|
||||
status. You can use this command to determine which migrations have been run.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx status -e development
|
||||
|
||||
This command exits with code 0 if the database is up-to-date (ie. all migrations are up) or one of the following codes otherwise:
|
||||
|
||||
* 2: There is at least one missing migration.
|
||||
* 3: There is at least one down migration.
|
||||
|
||||
An exit code of 1 means an application error has occurred.
|
||||
|
||||
The Seed Create Command
|
||||
-----------------------
|
||||
|
||||
The Seed Create command can be used to create new database seed classes. It
|
||||
requires one argument, the name of the class. The class name should be specified
|
||||
in CamelCase format.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx seed:create MyNewSeeder
|
||||
|
||||
Open the new seed file in your text editor to add your database seed commands.
|
||||
Phinx creates seed files using the path specified in your configuration file.
|
||||
Please see the :doc:`Configuration <configuration>` chapter for more information.
|
||||
|
||||
You are able to override the template file used by Phinx by supplying an
|
||||
alternative template filename.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx seed:create MyNewSeeder --template="<file>"
|
||||
|
||||
The Seed Run Command
|
||||
--------------------
|
||||
|
||||
The Seed Run command runs all of the available seed classes or optionally just
|
||||
one.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx seed:run -e development
|
||||
|
||||
To run only one seed class use the ``--seed`` parameter or ``-s`` for short.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ phinx seed:run -e development -s MyNewSeeder
|
||||
|
||||
Configuration File Parameter
|
||||
----------------------------
|
||||
|
||||
When running Phinx from the command line, you may specify a configuration file
|
||||
using the ``--configuration`` or ``-c`` parameter. In addition to YAML, the
|
||||
configuration file may be the computed output of a PHP file as a PHP array:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
return [
|
||||
"paths" => [
|
||||
"migrations" => "application/migrations"
|
||||
],
|
||||
"environments" => [
|
||||
"default_migration_table" => "phinxlog",
|
||||
"default_environment" => "dev",
|
||||
"dev" => [
|
||||
"adapter" => "mysql",
|
||||
"host" => $_ENV['DB_HOST'],
|
||||
"name" => $_ENV['DB_NAME'],
|
||||
"user" => $_ENV['DB_USER'],
|
||||
"pass" => $_ENV['DB_PASS'],
|
||||
"port" => $_ENV['DB_PORT']
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
Phinx auto-detects which language parser to use for files with ``*.yaml``, ``*.yml``, ``*.json``, and ``*.php`` extensions.
|
||||
The appropriate parser may also be specified via the ``--parser`` and ``-p`` parameters. Anything other than ``"json"`` or
|
||||
``"php"`` is treated as YAML.
|
||||
|
||||
When using a PHP array, you can provide a ``connection`` key with an existing PDO instance. It is also important to pass
|
||||
the database name too, as Phinx requires this for certain methods such as ``hasTable()``:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
return [
|
||||
"paths" => [
|
||||
"migrations" => "application/migrations"
|
||||
),
|
||||
"environments" => [
|
||||
"default_migration_table" => "phinxlog",
|
||||
"default_environment" => "dev",
|
||||
"dev" => [
|
||||
"name" => "dev_db",
|
||||
"connection" => $pdo_instance
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
Running Phinx in a Web App
|
||||
--------------------------
|
||||
|
||||
Phinx can also be run inside of a web application by using the ``Phinx\Wrapper\TextWrapper``
|
||||
class. An example of this is provided in ``app/web.php``, which can be run as a
|
||||
standalone server:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ php -S localhost:8000 vendor/robmorgan/phinx/app/web.php
|
||||
|
||||
This will create local web server at `<http://localhost:8000>`__ which will show current
|
||||
migration status by default. To run migrations up, use `<http://localhost:8000/migrate>`__
|
||||
and to rollback use `<http://localhost:8000/rollback>`__.
|
||||
|
||||
**The included web app is only an example and should not be used in production!**
|
||||
|
||||
.. note::
|
||||
|
||||
To modify configuration variables at runtime and override ``%%PHINX_DBNAME%%``
|
||||
or other another dynamic option, set ``$_SERVER['PHINX_DBNAME']`` before
|
||||
running commands. Available options are documented in the Configuration page.
|
||||
|
||||
Wrapping Phinx in another Symfony Console Application
|
||||
-----------------------------------------------------
|
||||
|
||||
Phinx can be wrapped and run as part of a separate Symfony console application. This
|
||||
may be desirable to present a unified interface to the user for all aspects of your
|
||||
application, or because you wish to run multiple Phinx commands. While you could
|
||||
run the commands through ``exec`` or use the above ``Phinx\Wrapper\TextWrapper``,
|
||||
though this makes it hard to deal with the return code and output in a similar fashion
|
||||
as your application.
|
||||
|
||||
Luckily, Symfony makes doing this sort of "meta" command straight-forward:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Phinx\Console\PhinxApplication;
|
||||
|
||||
// ...
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
|
||||
$phinx = new PhinxApplication();
|
||||
$command = $phinx->find('migrate');
|
||||
|
||||
$arguments = [
|
||||
'command' => 'migrate',
|
||||
'--environment' => 'production',
|
||||
'--configuration' => '/path/to/phinx/config/file'
|
||||
];
|
||||
|
||||
$input = new ArrayInput($arguments);
|
||||
$returnCode = $command->run(new ArrayInput($arguments), $output);
|
||||
// ...
|
||||
}
|
||||
|
||||
Here, you are instantianting the ``PhinxApplication``, telling it to find the ``migrate``
|
||||
command, defining the arguments to pass to it (which match the commandline arguments and flags),
|
||||
and then finally running the command, passing it the same ``OutputInterface`` that your
|
||||
application uses.
|
||||
|
||||
See this `Symfony page <https://symfony.com/doc/current/console/calling_commands.html>`_ for more information.
|
||||
|
||||
Using Phinx with PHPUnit
|
||||
--------------------------
|
||||
|
||||
Phinx can be used within your unit tests to prepare or seed the database. You can use it programatically :
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
public function setUp ()
|
||||
{
|
||||
$app = new PhinxApplication();
|
||||
$app->setAutoExit(false);
|
||||
$app->run(new StringInput('migrate'), new NullOutput());
|
||||
}
|
||||
|
||||
If you use a memory database, you'll need to give Phinx a specific PDO instance. You can interact with Phinx directly
|
||||
using the Manager class :
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
use PDO;
|
||||
use Phinx\Config\Config;
|
||||
use Phinx\Migration\Manager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Input\StringInput;
|
||||
use Symfony\Component\Console\Output\NullOutput;
|
||||
|
||||
class DatabaseTestCase extends TestCase {
|
||||
|
||||
public function setUp ()
|
||||
{
|
||||
$pdo = new PDO('sqlite::memory:', null, null, [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
|
||||
]);
|
||||
$configPath = __DIR__ . '/../phinx.php';
|
||||
$configArray = require $configPath;
|
||||
$configArray['environments']['test'] = [
|
||||
'adapter' => 'sqlite',
|
||||
'connection' => $pdo,
|
||||
'name' => ':memory:',
|
||||
];
|
||||
$config = new Config(
|
||||
$configArray,
|
||||
$configPath
|
||||
);
|
||||
$manager = new Manager($config, new StringInput(' '), new NullOutput());
|
||||
$manager->migrate('test');
|
||||
$manager->seed('test');
|
||||
$this->pdo = $pdo;
|
||||
// You can change default fetch mode after the seeding
|
||||
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import sys, os
|
||||
|
||||
# Append the top level directory of the docs, so we can import from the config dir.
|
||||
sys.path.insert(0, os.path.abspath('..'))
|
||||
|
||||
# Pull in all the configuration options defined in the global config file..
|
||||
from config.all import *
|
||||
|
||||
language = 'en'
|
||||
@@ -0,0 +1,579 @@
|
||||
.. index::
|
||||
single: Configuration
|
||||
|
||||
Configuration
|
||||
=============
|
||||
|
||||
When you initialize your project using the :doc:`Init Command<commands>`, Phinx
|
||||
creates a default file in the root of your project directory. By default, this
|
||||
file uses the YAML data serialization format, but you can use the ``--format``
|
||||
command line option to specify either ``yaml``, ``yml``, ``json``, or ``php``.
|
||||
|
||||
If a ``--configuration`` command line option is given, Phinx will load the
|
||||
specified file. Otherwise, it will attempt to find ``phinx.php``, ``phinx.json``,
|
||||
``phinx.yml``, or ``phinx.yaml`` and load the first file found. See the
|
||||
:doc:`Commands <commands>` chapter for more information.
|
||||
|
||||
.. warning::
|
||||
|
||||
Remember to store the configuration file outside of a publicly accessible
|
||||
directory on your webserver. This file contains your database credentials
|
||||
and may be accidentally served as plain text.
|
||||
|
||||
Note that while JSON and YAML files are *parsed*, the PHP file is *included*.
|
||||
This means that:
|
||||
|
||||
* It must `return` an array of configuration items.
|
||||
* The variable scope is local, i.e. you would need to explicitly declare
|
||||
any global variables your initialization file reads or modifies.
|
||||
* Its standard output is suppressed.
|
||||
* Unlike with JSON and YAML, it is possible to omit environment connection details
|
||||
and instead specify ``connection`` which must contain an initialized PDO instance.
|
||||
This is useful when you want your migrations to interact with your application
|
||||
and/or share the same connection. However remember to also pass the database name
|
||||
as Phinx cannot infer this from the PDO connection.
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
$app = require 'app/phinx.php';
|
||||
$pdo = $app->getDatabase()->getPdo();
|
||||
|
||||
return [
|
||||
'environments' => [
|
||||
'default_environment' => 'development',
|
||||
'development' => [
|
||||
'name' => 'devdb',
|
||||
'connection' => $pdo
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
Migration Paths
|
||||
---------------
|
||||
|
||||
The first option specifies the path to your migration directory. Phinx uses
|
||||
``%%PHINX_CONFIG_DIR%%/db/migrations`` by default.
|
||||
|
||||
.. note::
|
||||
|
||||
``%%PHINX_CONFIG_DIR%%`` is a special token and is automatically replaced
|
||||
with the root directory where your phinx configuration file is stored.
|
||||
|
||||
In order to overwrite the default ``%%PHINX_CONFIG_DIR%%/db/migrations``, you
|
||||
need to add the following to the configuration.
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
paths:
|
||||
migrations: /your/full/path
|
||||
|
||||
You can also provide multiple migration paths by using an array in your configuration:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
paths:
|
||||
migrations:
|
||||
- application/module1/migrations
|
||||
- application/module2/migrations
|
||||
|
||||
Class namespaces may be specified by adding a key to each migration path:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
paths:
|
||||
migrations:
|
||||
App\Module1\Migrations: application/module1/migrations
|
||||
App\Module2\Migrations: application/module2/migrations
|
||||
|
||||
|
||||
|
||||
You can also use the ``%%PHINX_CONFIG_DIR%%`` token in your path.
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
paths:
|
||||
migrations: '%%PHINX_CONFIG_DIR%%/your/relative/path'
|
||||
|
||||
Migrations are captured with ``glob``, so you can define a pattern for multiple
|
||||
directories.
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
paths:
|
||||
migrations: '%%PHINX_CONFIG_DIR%%/module/*/{data,scripts}/migrations'
|
||||
|
||||
Custom Migration Base
|
||||
---------------------
|
||||
|
||||
By default all migrations will extend from Phinx's `AbstractMigration` class.
|
||||
This can be set to a custom class that extends from `AbstractMigration` by
|
||||
setting ``migration_base_class`` in your config:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
migration_base_class: MyMagicalMigration
|
||||
|
||||
Seed Paths
|
||||
----------
|
||||
|
||||
The second option specifies the path to your seed directory. Phinx uses
|
||||
``%%PHINX_CONFIG_DIR%%/db/seeds`` by default.
|
||||
|
||||
.. note::
|
||||
|
||||
``%%PHINX_CONFIG_DIR%%`` is a special token and is automatically replaced
|
||||
with the root directory where your configuration file is stored.
|
||||
|
||||
In order to overwrite the default ``%%PHINX_CONFIG_DIR%%/db/seeds``, you
|
||||
need to add the following to the yaml configuration.
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
paths:
|
||||
seeds: /your/full/path
|
||||
|
||||
You can also provide multiple seed paths by using an array in your configuration:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
paths:
|
||||
seeds:
|
||||
- /your/full/path1
|
||||
- /your/full/path2
|
||||
|
||||
|
||||
You can also use the ``%%PHINX_CONFIG_DIR%%`` token in your path.
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
paths:
|
||||
seeds: '%%PHINX_CONFIG_DIR%%/your/relative/path'
|
||||
|
||||
Class namespaces may be specified by adding a key to each seed path:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
paths:
|
||||
seeds:
|
||||
App\Module1\Seeds: application/module1/seeds
|
||||
App\Module2\Seeds: application/module2/seeds
|
||||
|
||||
|
||||
Custom Seeder Base
|
||||
---------------------
|
||||
|
||||
By default all seeders will extend from Phinx's `AbstractSeed` class.
|
||||
This can be set to a custom class that extends from `AbstractSeed` by
|
||||
setting ``seed_base_class`` in your config:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
seed_base_class: MyMagicalSeeder
|
||||
|
||||
Custom Migration Template
|
||||
-------------------------
|
||||
|
||||
Custom template for Migrations could be used either by defining template file path
|
||||
in configuration file:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
templates:
|
||||
file: src/templates/customMigrationTemplate.php
|
||||
|
||||
|
||||
Custom Seeder Template
|
||||
----------------------
|
||||
|
||||
Custom Seeder template could be used either by defining template file path
|
||||
in configuration file:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
templates:
|
||||
seedFile: src/templates/customSeederTemplate.php
|
||||
|
||||
|
||||
Environments
|
||||
------------
|
||||
|
||||
One of the key features of Phinx is support for multiple database environments.
|
||||
You can use Phinx to create migrations on your development environment, then
|
||||
run the same migrations on your production environment. Environments are
|
||||
specified under the ``environments`` nested collection. For example:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
environments:
|
||||
default_migration_table: phinxlog
|
||||
default_environment: development
|
||||
production:
|
||||
adapter: mysql
|
||||
host: localhost
|
||||
name: production_db
|
||||
user: root
|
||||
pass: ''
|
||||
port: 3306
|
||||
charset: utf8mb4
|
||||
collation: utf8mb4_unicode_ci
|
||||
|
||||
would define a new environment called ``production``.
|
||||
|
||||
In a situation when multiple developers work on the same project and each has
|
||||
a different environment (e.g. a convention such as ``<environment
|
||||
type>-<developer name>-<machine name>``), or when you need to have separate
|
||||
environments for separate purposes (branches, testing, etc) use environment
|
||||
variable `PHINX_ENVIRONMENT` to override the default environment in the yaml
|
||||
file:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
export PHINX_ENVIRONMENT=dev-`whoami`-`hostname`
|
||||
|
||||
Migration Table
|
||||
---------------
|
||||
|
||||
To keep track of the migration statuses for an environment, phinx creates
|
||||
a table to store this information. You can customize where this table
|
||||
is created by configuring ``default_migration_table`` to be used as default
|
||||
for all environments:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
environment:
|
||||
default_migration_table: phinxlog
|
||||
|
||||
If this field is omitted, then it will default to ``phinxlog``. For
|
||||
databases that support it, e.g. Postgres, the schema name can be prefixed
|
||||
with a period separator (``.``). For example, ``phinx.log`` will create
|
||||
the table ``log`` in the ``phinx`` schema instead of ``phinxlog`` in the
|
||||
``public`` (default) schema.
|
||||
|
||||
You may also specify the ``migration_table`` on a per environment basis.
|
||||
Any environment that does not have a ``migration_table`` specified will
|
||||
fallback to using the ``default_migration_table`` that is defined at the
|
||||
top level. An example of how you might use this is as follows:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
environment:
|
||||
default_migration_table: phinxlog
|
||||
development:
|
||||
migration_table: phinxlog_dev
|
||||
# rest of the development settings
|
||||
production:
|
||||
# rest of the production settings
|
||||
|
||||
In the above example, ``development`` will look to the ``phinxlog_dev``
|
||||
table for migration statues while ``production`` will use ``phinxlog``.
|
||||
|
||||
Table Prefix and Suffix
|
||||
-----------------------
|
||||
|
||||
You can define a table prefix and table suffix:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
environments:
|
||||
development:
|
||||
....
|
||||
table_prefix: dev_
|
||||
table_suffix: _v1
|
||||
testing:
|
||||
....
|
||||
table_prefix: test_
|
||||
table_suffix: _v2
|
||||
|
||||
|
||||
Socket Connections
|
||||
------------------
|
||||
|
||||
When using the MySQL adapter, it is also possible to use sockets instead of
|
||||
network connections. The socket path is configured with ``unix_socket``:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
environments:
|
||||
default_migration_table: phinxlog
|
||||
default_environment: development
|
||||
production:
|
||||
adapter: mysql
|
||||
name: production_db
|
||||
user: root
|
||||
pass: ''
|
||||
unix_socket: /var/run/mysql/mysql.sock
|
||||
charset: utf8
|
||||
|
||||
External Variables
|
||||
------------------
|
||||
|
||||
Phinx will automatically grab any environment variable prefixed with ``PHINX_``
|
||||
and make it available as a token in the config file. The token will have
|
||||
exactly the same name as the variable but you must access it by wrapping two
|
||||
``%%`` symbols on either side. e.g: ``'%%PHINX_DBUSER%%'``. This is especially
|
||||
useful if you wish to store your secret database credentials directly on the
|
||||
server and not in a version control system. This feature can be easily
|
||||
demonstrated by the following example:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
environments:
|
||||
default_migration_table: phinxlog
|
||||
default_environment: development
|
||||
production:
|
||||
adapter: mysql
|
||||
host: '%%PHINX_DBHOST%%'
|
||||
name: '%%PHINX_DBNAME%%'
|
||||
user: '%%PHINX_DBUSER%%'
|
||||
pass: '%%PHINX_DBPASS%%'
|
||||
port: 3306
|
||||
charset: utf8
|
||||
|
||||
Data Source Names
|
||||
-----------------
|
||||
|
||||
Phinx supports the use of data source names (DSN) to specify the connection
|
||||
options, which can be useful if you use a single environment variable to hold
|
||||
the database credentials. PDO has a different DSN formats depending on the
|
||||
underlying driver, so Phinx uses a database-agnostic DSN format used by other
|
||||
projects (Doctrine, Rails, AMQP, PaaS, etc).
|
||||
|
||||
.. code-block:: text
|
||||
|
||||
<adapter>://[<user>[:<pass>]@]<host>[:<port>]/<name>[?<additionalOptions>]
|
||||
|
||||
* A DSN requires at least ``adapter``, ``host`` and ``name``.
|
||||
* You cannot specify a password without a username.
|
||||
* ``port`` must be a positive integer.
|
||||
* ``additionalOptions`` takes the form of a query string, and will be passed to
|
||||
the adapter in the options array.
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
environments:
|
||||
default_migration_table: phinxlog
|
||||
default_environment: development
|
||||
production:
|
||||
# Example data source name
|
||||
dsn: mysql://root@localhost:3306/mydb?charset=utf8
|
||||
|
||||
Once a DSN is parsed, it's values are merged with the already existing
|
||||
connection options. Values in specified in a DSN will never override any value
|
||||
specified directly as connection options.
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
environments:
|
||||
default_migration_table: phinxlog
|
||||
default_environment: development
|
||||
development:
|
||||
dsn: '%%DATABASE_URL%%'
|
||||
production:
|
||||
dsn: '%%DATABASE_URL%%'
|
||||
name: production_database
|
||||
|
||||
If the supplied DSN is invalid, then it is completely ignored.
|
||||
|
||||
Supported Adapters
|
||||
------------------
|
||||
|
||||
Phinx currently supports the following database adapters natively:
|
||||
|
||||
* `MySQL <https://www.mysql.com/>`_: specify the ``mysql`` adapter.
|
||||
* `PostgreSQL <https://www.postgresql.org/>`_: specify the ``pgsql`` adapter.
|
||||
* `SQLite <https://www.sqlite.org/>`_: specify the ``sqlite`` adapter.
|
||||
* `SQL Server <https://www.microsoft.com/sqlserver>`_: specify the ``sqlsrv`` adapter.
|
||||
|
||||
The following settings are available for the adapters:
|
||||
|
||||
adapter
|
||||
The name of the adapter to use, e.g. ``pgsql``.
|
||||
host
|
||||
The database server's hostname (or IP address).
|
||||
port
|
||||
The database server's TCP port number.
|
||||
user
|
||||
The username for the database.
|
||||
pass
|
||||
The password for the database.
|
||||
name
|
||||
The name of the database for this environment. For SQLite, it's recommended to use an absolute path,
|
||||
without the file extension.
|
||||
suffix
|
||||
The suffix to use for the SQLite database file. Defaults to ``.sqlite3``.
|
||||
schema
|
||||
For PostgreSQL, allows specifying the schema to use for the database. Defaults to ``public``.
|
||||
|
||||
For each adapter, you may configure the behavior of the underlying PDO object by setting in your
|
||||
config object the lowercase version of the constant name. This works for both PDO options
|
||||
(e.g. ``\PDO::ATTR_CASE`` would be ``attr_case``) and adapter specific options (e.g. for MySQL
|
||||
you may set ``\PDO::MYSQL_ATTR_IGNORE_SPACE`` as ``mysql_attr_ignore_space``). Please consult
|
||||
the `PDO documentation <https://www.php.net/manual/en/book.pdo.php>`_ for the allowed attributes
|
||||
and their values.
|
||||
|
||||
For example, to set the above example options:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
$config = [
|
||||
"environments" => [
|
||||
"development" => [
|
||||
"adapter" => "mysql",
|
||||
# other adapter settings
|
||||
"attr_case" => \PDO::ATTR_CASE,
|
||||
"mysql_attr_ignore_space" => 1,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
By default, the only attribute that Phinx sets is ``\PDO::ATTR_ERRMODE`` to ``PDO::ERRMODE_EXCEPTION``. It is
|
||||
not recommended to override this.
|
||||
|
||||
MySQL
|
||||
`````````````````
|
||||
|
||||
The MySQL adapter has an unfortunate limitation in that certain actions cause an
|
||||
`implicit commit <https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html>`_ regardless of transaction
|
||||
state. Notably this list includes ``CREATE TABLE``, ``ALTER TABLE``, and ``DROP TABLE``, which are the most
|
||||
common operations that Phinx will run. This means that unlike other adapters which will attempt to gracefully
|
||||
rollback a transaction on a failed migration, if a migration fails for MySQL, it may leave your DB in a partially
|
||||
migrated state.
|
||||
|
||||
SQLite
|
||||
`````````````````
|
||||
|
||||
Declaring an SQLite database uses a simplified structure:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
environments:
|
||||
development:
|
||||
adapter: sqlite
|
||||
name: ./data/derby
|
||||
suffix: ".db" # Defaults to ".sqlite3"
|
||||
testing:
|
||||
adapter: sqlite
|
||||
memory: true # Setting memory to *any* value overrides name
|
||||
|
||||
Starting with PHP 8.1 the SQlite adapter supports ``cache`` and ``mode``
|
||||
query parameters by using the `URI scheme <https://www.sqlite.org/uri.html>`_ as long as ``open_basedir`` is unset.
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
environments:
|
||||
testing:
|
||||
adapter: sqlite
|
||||
name: my_app
|
||||
mode: memory # Determines if the new database is opened read-only, read-write, read-write and created if it does not exist, or that the database is a pure in-memory database that never interacts with disk, respectively.
|
||||
cache: shared # Determines if the new database is opened using shared cache mode or with a private cache.
|
||||
|
||||
SQL Server
|
||||
`````````````````
|
||||
|
||||
When using the ``sqlsrv`` adapter and connecting to a named instance you should
|
||||
omit the ``port`` setting as SQL Server will negotiate the port automatically.
|
||||
Additionally, omit the ``charset: utf8`` or change to ``charset: 65001`` which
|
||||
corresponds to UTF8 for SQL Server.
|
||||
|
||||
Custom Adapters
|
||||
`````````````````
|
||||
|
||||
You can provide a custom adapter by registering an implementation of the `Phinx\\Db\\Adapter\\AdapterInterface`
|
||||
with `AdapterFactory`:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
$name = 'fizz';
|
||||
$class = 'Acme\Adapter\FizzAdapter';
|
||||
|
||||
AdapterFactory::instance()->registerAdapter($name, $class);
|
||||
|
||||
Adapters can be registered any time before `$app->run()` is called, which normally
|
||||
called by `bin/phinx`.
|
||||
|
||||
Templates
|
||||
---------
|
||||
|
||||
You may override how phinx generates the template used with in a handful of ways:
|
||||
|
||||
* file - path to an alternative file to use.
|
||||
* class - class to use for the template, must implement the ``Phinx\Migration\CreationInterface`` interface.
|
||||
* style - style to use for template, either ``change`` or ``up_down``, defaults to ``change`` if not set.
|
||||
|
||||
You should only use one of these options. These can be overridden by passing command line options to the
|
||||
:doc:`Create Command <commands`. Example usage within the config file is:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
templates:
|
||||
style: up_down
|
||||
|
||||
Aliases
|
||||
-------
|
||||
|
||||
Template creation class names can be aliased and used with the ``--class`` command line option for the :doc:`Create Command <commands>`.
|
||||
|
||||
The aliased classes will still be required to implement the ``Phinx\Migration\CreationInterface`` interface.
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
aliases:
|
||||
permission: \Namespace\Migrations\PermissionMigrationTemplateGenerator
|
||||
view: \Namespace\Migrations\ViewMigrationTemplateGenerator
|
||||
|
||||
Version Order
|
||||
-------------
|
||||
|
||||
When rolling back or printing the status of migrations, Phinx orders the executed migrations according to the
|
||||
``version_order`` option, which can have the following values:
|
||||
|
||||
* ``creation`` (the default): migrations are ordered by their creation time, which is also part of their filename.
|
||||
* ``execution``: migrations are ordered by their execution time, also known as start time.
|
||||
|
||||
Bootstrap Path
|
||||
---------------
|
||||
|
||||
You can provide a path to a `bootstrap` php file that will be included before any phinx commands are run. Note that
|
||||
setting External Variables to modify the config will not work because the config has already been parsed by this point.
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
paths:
|
||||
bootstrap: 'phinx-bootstrap.php'
|
||||
|
||||
Within the bootstrap script, the following variables will be available:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
/**
|
||||
* @var string $filename The file name as provided by the configuration
|
||||
* @var string $filePath The absolute, real path to the file
|
||||
* @var \Symfony\Component\Console\Input\InputInterface $input The executing command's input object
|
||||
* @var \Symfony\Component\Console\Output\OutputInterface $output The executing command's output object
|
||||
* @var \Phinx\Console\Command\AbstractCommand $context the executing command object
|
||||
*/
|
||||
|
||||
Feature Flags
|
||||
-------------
|
||||
|
||||
For some breaking changes, Phinx offers a way to opt-out of new behavior. The following flags are available:
|
||||
|
||||
* ``unsigned_primary_keys``: Should Phinx create primary keys as unsigned integers? (default: ``true``)
|
||||
* ``column_null_default``: Should Phinx create columns as null by default? (default: ``true``)
|
||||
|
||||
Since MySQL ``TIMESTAMP`` fields do not support dates past 2038-01-19, you have the option to use ``DATETIME`` field
|
||||
types for fields created by the ``addTimestamps()`` function:
|
||||
|
||||
* ``add_timestamps_use_datetime``: Should Phinx create created_at and updated_at fields as datetime? (default: ``false``)
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
feature_flags:
|
||||
unsigned_primary_keys: false
|
||||
|
||||
These values can also be set by modifying class fields on the ```Phinx\Config\FeatureFlags``` class, converting
|
||||
the flag name to ``camelCase``, for example:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
Phinx\Config\FeatureFlags::$unsignedPrimaryKeys = false;
|
||||
@@ -0,0 +1,14 @@
|
||||
Contents
|
||||
########
|
||||
|
||||
.. toctree::
|
||||
:caption: Phinx
|
||||
|
||||
intro
|
||||
goals
|
||||
install
|
||||
migrations
|
||||
seeding
|
||||
commands
|
||||
configuration
|
||||
copyright
|
||||
@@ -0,0 +1,29 @@
|
||||
.. index::
|
||||
single: Copyright
|
||||
|
||||
Copyright
|
||||
=========
|
||||
|
||||
License
|
||||
|
||||
(The MIT license)
|
||||
|
||||
Copyright (c) 2012 Rob Morgan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
.. index::
|
||||
single: Goals
|
||||
|
||||
Goals
|
||||
=====
|
||||
|
||||
Phinx was developed with the following goals in mind:
|
||||
|
||||
* Be portable amongst the most popular database vendors.
|
||||
* Be PHP framework independent.
|
||||
* Have a simple install process.
|
||||
* Have an easy to use command-line operation.
|
||||
* Integrate with various other PHP tools (Phing, PHPUnit) and web frameworks.
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
:orphan:
|
||||
|
||||
Phinx Documentation
|
||||
===================
|
||||
|
||||
Phinx makes it ridiculously easy to manage the database migrations for your PHP
|
||||
app. In less than 5 minutes, you can install Phinx using Composer and create
|
||||
your first database migration. Phinx is just about migrations without all the
|
||||
bloat of a database ORM system or application framework.
|
||||
|
||||
Phinx requires a 64-bit version of PHP to function.
|
||||
|
||||
Contents
|
||||
========
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
|
||||
intro
|
||||
goals
|
||||
install
|
||||
migrations
|
||||
seeding
|
||||
commands
|
||||
configuration
|
||||
copyright
|
||||
|
||||
Indices and tables
|
||||
==================
|
||||
|
||||
* :ref:`genindex`
|
||||
* :ref:`modindex`
|
||||
* :ref:`search`
|
||||
@@ -0,0 +1,28 @@
|
||||
.. index::
|
||||
single: Installation
|
||||
|
||||
Installation
|
||||
============
|
||||
|
||||
Phinx should be installed using Composer, which is a tool for dependency
|
||||
management in PHP. Please visit the `Composer <https://getcomposer.org/>`_
|
||||
website for more information.
|
||||
|
||||
.. note::
|
||||
|
||||
Phinx requires at least PHP 8.1 (or later).
|
||||
|
||||
To install Phinx, simply require it using Composer:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
php composer.phar require robmorgan/phinx
|
||||
|
||||
Create folders in your project following the structure ``db/migrations`` with adequate permissions.
|
||||
It is where your migration files will live and should be writable.
|
||||
|
||||
Phinx can now be executed from within your project:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
vendor/bin/phinx init
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
.. index::
|
||||
single: Introduction
|
||||
|
||||
Introduction
|
||||
============
|
||||
|
||||
Good developers always version their code using a SCM system, so why don't they
|
||||
do the same for their database schema?
|
||||
|
||||
Phinx allows developers to alter and manipulate databases in a clear and
|
||||
concise way. It avoids the use of writing SQL by hand and instead offers a
|
||||
powerful API for creating migrations using PHP code. Developers can then
|
||||
version these migrations using their preferred SCM system. This makes Phinx
|
||||
migrations portable between different database systems. Phinx keeps track of
|
||||
which migrations have been run, so you can worry less about the state of your
|
||||
database and instead focus on building better software.
|
||||
+1988
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,154 @@
|
||||
:orphan:
|
||||
|
||||
.. index::
|
||||
single: Supporting namespaces
|
||||
|
||||
PSR-4 compliance
|
||||
==================
|
||||
|
||||
Phinx allows the use of namespaces in Migrations and Seeders.
|
||||
Migrations require a timestamp in the filename, and therefore won't be fully PSR-4 compliant. Seeders do not need a timestamp and will be fully PSR-4 compliant.
|
||||
|
||||
Using namespaces
|
||||
------------------------
|
||||
1) locate your Phinx config file, the config file may be in one of following three formats: PHP, YAML or JSON.
|
||||
2) Locate the "paths" key inside the config file, it should look something like one of the below examples.
|
||||
- (NB. the "migrations" and "seeds" keys may be both an array or a string, so don't be alarmed if yours looks different)
|
||||
|
||||
PHP:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
'paths' => [
|
||||
'migrations' => 'database/migrations',
|
||||
'seeds' => 'database/seeds',
|
||||
],
|
||||
|
||||
|
||||
YAML:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
paths:
|
||||
migrations: ./database/migrations
|
||||
seeds: ./database/seeds
|
||||
|
||||
JSON:
|
||||
|
||||
.. code-block:: json
|
||||
|
||||
{
|
||||
"paths": {
|
||||
"migrations": "database/migrations",
|
||||
"seeds": "database/seeds"
|
||||
}
|
||||
}
|
||||
|
||||
3) Enabling namespaces is a fairly simple task, we're going to turn the "migrations" and "seeds" keys into arrays.
|
||||
- Any value without a key is a global-non-namespaced path
|
||||
- Any keyed value will use the key as namespace
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
'paths' => [
|
||||
'migrations' => [
|
||||
'/path/to/migration/without/namespace', // Non-namespaced migrations
|
||||
'Foo' => '/path/to/migration/Foo', // Migrations in the Foo namespace
|
||||
],
|
||||
'seeds' => [
|
||||
'/path/to/seeds/without/namespace', // Non-namespaced seeders
|
||||
'Baz' => '/path/to/seeds/Baz', // Seeders in the Baz namespace
|
||||
]
|
||||
],
|
||||
|
||||
PHP is a bit special in this case, as it allows keyless and keyed values in the same array. To make this configuration work in YAML and JSON, we have to key the non-namespaced path with "0".
|
||||
|
||||
.. code-block:: json
|
||||
|
||||
{
|
||||
"paths": {
|
||||
"migrations": {
|
||||
"0": "./db/migrations",
|
||||
"Foo\\Bar": "./src/FooBar/db/migrations"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
paths:
|
||||
migrations:
|
||||
0: ./db/migrations
|
||||
Foo\\Bar: ./src/FooBar/db/migrations
|
||||
|
||||
Path resolving
|
||||
^^^^^^^^^^^^^^
|
||||
|
||||
Let's take a closer look on how the paths are resolved, let's start with the non-namespaced path.
|
||||
|
||||
"./" refers to the project-root, therefore "./db/migrations" would resolve to <project-root>/db/migrations.
|
||||
This is the directory where Phinx will look for migrations when migrating.
|
||||
NB. these migrations must not have a namespace.
|
||||
|
||||
.. image:: https://i.imgur.com/l84308Q.jpg
|
||||
|
||||
This image shows the path for "./db/migrations" where "Phinx" is the project root.
|
||||
|
||||
And the namespaced path would be resolved as shown below.
|
||||
|
||||
"./src/FooBar/db/migrations" would resolve to <project-root>/src/FooBar/db/migrations, which is where Phinx will look for migrations in the Foo\\Bar namespace.
|
||||
|
||||
.. image:: https://i.imgur.com/2mg0V8V.jpg
|
||||
|
||||
The file path would look like this, if the project-root was "Phinx"
|
||||
|
||||
File examples
|
||||
^^^^^^^^^^^^^
|
||||
|
||||
The non-namespaced file in <project-root>/db/migrations may look like the following example.
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
|
||||
use Phinx\Migration\AbstractMigration;
|
||||
|
||||
class CreateUserTable extends AbstractMigration
|
||||
{
|
||||
public function change()
|
||||
{
|
||||
$table = $this->table('users');
|
||||
$table->addColumn('name', 'string')->create();
|
||||
}
|
||||
}
|
||||
|
||||
Whereas the namespaced file will be found in <project-root>/src/FoorBar/db/migrations and can look like this:
|
||||
(Notice the namespace is the same as defined in the paths config).
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
|
||||
namespace Foo\Bar;
|
||||
|
||||
use Phinx\Migration\AbstractMigration;
|
||||
|
||||
class CreateUserTable extends AbstractMigration
|
||||
{
|
||||
public function change()
|
||||
{
|
||||
$table = $this->table('users');
|
||||
$table->addColumn('name', 'string')->create();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
4) That's it, you're ready to go, to create a migration simply run: *$ phinx create CreateUsersTable [--path ./src/FoorBar/db/migrations]*
|
||||
|
||||
- If multiple paths are configured, but none provided with the --path flag, you will be prompted for which path to use.
|
||||
|
||||
|
||||
Did you run into an issue?
|
||||
--------------------------
|
||||
|
||||
- Due to the way the migrations are created, it is impossible to generate a migration in the *global* namespace with a class-name that is the same as a migration in a user-defined namespace.
|
||||
+234
@@ -0,0 +1,234 @@
|
||||
.. index::
|
||||
single: Database Seeding
|
||||
|
||||
Database Seeding
|
||||
================
|
||||
|
||||
In version 0.5.0 Phinx introduced support for seeding your database with test
|
||||
data. Seed classes are a great way to easily fill your database with data after
|
||||
it's created. By default they are stored in the `seeds` directory; however, this
|
||||
path can be changed in your configuration file.
|
||||
|
||||
.. note::
|
||||
|
||||
Database seeding is entirely optional, and Phinx does not create a `seeds`
|
||||
directory by default.
|
||||
|
||||
Creating a New Seed Class
|
||||
-------------------------
|
||||
|
||||
Phinx includes a command to easily generate a new seed class:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ php vendor/bin/phinx seed:create UserSeeder
|
||||
|
||||
If you have specified multiple seed paths, you will be asked to select which
|
||||
path to create the new seed class in.
|
||||
|
||||
It is based on a skeleton template:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
|
||||
use Phinx\Seed\AbstractSeed;
|
||||
|
||||
class MyNewSeeder extends AbstractSeed
|
||||
{
|
||||
/**
|
||||
* Run Method.
|
||||
*
|
||||
* Write your database seeder using this method.
|
||||
*
|
||||
* More information on writing seeders is available here:
|
||||
* https://book.cakephp.org/phinx/0/en/seeding.html
|
||||
*/
|
||||
public function run() : void
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
The AbstractSeed Class
|
||||
----------------------
|
||||
|
||||
All Phinx seeds extend from the ``AbstractSeed`` class. This class provides the
|
||||
necessary support to create your seed classes. Seed classes are primarily used
|
||||
to insert test data.
|
||||
|
||||
The Run Method
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
The run method is automatically invoked by Phinx when you execute the `seed:run`
|
||||
command. You should use this method to insert your test data.
|
||||
|
||||
.. note::
|
||||
|
||||
Unlike with migrations, Phinx does not keep track of which seed classes have
|
||||
been run. This means database seeders can be run repeatedly. Keep this in
|
||||
mind when developing them.
|
||||
|
||||
The Init Method
|
||||
~~~~~~~~~~~~~~~
|
||||
|
||||
The ``init()`` method is run by Phinx before the run method if it exists. This
|
||||
can be used to initialize properties of the Seed class before using run.
|
||||
|
||||
The Should Execute Method
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The ``shouldExecute()`` method is run by Phinx before executing the seed.
|
||||
This can be used to prevent the seed from being executed at this time. It always
|
||||
returns true by default. You can override it in your custom ``AbstractSeed``
|
||||
implementation.
|
||||
|
||||
Foreign Key Dependencies
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Often you'll find that seeders need to run in a particular order, so they don't
|
||||
violate foreign key constraints. To define this order, you can implement the
|
||||
``getDependencies()`` method that returns an array of seeders to run before the
|
||||
current seeder:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
|
||||
use Phinx\Seed\AbstractSeed;
|
||||
|
||||
class ShoppingCartSeeder extends AbstractSeed
|
||||
{
|
||||
public function getDependencies()
|
||||
{
|
||||
return [
|
||||
'UserSeeder',
|
||||
'ShopItemSeeder'
|
||||
];
|
||||
}
|
||||
|
||||
public function run() : void
|
||||
{
|
||||
// Seed the shopping cart after the `UserSeeder` and
|
||||
// `ShopItemSeeder` have been run.
|
||||
}
|
||||
}
|
||||
|
||||
.. note::
|
||||
|
||||
Dependencies are only considered when executing all seed classes (default behavior).
|
||||
They won't be considered when running specific seed classes.
|
||||
|
||||
Inserting Data
|
||||
--------------
|
||||
|
||||
Using The Table Object
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Seed classes can also use the familiar `Table` object to insert data. You can
|
||||
retrieve an instance of the Table object by calling the ``table()`` method from
|
||||
within your seed class and then use the `insert()` method to insert data:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
|
||||
use Phinx\Seed\AbstractSeed;
|
||||
|
||||
class PostsSeeder extends AbstractSeed
|
||||
{
|
||||
public function run() : void
|
||||
{
|
||||
$data = [
|
||||
[
|
||||
'body' => 'foo',
|
||||
'created' => date('Y-m-d H:i:s'),
|
||||
],[
|
||||
'body' => 'bar',
|
||||
'created' => date('Y-m-d H:i:s'),
|
||||
]
|
||||
];
|
||||
|
||||
$posts = $this->table('posts');
|
||||
$posts->insert($data)
|
||||
->saveData();
|
||||
}
|
||||
}
|
||||
|
||||
.. note::
|
||||
|
||||
You must call the `saveData()` method to commit your data to the table. Phinx
|
||||
will buffer data until you do so.
|
||||
|
||||
Truncating Tables
|
||||
-----------------
|
||||
|
||||
In addition to inserting data Phinx makes it trivial to empty your tables using the
|
||||
SQL `TRUNCATE` command:
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
|
||||
use Phinx\Seed\AbstractSeed;
|
||||
|
||||
class UserSeeder extends AbstractSeed
|
||||
{
|
||||
public function run() : void
|
||||
{
|
||||
$data = [
|
||||
[
|
||||
'body' => 'foo',
|
||||
'created' => date('Y-m-d H:i:s'),
|
||||
],
|
||||
[
|
||||
'body' => 'bar',
|
||||
'created' => date('Y-m-d H:i:s'),
|
||||
]
|
||||
];
|
||||
|
||||
$posts = $this->table('posts');
|
||||
$posts->insert($data)
|
||||
->saveData();
|
||||
|
||||
// empty the table
|
||||
$posts->truncate();
|
||||
}
|
||||
}
|
||||
|
||||
.. note::
|
||||
|
||||
SQLite doesn't natively support the `TRUNCATE` command so behind the scenes
|
||||
`DELETE FROM` is used. It is recommended to call the `VACUUM` command
|
||||
after truncating a table. Phinx does not do this automatically.
|
||||
|
||||
Executing Seed Classes
|
||||
----------------------
|
||||
|
||||
This is the easy part. To seed your database, simply use the `seed:run` command:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ php vendor/bin/phinx seed:run
|
||||
|
||||
By default, Phinx will execute all available seed classes. If you would like to
|
||||
run a specific class, simply pass in the name of it using the `-s` parameter:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ php vendor/bin/phinx seed:run -s UserSeeder
|
||||
|
||||
You can also run multiple seeders:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ php vendor/bin/phinx seed:run -s UserSeeder -s PermissionSeeder -s LogSeeder
|
||||
|
||||
You can also use the `-v` parameter for more output verbosity:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ php vendor/bin/phinx seed:run -v
|
||||
|
||||
The Phinx seed functionality provides a simple mechanism to easily and repeatably
|
||||
insert test data into your database.
|
||||
@@ -0,0 +1,551 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Config;
|
||||
|
||||
use Closure;
|
||||
use InvalidArgumentException;
|
||||
use Phinx\Db\Adapter\SQLiteAdapter;
|
||||
use Phinx\Util\Util;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ReturnTypeWillChange;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* Phinx configuration class.
|
||||
*/
|
||||
class Config implements ConfigInterface, NamespaceAwareInterface
|
||||
{
|
||||
use NamespaceAwareTrait;
|
||||
|
||||
/**
|
||||
* The value that identifies a version order by creation time.
|
||||
*/
|
||||
public const VERSION_ORDER_CREATION_TIME = 'creation';
|
||||
|
||||
/**
|
||||
* The value that identifies a version order by execution time.
|
||||
*/
|
||||
public const VERSION_ORDER_EXECUTION_TIME = 'execution';
|
||||
|
||||
public const TEMPLATE_STYLE_CHANGE = 'change';
|
||||
public const TEMPLATE_STYLE_UP_DOWN = 'up_down';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $values = [];
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected ?string $configFilePath = null;
|
||||
|
||||
/**
|
||||
* @param array $configArray Config array
|
||||
* @param string|null $configFilePath Config file path
|
||||
*/
|
||||
public function __construct(array $configArray, ?string $configFilePath = null)
|
||||
{
|
||||
$this->configFilePath = $configFilePath;
|
||||
$this->values = $this->replaceTokens($configArray);
|
||||
|
||||
if (isset($this->values['feature_flags'])) {
|
||||
FeatureFlags::setFlagsFromConfig($this->values['feature_flags']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the config class using a Yaml file path.
|
||||
*
|
||||
* @param string $configFilePath Path to the Yaml File
|
||||
* @throws \RuntimeException
|
||||
* @return \Phinx\Config\ConfigInterface
|
||||
*/
|
||||
public static function fromYaml(string $configFilePath): ConfigInterface
|
||||
{
|
||||
if (!class_exists('Symfony\\Component\\Yaml\\Yaml', true)) {
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new RuntimeException('Missing yaml parser, symfony/yaml package is not installed.');
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
$configFile = file_get_contents($configFilePath);
|
||||
$configArray = Yaml::parse($configFile);
|
||||
|
||||
if (!is_array($configArray)) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'File \'%s\' must be valid YAML',
|
||||
$configFilePath,
|
||||
));
|
||||
}
|
||||
|
||||
return new static($configArray, $configFilePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the config class using a JSON file path.
|
||||
*
|
||||
* @param string $configFilePath Path to the JSON File
|
||||
* @throws \RuntimeException
|
||||
* @return \Phinx\Config\ConfigInterface
|
||||
*/
|
||||
public static function fromJson(string $configFilePath): ConfigInterface
|
||||
{
|
||||
if (!function_exists('json_decode')) {
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new RuntimeException('Need to install JSON PHP extension to use JSON config');
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
$configArray = json_decode(file_get_contents($configFilePath), true);
|
||||
if (!is_array($configArray)) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'File \'%s\' must be valid JSON',
|
||||
$configFilePath,
|
||||
));
|
||||
}
|
||||
|
||||
return new static($configArray, $configFilePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the config class using a PHP file path.
|
||||
*
|
||||
* @param string $configFilePath Path to the PHP File
|
||||
* @throws \RuntimeException
|
||||
* @return \Phinx\Config\ConfigInterface
|
||||
*/
|
||||
public static function fromPhp(string $configFilePath): ConfigInterface
|
||||
{
|
||||
ob_start();
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
$configArray = include $configFilePath;
|
||||
|
||||
// Hide console output
|
||||
ob_end_clean();
|
||||
|
||||
if (!is_array($configArray)) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'PHP file \'%s\' must return an array',
|
||||
$configFilePath,
|
||||
));
|
||||
}
|
||||
|
||||
return new static($configArray, $configFilePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getEnvironments(): ?array
|
||||
{
|
||||
if (isset($this->values['environments'])) {
|
||||
$environments = [];
|
||||
foreach ($this->values['environments'] as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$environments[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $environments;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getEnvironment(string $name): ?array
|
||||
{
|
||||
$environments = $this->getEnvironments();
|
||||
|
||||
if (isset($environments[$name])) {
|
||||
if (
|
||||
isset($this->values['environments']['default_migration_table'])
|
||||
&& !isset($environments[$name]['migration_table'])
|
||||
) {
|
||||
$environments[$name]['migration_table'] =
|
||||
$this->values['environments']['default_migration_table'];
|
||||
}
|
||||
|
||||
if (
|
||||
isset($environments[$name]['adapter'])
|
||||
&& $environments[$name]['adapter'] === 'sqlite'
|
||||
&& SQLiteAdapter::isMemory($environments[$name])
|
||||
) {
|
||||
$environments[$name]['name'] = SQLiteAdapter::MEMORY;
|
||||
}
|
||||
|
||||
return $this->parseAgnosticDsn($environments[$name]);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasEnvironment(string $name): bool
|
||||
{
|
||||
return $this->getEnvironment($name) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getDefaultEnvironment(): string
|
||||
{
|
||||
// The $PHINX_ENVIRONMENT variable overrides all other default settings
|
||||
$env = getenv('PHINX_ENVIRONMENT');
|
||||
if (!empty($env)) {
|
||||
if ($this->hasEnvironment($env)) {
|
||||
return $env;
|
||||
}
|
||||
|
||||
throw new RuntimeException(sprintf(
|
||||
'The environment configuration (read from $PHINX_ENVIRONMENT) for \'%s\' is missing',
|
||||
$env,
|
||||
));
|
||||
}
|
||||
|
||||
// deprecated: to be removed 0.13
|
||||
if (isset($this->values['environments']['default_database'])) {
|
||||
trigger_error('default_database in the config has been deprecated since 0.12, use default_environment instead.', E_USER_DEPRECATED);
|
||||
$this->values['environments']['default_environment'] = $this->values['environments']['default_database'];
|
||||
}
|
||||
|
||||
// if the user has configured a default environment then use it,
|
||||
// providing it actually exists!
|
||||
if (isset($this->values['environments']['default_environment'])) {
|
||||
if ($this->hasEnvironment($this->values['environments']['default_environment'])) {
|
||||
return $this->values['environments']['default_environment'];
|
||||
}
|
||||
|
||||
throw new RuntimeException(sprintf(
|
||||
'The environment configuration for \'%s\' is missing',
|
||||
$this->values['environments']['default_environment'],
|
||||
));
|
||||
}
|
||||
|
||||
// else default to the first available one
|
||||
if (is_array($this->getEnvironments()) && count($this->getEnvironments()) > 0) {
|
||||
$names = array_keys($this->getEnvironments());
|
||||
|
||||
return $names[0];
|
||||
}
|
||||
|
||||
throw new RuntimeException('Could not find a default environment');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getAlias($alias): ?string
|
||||
{
|
||||
return !empty($this->values['aliases'][$alias]) ? $this->values['aliases'][$alias] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getAliases(): array
|
||||
{
|
||||
return !empty($this->values['aliases']) ? $this->values['aliases'] : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getConfigFilePath(): ?string
|
||||
{
|
||||
return $this->configFilePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws \UnexpectedValueException
|
||||
*/
|
||||
public function getMigrationPaths(): array
|
||||
{
|
||||
if (!isset($this->values['paths']['migrations'])) {
|
||||
throw new UnexpectedValueException('Migrations path missing from config file');
|
||||
}
|
||||
|
||||
if (is_string($this->values['paths']['migrations'])) {
|
||||
$this->values['paths']['migrations'] = [$this->values['paths']['migrations']];
|
||||
}
|
||||
|
||||
return $this->values['paths']['migrations'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws \UnexpectedValueException
|
||||
*/
|
||||
public function getSeedPaths(): array
|
||||
{
|
||||
if (!isset($this->values['paths']['seeds'])) {
|
||||
throw new UnexpectedValueException('Seeds path missing from config file');
|
||||
}
|
||||
|
||||
if (is_string($this->values['paths']['seeds'])) {
|
||||
$this->values['paths']['seeds'] = [$this->values['paths']['seeds']];
|
||||
}
|
||||
|
||||
return $this->values['paths']['seeds'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getMigrationBaseClassName(bool $dropNamespace = true): string
|
||||
{
|
||||
$className = !isset($this->values['migration_base_class']) ? 'Phinx\Migration\AbstractMigration' : $this->values['migration_base_class'];
|
||||
|
||||
return $dropNamespace ? (substr(strrchr($className, '\\'), 1) ?: $className) : $className;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getSeedBaseClassName(bool $dropNamespace = true): string
|
||||
{
|
||||
$className = !isset($this->values['seed_base_class']) ? 'Phinx\Seed\AbstractSeed' : $this->values['seed_base_class'];
|
||||
|
||||
return $dropNamespace ? substr(strrchr($className, '\\'), 1) : $className;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getTemplateFile(): string|false
|
||||
{
|
||||
if (!isset($this->values['templates']['file'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->values['templates']['file'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getTemplateClass(): string|false
|
||||
{
|
||||
if (!isset($this->values['templates']['class'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->values['templates']['class'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getTemplateStyle(): string
|
||||
{
|
||||
if (!isset($this->values['templates']['style'])) {
|
||||
return self::TEMPLATE_STYLE_CHANGE;
|
||||
}
|
||||
|
||||
return $this->values['templates']['style'] === self::TEMPLATE_STYLE_UP_DOWN ? self::TEMPLATE_STYLE_UP_DOWN : self::TEMPLATE_STYLE_CHANGE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getDataDomain(): array
|
||||
{
|
||||
if (!isset($this->values['data_domain'])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->values['data_domain'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getContainer(): ?ContainerInterface
|
||||
{
|
||||
if (!isset($this->values['container'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->values['container'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getVersionOrder(): string
|
||||
{
|
||||
if (!isset($this->values['version_order'])) {
|
||||
return self::VERSION_ORDER_CREATION_TIME;
|
||||
}
|
||||
|
||||
return $this->values['version_order'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function isVersionOrderCreationTime(): bool
|
||||
{
|
||||
$versionOrder = $this->getVersionOrder();
|
||||
|
||||
return $versionOrder == self::VERSION_ORDER_CREATION_TIME;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getBootstrapFile(): string|false
|
||||
{
|
||||
if (!isset($this->values['paths']['bootstrap'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->values['paths']['bootstrap'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace tokens in the specified array.
|
||||
*
|
||||
* @param array $arr Array to replace
|
||||
* @return array
|
||||
*/
|
||||
protected function replaceTokens(array $arr): array
|
||||
{
|
||||
// Get environment variables
|
||||
// Depending on configuration of server / OS and variables_order directive,
|
||||
// environment variables either end up in $_SERVER (most likely) or $_ENV,
|
||||
// so we search through both
|
||||
$tokens = [];
|
||||
foreach (array_merge($_ENV, $_SERVER) as $varname => $varvalue) {
|
||||
if (strpos($varname, 'PHINX_') === 0) {
|
||||
$tokens['%%' . $varname . '%%'] = $varvalue;
|
||||
}
|
||||
}
|
||||
|
||||
// Phinx defined tokens (override env tokens)
|
||||
$tokens['%%PHINX_CONFIG_PATH%%'] = $this->getConfigFilePath();
|
||||
$tokens['%%PHINX_CONFIG_DIR%%'] = $this->getConfigFilePath() !== null ? dirname($this->getConfigFilePath()) : '';
|
||||
|
||||
// Recurse the array and replace tokens
|
||||
return $this->recurseArrayForTokens($arr, $tokens);
|
||||
}
|
||||
|
||||
/**
|
||||
* Recurse an array for the specified tokens and replace them.
|
||||
*
|
||||
* @param array $arr Array to recurse
|
||||
* @param string[] $tokens Array of tokens to search for
|
||||
* @return array
|
||||
*/
|
||||
protected function recurseArrayForTokens(array $arr, array $tokens): array
|
||||
{
|
||||
$out = [];
|
||||
foreach ($arr as $name => $value) {
|
||||
if (is_array($value)) {
|
||||
$out[$name] = $this->recurseArrayForTokens($value, $tokens);
|
||||
continue;
|
||||
}
|
||||
if (is_string($value)) {
|
||||
foreach ($tokens as $token => $tval) {
|
||||
$value = str_replace($token, $tval ?? '', $value);
|
||||
}
|
||||
$out[$name] = $value;
|
||||
continue;
|
||||
}
|
||||
$out[$name] = $value;
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a database-agnostic DSN into individual options.
|
||||
*
|
||||
* @param array<string, mixed> $options Options
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function parseAgnosticDsn(array $options): array
|
||||
{
|
||||
$parsed = Util::parseDsn($options['dsn'] ?? '');
|
||||
if ($parsed) {
|
||||
unset($options['dsn']);
|
||||
}
|
||||
|
||||
$options += $parsed;
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @param mixed $id ID
|
||||
* @param mixed $value Value
|
||||
* @return void
|
||||
*/
|
||||
public function offsetSet($id, $value): void
|
||||
{
|
||||
$this->values[$id] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @param mixed $id ID
|
||||
* @throws \InvalidArgumentException
|
||||
* @return mixed
|
||||
*/
|
||||
#[ReturnTypeWillChange]
|
||||
public function offsetGet($id)
|
||||
{
|
||||
if (!array_key_exists($id, $this->values)) {
|
||||
throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
|
||||
}
|
||||
|
||||
return $this->values[$id] instanceof Closure ? $this->values[$id]($this) : $this->values[$id];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @param mixed $id ID
|
||||
* @return bool
|
||||
*/
|
||||
public function offsetExists($id): bool
|
||||
{
|
||||
return isset($this->values[$id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @param mixed $id ID
|
||||
* @return void
|
||||
*/
|
||||
public function offsetUnset($id): void
|
||||
{
|
||||
unset($this->values[$id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getSeedTemplateFile(): ?string
|
||||
{
|
||||
return $this->values['templates']['seedFile'] ?? null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Config;
|
||||
|
||||
use ArrayAccess;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Phinx configuration interface.
|
||||
*/
|
||||
interface ConfigInterface extends ArrayAccess
|
||||
{
|
||||
/**
|
||||
* Returns the configuration for each environment.
|
||||
*
|
||||
* This method returns <code>null</code> if no environments exist.
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function getEnvironments(): ?array;
|
||||
|
||||
/**
|
||||
* Returns the configuration for a given environment.
|
||||
*
|
||||
* This method returns <code>null</code> if the specified environment
|
||||
* doesn't exist.
|
||||
*
|
||||
* @param string $name Environment Name
|
||||
* @return array|null
|
||||
*/
|
||||
public function getEnvironment(string $name): ?array;
|
||||
|
||||
/**
|
||||
* Does the specified environment exist in the configuration file?
|
||||
*
|
||||
* @param string $name Environment Name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasEnvironment(string $name): bool;
|
||||
|
||||
/**
|
||||
* Gets the default environment name.
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultEnvironment(): string;
|
||||
|
||||
/**
|
||||
* Get the aliased value from a supplied alias.
|
||||
*
|
||||
* @param string $alias Alias
|
||||
* @return string|null
|
||||
*/
|
||||
public function getAlias(string $alias): ?string;
|
||||
|
||||
/**
|
||||
* Get all the aliased values.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getAliases(): array;
|
||||
|
||||
/**
|
||||
* Gets the config file path.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getConfigFilePath(): ?string;
|
||||
|
||||
/**
|
||||
* Gets the paths to search for migration files.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getMigrationPaths(): array;
|
||||
|
||||
/**
|
||||
* Gets the paths to search for seed files.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getSeedPaths(): array;
|
||||
|
||||
/**
|
||||
* Get the template file name.
|
||||
*
|
||||
* @return string|false
|
||||
*/
|
||||
public function getTemplateFile(): string|false;
|
||||
|
||||
/**
|
||||
* Get the template class name.
|
||||
*
|
||||
* @return string|false
|
||||
*/
|
||||
public function getTemplateClass(): string|false;
|
||||
|
||||
/**
|
||||
* Get the template style to use, either change or up_down.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTemplateStyle(): string;
|
||||
|
||||
/**
|
||||
* Get the user-provided container for instantiating seeds
|
||||
*
|
||||
* @return \Psr\Container\ContainerInterface|null
|
||||
*/
|
||||
public function getContainer(): ?ContainerInterface;
|
||||
|
||||
/**
|
||||
* Get the data domain array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDataDomain(): array;
|
||||
|
||||
/**
|
||||
* Get the version order.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getVersionOrder(): string;
|
||||
|
||||
/**
|
||||
* Is version order creation time?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isVersionOrderCreationTime(): bool;
|
||||
|
||||
/**
|
||||
* Get the bootstrap file path
|
||||
*
|
||||
* @return string|false
|
||||
*/
|
||||
public function getBootstrapFile(): string|false;
|
||||
|
||||
/**
|
||||
* Gets the base class name for migrations.
|
||||
*
|
||||
* @param bool $dropNamespace Return the base migration class name without the namespace.
|
||||
* @return string
|
||||
*/
|
||||
public function getMigrationBaseClassName(bool $dropNamespace = true): string;
|
||||
|
||||
/**
|
||||
* Gets the base class name for seeders.
|
||||
*
|
||||
* @param bool $dropNamespace Return the base seeder class name without the namespace.
|
||||
* @return string
|
||||
*/
|
||||
public function getSeedBaseClassName(bool $dropNamespace = true): string;
|
||||
|
||||
/**
|
||||
* Get the seeder template file name or null if not set.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getSeedTemplateFile(): ?string;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Config;
|
||||
|
||||
/**
|
||||
* Class to hold features flags to toggle breaking changes in Phinx.
|
||||
*
|
||||
* New flags should be added very sparingly.
|
||||
*/
|
||||
class FeatureFlags
|
||||
{
|
||||
/**
|
||||
* @var bool Should Phinx create unsigned primary keys by default?
|
||||
*/
|
||||
public static bool $unsignedPrimaryKeys = true;
|
||||
/**
|
||||
* @var bool Should Phinx create columns NULL by default?
|
||||
*/
|
||||
public static bool $columnNullDefault = true;
|
||||
/**
|
||||
* @var bool Should Phinx create datetime columns for addTimestamps instead of timestamp?
|
||||
*/
|
||||
public static bool $addTimestampsUseDateTime = false;
|
||||
|
||||
/**
|
||||
* Set the feature flags from the `feature_flags` section of the overall
|
||||
* config.
|
||||
*
|
||||
* @param array $config The `feature_flags` section of the config
|
||||
*/
|
||||
public static function setFlagsFromConfig(array $config): void
|
||||
{
|
||||
if (isset($config['unsigned_primary_keys'])) {
|
||||
self::$unsignedPrimaryKeys = (bool)$config['unsigned_primary_keys'];
|
||||
}
|
||||
if (isset($config['column_null_default'])) {
|
||||
self::$columnNullDefault = (bool)$config['column_null_default'];
|
||||
}
|
||||
if (isset($config['add_timestamps_use_datetime'])) {
|
||||
self::$addTimestampsUseDateTime = (bool)$config['add_timestamps_use_datetime'];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Config;
|
||||
|
||||
/**
|
||||
* Config aware getNamespaceByPath method.
|
||||
*/
|
||||
interface NamespaceAwareInterface
|
||||
{
|
||||
/**
|
||||
* Get Migration Namespace associated with path.
|
||||
*
|
||||
* @param string $path Path
|
||||
* @return string|null
|
||||
*/
|
||||
public function getMigrationNamespaceByPath(string $path): ?string;
|
||||
|
||||
/**
|
||||
* Get Seed Namespace associated with path.
|
||||
*
|
||||
* @param string $path Path
|
||||
* @return string|null
|
||||
*/
|
||||
public function getSeedNamespaceByPath(string $path): ?string;
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Config;
|
||||
|
||||
/**
|
||||
* Trait implemented NamespaceAwareInterface.
|
||||
*/
|
||||
trait NamespaceAwareTrait
|
||||
{
|
||||
/**
|
||||
* Gets the paths to search for migration files.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
abstract public function getMigrationPaths(): array;
|
||||
|
||||
/**
|
||||
* Gets the paths to search for seed files.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
abstract public function getSeedPaths(): array;
|
||||
|
||||
/**
|
||||
* Search $needle in $haystack and return key associate with him.
|
||||
*
|
||||
* @param string $needle Needle
|
||||
* @param string[] $haystack Haystack
|
||||
* @return string|null
|
||||
*/
|
||||
protected function searchNamespace(string $needle, array $haystack): ?string
|
||||
{
|
||||
$needle = realpath($needle);
|
||||
$haystack = array_map('realpath', $haystack);
|
||||
|
||||
$key = array_search($needle, $haystack, true);
|
||||
|
||||
return is_string($key) ? trim($key, '\\') : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Migration Namespace associated with path.
|
||||
*
|
||||
* @param string $path Path
|
||||
* @return string|null
|
||||
*/
|
||||
public function getMigrationNamespaceByPath(string $path): ?string
|
||||
{
|
||||
$paths = $this->getMigrationPaths();
|
||||
|
||||
return $this->searchNamespace($path, $paths);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Seed Namespace associated with path.
|
||||
*
|
||||
* @param string $path Path
|
||||
* @return string|null
|
||||
*/
|
||||
public function getSeedNamespaceByPath(string $path): ?string
|
||||
{
|
||||
$paths = $this->getSeedPaths();
|
||||
|
||||
return $this->searchNamespace($path, $paths);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,507 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Console\Command;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use PDO;
|
||||
use Phinx\Config\Config;
|
||||
use Phinx\Config\ConfigInterface;
|
||||
use Phinx\Db\Adapter\AdapterInterface;
|
||||
use Phinx\Db\Adapter\SQLiteAdapter;
|
||||
use Phinx\Migration\Manager;
|
||||
use Phinx\Util\Util;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* Abstract command, contains bootstrapping info
|
||||
*/
|
||||
abstract class AbstractCommand extends Command
|
||||
{
|
||||
public const FORMAT_JSON = 'json';
|
||||
public const FORMAT_YML_ALIAS = 'yaml';
|
||||
public const FORMAT_YML = 'yml';
|
||||
public const FORMAT_PHP = 'php';
|
||||
public const FORMAT_DEFAULT = 'php';
|
||||
|
||||
/**
|
||||
* The location of the default change migration template.
|
||||
*/
|
||||
protected const DEFAULT_CHANGE_MIGRATION_TEMPLATE = '/../../Migration/Migration.change.template.php.dist';
|
||||
|
||||
/**
|
||||
* The location of the default up/down migration template.
|
||||
*/
|
||||
protected const DEFAULT_UP_DOWN_MIGRATION_TEMPLATE = '/../../Migration/Migration.up_down.template.php.dist';
|
||||
|
||||
/**
|
||||
* The location of the default seed template.
|
||||
*/
|
||||
protected const DEFAULT_SEED_TEMPLATE = '/../../Seed/Seed.template.php.dist';
|
||||
|
||||
/**
|
||||
* @var \Phinx\Config\ConfigInterface|null
|
||||
*/
|
||||
protected ?ConfigInterface $config = null;
|
||||
|
||||
/**
|
||||
* @var \Phinx\Db\Adapter\AdapterInterface
|
||||
*/
|
||||
protected AdapterInterface $adapter;
|
||||
|
||||
/**
|
||||
* @var \Phinx\Migration\Manager
|
||||
*/
|
||||
protected Manager $manager;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected int $verbosityLevel = OutputInterface::OUTPUT_NORMAL | OutputInterface::VERBOSITY_NORMAL;
|
||||
|
||||
/**
|
||||
* Exit code for when command executes successfully
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const CODE_SUCCESS = 0;
|
||||
|
||||
/**
|
||||
* Exit code for when command hits a non-recoverable error during execution
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const CODE_ERROR = 1;
|
||||
|
||||
/**
|
||||
* Exit code for when status command is run and there are missing migrations
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const CODE_STATUS_MISSING = 2;
|
||||
|
||||
/**
|
||||
* Exit code for when status command is run and there are no missing migations,
|
||||
* but does have down migrations
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public const CODE_STATUS_DOWN = 3;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->addOption('--configuration', '-c', InputOption::VALUE_REQUIRED, 'The configuration file to load');
|
||||
$this->addOption('--parser', '-p', InputOption::VALUE_REQUIRED, 'Parser used to read the config file. Defaults to YAML');
|
||||
$this->addOption('--no-info', null, InputOption::VALUE_NONE, 'Hides all debug information');
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap Phinx.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @return void
|
||||
*/
|
||||
public function bootstrap(InputInterface $input, OutputInterface $output): void
|
||||
{
|
||||
if ($input->hasParameterOption('--no-info')) {
|
||||
$this->verbosityLevel = OutputInterface::VERBOSITY_VERBOSE;
|
||||
}
|
||||
|
||||
if (!$this->hasConfig()) {
|
||||
$this->loadConfig($input, $output);
|
||||
}
|
||||
|
||||
$this->loadManager($input, $output);
|
||||
|
||||
$bootstrap = $this->getConfig()->getBootstrapFile();
|
||||
if ($bootstrap) {
|
||||
$output->writeln('<info>using bootstrap</info> ' . Util::relativePath($bootstrap) . ' ', $this->verbosityLevel);
|
||||
Util::loadPhpFile($bootstrap, $input, $output, $this);
|
||||
}
|
||||
|
||||
// report the paths
|
||||
$paths = $this->getConfig()->getMigrationPaths();
|
||||
|
||||
$output->writeln('<info>using migration paths</info> ', $this->verbosityLevel);
|
||||
|
||||
foreach (Util::globAll($paths) as $path) {
|
||||
$output->writeln('<info> - ' . realpath($path) . '</info>', $this->verbosityLevel);
|
||||
}
|
||||
|
||||
try {
|
||||
$paths = $this->getConfig()->getSeedPaths();
|
||||
|
||||
$output->writeln('<info>using seed paths</info> ', $this->verbosityLevel);
|
||||
|
||||
foreach (Util::globAll($paths) as $path) {
|
||||
$output->writeln('<info> - ' . realpath($path) . '</info>', $this->verbosityLevel);
|
||||
}
|
||||
} catch (UnexpectedValueException $e) {
|
||||
// do nothing as seeds are optional
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the config.
|
||||
*
|
||||
* @param \Phinx\Config\ConfigInterface $config Config
|
||||
* @return $this
|
||||
*/
|
||||
public function setConfig(ConfigInterface $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasConfig(): bool
|
||||
{
|
||||
return $this->config !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the config.
|
||||
*
|
||||
* @return \Phinx\Config\ConfigInterface
|
||||
*/
|
||||
public function getConfig(): ConfigInterface
|
||||
{
|
||||
if ($this->config === null) {
|
||||
throw new RuntimeException('No config set yet');
|
||||
}
|
||||
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the database adapter.
|
||||
*
|
||||
* @param \Phinx\Db\Adapter\AdapterInterface $adapter Adapter
|
||||
* @return $this
|
||||
*/
|
||||
public function setAdapter(AdapterInterface $adapter)
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the database adapter.
|
||||
*
|
||||
* @return \Phinx\Db\Adapter\AdapterInterface
|
||||
*/
|
||||
public function getAdapter(): AdapterInterface
|
||||
{
|
||||
return $this->adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the migration manager.
|
||||
*
|
||||
* @param \Phinx\Migration\Manager $manager Manager
|
||||
* @return $this
|
||||
*/
|
||||
public function setManager(Manager $manager)
|
||||
{
|
||||
$this->manager = $manager;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the migration manager.
|
||||
*
|
||||
* @return \Phinx\Migration\Manager|null
|
||||
*/
|
||||
public function getManager(): ?Manager
|
||||
{
|
||||
return $this->manager ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns config file path
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @return string
|
||||
*/
|
||||
protected function locateConfigFile(InputInterface $input): string
|
||||
{
|
||||
$configFile = $input->hasOption('configuration') ? $input->getOption('configuration') : null;
|
||||
|
||||
$useDefault = false;
|
||||
|
||||
if ($configFile === null || $configFile === false) {
|
||||
$useDefault = true;
|
||||
}
|
||||
|
||||
$cwd = getcwd();
|
||||
|
||||
// locate the phinx config file
|
||||
// In future walk the tree in reverse (max 10 levels)
|
||||
$locator = new FileLocator([
|
||||
$cwd . DIRECTORY_SEPARATOR,
|
||||
]);
|
||||
|
||||
if (!$useDefault) {
|
||||
// Locate() throws an exception if the file does not exist
|
||||
return $locator->locate($configFile, $cwd, true);
|
||||
}
|
||||
|
||||
$possibleConfigFiles = ['phinx.php', 'phinx.json', 'phinx.yaml', 'phinx.yml'];
|
||||
foreach ($possibleConfigFiles as $configFile) {
|
||||
try {
|
||||
return $locator->locate($configFile, $cwd, true);
|
||||
} catch (InvalidArgumentException $exception) {
|
||||
$lastException = $exception;
|
||||
}
|
||||
}
|
||||
throw $lastException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the config file and load it into the config object
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @throws \InvalidArgumentException
|
||||
* @return void
|
||||
*/
|
||||
protected function loadConfig(InputInterface $input, OutputInterface $output): void
|
||||
{
|
||||
$configFilePath = $this->locateConfigFile($input);
|
||||
$output->writeln('<info>using config file</info> ' . Util::relativePath($configFilePath), $this->verbosityLevel);
|
||||
|
||||
/** @var string|null $parser */
|
||||
$parser = $input->getOption('parser');
|
||||
|
||||
// If no parser is specified try to determine the correct one from the file extension. Defaults to YAML
|
||||
if ($parser === null) {
|
||||
$extension = pathinfo($configFilePath, PATHINFO_EXTENSION);
|
||||
|
||||
switch (strtolower($extension)) {
|
||||
case self::FORMAT_JSON:
|
||||
$parser = self::FORMAT_JSON;
|
||||
break;
|
||||
case self::FORMAT_YML_ALIAS:
|
||||
case self::FORMAT_YML:
|
||||
$parser = self::FORMAT_YML;
|
||||
break;
|
||||
case self::FORMAT_PHP:
|
||||
default:
|
||||
$parser = self::FORMAT_DEFAULT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (strtolower($parser)) {
|
||||
case self::FORMAT_JSON:
|
||||
$config = Config::fromJson($configFilePath);
|
||||
break;
|
||||
case self::FORMAT_PHP:
|
||||
$config = Config::fromPhp($configFilePath);
|
||||
break;
|
||||
case self::FORMAT_YML_ALIAS:
|
||||
case self::FORMAT_YML:
|
||||
$config = Config::fromYaml($configFilePath);
|
||||
break;
|
||||
default:
|
||||
throw new InvalidArgumentException(sprintf('\'%s\' is not a valid parser.', $parser));
|
||||
}
|
||||
|
||||
$output->writeln('<info>using config parser</info> ' . $parser, $this->verbosityLevel);
|
||||
|
||||
$this->setConfig($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the migrations manager and inject the config
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @return void
|
||||
*/
|
||||
protected function loadManager(InputInterface $input, OutputInterface $output): void
|
||||
{
|
||||
if (!isset($this->manager)) {
|
||||
$manager = new Manager($this->getConfig(), $input, $output);
|
||||
$manager->setVerbosityLevel($this->verbosityLevel);
|
||||
$container = $this->getConfig()->getContainer();
|
||||
if ($container !== null) {
|
||||
$manager->setContainer($container);
|
||||
}
|
||||
$this->setManager($manager);
|
||||
} else {
|
||||
$manager = $this->getManager();
|
||||
$manager->setInput($input);
|
||||
$manager->setOutput($output);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the migration directory exists and is writable.
|
||||
*
|
||||
* @param string $path Path
|
||||
* @throws \InvalidArgumentException
|
||||
* @return void
|
||||
*/
|
||||
protected function verifyMigrationDirectory(string $path): void
|
||||
{
|
||||
if (!is_dir($path)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Migration directory "%s" does not exist',
|
||||
$path,
|
||||
));
|
||||
}
|
||||
|
||||
if (!is_writable($path)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Migration directory "%s" is not writable',
|
||||
$path,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the seed directory exists and is writable.
|
||||
*
|
||||
* @param string $path Path
|
||||
* @throws \InvalidArgumentException
|
||||
* @return void
|
||||
*/
|
||||
protected function verifySeedDirectory(string $path): void
|
||||
{
|
||||
if (!is_dir($path)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Seed directory "%s" does not exist',
|
||||
$path,
|
||||
));
|
||||
}
|
||||
|
||||
if (!is_writable($path)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Seed directory "%s" is not writable',
|
||||
$path,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the migration template filename.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getMigrationTemplateFilename(string $style): string
|
||||
{
|
||||
return $style === Config::TEMPLATE_STYLE_CHANGE ? __DIR__ . self::DEFAULT_CHANGE_MIGRATION_TEMPLATE : __DIR__ . self::DEFAULT_UP_DOWN_MIGRATION_TEMPLATE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the seed template filename.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getSeedTemplateFilename(): string
|
||||
{
|
||||
return __DIR__ . self::DEFAULT_SEED_TEMPLATE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write out environment information to the OutputInterface
|
||||
*/
|
||||
protected function writeEnvironmentOutput(?string &$environment, OutputInterface $output): bool
|
||||
{
|
||||
if ($environment === null) {
|
||||
$environment = $this->getConfig()->getDefaultEnvironment();
|
||||
$output->writeln('<comment>warning</comment> no environment specified, defaulting to: ' . $environment, $this->verbosityLevel);
|
||||
} else {
|
||||
$output->writeln('<info>using environment</info> ' . $environment, $this->verbosityLevel);
|
||||
}
|
||||
|
||||
if (!$this->getConfig()->hasEnvironment($environment)) {
|
||||
self::getErrorOutput($output)->writeln(sprintf('<error>The environment "%s" does not exist</error>', $environment));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write out options information to the OutputInterface
|
||||
*/
|
||||
protected function writeInformationOutput(?string &$environment, OutputInterface $output): bool
|
||||
{
|
||||
$success = $this->writeEnvironmentOutput($environment, $output);
|
||||
if (!$success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$envOptions = $this->getConfig()->getEnvironment($environment);
|
||||
if (isset($envOptions['adapter'])) {
|
||||
$output->writeln('<info>using adapter</info> ' . $envOptions['adapter'], $this->verbosityLevel);
|
||||
}
|
||||
|
||||
if (isset($envOptions['wrapper'])) {
|
||||
$output->writeln('<info>using wrapper</info> ' . $envOptions['wrapper'], $this->verbosityLevel);
|
||||
}
|
||||
|
||||
if (isset($envOptions['name'])) {
|
||||
$name = $envOptions['name'];
|
||||
// We do error handling for missing adapter or connection is invalid later on running a command
|
||||
$adapter = $envOptions['adapter'] ?? null;
|
||||
if (isset($envOptions['connection']) && $envOptions['connection'] instanceof PDO) {
|
||||
$adapter = $envOptions['connection']->getAttribute(PDO::ATTR_DRIVER_NAME);
|
||||
}
|
||||
if ($adapter === 'sqlite') {
|
||||
$name .= SQLiteAdapter::getSuffix($envOptions);
|
||||
}
|
||||
$output->writeln('<info>using database</info> ' . $name, $this->verbosityLevel);
|
||||
} else {
|
||||
self::getErrorOutput($output)->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($envOptions['table_prefix'])) {
|
||||
$output->writeln('<info>using table prefix</info> ' . $envOptions['table_prefix'], $this->verbosityLevel);
|
||||
}
|
||||
if (isset($envOptions['table_suffix'])) {
|
||||
$output->writeln('<info>using table suffix</info> ' . $envOptions['table_suffix'], $this->verbosityLevel);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the error output to use
|
||||
*
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @return \Symfony\Component\Console\Output\OutputInterface
|
||||
*/
|
||||
protected static function getErrorOutput(OutputInterface $output): OutputInterface
|
||||
{
|
||||
return $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Console\Command;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(name: 'breakpoint')]
|
||||
class Breakpoint extends AbstractCommand
|
||||
{
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
|
||||
protected static $defaultName = 'breakpoint';
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function configure(): void
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment.');
|
||||
|
||||
$this->setDescription('Manage breakpoints')
|
||||
->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to target for the breakpoint')
|
||||
->addOption('--set', '-s', InputOption::VALUE_NONE, 'Set the breakpoint')
|
||||
->addOption('--unset', '-u', InputOption::VALUE_NONE, 'Unset the breakpoint')
|
||||
->addOption('--remove-all', '-r', InputOption::VALUE_NONE, 'Remove all breakpoints')
|
||||
->setHelp(
|
||||
<<<EOT
|
||||
The <info>breakpoint</info> command allows you to toggle, set, or unset a breakpoint against a specific target to inhibit rollbacks beyond a certain target.
|
||||
If no target is supplied then the most recent migration will be used.
|
||||
You cannot specify un-migrated targets
|
||||
|
||||
<info>phinx breakpoint -e development</info>
|
||||
<info>phinx breakpoint -e development -t 20110103081132</info>
|
||||
<info>phinx breakpoint -e development -r</info>
|
||||
EOT,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle the breakpoint.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @throws \InvalidArgumentException
|
||||
* @return int integer 0 on success, or an error code.
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$this->bootstrap($input, $output);
|
||||
|
||||
/** @var string|null $environment */
|
||||
$environment = $input->getOption('environment');
|
||||
$version = (int)$input->getOption('target') ?: null;
|
||||
$removeAll = $input->getOption('remove-all');
|
||||
$set = $input->getOption('set');
|
||||
$unset = $input->getOption('unset');
|
||||
|
||||
$success = $this->writeEnvironmentOutput($environment, $output);
|
||||
if (!$success) {
|
||||
return self::CODE_ERROR;
|
||||
}
|
||||
|
||||
if ($version && $removeAll) {
|
||||
throw new InvalidArgumentException('Cannot toggle a breakpoint and remove all breakpoints at the same time.');
|
||||
}
|
||||
|
||||
if (($set && $unset) || ($set && $removeAll) || ($unset && $removeAll)) {
|
||||
throw new InvalidArgumentException('Cannot use more than one of --set, --unset, or --remove-all at the same time.');
|
||||
}
|
||||
|
||||
if ($removeAll) {
|
||||
// Remove all breakpoints.
|
||||
$this->getManager()->removeBreakpoints($environment);
|
||||
} elseif ($set) {
|
||||
// Set the breakpoint.
|
||||
$this->getManager()->setBreakpoint($environment, $version);
|
||||
} elseif ($unset) {
|
||||
// Unset the breakpoint.
|
||||
$this->getManager()->unsetBreakpoint($environment, $version);
|
||||
} else {
|
||||
// Toggle the breakpoint.
|
||||
$this->getManager()->toggleBreakpoint($environment, $version);
|
||||
}
|
||||
|
||||
return self::CODE_SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,348 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Console\Command;
|
||||
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use Phinx\Config\Config;
|
||||
use Phinx\Config\NamespaceAwareInterface;
|
||||
use Phinx\Util\Util;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
use Symfony\Component\Console\Question\ConfirmationQuestion;
|
||||
|
||||
#[AsCommand(name: 'create')]
|
||||
class Create extends AbstractCommand
|
||||
{
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
|
||||
protected static $defaultName = 'create';
|
||||
|
||||
/**
|
||||
* The name of the interface that any external template creation class is required to implement.
|
||||
*/
|
||||
public const CREATION_INTERFACE = 'Phinx\Migration\CreationInterface';
|
||||
|
||||
// PHP keywords from https://www.php.net/manual/en/reserved.keywords.php
|
||||
private array $keywords = [
|
||||
'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const',
|
||||
'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor',
|
||||
'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'finally', 'for', 'foreach',
|
||||
'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface',
|
||||
'isset', 'list', 'namespace', 'new', 'or', 'parent', 'private', 'protected', 'public', 'return','static',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function configure(): void
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->setDescription('Create a new migration')
|
||||
->addArgument('name', InputArgument::OPTIONAL, 'Class name of the migration (in CamelCase)')
|
||||
->setHelp(sprintf(
|
||||
'%sCreates a new database migration%s',
|
||||
PHP_EOL,
|
||||
PHP_EOL,
|
||||
));
|
||||
|
||||
// An alternative template.
|
||||
$this->addOption('template', 't', InputOption::VALUE_REQUIRED, 'Use an alternative template');
|
||||
|
||||
// A classname to be used to gain access to the template content as well as the ability to
|
||||
// have a callback once the migration file has been created.
|
||||
$this->addOption('class', 'l', InputOption::VALUE_REQUIRED, 'Use a class implementing "' . self::CREATION_INTERFACE . '" to generate the template');
|
||||
|
||||
// Allow the migration path to be chosen non-interactively.
|
||||
$this->addOption('path', null, InputOption::VALUE_REQUIRED, 'Specify the path in which to create this migration');
|
||||
|
||||
$this->addOption('style', null, InputOption::VALUE_REQUIRED, 'Specify the style of migration to create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the confirmation question asking if the user wants to create the
|
||||
* migrations directory.
|
||||
*
|
||||
* @return \Symfony\Component\Console\Question\ConfirmationQuestion
|
||||
*/
|
||||
protected function getCreateMigrationDirectoryQuestion(): ConfirmationQuestion
|
||||
{
|
||||
return new ConfirmationQuestion('Create migrations directory? [y]/n ', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the question that allows the user to select which migration path to use.
|
||||
*
|
||||
* @param string[] $paths Paths
|
||||
* @return \Symfony\Component\Console\Question\ChoiceQuestion
|
||||
*/
|
||||
protected function getSelectMigrationPathQuestion(array $paths): ChoiceQuestion
|
||||
{
|
||||
return new ChoiceQuestion('Which migrations path would you like to use?', $paths, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the migration path to create the migration in.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @throws \Exception
|
||||
* @return string
|
||||
*/
|
||||
protected function getMigrationPath(InputInterface $input, OutputInterface $output): string
|
||||
{
|
||||
// First, try the non-interactive option:
|
||||
$path = $input->getOption('path');
|
||||
|
||||
if (!empty($path)) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
$paths = $this->getConfig()->getMigrationPaths();
|
||||
|
||||
// No paths? That's a problem.
|
||||
if (empty($paths)) {
|
||||
throw new Exception('No migration paths set in your Phinx configuration file.');
|
||||
}
|
||||
|
||||
$paths = Util::globAll($paths);
|
||||
|
||||
if (empty($paths)) {
|
||||
throw new Exception(
|
||||
'You probably used curly braces to define migration path in your Phinx configuration file, ' .
|
||||
'but no directories have been matched using this pattern. ' .
|
||||
'You need to create a migration directory manually.',
|
||||
);
|
||||
}
|
||||
|
||||
// Only one path set, so select that:
|
||||
if (count($paths) === 1) {
|
||||
return array_shift($paths);
|
||||
}
|
||||
|
||||
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
|
||||
$helper = $this->getHelper('question');
|
||||
$question = $this->getSelectMigrationPathQuestion($paths);
|
||||
|
||||
return $helper->ask($input, $output, $question);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the new migration.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @throws \RuntimeException
|
||||
* @throws \InvalidArgumentException
|
||||
* @return int 0 on success
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$this->bootstrap($input, $output);
|
||||
|
||||
// get the migration path from the config
|
||||
$path = $this->getMigrationPath($input, $output);
|
||||
|
||||
if (!file_exists($path)) {
|
||||
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
|
||||
$helper = $this->getHelper('question');
|
||||
$question = $this->getCreateMigrationDirectoryQuestion();
|
||||
|
||||
if ($helper->ask($input, $output, $question)) {
|
||||
mkdir($path, 0755, true);
|
||||
}
|
||||
}
|
||||
|
||||
$this->verifyMigrationDirectory($path);
|
||||
|
||||
$config = $this->getConfig();
|
||||
$namespace = $config instanceof NamespaceAwareInterface ? $config->getMigrationNamespaceByPath($path) : null;
|
||||
|
||||
$path = realpath($path);
|
||||
$className = $input->getArgument('name');
|
||||
if ($className !== null && in_array(strtolower($className), $this->keywords)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The migration class name "%s" is a reserved PHP keyword. Please choose a different class name.',
|
||||
$className,
|
||||
));
|
||||
}
|
||||
|
||||
$offset = 0;
|
||||
do {
|
||||
$timestamp = Util::getCurrentTimestamp($offset++);
|
||||
} while (!Util::isUniqueTimestamp($path, $timestamp));
|
||||
|
||||
if ($className === null) {
|
||||
$className = 'V' . $timestamp;
|
||||
$fileName = '';
|
||||
} else {
|
||||
if (!Util::isValidPhinxClassName($className)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The migration class name "%s" is invalid. Please use CamelCase format.',
|
||||
$className,
|
||||
));
|
||||
}
|
||||
|
||||
$fileName = Util::toSnakeCase($className);
|
||||
}
|
||||
$fileName = $timestamp . $fileName . '.php';
|
||||
|
||||
if (!Util::isUniqueMigrationClassName($className, $path)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The migration class name "%s%s" already exists',
|
||||
$namespace ? $namespace . '\\' : '',
|
||||
$className,
|
||||
));
|
||||
}
|
||||
|
||||
$filePath = $path . DIRECTORY_SEPARATOR . $fileName;
|
||||
|
||||
if (is_file($filePath)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The file "%s" already exists',
|
||||
$filePath,
|
||||
));
|
||||
}
|
||||
|
||||
// Get the alternative template and static class options from the config, but only allow one of them.
|
||||
$defaultAltTemplate = $this->getConfig()->getTemplateFile();
|
||||
$defaultCreationClassName = $this->getConfig()->getTemplateClass();
|
||||
$defaultStyle = $this->getConfig()->getTemplateStyle();
|
||||
if ($defaultAltTemplate && $defaultCreationClassName) {
|
||||
throw new InvalidArgumentException('Cannot define template:class and template:file at the same time');
|
||||
}
|
||||
|
||||
// Get the alternative template and static class options from the command line, but only allow one of them.
|
||||
/** @var string|null $altTemplate */
|
||||
$altTemplate = $input->getOption('template');
|
||||
/** @var string|null $creationClassName */
|
||||
$creationClassName = $input->getOption('class');
|
||||
$style = $input->getOption('style');
|
||||
|
||||
if ($altTemplate && $creationClassName) {
|
||||
throw new InvalidArgumentException('Cannot use --template and --class at the same time');
|
||||
}
|
||||
|
||||
if ($style && !in_array($style, [Config::TEMPLATE_STYLE_CHANGE, Config::TEMPLATE_STYLE_UP_DOWN])) {
|
||||
throw new InvalidArgumentException('--style should be one of ' . Config::TEMPLATE_STYLE_CHANGE . ' or ' . Config::TEMPLATE_STYLE_UP_DOWN);
|
||||
}
|
||||
|
||||
// If no commandline options then use the defaults.
|
||||
if (!$altTemplate && !$creationClassName) {
|
||||
$altTemplate = $defaultAltTemplate;
|
||||
$creationClassName = $defaultCreationClassName;
|
||||
}
|
||||
|
||||
// Verify the alternative template file's existence.
|
||||
if ($altTemplate && !is_file($altTemplate)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The alternative template file "%s" does not exist',
|
||||
$altTemplate,
|
||||
));
|
||||
}
|
||||
|
||||
// Verify that the template creation class (or the aliased class) exists and that it implements the required interface.
|
||||
$aliasedClassName = null;
|
||||
if ($creationClassName) {
|
||||
// Supplied class does not exist, is it aliased?
|
||||
if (!class_exists($creationClassName)) {
|
||||
$aliasedClassName = $this->getConfig()->getAlias($creationClassName);
|
||||
if ($aliasedClassName && !class_exists($aliasedClassName)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The class "%s" via the alias "%s" does not exist',
|
||||
$aliasedClassName,
|
||||
$creationClassName,
|
||||
));
|
||||
} elseif (!$aliasedClassName) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The class "%s" does not exist',
|
||||
$creationClassName,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Does the class implement the required interface?
|
||||
if (!$aliasedClassName && !is_subclass_of($creationClassName, self::CREATION_INTERFACE)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The class "%s" does not implement the required interface "%s"',
|
||||
$creationClassName,
|
||||
self::CREATION_INTERFACE,
|
||||
));
|
||||
} elseif ($aliasedClassName && !is_subclass_of($aliasedClassName, self::CREATION_INTERFACE)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The class "%s" via the alias "%s" does not implement the required interface "%s"',
|
||||
$aliasedClassName,
|
||||
$creationClassName,
|
||||
self::CREATION_INTERFACE,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Use the aliased class.
|
||||
$creationClassName = $aliasedClassName ?: $creationClassName;
|
||||
|
||||
// Determine the appropriate mechanism to get the template
|
||||
if ($creationClassName) {
|
||||
// Get the template from the creation class
|
||||
$creationClass = new $creationClassName($input, $output);
|
||||
$contents = $creationClass->getMigrationTemplate();
|
||||
} else {
|
||||
// Load the alternative template if it is defined.
|
||||
$contents = file_get_contents($altTemplate ?: $this->getMigrationTemplateFilename($style ?: $defaultStyle));
|
||||
}
|
||||
|
||||
// inject the class names appropriate to this migration
|
||||
$classes = [
|
||||
'$namespaceDefinition' => $namespace !== null ? (PHP_EOL . 'namespace ' . $namespace . ';' . PHP_EOL) : '',
|
||||
'$namespace' => $namespace,
|
||||
'$useClassName' => $this->getConfig()->getMigrationBaseClassName(false),
|
||||
'$className' => $className,
|
||||
'$version' => Util::getVersionFromFileName($fileName),
|
||||
'$baseClassName' => $this->getConfig()->getMigrationBaseClassName(true),
|
||||
];
|
||||
$contents = strtr($contents, $classes);
|
||||
|
||||
if (file_put_contents($filePath, $contents) === false) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'The file "%s" could not be written to',
|
||||
$path,
|
||||
));
|
||||
}
|
||||
|
||||
// Do we need to do the post creation call to the creation class?
|
||||
if (isset($creationClass)) {
|
||||
/** @var \Phinx\Migration\CreationInterface $creationClass */
|
||||
$creationClass->postMigrationCreation($filePath, $className, $this->getConfig()->getMigrationBaseClassName());
|
||||
}
|
||||
|
||||
$output->writeln('<info>using migration base class</info> ' . $classes['$useClassName'], $this->verbosityLevel);
|
||||
|
||||
if (!empty($altTemplate)) {
|
||||
$output->writeln('<info>using alternative template</info> ' . $altTemplate, $this->verbosityLevel);
|
||||
} elseif (!empty($creationClassName)) {
|
||||
$output->writeln('<info>using template creation class</info> ' . $creationClassName, $this->verbosityLevel);
|
||||
} else {
|
||||
$output->writeln('<info>using default template</info>', $this->verbosityLevel);
|
||||
}
|
||||
|
||||
$output->writeln('<info>created</info> ' . Util::relativePath($filePath), $this->verbosityLevel);
|
||||
|
||||
return self::CODE_SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Console\Command;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(name: 'init')]
|
||||
class Init extends Command
|
||||
{
|
||||
protected const FILE_NAME = 'phinx';
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected static array $supportedFormats = [
|
||||
AbstractCommand::FORMAT_JSON,
|
||||
AbstractCommand::FORMAT_YML_ALIAS,
|
||||
AbstractCommand::FORMAT_YML,
|
||||
AbstractCommand::FORMAT_PHP,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
|
||||
protected static $defaultName = 'init';
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->setDescription('Initialize the application for Phinx')
|
||||
->addOption(
|
||||
'--format',
|
||||
'-f',
|
||||
InputArgument::OPTIONAL,
|
||||
'What format should we use to initialize?',
|
||||
AbstractCommand::FORMAT_DEFAULT,
|
||||
)
|
||||
->addArgument('path', InputArgument::OPTIONAL, 'Which path should we initialize for Phinx?')
|
||||
->setHelp(sprintf(
|
||||
'%sInitializes the application for Phinx%s',
|
||||
PHP_EOL,
|
||||
PHP_EOL,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the application.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Interface implemented by all input classes.
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Interface implemented by all output classes.
|
||||
* @return int 0 on success
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$format = strtolower($input->getOption('format'));
|
||||
$path = $this->resolvePath($input, $format);
|
||||
$this->writeConfig($path, $format);
|
||||
|
||||
$output->writeln("<info>created</info> {$path}");
|
||||
|
||||
return AbstractCommand::CODE_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return valid $path for Phinx's config file.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Interface implemented by all input classes.
|
||||
* @param string $format Format to resolve for
|
||||
* @throws \InvalidArgumentException
|
||||
* @return string
|
||||
*/
|
||||
protected function resolvePath(InputInterface $input, string $format): string
|
||||
{
|
||||
// get the migration path from the config
|
||||
$path = (string)$input->getArgument('path');
|
||||
|
||||
if (!in_array($format, static::$supportedFormats, true)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Invalid format "%s". Format must be either ' . implode(', ', static::$supportedFormats) . '.',
|
||||
$format,
|
||||
));
|
||||
}
|
||||
|
||||
// Fallback
|
||||
if (!$path) {
|
||||
$path = getcwd() . DIRECTORY_SEPARATOR . self::FILE_NAME . '.' . $format;
|
||||
}
|
||||
|
||||
// Adding file name if necessary
|
||||
if (is_dir($path)) {
|
||||
$path .= DIRECTORY_SEPARATOR . self::FILE_NAME . '.' . $format;
|
||||
}
|
||||
|
||||
// Check if path is available
|
||||
$dirname = dirname($path);
|
||||
if (is_dir($dirname) && !is_file($path)) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
// Path is valid, but file already exists
|
||||
if (is_file($path)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Config file "%s" already exists.',
|
||||
$path,
|
||||
));
|
||||
}
|
||||
|
||||
// Dir is invalid
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Invalid path "%s" for config file.',
|
||||
$path,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes Phinx's config in provided $path
|
||||
*
|
||||
* @param string $path Config file's path.
|
||||
* @param string $format Format to use for config file
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws \RuntimeException
|
||||
* @return void
|
||||
*/
|
||||
protected function writeConfig(string $path, string $format = AbstractCommand::FORMAT_DEFAULT): void
|
||||
{
|
||||
// Check if dir is writable
|
||||
$dirname = dirname($path);
|
||||
if (!is_writable($dirname)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The directory "%s" is not writable',
|
||||
$dirname,
|
||||
));
|
||||
}
|
||||
|
||||
if ($format === AbstractCommand::FORMAT_YML_ALIAS) {
|
||||
$format = AbstractCommand::FORMAT_YML;
|
||||
}
|
||||
|
||||
// load the config template
|
||||
if (is_dir(__DIR__ . '/../../../../data')) {
|
||||
$contents = file_get_contents(__DIR__ . '/../../../../data/' . self::FILE_NAME . '.' . $format . '.dist');
|
||||
} else {
|
||||
throw new RuntimeException(sprintf(
|
||||
'Could not find template for format "%s".',
|
||||
$format,
|
||||
));
|
||||
}
|
||||
|
||||
if (file_put_contents($path, $contents) === false) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'The file "%s" could not be written to',
|
||||
$path,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Console\Command;
|
||||
|
||||
use Phinx\Util\Util;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(name: 'list:aliases')]
|
||||
class ListAliases extends AbstractCommand
|
||||
{
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
|
||||
protected static $defaultName = 'list:aliases';
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function configure(): void
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->setDescription('List template class aliases')
|
||||
->setHelp('The <info>list:aliases</info> command lists the migration template generation class aliases');
|
||||
}
|
||||
|
||||
/**
|
||||
* List migration template creation aliases.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @return int 0 on success
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$this->bootstrap($input, $output);
|
||||
|
||||
$aliases = $this->config->getAliases();
|
||||
|
||||
if ($aliases) {
|
||||
$maxAliasLength = max(array_map('strlen', array_keys($aliases)));
|
||||
$maxClassLength = max(array_map('strlen', $aliases));
|
||||
$output->writeln(
|
||||
array_merge(
|
||||
[
|
||||
'',
|
||||
sprintf('%s %s', str_pad('Alias', $maxAliasLength), str_pad('Class', $maxClassLength)),
|
||||
sprintf('%s %s', str_repeat('=', $maxAliasLength), str_repeat('=', $maxClassLength)),
|
||||
],
|
||||
array_map(
|
||||
function ($alias, $class) use ($maxAliasLength, $maxClassLength) {
|
||||
return sprintf('%s %s', str_pad($alias, $maxAliasLength), str_pad($class, $maxClassLength));
|
||||
},
|
||||
array_keys($aliases),
|
||||
$aliases,
|
||||
),
|
||||
),
|
||||
$this->verbosityLevel,
|
||||
);
|
||||
} else {
|
||||
$output->writeln(
|
||||
'<comment>warning</comment> no aliases defined in ' . Util::relativePath(
|
||||
$this->config->getConfigFilePath(),
|
||||
),
|
||||
$this->verbosityLevel,
|
||||
);
|
||||
}
|
||||
|
||||
return self::CODE_SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Console\Command;
|
||||
|
||||
use DateTime;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Throwable;
|
||||
|
||||
#[AsCommand(name: 'migrate')]
|
||||
class Migrate extends AbstractCommand
|
||||
{
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
|
||||
protected static $defaultName = 'migrate';
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function configure(): void
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment');
|
||||
|
||||
$this->setDescription('Migrate the database')
|
||||
->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to migrate to')
|
||||
->addOption('--date', '-d', InputOption::VALUE_REQUIRED, 'The date to migrate to')
|
||||
->addOption('--count', '-k', InputOption::VALUE_REQUIRED, 'The number of migrations to run')
|
||||
->addOption('--dry-run', '-x', InputOption::VALUE_NONE, 'Dump query to standard output instead of executing it')
|
||||
->addOption('--fake', null, InputOption::VALUE_NONE, "Mark any migrations selected as run, but don't actually execute them")
|
||||
->setHelp(
|
||||
<<<EOT
|
||||
The <info>migrate</info> command runs all available migrations, optionally up to a specific version, date, or count.
|
||||
|
||||
<info>phinx migrate -e development</info>
|
||||
<info>phinx migrate -e development -t 20110103081132</info>
|
||||
<info>phinx migrate -e development -d 20110103</info>
|
||||
<info>phinx migrate -e development -k 5</info>
|
||||
<info>phinx migrate -e development -v</info>
|
||||
|
||||
EOT,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate the database.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @return int integer 0 on success, or an error code.
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$this->bootstrap($input, $output);
|
||||
|
||||
$version = $input->getOption('target') !== null ? (int)$input->getOption('target') : null;
|
||||
/** @var string|null $environment */
|
||||
$environment = $input->getOption('environment');
|
||||
$date = $input->getOption('date');
|
||||
$count = $input->getOption('count') !== null ? (int)$input->getOption('count') : null;
|
||||
$fake = (bool)$input->getOption('fake');
|
||||
|
||||
$success = $this->writeInformationOutput($environment, $output);
|
||||
if (!$success) {
|
||||
return self::CODE_ERROR;
|
||||
}
|
||||
|
||||
$versionOrder = $this->getConfig()->getVersionOrder();
|
||||
$output->writeln('<info>ordering by</info> ' . $versionOrder . ' time', $this->verbosityLevel);
|
||||
|
||||
if ($fake) {
|
||||
$output->writeln('<comment>warning</comment> performing fake migrations', $this->verbosityLevel);
|
||||
}
|
||||
|
||||
try {
|
||||
// run the migrations
|
||||
$start = microtime(true);
|
||||
if ($count !== null) {
|
||||
$this->getManager()->migrateToCount($environment, $count, $fake);
|
||||
} elseif ($date !== null) {
|
||||
$this->getManager()->migrateToDateTime($environment, new DateTime($date), $fake);
|
||||
} else {
|
||||
$this->getManager()->migrate($environment, $version, $fake);
|
||||
}
|
||||
$end = microtime(true);
|
||||
} catch (Throwable $e) {
|
||||
self::getErrorOutput($output)->writeln('<error>' . $e->__toString() . '</error>');
|
||||
|
||||
return self::CODE_ERROR;
|
||||
}
|
||||
|
||||
$output->writeln('', $this->verbosityLevel);
|
||||
$output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>', $this->verbosityLevel);
|
||||
|
||||
return self::CODE_SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Console\Command;
|
||||
|
||||
use DateTime;
|
||||
use InvalidArgumentException;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(name: 'rollback')]
|
||||
class Rollback extends AbstractCommand
|
||||
{
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
|
||||
protected static $defaultName = 'rollback';
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function configure(): void
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment');
|
||||
|
||||
$this->setDescription('Rollback the last or to a specific migration')
|
||||
->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to rollback to')
|
||||
->addOption('--date', '-d', InputOption::VALUE_REQUIRED, 'The date to rollback to')
|
||||
->addOption('--force', '-f', InputOption::VALUE_NONE, 'Force rollback to ignore breakpoints')
|
||||
->addOption('--dry-run', '-x', InputOption::VALUE_NONE, 'Dump query to standard output instead of executing it')
|
||||
->addOption('--fake', null, InputOption::VALUE_NONE, "Mark any rollbacks selected as run, but don't actually execute them")
|
||||
->setHelp(
|
||||
<<<EOT
|
||||
The <info>rollback</info> command reverts the last migration, or optionally up to a specific version
|
||||
|
||||
<info>phinx rollback -e development</info>
|
||||
<info>phinx rollback -e development -t 20111018185412</info>
|
||||
<info>phinx rollback -e development -d 20111018</info>
|
||||
<info>phinx rollback -e development -v</info>
|
||||
<info>phinx rollback -e development -t 20111018185412 -f</info>
|
||||
|
||||
If you have a breakpoint set, then you can rollback to target 0 and the rollbacks will stop at the breakpoint.
|
||||
<info>phinx rollback -e development -t 0 </info>
|
||||
|
||||
The <info>version_order</info> configuration option is used to determine the order of the migrations when rolling back.
|
||||
This can be used to allow the rolling back of the last executed migration instead of the last created one, or combined
|
||||
with the <info>-d|--date</info> option to rollback to a certain date using the migration start times to order them.
|
||||
|
||||
EOT,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rollback the migration.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @return int integer 0 on success, or an error code.
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$this->bootstrap($input, $output);
|
||||
|
||||
$environment = $input->getOption('environment');
|
||||
$version = $input->getOption('target');
|
||||
$date = $input->getOption('date');
|
||||
$force = (bool)$input->getOption('force');
|
||||
$fake = (bool)$input->getOption('fake');
|
||||
|
||||
$success = $this->writeInformationOutput($environment, $output);
|
||||
if (!$success) {
|
||||
return self::CODE_ERROR;
|
||||
}
|
||||
|
||||
$versionOrder = $this->getConfig()->getVersionOrder();
|
||||
$output->writeln('<info>ordering by</info> ' . $versionOrder . ' time', $this->verbosityLevel);
|
||||
|
||||
if ($fake) {
|
||||
$output->writeln('<comment>warning</comment> performing fake rollbacks', $this->verbosityLevel);
|
||||
}
|
||||
|
||||
// rollback the specified environment
|
||||
if ($date === null) {
|
||||
$targetMustMatchVersion = true;
|
||||
$target = $version;
|
||||
} else {
|
||||
$targetMustMatchVersion = false;
|
||||
$target = $this->getTargetFromDate($date);
|
||||
}
|
||||
|
||||
$start = microtime(true);
|
||||
$this->getManager()->rollback($environment, $target, $force, $targetMustMatchVersion, $fake);
|
||||
$end = microtime(true);
|
||||
|
||||
$output->writeln('', $this->verbosityLevel);
|
||||
$output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>', $this->verbosityLevel);
|
||||
|
||||
return self::CODE_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Target from Date
|
||||
*
|
||||
* @param string $date The date to convert to a target.
|
||||
* @throws \InvalidArgumentException
|
||||
* @return string The target
|
||||
*/
|
||||
public function getTargetFromDate(string $date): string
|
||||
{
|
||||
if (!preg_match('/^\d{4,14}$/', $date)) {
|
||||
throw new InvalidArgumentException('Invalid date. Format is YYYY[MM[DD[HH[II[SS]]]]].');
|
||||
}
|
||||
|
||||
// what we need to append to the date according to the possible date string lengths
|
||||
$dateStrlenToAppend = [
|
||||
14 => '',
|
||||
12 => '00',
|
||||
10 => '0000',
|
||||
8 => '000000',
|
||||
6 => '01000000',
|
||||
4 => '0101000000',
|
||||
];
|
||||
|
||||
if (!isset($dateStrlenToAppend[strlen($date)])) {
|
||||
throw new InvalidArgumentException('Invalid date. Format is YYYY[MM[DD[HH[II[SS]]]]].');
|
||||
}
|
||||
|
||||
$target = $date . $dateStrlenToAppend[strlen($date)];
|
||||
|
||||
$dateTime = DateTime::createFromFormat('YmdHis', $target);
|
||||
|
||||
if ($dateTime === false) {
|
||||
throw new InvalidArgumentException('Invalid date. Format is YYYY[MM[DD[HH[II[SS]]]]].');
|
||||
}
|
||||
|
||||
return $dateTime->format('YmdHis');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Console\Command;
|
||||
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use Phinx\Config\NamespaceAwareInterface;
|
||||
use Phinx\Util\Util;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
use Symfony\Component\Console\Question\ConfirmationQuestion;
|
||||
|
||||
#[AsCommand(name: 'seed:create')]
|
||||
class SeedCreate extends AbstractCommand
|
||||
{
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
|
||||
protected static $defaultName = 'seed:create';
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function configure(): void
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->setDescription('Create a new database seeder')
|
||||
->addArgument('name', InputArgument::REQUIRED, 'What is the name of the seeder?')
|
||||
->addOption('path', null, InputOption::VALUE_REQUIRED, 'Specify the path in which to create this seeder')
|
||||
->setHelp(sprintf(
|
||||
'%sCreates a new database seeder%s',
|
||||
PHP_EOL,
|
||||
PHP_EOL,
|
||||
));
|
||||
|
||||
// An alternative template.
|
||||
$this->addOption('template', 't', InputOption::VALUE_REQUIRED, 'Use an alternative template');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the confirmation question asking if the user wants to create the
|
||||
* seeds directory.
|
||||
*
|
||||
* @return \Symfony\Component\Console\Question\ConfirmationQuestion
|
||||
*/
|
||||
protected function getCreateSeedDirectoryQuestion(): ConfirmationQuestion
|
||||
{
|
||||
return new ConfirmationQuestion('Create seeds directory? [y]/n ', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the question that allows the user to select which seed path to use.
|
||||
*
|
||||
* @param string[] $paths Paths
|
||||
* @return \Symfony\Component\Console\Question\ChoiceQuestion
|
||||
*/
|
||||
protected function getSelectSeedPathQuestion(array $paths): ChoiceQuestion
|
||||
{
|
||||
return new ChoiceQuestion('Which seeds path would you like to use?', $paths, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the seed path to create the seeder in.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @throws \Exception
|
||||
* @return string
|
||||
*/
|
||||
protected function getSeedPath(InputInterface $input, OutputInterface $output): string
|
||||
{
|
||||
// First, try the non-interactive option:
|
||||
$path = $input->getOption('path');
|
||||
|
||||
if (!empty($path)) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
$paths = $this->getConfig()->getSeedPaths();
|
||||
|
||||
// No paths? That's a problem.
|
||||
if (empty($paths)) {
|
||||
throw new Exception('No seed paths set in your Phinx configuration file.');
|
||||
}
|
||||
|
||||
$paths = Util::globAll($paths);
|
||||
|
||||
if (empty($paths)) {
|
||||
throw new Exception(
|
||||
'You probably used curly braces to define seed path in your Phinx configuration file, ' .
|
||||
'but no directories have been matched using this pattern. ' .
|
||||
'You need to create a seed directory manually.',
|
||||
);
|
||||
}
|
||||
|
||||
// Only one path set, so select that:
|
||||
if (count($paths) === 1) {
|
||||
return array_shift($paths);
|
||||
}
|
||||
|
||||
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
|
||||
$helper = $this->getHelper('question');
|
||||
$question = $this->getSelectSeedPathQuestion($paths);
|
||||
|
||||
return $helper->ask($input, $output, $question);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the new seeder.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @throws \RuntimeException
|
||||
* @throws \InvalidArgumentException
|
||||
* @return int 0 on success
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$this->bootstrap($input, $output);
|
||||
|
||||
// get the seed path from the config
|
||||
$path = $this->getSeedPath($input, $output);
|
||||
|
||||
if (!file_exists($path)) {
|
||||
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
|
||||
$helper = $this->getHelper('question');
|
||||
$question = $this->getCreateSeedDirectoryQuestion();
|
||||
|
||||
if ($helper->ask($input, $output, $question)) {
|
||||
mkdir($path, 0755, true);
|
||||
}
|
||||
}
|
||||
|
||||
$this->verifySeedDirectory($path);
|
||||
|
||||
$path = realpath($path);
|
||||
/** @var string|null $className */
|
||||
$className = $input->getArgument('name');
|
||||
|
||||
if (!Util::isValidPhinxClassName($className)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The seed class name "%s" is invalid. Please use CamelCase format',
|
||||
$className,
|
||||
));
|
||||
}
|
||||
|
||||
// Compute the file path
|
||||
$filePath = $path . DIRECTORY_SEPARATOR . $className . '.php';
|
||||
|
||||
if (is_file($filePath)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The file "%s" already exists',
|
||||
basename($filePath),
|
||||
));
|
||||
}
|
||||
|
||||
// Get the alternative template option from the command line.
|
||||
$altTemplate = $input->getOption('template');
|
||||
|
||||
// Verify the alternative template file's existence.
|
||||
if ($altTemplate && !is_file($altTemplate)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The template file "%s" does not exist',
|
||||
$altTemplate,
|
||||
));
|
||||
}
|
||||
|
||||
// Command-line option must have higher priority than value from Config
|
||||
$config = $this->getConfig();
|
||||
if (is_null($altTemplate)) {
|
||||
$altTemplate = $config->getSeedTemplateFile();
|
||||
if (!is_null($altTemplate) && !is_file($altTemplate)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The template file `%s` from config does not exist',
|
||||
$altTemplate,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Determine the appropriate mechanism to get the template
|
||||
// Load the alternative template if it is defined.
|
||||
$contents = file_get_contents($altTemplate ?: $this->getSeedTemplateFilename());
|
||||
|
||||
$namespace = $config instanceof NamespaceAwareInterface ? $config->getSeedNamespaceByPath($path) : null;
|
||||
$classes = [
|
||||
'$namespaceDefinition' => $namespace !== null ? (PHP_EOL . 'namespace ' . $namespace . ';' . PHP_EOL) : '',
|
||||
'$namespace' => $namespace,
|
||||
'$useClassName' => $config->getSeedBaseClassName(false),
|
||||
'$className' => $className,
|
||||
'$baseClassName' => $config->getSeedBaseClassName(true),
|
||||
];
|
||||
$contents = strtr($contents, $classes);
|
||||
|
||||
if (file_put_contents($filePath, $contents) === false) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'The file "%s" could not be written to',
|
||||
$path,
|
||||
));
|
||||
}
|
||||
|
||||
$output->writeln('<info>using seed base class</info> ' . $classes['$useClassName'], $this->verbosityLevel);
|
||||
$output->writeln('<info>created</info> ' . Util::relativePath($filePath), $this->verbosityLevel);
|
||||
|
||||
return self::CODE_SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Console\Command;
|
||||
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(name: 'seed:run')]
|
||||
class SeedRun extends AbstractCommand
|
||||
{
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
|
||||
protected static $defaultName = 'seed:run';
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function configure(): void
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment');
|
||||
|
||||
$this->setDescription('Run database seeders')
|
||||
->addOption('--seed', '-s', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'What is the name of the seeder?')
|
||||
->addOption('--dry-run', '-x', InputOption::VALUE_NONE, 'Dump query to standard output instead of executing it')
|
||||
->setHelp(
|
||||
<<<EOT
|
||||
The <info>seed:run</info> command runs all available or individual seeders
|
||||
|
||||
<info>phinx seed:run -e development</info>
|
||||
<info>phinx seed:run -e development -s UserSeeder</info>
|
||||
<info>phinx seed:run -e development -s UserSeeder -s PermissionSeeder -s LogSeeder</info>
|
||||
<info>phinx seed:run -e development -v</info>
|
||||
|
||||
EOT,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run database seeders.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @return int integer 0 on success, or an error code.
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$this->bootstrap($input, $output);
|
||||
|
||||
/** @var array<string>|null $seedSet */
|
||||
$seedSet = $input->getOption('seed');
|
||||
/** @var string|null $environment */
|
||||
$environment = $input->getOption('environment');
|
||||
|
||||
$success = $this->writeInformationOutput($environment, $output);
|
||||
if (!$success) {
|
||||
return self::CODE_ERROR;
|
||||
}
|
||||
|
||||
$start = microtime(true);
|
||||
|
||||
if (empty($seedSet)) {
|
||||
// run all the seed(ers)
|
||||
$this->getManager()->seed($environment);
|
||||
} else {
|
||||
// run seed(ers) specified in a comma-separated list of classes
|
||||
foreach ($seedSet as $seed) {
|
||||
$this->getManager()->seed($environment, trim($seed));
|
||||
}
|
||||
}
|
||||
|
||||
$end = microtime(true);
|
||||
|
||||
$output->writeln('', $this->verbosityLevel);
|
||||
$output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>', $this->verbosityLevel);
|
||||
|
||||
return self::CODE_SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Console\Command;
|
||||
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(name: 'status')]
|
||||
class Status extends AbstractCommand
|
||||
{
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
|
||||
protected static $defaultName = 'status';
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function configure(): void
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment.');
|
||||
|
||||
$this->setDescription('Show migration status')
|
||||
->addOption('--format', '-f', InputOption::VALUE_REQUIRED, 'The output format: text or json. Defaults to text.')
|
||||
->setHelp(
|
||||
<<<EOT
|
||||
The <info>status</info> command prints a list of all migrations, along with their current status
|
||||
|
||||
<info>phinx status -e development</info>
|
||||
<info>phinx status -e development -f json</info>
|
||||
|
||||
The <info>version_order</info> configuration option is used to determine the order of the status migrations.
|
||||
EOT,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the migration status.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @return int 0 if all migrations are up, or an error code
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$this->bootstrap($input, $output);
|
||||
|
||||
/** @var string|null $environment */
|
||||
$environment = $input->getOption('environment');
|
||||
/** @var string|null $environment */
|
||||
$format = $input->getOption('format');
|
||||
|
||||
$success = $this->writeEnvironmentOutput($environment, $output);
|
||||
if (!$success) {
|
||||
return self::CODE_ERROR;
|
||||
}
|
||||
|
||||
if ($format !== null) {
|
||||
$output->writeln('<info>using format</info> ' . $format, $this->verbosityLevel);
|
||||
}
|
||||
|
||||
$output->writeln('<info>ordering by </info>' . $this->getConfig()->getVersionOrder() . ' time', $this->verbosityLevel);
|
||||
|
||||
// print the status
|
||||
$result = $this->getManager()->printStatus($environment, $format);
|
||||
|
||||
if ($result['hasMissingMigration']) {
|
||||
return self::CODE_STATUS_MISSING;
|
||||
} elseif ($result['hasDownMigration']) {
|
||||
return self::CODE_STATUS_DOWN;
|
||||
}
|
||||
|
||||
return self::CODE_SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Console\Command;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Phinx\Migration\Manager\Environment;
|
||||
use Phinx\Util\Util;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(name: 'test')]
|
||||
class Test extends AbstractCommand
|
||||
{
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
|
||||
protected static $defaultName = 'test';
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function configure(): void
|
||||
{
|
||||
parent::configure();
|
||||
|
||||
$this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment');
|
||||
|
||||
$this->setDescription('Verify the configuration file')
|
||||
->setHelp(
|
||||
<<<EOT
|
||||
The <info>test</info> command is used to verify the phinx configuration file and optionally an environment
|
||||
|
||||
<info>phinx test</info>
|
||||
<info>phinx test -e development</info>
|
||||
|
||||
If the environment option is set, it will test that phinx can connect to the DB associated with that environment
|
||||
EOT,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify configuration file
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @throws \InvalidArgumentException
|
||||
* @return int 0 on success
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
if (!$this->hasConfig()) {
|
||||
$this->loadConfig($input, $output);
|
||||
}
|
||||
|
||||
$this->loadManager($input, $output);
|
||||
|
||||
// Verify the migrations path(s)
|
||||
array_map(
|
||||
[$this, 'verifyMigrationDirectory'],
|
||||
Util::globAll($this->getConfig()->getMigrationPaths()),
|
||||
);
|
||||
|
||||
// Verify the seed path(s)
|
||||
array_map(
|
||||
[$this, 'verifySeedDirectory'],
|
||||
Util::globAll($this->getConfig()->getSeedPaths()),
|
||||
);
|
||||
|
||||
$envName = $input->getOption('environment');
|
||||
if ($envName) {
|
||||
if (!$this->getConfig()->hasEnvironment($envName)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The environment "%s" does not exist',
|
||||
$envName,
|
||||
));
|
||||
}
|
||||
|
||||
$output->writeln(sprintf('<info>validating environment</info> %s', $envName), $this->verbosityLevel);
|
||||
$environment = new Environment(
|
||||
$envName,
|
||||
$this->getConfig()->getEnvironment($envName),
|
||||
);
|
||||
// validate environment connection
|
||||
$environment->getAdapter()->connect();
|
||||
}
|
||||
|
||||
$output->writeln('<info>success!</info>', $this->verbosityLevel);
|
||||
|
||||
return self::CODE_SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Console;
|
||||
|
||||
use Composer\InstalledVersions;
|
||||
use Phinx\Console\Command\Breakpoint;
|
||||
use Phinx\Console\Command\Create;
|
||||
use Phinx\Console\Command\Init;
|
||||
use Phinx\Console\Command\ListAliases;
|
||||
use Phinx\Console\Command\Migrate;
|
||||
use Phinx\Console\Command\Rollback;
|
||||
use Phinx\Console\Command\SeedCreate;
|
||||
use Phinx\Console\Command\SeedRun;
|
||||
use Phinx\Console\Command\Status;
|
||||
use Phinx\Console\Command\Test;
|
||||
use Symfony\Component\Console\Application;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Phinx console application.
|
||||
*/
|
||||
class PhinxApplication extends Application
|
||||
{
|
||||
/**
|
||||
* @var string The current application version as determined by the getVersion() function.
|
||||
*/
|
||||
private string $version;
|
||||
|
||||
/**
|
||||
* Initialize the Phinx console application.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('Phinx by CakePHP - https://phinx.org.', $this->getVersion());
|
||||
|
||||
$this->addCommands([
|
||||
new Init(),
|
||||
new Create(),
|
||||
new Migrate(),
|
||||
new Rollback(),
|
||||
new Status(),
|
||||
new Breakpoint(),
|
||||
new Test(),
|
||||
new SeedCreate(),
|
||||
new SeedRun(),
|
||||
new ListAliases(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the current application.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input An Input instance
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output An Output instance
|
||||
* @return int 0 if everything went fine, or an error code
|
||||
*/
|
||||
public function doRun(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
// always show the version information except when the user invokes the help
|
||||
// command as that already does it
|
||||
if ($input->hasParameterOption('--no-info') === false) {
|
||||
if (($input->hasParameterOption(['--help', '-h']) !== false) || ($input->getFirstArgument() !== null && $input->getFirstArgument() !== 'list')) {
|
||||
$output->writeln($this->getLongVersion());
|
||||
$output->writeln('');
|
||||
}
|
||||
}
|
||||
|
||||
return parent::doRun($input, $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current application version.
|
||||
*
|
||||
* @return string The application version if it could be found, otherwise 'UNKNOWN'
|
||||
*/
|
||||
public function getVersion(): string
|
||||
{
|
||||
if (isset($this->version)) {
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
// humbug/box will replace this with actual version when building
|
||||
// so use that if available
|
||||
$gitTag = '@git_tag@';
|
||||
if (!str_starts_with($gitTag, '@')) {
|
||||
return $this->version = $gitTag;
|
||||
}
|
||||
|
||||
// Otherwise fallback to the version as reported by composer
|
||||
if (class_exists(InstalledVersions::class)) {
|
||||
return $this->version = InstalledVersions::getPrettyVersion('robmorgan/phinx') ?? 'UNKNOWN';
|
||||
}
|
||||
|
||||
return $this->version = 'UNKNOWN';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Action;
|
||||
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
abstract class Action
|
||||
{
|
||||
/**
|
||||
* @var \Phinx\Db\Table\Table
|
||||
*/
|
||||
protected Table $table;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table the Table to apply the action to
|
||||
*/
|
||||
public function __construct(Table $table)
|
||||
{
|
||||
$this->table = $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* The table this action will be applied to
|
||||
*
|
||||
* @return \Phinx\Db\Table\Table
|
||||
*/
|
||||
public function getTable(): Table
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Action;
|
||||
|
||||
use Phinx\Db\Table\Column;
|
||||
use Phinx\Db\Table\Table;
|
||||
use Phinx\Util\Literal;
|
||||
|
||||
class AddColumn extends Action
|
||||
{
|
||||
/**
|
||||
* The column to add
|
||||
*
|
||||
* @var \Phinx\Db\Table\Column
|
||||
*/
|
||||
protected Column $column;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to add the column to
|
||||
* @param \Phinx\Db\Table\Column $column The column to add
|
||||
*/
|
||||
public function __construct(Table $table, Column $column)
|
||||
{
|
||||
parent::__construct($table);
|
||||
$this->column = $column;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new AddColumn object after assembling the given commands
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to add the column to
|
||||
* @param string $columnName The column name
|
||||
* @param string|\Phinx\Util\Literal $type The column type
|
||||
* @param array<string, mixed> $options The column options
|
||||
* @return static
|
||||
*/
|
||||
public static function build(Table $table, string $columnName, string|Literal $type, array $options = []): static
|
||||
{
|
||||
$column = new Column();
|
||||
$column->setName($columnName);
|
||||
$column->setType($type);
|
||||
$column->setOptions($options); // map options to column methods
|
||||
|
||||
return new static($table, $column);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the column to be added
|
||||
*
|
||||
* @return \Phinx\Db\Table\Column
|
||||
*/
|
||||
public function getColumn(): Column
|
||||
{
|
||||
return $this->column;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Action;
|
||||
|
||||
use Phinx\Db\Table\ForeignKey;
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
class AddForeignKey extends Action
|
||||
{
|
||||
/**
|
||||
* The foreign key to add
|
||||
*
|
||||
* @var \Phinx\Db\Table\ForeignKey
|
||||
*/
|
||||
protected ForeignKey $foreignKey;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to add the foreign key to
|
||||
* @param \Phinx\Db\Table\ForeignKey $fk The foreign key to add
|
||||
*/
|
||||
public function __construct(Table $table, ForeignKey $fk)
|
||||
{
|
||||
parent::__construct($table);
|
||||
$this->foreignKey = $fk;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new AddForeignKey object after building the foreign key with
|
||||
* the passed attributes
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table object to add the foreign key to
|
||||
* @param string|string[] $columns The columns for the foreign key
|
||||
* @param \Phinx\Db\Table\Table|string $referencedTable The table the foreign key references
|
||||
* @param string|string[] $referencedColumns The columns in the referenced table
|
||||
* @param array<string, mixed> $options Extra options for the foreign key
|
||||
* @param string|null $name The name of the foreign key
|
||||
* @return static
|
||||
*/
|
||||
public static function build(Table $table, string|array $columns, Table|string $referencedTable, string|array $referencedColumns = ['id'], array $options = [], ?string $name = null): static
|
||||
{
|
||||
if (is_string($referencedColumns)) {
|
||||
$referencedColumns = [$referencedColumns]; // str to array
|
||||
}
|
||||
|
||||
if (is_string($referencedTable)) {
|
||||
$referencedTable = new Table($referencedTable);
|
||||
}
|
||||
|
||||
$fk = new ForeignKey();
|
||||
$fk->setReferencedTable($referencedTable)
|
||||
->setColumns($columns)
|
||||
->setReferencedColumns($referencedColumns)
|
||||
->setOptions($options);
|
||||
|
||||
if ($name !== null) {
|
||||
$fk->setConstraint($name);
|
||||
}
|
||||
|
||||
return new static($table, $fk);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the foreign key to be added
|
||||
*
|
||||
* @return \Phinx\Db\Table\ForeignKey
|
||||
*/
|
||||
public function getForeignKey(): ForeignKey
|
||||
{
|
||||
return $this->foreignKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Action;
|
||||
|
||||
use Phinx\Db\Table\Index;
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
class AddIndex extends Action
|
||||
{
|
||||
/**
|
||||
* The index to add to the table
|
||||
*
|
||||
* @var \Phinx\Db\Table\Index
|
||||
*/
|
||||
protected Index $index;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to add the index to
|
||||
* @param \Phinx\Db\Table\Index $index The index to be added
|
||||
*/
|
||||
public function __construct(Table $table, Index $index)
|
||||
{
|
||||
parent::__construct($table);
|
||||
$this->index = $index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new AddIndex object after building the index object with the
|
||||
* provided arguments
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to add the index to
|
||||
* @param string|string[]|\Phinx\Db\Table\Index $columns The columns to index
|
||||
* @param array<string, mixed> $options Additional options for the index creation
|
||||
* @return static
|
||||
*/
|
||||
public static function build(Table $table, string|array|Index $columns, array $options = []): static
|
||||
{
|
||||
// create a new index object if strings or an array of strings were supplied
|
||||
$index = $columns;
|
||||
|
||||
if (!$columns instanceof Index) {
|
||||
$index = new Index();
|
||||
|
||||
$index->setColumns($columns);
|
||||
$index->setOptions($options);
|
||||
}
|
||||
|
||||
return new static($table, $index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index to be added
|
||||
*
|
||||
* @return \Phinx\Db\Table\Index
|
||||
*/
|
||||
public function getIndex(): Index
|
||||
{
|
||||
return $this->index;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Action;
|
||||
|
||||
use Phinx\Db\Table\Column;
|
||||
use Phinx\Db\Table\Table;
|
||||
use Phinx\Util\Literal;
|
||||
|
||||
class ChangeColumn extends Action
|
||||
{
|
||||
/**
|
||||
* The column definition
|
||||
*
|
||||
* @var \Phinx\Db\Table\Column
|
||||
*/
|
||||
protected Column $column;
|
||||
|
||||
/**
|
||||
* The name of the column to be changed
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected string $columnName;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to alter
|
||||
* @param string $columnName The name of the column to change
|
||||
* @param \Phinx\Db\Table\Column $column The column definition
|
||||
*/
|
||||
public function __construct(Table $table, string $columnName, Column $column)
|
||||
{
|
||||
parent::__construct($table);
|
||||
$this->columnName = $columnName;
|
||||
$this->column = $column;
|
||||
|
||||
// if the name was omitted use the existing column name
|
||||
if ($column->getName() === null || strlen($column->getName()) === 0) {
|
||||
$column->setName($columnName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new ChangeColumn object after building the column definition
|
||||
* out of the provided arguments
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to alter
|
||||
* @param string $columnName The name of the column to change
|
||||
* @param string|\Phinx\Util\Literal $type The type of the column
|
||||
* @param array<string, mixed> $options Additional options for the column
|
||||
* @return static
|
||||
*/
|
||||
public static function build(Table $table, string $columnName, string|Literal $type, array $options = []): static
|
||||
{
|
||||
$column = new Column();
|
||||
$column->setName($columnName);
|
||||
$column->setType($type);
|
||||
$column->setOptions($options); // map options to column methods
|
||||
|
||||
return new static($table, $columnName, $column);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the column to change
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getColumnName(): string
|
||||
{
|
||||
return $this->columnName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the column definition
|
||||
*
|
||||
* @return \Phinx\Db\Table\Column
|
||||
*/
|
||||
public function getColumn(): Column
|
||||
{
|
||||
return $this->column;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Action;
|
||||
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
class ChangeComment extends Action
|
||||
{
|
||||
/**
|
||||
* The new comment for the table
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected ?string $newComment = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to be changed
|
||||
* @param string|null $newComment The new comment for the table
|
||||
*/
|
||||
public function __construct(Table $table, ?string $newComment)
|
||||
{
|
||||
parent::__construct($table);
|
||||
$this->newComment = $newComment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the new comment for the table
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getNewComment(): ?string
|
||||
{
|
||||
return $this->newComment;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Action;
|
||||
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
class ChangePrimaryKey extends Action
|
||||
{
|
||||
/**
|
||||
* The new columns for the primary key
|
||||
*
|
||||
* @var string|string[]|null
|
||||
*/
|
||||
protected string|array|null $newColumns = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to be changed
|
||||
* @param string|string[]|null $newColumns The new columns for the primary key
|
||||
*/
|
||||
public function __construct(Table $table, string|array|null $newColumns)
|
||||
{
|
||||
parent::__construct($table);
|
||||
$this->newColumns = $newColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the new columns for the primary key
|
||||
*
|
||||
* @return string|string[]|null
|
||||
*/
|
||||
public function getNewColumns(): string|array|null
|
||||
{
|
||||
return $this->newColumns;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Action;
|
||||
|
||||
class CreateTable extends Action
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Action;
|
||||
|
||||
use Phinx\Db\Table\ForeignKey;
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
class DropForeignKey extends Action
|
||||
{
|
||||
/**
|
||||
* The foreign key to remove
|
||||
*
|
||||
* @var \Phinx\Db\Table\ForeignKey
|
||||
*/
|
||||
protected ForeignKey $foreignKey;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to remove the constraint from
|
||||
* @param \Phinx\Db\Table\ForeignKey $foreignKey The foreign key to remove
|
||||
*/
|
||||
public function __construct(Table $table, ForeignKey $foreignKey)
|
||||
{
|
||||
parent::__construct($table);
|
||||
$this->foreignKey = $foreignKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new DropForeignKey object after building the ForeignKey
|
||||
* definition out of the passed arguments.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to delete the foreign key from
|
||||
* @param string|string[] $columns The columns participating in the foreign key
|
||||
* @param string|null $constraint The constraint name
|
||||
* @return static
|
||||
*/
|
||||
public static function build(Table $table, string|array $columns, ?string $constraint = null): static
|
||||
{
|
||||
if (is_string($columns)) {
|
||||
$columns = [$columns];
|
||||
}
|
||||
|
||||
$foreignKey = new ForeignKey();
|
||||
$foreignKey->setColumns($columns);
|
||||
|
||||
if ($constraint) {
|
||||
$foreignKey->setConstraint($constraint);
|
||||
}
|
||||
|
||||
return new static($table, $foreignKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the foreign key to remove
|
||||
*
|
||||
* @return \Phinx\Db\Table\ForeignKey
|
||||
*/
|
||||
public function getForeignKey(): ForeignKey
|
||||
{
|
||||
return $this->foreignKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Action;
|
||||
|
||||
use Phinx\Db\Table\Index;
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
class DropIndex extends Action
|
||||
{
|
||||
/**
|
||||
* The index to drop
|
||||
*
|
||||
* @var \Phinx\Db\Table\Index
|
||||
*/
|
||||
protected Index $index;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table owning the index
|
||||
* @param \Phinx\Db\Table\Index $index The index to be dropped
|
||||
*/
|
||||
public function __construct(Table $table, Index $index)
|
||||
{
|
||||
parent::__construct($table);
|
||||
$this->index = $index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new DropIndex object after assembling the passed
|
||||
* arguments.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table where the index is
|
||||
* @param string[] $columns the indexed columns
|
||||
* @return static
|
||||
*/
|
||||
public static function build(Table $table, array $columns = []): static
|
||||
{
|
||||
$index = new Index();
|
||||
$index->setColumns($columns);
|
||||
|
||||
return new static($table, $index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new DropIndex when the name of the index to drop
|
||||
* is known.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table where the index is
|
||||
* @param string $name The name of the index
|
||||
* @return static
|
||||
*/
|
||||
public static function buildFromName(Table $table, string $name): static
|
||||
{
|
||||
$index = new Index();
|
||||
$index->setName($name);
|
||||
|
||||
return new static($table, $index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index to be dropped
|
||||
*
|
||||
* @return \Phinx\Db\Table\Index
|
||||
*/
|
||||
public function getIndex(): Index
|
||||
{
|
||||
return $this->index;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Action;
|
||||
|
||||
class DropTable extends Action
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Action;
|
||||
|
||||
use Phinx\Db\Table\Column;
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
class RemoveColumn extends Action
|
||||
{
|
||||
/**
|
||||
* The column to be removed
|
||||
*
|
||||
* @var \Phinx\Db\Table\Column
|
||||
*/
|
||||
protected Column $column;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table where the column is
|
||||
* @param \Phinx\Db\Table\Column $column The column to be removed
|
||||
*/
|
||||
public function __construct(Table $table, Column $column)
|
||||
{
|
||||
parent::__construct($table);
|
||||
$this->column = $column;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new RemoveColumn object after assembling the
|
||||
* passed arguments.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table where the column is
|
||||
* @param string $columnName The name of the column to drop
|
||||
* @return static
|
||||
*/
|
||||
public static function build(Table $table, string $columnName): static
|
||||
{
|
||||
$column = new Column();
|
||||
$column->setName($columnName);
|
||||
|
||||
return new static($table, $column);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the column to be dropped
|
||||
*
|
||||
* @return \Phinx\Db\Table\Column
|
||||
*/
|
||||
public function getColumn(): Column
|
||||
{
|
||||
return $this->column;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Action;
|
||||
|
||||
use Phinx\Db\Table\Column;
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
class RenameColumn extends Action
|
||||
{
|
||||
/**
|
||||
* The column to be renamed
|
||||
*
|
||||
* @var \Phinx\Db\Table\Column
|
||||
*/
|
||||
protected Column $column;
|
||||
|
||||
/**
|
||||
* The new name for the column
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected string $newName;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table where the column is
|
||||
* @param \Phinx\Db\Table\Column $column The column to be renamed
|
||||
* @param string $newName The new name for the column
|
||||
*/
|
||||
public function __construct(Table $table, Column $column, string $newName)
|
||||
{
|
||||
parent::__construct($table);
|
||||
$this->newName = $newName;
|
||||
$this->column = $column;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new RenameColumn object after building the passed
|
||||
* arguments
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table where the column is
|
||||
* @param string $columnName The name of the column to be changed
|
||||
* @param string $newName The new name for the column
|
||||
* @return static
|
||||
*/
|
||||
public static function build(Table $table, string $columnName, string $newName): static
|
||||
{
|
||||
$column = new Column();
|
||||
$column->setName($columnName);
|
||||
|
||||
return new static($table, $column, $newName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the column to be changed
|
||||
*
|
||||
* @return \Phinx\Db\Table\Column
|
||||
*/
|
||||
public function getColumn(): Column
|
||||
{
|
||||
return $this->column;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the new name for the column
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getNewName(): string
|
||||
{
|
||||
return $this->newName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Action;
|
||||
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
class RenameTable extends Action
|
||||
{
|
||||
/**
|
||||
* The new name for the table
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected string $newName;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to be renamed
|
||||
* @param string $newName The new name for the table
|
||||
*/
|
||||
public function __construct(Table $table, string $newName)
|
||||
{
|
||||
parent::__construct($table);
|
||||
$this->newName = $newName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the new name for the table
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getNewName(): string
|
||||
{
|
||||
return $this->newName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,413 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Adapter;
|
||||
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use Phinx\Db\Table;
|
||||
use Phinx\Db\Table\Column;
|
||||
use Phinx\Util\Literal;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\NullOutput;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Base Abstract Database Adapter.
|
||||
*/
|
||||
abstract class AbstractAdapter implements AdapterInterface
|
||||
{
|
||||
/**
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
protected array $options = [];
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\Console\Input\InputInterface|null
|
||||
*/
|
||||
protected ?InputInterface $input = null;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\Console\Output\OutputInterface
|
||||
*/
|
||||
protected OutputInterface $output;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $createdTables = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $schemaTableName = 'phinxlog';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $dataDomain = [];
|
||||
|
||||
/**
|
||||
* Class Constructor.
|
||||
*
|
||||
* @param array<string, mixed> $options Options
|
||||
* @param \Symfony\Component\Console\Input\InputInterface|null $input Input Interface
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface|null $output Output Interface
|
||||
*/
|
||||
public function __construct(array $options, ?InputInterface $input = null, ?OutputInterface $output = null)
|
||||
{
|
||||
$this->setOptions($options);
|
||||
if ($input !== null) {
|
||||
$this->setInput($input);
|
||||
}
|
||||
if ($output !== null) {
|
||||
$this->setOutput($output);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setOptions(array $options): AdapterInterface
|
||||
{
|
||||
$this->options = $options;
|
||||
|
||||
if (isset($options['default_migration_table'])) {
|
||||
trigger_error('The default_migration_table setting for adapter has been deprecated since 0.13.0. Use `migration_table` instead.', E_USER_DEPRECATED);
|
||||
if (!isset($options['migration_table'])) {
|
||||
$options['migration_table'] = $options['default_migration_table'];
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($options['migration_table'])) {
|
||||
$this->setSchemaTableName($options['migration_table']);
|
||||
}
|
||||
|
||||
if (isset($options['data_domain'])) {
|
||||
$this->setDataDomain($options['data_domain']);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOptions(): array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasOption(string $name): bool
|
||||
{
|
||||
return isset($this->options[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOption(string $name): mixed
|
||||
{
|
||||
if (!$this->hasOption($name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->options[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setInput(InputInterface $input): AdapterInterface
|
||||
{
|
||||
$this->input = $input;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getInput(): ?InputInterface
|
||||
{
|
||||
return $this->input;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setOutput(OutputInterface $output): AdapterInterface
|
||||
{
|
||||
$this->output = $output;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOutput(): OutputInterface
|
||||
{
|
||||
if (!isset($this->output)) {
|
||||
$output = new NullOutput();
|
||||
$this->setOutput($output);
|
||||
}
|
||||
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @return array<int>
|
||||
*/
|
||||
public function getVersions(): array
|
||||
{
|
||||
$rows = $this->getVersionLog();
|
||||
|
||||
return array_keys($rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the schema table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSchemaTableName(): string
|
||||
{
|
||||
return $this->schemaTableName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the schema table name.
|
||||
*
|
||||
* @param string $schemaTableName Schema Table Name
|
||||
* @return $this
|
||||
*/
|
||||
public function setSchemaTableName(string $schemaTableName)
|
||||
{
|
||||
$this->schemaTableName = $schemaTableName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the data domain.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDataDomain(): array
|
||||
{
|
||||
return $this->dataDomain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the data domain.
|
||||
*
|
||||
* @param array $dataDomain Array for the data domain
|
||||
* @return $this
|
||||
*/
|
||||
public function setDataDomain(array $dataDomain)
|
||||
{
|
||||
$this->dataDomain = [];
|
||||
|
||||
// Iterate over data domain field definitions and perform initial and
|
||||
// simple normalization. We make sure the definition as a base 'type'
|
||||
// and it is compatible with the base Phinx types.
|
||||
foreach ($dataDomain as $type => $options) {
|
||||
if (!isset($options['type'])) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'You must specify a type for data domain type "%s".',
|
||||
$type,
|
||||
));
|
||||
}
|
||||
|
||||
// Replace type if it's the name of a Phinx constant
|
||||
if (defined('static::' . $options['type'])) {
|
||||
$options['type'] = constant('static::' . $options['type']);
|
||||
}
|
||||
|
||||
if (!in_array($options['type'], $this->getColumnTypes(), true)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'An invalid column type "%s" was specified for data domain type "%s".',
|
||||
$options['type'],
|
||||
$type,
|
||||
));
|
||||
}
|
||||
|
||||
$internal_type = $options['type'];
|
||||
unset($options['type']);
|
||||
|
||||
// Do a simple replacement for the 'length' / 'limit' option and
|
||||
// detect hinting values for 'limit'.
|
||||
if (isset($options['length'])) {
|
||||
$options['limit'] = $options['length'];
|
||||
unset($options['length']);
|
||||
}
|
||||
|
||||
if (isset($options['limit']) && !is_numeric($options['limit'])) {
|
||||
if (!defined('static::' . $options['limit'])) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'An invalid limit value "%s" was specified for data domain type "%s".',
|
||||
$options['limit'],
|
||||
$type,
|
||||
));
|
||||
}
|
||||
|
||||
$options['limit'] = constant('static::' . $options['limit']);
|
||||
}
|
||||
|
||||
// Save the data domain types in a more suitable format
|
||||
$this->dataDomain[$type] = [
|
||||
'type' => $internal_type,
|
||||
'options' => $options,
|
||||
];
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getColumnForType(string $columnName, string $type, array $options): Column
|
||||
{
|
||||
$column = new Column();
|
||||
$column->setName($columnName);
|
||||
|
||||
if (array_key_exists($type, $this->getDataDomain())) {
|
||||
$column->setType($this->dataDomain[$type]['type']);
|
||||
$column->setOptions($this->dataDomain[$type]['options']);
|
||||
} else {
|
||||
$column->setType($type);
|
||||
}
|
||||
|
||||
$column->setOptions($options);
|
||||
|
||||
return $column;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws \InvalidArgumentException
|
||||
* @return void
|
||||
*/
|
||||
public function createSchemaTable(): void
|
||||
{
|
||||
try {
|
||||
$options = [
|
||||
'id' => false,
|
||||
'primary_key' => 'version',
|
||||
];
|
||||
|
||||
$table = new Table($this->getSchemaTableName(), $options, $this);
|
||||
$table->addColumn('version', 'biginteger', ['null' => false])
|
||||
->addColumn('migration_name', 'string', ['limit' => 100, 'default' => null, 'null' => true])
|
||||
->addColumn('start_time', 'timestamp', ['default' => null, 'null' => true])
|
||||
->addColumn('end_time', 'timestamp', ['default' => null, 'null' => true])
|
||||
->addColumn('breakpoint', 'boolean', ['default' => false, 'null' => false])
|
||||
->save();
|
||||
} catch (Exception $exception) {
|
||||
throw new InvalidArgumentException(
|
||||
'There was a problem creating the schema table: ' . $exception->getMessage(),
|
||||
(int)$exception->getCode(),
|
||||
$exception,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getAdapterType(): string
|
||||
{
|
||||
return $this->getOption('adapter');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isValidColumnType(Column $column): bool
|
||||
{
|
||||
return $column->getType() instanceof Literal || in_array($column->getType(), $this->getColumnTypes(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if instead of executing queries a dump to standard output is needed
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isDryRunEnabled(): bool
|
||||
{
|
||||
/** @var \Symfony\Component\Console\Input\InputInterface|null $input */
|
||||
$input = $this->getInput();
|
||||
|
||||
return $input && $input->hasOption('dry-run') ? (bool)$input->getOption('dry-run') : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds user-created tables (e.g. not phinxlog) to a cached list
|
||||
*
|
||||
* @param string $tableName The name of the table
|
||||
* @return void
|
||||
*/
|
||||
protected function addCreatedTable(string $tableName): void
|
||||
{
|
||||
$tableName = $this->quoteTableName($tableName);
|
||||
if (substr_compare($tableName, 'phinxlog', -strlen('phinxlog')) !== 0) {
|
||||
$this->createdTables[] = $tableName;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the name of the cached table
|
||||
*
|
||||
* @param string $tableName Original name of the table
|
||||
* @param string $newTableName New name of the table
|
||||
* @return void
|
||||
*/
|
||||
protected function updateCreatedTableName(string $tableName, string $newTableName): void
|
||||
{
|
||||
$tableName = $this->quoteTableName($tableName);
|
||||
$newTableName = $this->quoteTableName($newTableName);
|
||||
$key = array_search($tableName, $this->createdTables, true);
|
||||
if ($key !== false) {
|
||||
$this->createdTables[$key] = $newTableName;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes table from the cached created list
|
||||
*
|
||||
* @param string $tableName The name of the table
|
||||
* @return void
|
||||
*/
|
||||
protected function removeCreatedTable(string $tableName): void
|
||||
{
|
||||
$tableName = $this->quoteTableName($tableName);
|
||||
$key = array_search($tableName, $this->createdTables, true);
|
||||
if ($key !== false) {
|
||||
unset($this->createdTables[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the table is in the cached list of created tables
|
||||
*
|
||||
* @param string $tableName The name of the table
|
||||
* @return bool
|
||||
*/
|
||||
protected function hasCreatedTable(string $tableName): bool
|
||||
{
|
||||
$tableName = $this->quoteTableName($tableName);
|
||||
|
||||
return in_array($tableName, $this->createdTables, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Adapter;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Adapter factory and registry.
|
||||
*
|
||||
* Used for registering adapters and creating instances of adapters.
|
||||
*/
|
||||
class AdapterFactory
|
||||
{
|
||||
/**
|
||||
* @var static|null
|
||||
*/
|
||||
protected static ?AdapterFactory $instance = null;
|
||||
|
||||
/**
|
||||
* Get the factory singleton instance.
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function instance(): static
|
||||
{
|
||||
if (!static::$instance) {
|
||||
static::$instance = new static();
|
||||
}
|
||||
|
||||
return static::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class map of database adapters, indexed by PDO::ATTR_DRIVER_NAME.
|
||||
*
|
||||
* @var array<string, \Phinx\Db\Adapter\AdapterInterface|string>
|
||||
* @phpstan-var array<string, \Phinx\Db\Adapter\AdapterInterface|class-string<\Phinx\Db\Adapter\AdapterInterface>>
|
||||
*/
|
||||
protected array $adapters = [
|
||||
'mysql' => 'Phinx\Db\Adapter\MysqlAdapter',
|
||||
'pgsql' => 'Phinx\Db\Adapter\PostgresAdapter',
|
||||
'sqlite' => 'Phinx\Db\Adapter\SQLiteAdapter',
|
||||
'sqlsrv' => 'Phinx\Db\Adapter\SqlServerAdapter',
|
||||
];
|
||||
|
||||
/**
|
||||
* Class map of adapters wrappers, indexed by name.
|
||||
*
|
||||
* @var array<string, \Phinx\Db\Adapter\WrapperInterface|string>
|
||||
*/
|
||||
protected array $wrappers = [
|
||||
'prefix' => 'Phinx\Db\Adapter\TablePrefixAdapter',
|
||||
'proxy' => 'Phinx\Db\Adapter\ProxyAdapter',
|
||||
'timed' => 'Phinx\Db\Adapter\TimedOutputAdapter',
|
||||
];
|
||||
|
||||
/**
|
||||
* Register an adapter class with a given name.
|
||||
*
|
||||
* @param string $name Name
|
||||
* @param object|string $class Class
|
||||
* @throws \RuntimeException
|
||||
* @return $this
|
||||
*/
|
||||
public function registerAdapter(string $name, object|string $class)
|
||||
{
|
||||
if (!is_subclass_of($class, 'Phinx\Db\Adapter\AdapterInterface')) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'Adapter class "%s" must implement Phinx\\Db\\Adapter\\AdapterInterface',
|
||||
is_string($class) ? $class : get_class($class),
|
||||
));
|
||||
}
|
||||
$this->adapters[$name] = $class;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an adapter class by name.
|
||||
*
|
||||
* @param string $name Name
|
||||
* @throws \RuntimeException
|
||||
* @return object|string
|
||||
* @phpstan-return object|class-string<\Phinx\Db\Adapter\AdapterInterface>
|
||||
*/
|
||||
protected function getClass(string $name): object|string
|
||||
{
|
||||
if (empty($this->adapters[$name])) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'Adapter "%s" has not been registered',
|
||||
$name,
|
||||
));
|
||||
}
|
||||
|
||||
return $this->adapters[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an adapter instance by name.
|
||||
*
|
||||
* @param string $name Name
|
||||
* @param array<string, mixed> $options Options
|
||||
* @return \Phinx\Db\Adapter\AdapterInterface
|
||||
*/
|
||||
public function getAdapter(string $name, array $options): AdapterInterface
|
||||
{
|
||||
$class = $this->getClass($name);
|
||||
|
||||
return new $class($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or replace a wrapper with a fully qualified class name.
|
||||
*
|
||||
* @param string $name Name
|
||||
* @param object|string $class Class
|
||||
* @throws \RuntimeException
|
||||
* @return $this
|
||||
*/
|
||||
public function registerWrapper(string $name, object|string $class)
|
||||
{
|
||||
if (!is_subclass_of($class, 'Phinx\Db\Adapter\WrapperInterface')) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'Wrapper class "%s" must be implement Phinx\\Db\\Adapter\\WrapperInterface',
|
||||
is_string($class) ? $class : get_class($class),
|
||||
));
|
||||
}
|
||||
$this->wrappers[$name] = $class;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a wrapper class by name.
|
||||
*
|
||||
* @param string $name Name
|
||||
* @throws \RuntimeException
|
||||
* @return \Phinx\Db\Adapter\WrapperInterface|string
|
||||
*/
|
||||
protected function getWrapperClass(string $name): WrapperInterface|string
|
||||
{
|
||||
if (empty($this->wrappers[$name])) {
|
||||
throw new RuntimeException(sprintf(
|
||||
'Wrapper "%s" has not been registered',
|
||||
$name,
|
||||
));
|
||||
}
|
||||
|
||||
return $this->wrappers[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a wrapper instance by name.
|
||||
*
|
||||
* @param string $name Name
|
||||
* @param \Phinx\Db\Adapter\AdapterInterface $adapter Adapter
|
||||
* @return \Phinx\Db\Adapter\AdapterWrapper
|
||||
*/
|
||||
public function getWrapper(string $name, AdapterInterface $adapter): AdapterWrapper
|
||||
{
|
||||
$class = $this->getWrapperClass($name);
|
||||
|
||||
return new $class($adapter);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,538 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Adapter;
|
||||
|
||||
use Cake\Database\Query;
|
||||
use Cake\Database\Query\DeleteQuery;
|
||||
use Cake\Database\Query\InsertQuery;
|
||||
use Cake\Database\Query\SelectQuery;
|
||||
use Cake\Database\Query\UpdateQuery;
|
||||
use Phinx\Db\Table\Column;
|
||||
use Phinx\Db\Table\Table;
|
||||
use Phinx\Migration\MigrationInterface;
|
||||
use Phinx\Util\Literal;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Adapter Interface.
|
||||
*
|
||||
* @method \PDO getConnection()
|
||||
*/
|
||||
interface AdapterInterface
|
||||
{
|
||||
public const PHINX_TYPE_STRING = 'string';
|
||||
public const PHINX_TYPE_CHAR = 'char';
|
||||
public const PHINX_TYPE_TEXT = 'text';
|
||||
public const PHINX_TYPE_INTEGER = 'integer';
|
||||
public const PHINX_TYPE_TINY_INTEGER = 'tinyinteger';
|
||||
public const PHINX_TYPE_SMALL_INTEGER = 'smallinteger';
|
||||
public const PHINX_TYPE_BIG_INTEGER = 'biginteger';
|
||||
public const PHINX_TYPE_BIT = 'bit';
|
||||
public const PHINX_TYPE_FLOAT = 'float';
|
||||
public const PHINX_TYPE_DECIMAL = 'decimal';
|
||||
public const PHINX_TYPE_DOUBLE = 'double';
|
||||
public const PHINX_TYPE_DATETIME = 'datetime';
|
||||
public const PHINX_TYPE_TIMESTAMP = 'timestamp';
|
||||
public const PHINX_TYPE_TIME = 'time';
|
||||
public const PHINX_TYPE_DATE = 'date';
|
||||
public const PHINX_TYPE_BINARY = 'binary';
|
||||
public const PHINX_TYPE_VARBINARY = 'varbinary';
|
||||
public const PHINX_TYPE_BINARYUUID = 'binaryuuid';
|
||||
public const PHINX_TYPE_BLOB = 'blob';
|
||||
public const PHINX_TYPE_TINYBLOB = 'tinyblob'; // Specific to Mysql.
|
||||
public const PHINX_TYPE_MEDIUMBLOB = 'mediumblob'; // Specific to Mysql
|
||||
public const PHINX_TYPE_LONGBLOB = 'longblob'; // Specific to Mysql
|
||||
public const PHINX_TYPE_BOOLEAN = 'boolean';
|
||||
public const PHINX_TYPE_JSON = 'json';
|
||||
public const PHINX_TYPE_JSONB = 'jsonb';
|
||||
public const PHINX_TYPE_UUID = 'uuid';
|
||||
public const PHINX_TYPE_FILESTREAM = 'filestream';
|
||||
|
||||
// Geospatial database types
|
||||
public const PHINX_TYPE_GEOMETRY = 'geometry';
|
||||
public const PHINX_TYPE_GEOGRAPHY = 'geography';
|
||||
public const PHINX_TYPE_POINT = 'point';
|
||||
public const PHINX_TYPE_LINESTRING = 'linestring';
|
||||
public const PHINX_TYPE_POLYGON = 'polygon';
|
||||
|
||||
public const PHINX_TYPES_GEOSPATIAL = [
|
||||
self::PHINX_TYPE_GEOMETRY,
|
||||
self::PHINX_TYPE_POINT,
|
||||
self::PHINX_TYPE_LINESTRING,
|
||||
self::PHINX_TYPE_POLYGON,
|
||||
];
|
||||
|
||||
// only for mysql so far
|
||||
public const PHINX_TYPE_MEDIUM_INTEGER = 'mediuminteger';
|
||||
public const PHINX_TYPE_ENUM = 'enum';
|
||||
public const PHINX_TYPE_SET = 'set';
|
||||
public const PHINX_TYPE_YEAR = 'year';
|
||||
|
||||
// only for postgresql so far
|
||||
public const PHINX_TYPE_CIDR = 'cidr';
|
||||
public const PHINX_TYPE_INET = 'inet';
|
||||
public const PHINX_TYPE_MACADDR = 'macaddr';
|
||||
public const PHINX_TYPE_INTERVAL = 'interval';
|
||||
|
||||
/**
|
||||
* Get all migrated version numbers.
|
||||
*
|
||||
* @return array<int>
|
||||
*/
|
||||
public function getVersions(): array;
|
||||
|
||||
/**
|
||||
* Get all migration log entries, indexed by version creation time and sorted ascendingly by the configuration's
|
||||
* version order option
|
||||
*
|
||||
* @return array<int, mixed>
|
||||
*/
|
||||
public function getVersionLog(): array;
|
||||
|
||||
/**
|
||||
* Set adapter configuration options.
|
||||
*
|
||||
* @param array<string, mixed> $options Options
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions(array $options);
|
||||
|
||||
/**
|
||||
* Get all adapter options.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function getOptions(): array;
|
||||
|
||||
/**
|
||||
* Check if an option has been set.
|
||||
*
|
||||
* @param string $name Name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasOption(string $name): bool;
|
||||
|
||||
/**
|
||||
* Get a single adapter option, or null if the option does not exist.
|
||||
*
|
||||
* @param string $name Name
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOption(string $name): mixed;
|
||||
|
||||
/**
|
||||
* Sets the console input.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @return $this
|
||||
*/
|
||||
public function setInput(InputInterface $input);
|
||||
|
||||
/**
|
||||
* Gets the console input.
|
||||
*
|
||||
* @return \Symfony\Component\Console\Input\InputInterface|null
|
||||
*/
|
||||
public function getInput(): ?InputInterface;
|
||||
|
||||
/**
|
||||
* Sets the console output.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @return $this
|
||||
*/
|
||||
public function setOutput(OutputInterface $output);
|
||||
|
||||
/**
|
||||
* Gets the console output.
|
||||
*
|
||||
* @return \Symfony\Component\Console\Output\OutputInterface
|
||||
*/
|
||||
public function getOutput(): OutputInterface;
|
||||
|
||||
/**
|
||||
* Returns a new Phinx\Db\Table\Column using the existent data domain.
|
||||
*
|
||||
* @param string $columnName The desired column name
|
||||
* @param string $type The type for the column. Can be a data domain type.
|
||||
* @param array<string, mixed> $options Options array
|
||||
* @return \Phinx\Db\Table\Column
|
||||
*/
|
||||
public function getColumnForType(string $columnName, string $type, array $options): Column;
|
||||
|
||||
/**
|
||||
* Records a migration being run.
|
||||
*
|
||||
* @param \Phinx\Migration\MigrationInterface $migration Migration
|
||||
* @param string $direction Direction
|
||||
* @param string $startTime Start Time
|
||||
* @param string $endTime End Time
|
||||
* @return $this
|
||||
*/
|
||||
public function migrated(MigrationInterface $migration, string $direction, string $startTime, string $endTime);
|
||||
|
||||
/**
|
||||
* Toggle a migration breakpoint.
|
||||
*
|
||||
* @param \Phinx\Migration\MigrationInterface $migration Migration
|
||||
* @return $this
|
||||
*/
|
||||
public function toggleBreakpoint(MigrationInterface $migration);
|
||||
|
||||
/**
|
||||
* Reset all migration breakpoints.
|
||||
*
|
||||
* @return int The number of breakpoints reset
|
||||
*/
|
||||
public function resetAllBreakpoints(): int;
|
||||
|
||||
/**
|
||||
* Set a migration breakpoint.
|
||||
*
|
||||
* @param \Phinx\Migration\MigrationInterface $migration The migration target for the breakpoint set
|
||||
* @return $this
|
||||
*/
|
||||
public function setBreakpoint(MigrationInterface $migration);
|
||||
|
||||
/**
|
||||
* Unset a migration breakpoint.
|
||||
*
|
||||
* @param \Phinx\Migration\MigrationInterface $migration The migration target for the breakpoint unset
|
||||
* @return $this
|
||||
*/
|
||||
public function unsetBreakpoint(MigrationInterface $migration);
|
||||
|
||||
/**
|
||||
* Creates the schema table.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function createSchemaTable(): void;
|
||||
|
||||
/**
|
||||
* Returns the adapter type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAdapterType(): string;
|
||||
|
||||
/**
|
||||
* Initializes the database connection.
|
||||
*
|
||||
* @throws \RuntimeException When the requested database driver is not installed.
|
||||
* @return void
|
||||
*/
|
||||
public function connect(): void;
|
||||
|
||||
/**
|
||||
* Closes the database connection.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function disconnect(): void;
|
||||
|
||||
/**
|
||||
* Does the adapter support transactions?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasTransactions(): bool;
|
||||
|
||||
/**
|
||||
* Begin a transaction.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function beginTransaction(): void;
|
||||
|
||||
/**
|
||||
* Commit a transaction.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function commitTransaction(): void;
|
||||
|
||||
/**
|
||||
* Rollback a transaction.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rollbackTransaction(): void;
|
||||
|
||||
/**
|
||||
* Executes a SQL statement and returns the number of affected rows.
|
||||
*
|
||||
* @param string $sql SQL
|
||||
* @param array $params parameters to use for prepared query
|
||||
* @return int
|
||||
*/
|
||||
public function execute(string $sql, array $params = []): int;
|
||||
|
||||
/**
|
||||
* Executes a list of migration actions for the given table
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to execute the actions for
|
||||
* @param \Phinx\Db\Action\Action[] $actions The table to execute the actions for
|
||||
* @return void
|
||||
*/
|
||||
public function executeActions(Table $table, array $actions): void;
|
||||
|
||||
/**
|
||||
* Returns a new Query object
|
||||
*
|
||||
* @return \Cake\Database\Query
|
||||
*/
|
||||
public function getQueryBuilder(string $type): Query;
|
||||
|
||||
/**
|
||||
* Return a new SelectQuery object
|
||||
*
|
||||
* @return \Cake\Database\Query\SelectQuery
|
||||
*/
|
||||
public function getSelectBuilder(): SelectQuery;
|
||||
|
||||
/**
|
||||
* Return a new InsertQuery object
|
||||
*
|
||||
* @return \Cake\Database\Query\InsertQuery
|
||||
*/
|
||||
public function getInsertBuilder(): InsertQuery;
|
||||
|
||||
/**
|
||||
* Return a new UpdateQuery object
|
||||
*
|
||||
* @return \Cake\Database\Query\UpdateQuery
|
||||
*/
|
||||
public function getUpdateBuilder(): UpdateQuery;
|
||||
|
||||
/**
|
||||
* Return a new DeleteQuery object
|
||||
*
|
||||
* @return \Cake\Database\Query\DeleteQuery
|
||||
*/
|
||||
public function getDeleteBuilder(): DeleteQuery;
|
||||
|
||||
/**
|
||||
* Executes a SQL statement.
|
||||
*
|
||||
* The return type depends on the underlying adapter being used.
|
||||
*
|
||||
* @param string $sql SQL
|
||||
* @param array $params parameters to use for prepared query
|
||||
* @return mixed
|
||||
*/
|
||||
public function query(string $sql, array $params = []): mixed;
|
||||
|
||||
/**
|
||||
* Executes a query and returns only one row as an array.
|
||||
*
|
||||
* @param string $sql SQL
|
||||
* @return array|false
|
||||
*/
|
||||
public function fetchRow(string $sql): array|false;
|
||||
|
||||
/**
|
||||
* Executes a query and returns an array of rows.
|
||||
*
|
||||
* @param string $sql SQL
|
||||
* @return array
|
||||
*/
|
||||
public function fetchAll(string $sql): array;
|
||||
|
||||
/**
|
||||
* Inserts data into a table.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table Table where to insert data
|
||||
* @param array $row Row
|
||||
* @return void
|
||||
*/
|
||||
public function insert(Table $table, array $row): void;
|
||||
|
||||
/**
|
||||
* Inserts data into a table in a bulk.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table Table where to insert data
|
||||
* @param array $rows Rows
|
||||
* @return void
|
||||
*/
|
||||
public function bulkinsert(Table $table, array $rows): void;
|
||||
|
||||
/**
|
||||
* Quotes a table name for use in a query.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @return string
|
||||
*/
|
||||
public function quoteTableName(string $tableName): string;
|
||||
|
||||
/**
|
||||
* Quotes a column name for use in a query.
|
||||
*
|
||||
* @param string $columnName Table name
|
||||
* @return string
|
||||
*/
|
||||
public function quoteColumnName(string $columnName): string;
|
||||
|
||||
/**
|
||||
* Checks to see if a table exists.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasTable(string $tableName): bool;
|
||||
|
||||
/**
|
||||
* Creates the specified database table.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table Table
|
||||
* @param \Phinx\Db\Table\Column[] $columns List of columns in the table
|
||||
* @param \Phinx\Db\Table\Index[] $indexes List of indexes for the table
|
||||
* @return void
|
||||
*/
|
||||
public function createTable(Table $table, array $columns = [], array $indexes = []): void;
|
||||
|
||||
/**
|
||||
* Truncates the specified table
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @return void
|
||||
*/
|
||||
public function truncateTable(string $tableName): void;
|
||||
|
||||
/**
|
||||
* Returns table columns
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @return \Phinx\Db\Table\Column[]
|
||||
*/
|
||||
public function getColumns(string $tableName): array;
|
||||
|
||||
/**
|
||||
* Checks to see if a column exists.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @param string $columnName Column name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasColumn(string $tableName, string $columnName): bool;
|
||||
|
||||
/**
|
||||
* Checks to see if an index exists.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @param string|string[] $columns Column(s)
|
||||
* @return bool
|
||||
*/
|
||||
public function hasIndex(string $tableName, string|array $columns): bool;
|
||||
|
||||
/**
|
||||
* Checks to see if an index specified by name exists.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @param string $indexName Index name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasIndexByName(string $tableName, string $indexName): bool;
|
||||
|
||||
/**
|
||||
* Checks to see if the specified primary key exists.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @param string|string[] $columns Column(s)
|
||||
* @param string|null $constraint Constraint name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPrimaryKey(string $tableName, string|array $columns, ?string $constraint = null): bool;
|
||||
|
||||
/**
|
||||
* Checks to see if a foreign key exists.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @param string|string[] $columns Column(s)
|
||||
* @param string|null $constraint Constraint name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasForeignKey(string $tableName, string|array $columns, ?string $constraint = null): bool;
|
||||
|
||||
/**
|
||||
* Returns an array of the supported Phinx column types.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getColumnTypes(): array;
|
||||
|
||||
/**
|
||||
* Checks that the given column is of a supported type.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Column $column Column
|
||||
* @return bool
|
||||
*/
|
||||
public function isValidColumnType(Column $column): bool;
|
||||
|
||||
/**
|
||||
* Converts the Phinx logical type to the adapter's SQL type.
|
||||
*
|
||||
* @param \Phinx\Util\Literal|string $type Type
|
||||
* @param int|null $limit Limit
|
||||
* @return array
|
||||
*/
|
||||
public function getSqlType(Literal|string $type, ?int $limit = null): array;
|
||||
|
||||
/**
|
||||
* Creates a new database.
|
||||
*
|
||||
* @param string $name Database Name
|
||||
* @param array<string, mixed> $options Options
|
||||
* @return void
|
||||
*/
|
||||
public function createDatabase(string $name, array $options = []): void;
|
||||
|
||||
/**
|
||||
* Checks to see if a database exists.
|
||||
*
|
||||
* @param string $name Database Name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasDatabase(string $name): bool;
|
||||
|
||||
/**
|
||||
* Drops the specified database.
|
||||
*
|
||||
* @param string $name Database Name
|
||||
* @return void
|
||||
*/
|
||||
public function dropDatabase(string $name): void;
|
||||
|
||||
/**
|
||||
* Creates the specified schema or throws an exception
|
||||
* if there is no support for it.
|
||||
*
|
||||
* @param string $schemaName Schema Name
|
||||
* @return void
|
||||
*/
|
||||
public function createSchema(string $schemaName = 'public'): void;
|
||||
|
||||
/**
|
||||
* Drops the specified schema table or throws an exception
|
||||
* if there is no support for it.
|
||||
*
|
||||
* @param string $schemaName Schema name
|
||||
* @return void
|
||||
*/
|
||||
public function dropSchema(string $schemaName): void;
|
||||
|
||||
/**
|
||||
* Cast a value to a boolean appropriate for the adapter.
|
||||
*
|
||||
* @param mixed $value The value to be cast
|
||||
* @return mixed
|
||||
*/
|
||||
public function castToBool(mixed $value): mixed;
|
||||
}
|
||||
@@ -0,0 +1,524 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Adapter;
|
||||
|
||||
use Cake\Database\Query;
|
||||
use Cake\Database\Query\DeleteQuery;
|
||||
use Cake\Database\Query\InsertQuery;
|
||||
use Cake\Database\Query\SelectQuery;
|
||||
use Cake\Database\Query\UpdateQuery;
|
||||
use PDO;
|
||||
use Phinx\Db\Table\Column;
|
||||
use Phinx\Db\Table\Table;
|
||||
use Phinx\Migration\MigrationInterface;
|
||||
use Phinx\Util\Literal;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Adapter Wrapper.
|
||||
*
|
||||
* Proxy commands through to another adapter, allowing modification of
|
||||
* parameters during calls.
|
||||
*/
|
||||
abstract class AdapterWrapper implements AdapterInterface, WrapperInterface
|
||||
{
|
||||
/**
|
||||
* @var \Phinx\Db\Adapter\AdapterInterface
|
||||
*/
|
||||
protected AdapterInterface $adapter;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function __construct(AdapterInterface $adapter)
|
||||
{
|
||||
$this->setAdapter($adapter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setAdapter(AdapterInterface $adapter): AdapterInterface
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getAdapter(): AdapterInterface
|
||||
{
|
||||
return $this->adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setOptions(array $options): AdapterInterface
|
||||
{
|
||||
$this->adapter->setOptions($options);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOptions(): array
|
||||
{
|
||||
return $this->adapter->getOptions();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasOption(string $name): bool
|
||||
{
|
||||
return $this->adapter->hasOption($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOption(string $name): mixed
|
||||
{
|
||||
return $this->adapter->getOption($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setInput(InputInterface $input): AdapterInterface
|
||||
{
|
||||
$this->adapter->setInput($input);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getInput(): InputInterface
|
||||
{
|
||||
return $this->adapter->getInput();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setOutput(OutputInterface $output): AdapterInterface
|
||||
{
|
||||
$this->adapter->setOutput($output);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOutput(): OutputInterface
|
||||
{
|
||||
return $this->adapter->getOutput();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getColumnForType(string $columnName, string $type, array $options): Column
|
||||
{
|
||||
return $this->adapter->getColumnForType($columnName, $type, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function connect(): void
|
||||
{
|
||||
$this->getAdapter()->connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function disconnect(): void
|
||||
{
|
||||
$this->getAdapter()->disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function execute(string $sql, array $params = []): int
|
||||
{
|
||||
return $this->getAdapter()->execute($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function query(string $sql, array $params = []): mixed
|
||||
{
|
||||
return $this->getAdapter()->query($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function insert(Table $table, array $row): void
|
||||
{
|
||||
$this->getAdapter()->insert($table, $row);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function bulkinsert(Table $table, array $rows): void
|
||||
{
|
||||
$this->getAdapter()->bulkinsert($table, $rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function fetchRow(string $sql): array|false
|
||||
{
|
||||
return $this->getAdapter()->fetchRow($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function fetchAll(string $sql): array
|
||||
{
|
||||
return $this->getAdapter()->fetchAll($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getVersions(): array
|
||||
{
|
||||
return $this->getAdapter()->getVersions();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getVersionLog(): array
|
||||
{
|
||||
return $this->getAdapter()->getVersionLog();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function migrated(MigrationInterface $migration, string $direction, string $startTime, string $endTime): AdapterInterface
|
||||
{
|
||||
$this->getAdapter()->migrated($migration, $direction, $startTime, $endTime);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toggleBreakpoint(MigrationInterface $migration): AdapterInterface
|
||||
{
|
||||
$this->getAdapter()->toggleBreakpoint($migration);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function resetAllBreakpoints(): int
|
||||
{
|
||||
return $this->getAdapter()->resetAllBreakpoints();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setBreakpoint(MigrationInterface $migration): AdapterInterface
|
||||
{
|
||||
$this->getAdapter()->setBreakpoint($migration);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function unsetBreakpoint(MigrationInterface $migration): AdapterInterface
|
||||
{
|
||||
$this->getAdapter()->unsetBreakpoint($migration);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createSchemaTable(): void
|
||||
{
|
||||
$this->getAdapter()->createSchemaTable();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getColumnTypes(): array
|
||||
{
|
||||
return $this->getAdapter()->getColumnTypes();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isValidColumnType(Column $column): bool
|
||||
{
|
||||
return $this->getAdapter()->isValidColumnType($column);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasTransactions(): bool
|
||||
{
|
||||
return $this->getAdapter()->hasTransactions();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function beginTransaction(): void
|
||||
{
|
||||
$this->getAdapter()->beginTransaction();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function commitTransaction(): void
|
||||
{
|
||||
$this->getAdapter()->commitTransaction();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function rollbackTransaction(): void
|
||||
{
|
||||
$this->getAdapter()->rollbackTransaction();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function quoteTableName(string $tableName): string
|
||||
{
|
||||
return $this->getAdapter()->quoteTableName($tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function quoteColumnName(string $columnName): string
|
||||
{
|
||||
return $this->getAdapter()->quoteColumnName($columnName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasTable(string $tableName): bool
|
||||
{
|
||||
return $this->getAdapter()->hasTable($tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createTable(Table $table, array $columns = [], array $indexes = []): void
|
||||
{
|
||||
$this->getAdapter()->createTable($table, $columns, $indexes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getColumns(string $tableName): array
|
||||
{
|
||||
return $this->getAdapter()->getColumns($tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasColumn(string $tableName, string $columnName): bool
|
||||
{
|
||||
return $this->getAdapter()->hasColumn($tableName, $columnName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasIndex(string $tableName, string|array $columns): bool
|
||||
{
|
||||
return $this->getAdapter()->hasIndex($tableName, $columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasIndexByName(string $tableName, string $indexName): bool
|
||||
{
|
||||
return $this->getAdapter()->hasIndexByName($tableName, $indexName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasPrimaryKey(string $tableName, $columns, ?string $constraint = null): bool
|
||||
{
|
||||
return $this->getAdapter()->hasPrimaryKey($tableName, $columns, $constraint);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasForeignKey(string $tableName, $columns, ?string $constraint = null): bool
|
||||
{
|
||||
return $this->getAdapter()->hasForeignKey($tableName, $columns, $constraint);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getSqlType(Literal|string $type, ?int $limit = null): array
|
||||
{
|
||||
return $this->getAdapter()->getSqlType($type, $limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createDatabase(string $name, array $options = []): void
|
||||
{
|
||||
$this->getAdapter()->createDatabase($name, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasDatabase(string $name): bool
|
||||
{
|
||||
return $this->getAdapter()->hasDatabase($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function dropDatabase(string $name): void
|
||||
{
|
||||
$this->getAdapter()->dropDatabase($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createSchema(string $schemaName = 'public'): void
|
||||
{
|
||||
$this->getAdapter()->createSchema($schemaName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function dropSchema(string $schemaName): void
|
||||
{
|
||||
$this->getAdapter()->dropSchema($schemaName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function truncateTable(string $tableName): void
|
||||
{
|
||||
$this->getAdapter()->truncateTable($tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function castToBool($value): mixed
|
||||
{
|
||||
return $this->getAdapter()->castToBool($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \PDO
|
||||
*/
|
||||
public function getConnection(): PDO
|
||||
{
|
||||
return $this->getAdapter()->getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function executeActions(Table $table, array $actions): void
|
||||
{
|
||||
$this->getAdapter()->executeActions($table, $actions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getQueryBuilder(string $type): Query
|
||||
{
|
||||
return $this->getAdapter()->getQueryBuilder($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getSelectBuilder(): SelectQuery
|
||||
{
|
||||
return $this->getAdapter()->getSelectBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getInsertBuilder(): InsertQuery
|
||||
{
|
||||
return $this->getAdapter()->getInsertBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getUpdateBuilder(): UpdateQuery
|
||||
{
|
||||
return $this->getAdapter()->getUpdateBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getDeleteBuilder(): DeleteQuery
|
||||
{
|
||||
return $this->getAdapter()->getDeleteBuilder();
|
||||
}
|
||||
}
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Adapter;
|
||||
|
||||
use Phinx\Db\Table\Column;
|
||||
use Phinx\Db\Table\ForeignKey;
|
||||
use Phinx\Db\Table\Index;
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
/**
|
||||
* Represents an adapter that is capable of directly executing alter
|
||||
* instructions, without having to plan them first.
|
||||
*/
|
||||
interface DirectActionInterface
|
||||
{
|
||||
/**
|
||||
* Renames the specified database table.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @param string $newName New Name
|
||||
* @return void
|
||||
*/
|
||||
public function renameTable(string $tableName, string $newName): void;
|
||||
|
||||
/**
|
||||
* Drops the specified database table.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @return void
|
||||
*/
|
||||
public function dropTable(string $tableName): void;
|
||||
|
||||
/**
|
||||
* Changes the primary key of the specified database table.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table Table
|
||||
* @param string|string[]|null $newColumns Column name(s) to belong to the primary key, or null to drop the key
|
||||
* @return void
|
||||
*/
|
||||
public function changePrimaryKey(Table $table, string|array|null $newColumns): void;
|
||||
|
||||
/**
|
||||
* Changes the comment of the specified database table.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table Table
|
||||
* @param string|null $newComment New comment string, or null to drop the comment
|
||||
* @return void
|
||||
*/
|
||||
public function changeComment(Table $table, ?string $newComment): void;
|
||||
|
||||
/**
|
||||
* Adds the specified column to a database table.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table Table
|
||||
* @param \Phinx\Db\Table\Column $column Column
|
||||
* @return void
|
||||
*/
|
||||
public function addColumn(Table $table, Column $column): void;
|
||||
|
||||
/**
|
||||
* Renames the specified column.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @param string $columnName Column Name
|
||||
* @param string $newColumnName New Column Name
|
||||
* @return void
|
||||
*/
|
||||
public function renameColumn(string $tableName, string $columnName, string $newColumnName): void;
|
||||
|
||||
/**
|
||||
* Change a table column type.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @param string $columnName Column Name
|
||||
* @param \Phinx\Db\Table\Column $newColumn New Column
|
||||
* @return void
|
||||
*/
|
||||
public function changeColumn(string $tableName, string $columnName, Column $newColumn): void;
|
||||
|
||||
/**
|
||||
* Drops the specified column.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @param string $columnName Column Name
|
||||
* @return void
|
||||
*/
|
||||
public function dropColumn(string $tableName, string $columnName): void;
|
||||
|
||||
/**
|
||||
* Adds the specified index to a database table.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table Table
|
||||
* @param \Phinx\Db\Table\Index $index Index
|
||||
* @return void
|
||||
*/
|
||||
public function addIndex(Table $table, Index $index): void;
|
||||
|
||||
/**
|
||||
* Drops the specified index from a database table.
|
||||
*
|
||||
* @param string $tableName the name of the table
|
||||
* @param string|string[] $columns Column(s)
|
||||
* @return void
|
||||
*/
|
||||
public function dropIndex(string $tableName, string|array $columns): void;
|
||||
|
||||
/**
|
||||
* Drops the index specified by name from a database table.
|
||||
*
|
||||
* @param string $tableName The table name where the index is
|
||||
* @param string $indexName The name of the index
|
||||
* @return void
|
||||
*/
|
||||
public function dropIndexByName(string $tableName, string $indexName): void;
|
||||
|
||||
/**
|
||||
* Adds the specified foreign key to a database table.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to add the foreign key to
|
||||
* @param \Phinx\Db\Table\ForeignKey $foreignKey The foreign key to add
|
||||
* @return void
|
||||
*/
|
||||
public function addForeignKey(Table $table, ForeignKey $foreignKey): void;
|
||||
|
||||
/**
|
||||
* Drops the specified foreign key from a database table.
|
||||
*
|
||||
* @param string $tableName The table to drop the foreign key from
|
||||
* @param string[] $columns Column(s)
|
||||
* @param string|null $constraint Constraint name
|
||||
* @return void
|
||||
*/
|
||||
public function dropForeignKey(string $tableName, array $columns, ?string $constraint = null): void;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Adapter;
|
||||
|
||||
use Phinx\Db\Action\AddColumn;
|
||||
use Phinx\Db\Action\AddForeignKey;
|
||||
use Phinx\Db\Action\AddIndex;
|
||||
use Phinx\Db\Action\CreateTable;
|
||||
use Phinx\Db\Action\DropForeignKey;
|
||||
use Phinx\Db\Action\DropIndex;
|
||||
use Phinx\Db\Action\DropTable;
|
||||
use Phinx\Db\Action\RemoveColumn;
|
||||
use Phinx\Db\Action\RenameColumn;
|
||||
use Phinx\Db\Action\RenameTable;
|
||||
use Phinx\Db\Plan\Intent;
|
||||
use Phinx\Db\Plan\Plan;
|
||||
use Phinx\Db\Table\Table;
|
||||
use Phinx\Migration\IrreversibleMigrationException;
|
||||
|
||||
/**
|
||||
* Phinx Proxy Adapter.
|
||||
*
|
||||
* Used for recording migration commands to automatically reverse them.
|
||||
*/
|
||||
class ProxyAdapter extends AdapterWrapper
|
||||
{
|
||||
/**
|
||||
* @var \Phinx\Db\Action\Action[]
|
||||
*/
|
||||
protected array $commands = [];
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getAdapterType(): string
|
||||
{
|
||||
return 'ProxyAdapter';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createTable(Table $table, array $columns = [], array $indexes = []): void
|
||||
{
|
||||
$this->commands[] = new CreateTable($table);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function executeActions(Table $table, array $actions): void
|
||||
{
|
||||
$this->commands = array_merge($this->commands, $actions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an array of the recorded commands in reverse.
|
||||
*
|
||||
* @throws \Phinx\Migration\IrreversibleMigrationException if a command cannot be reversed.
|
||||
* @return \Phinx\Db\Plan\Intent
|
||||
*/
|
||||
public function getInvertedCommands(): Intent
|
||||
{
|
||||
$inverted = new Intent();
|
||||
|
||||
foreach (array_reverse($this->commands) as $command) {
|
||||
switch (true) {
|
||||
case $command instanceof CreateTable:
|
||||
/** @var \Phinx\Db\Action\CreateTable $command */
|
||||
$inverted->addAction(new DropTable($command->getTable()));
|
||||
break;
|
||||
|
||||
case $command instanceof RenameTable:
|
||||
/** @var \Phinx\Db\Action\RenameTable $command */
|
||||
$inverted->addAction(new RenameTable(new Table($command->getNewName()), $command->getTable()->getName()));
|
||||
break;
|
||||
|
||||
case $command instanceof AddColumn:
|
||||
/** @var \Phinx\Db\Action\AddColumn $command */
|
||||
$inverted->addAction(new RemoveColumn($command->getTable(), $command->getColumn()));
|
||||
break;
|
||||
|
||||
case $command instanceof RenameColumn:
|
||||
/** @var \Phinx\Db\Action\RenameColumn $command */
|
||||
$column = clone $command->getColumn();
|
||||
$name = $column->getName();
|
||||
$column->setName($command->getNewName());
|
||||
$inverted->addAction(new RenameColumn($command->getTable(), $column, $name));
|
||||
break;
|
||||
|
||||
case $command instanceof AddIndex:
|
||||
/** @var \Phinx\Db\Action\AddIndex $command */
|
||||
$inverted->addAction(new DropIndex($command->getTable(), $command->getIndex()));
|
||||
break;
|
||||
|
||||
case $command instanceof AddForeignKey:
|
||||
/** @var \Phinx\Db\Action\AddForeignKey $command */
|
||||
$inverted->addAction(new DropForeignKey($command->getTable(), $command->getForeignKey()));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new IrreversibleMigrationException(sprintf(
|
||||
'Cannot reverse a "%s" command',
|
||||
get_class($command),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return $inverted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the recorded commands in reverse.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function executeInvertedCommands(): void
|
||||
{
|
||||
$plan = new Plan($this->getInvertedCommands());
|
||||
$plan->executeInverse($this->getAdapter());
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,493 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Adapter;
|
||||
|
||||
use BadMethodCallException;
|
||||
use InvalidArgumentException;
|
||||
use Phinx\Db\Action\AddColumn;
|
||||
use Phinx\Db\Action\AddForeignKey;
|
||||
use Phinx\Db\Action\AddIndex;
|
||||
use Phinx\Db\Action\ChangeColumn;
|
||||
use Phinx\Db\Action\ChangeComment;
|
||||
use Phinx\Db\Action\ChangePrimaryKey;
|
||||
use Phinx\Db\Action\DropForeignKey;
|
||||
use Phinx\Db\Action\DropIndex;
|
||||
use Phinx\Db\Action\DropTable;
|
||||
use Phinx\Db\Action\RemoveColumn;
|
||||
use Phinx\Db\Action\RenameColumn;
|
||||
use Phinx\Db\Action\RenameTable;
|
||||
use Phinx\Db\Table\Column;
|
||||
use Phinx\Db\Table\ForeignKey;
|
||||
use Phinx\Db\Table\Index;
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
/**
|
||||
* Table prefix/suffix adapter.
|
||||
*
|
||||
* Used for inserting a prefix or suffix into table names.
|
||||
*/
|
||||
class TablePrefixAdapter extends AdapterWrapper implements DirectActionInterface
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getAdapterType(): string
|
||||
{
|
||||
return 'TablePrefixAdapter';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasTable(string $tableName): bool
|
||||
{
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
|
||||
return parent::hasTable($adapterTableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createTable(Table $table, array $columns = [], array $indexes = []): void
|
||||
{
|
||||
$adapterTable = new Table(
|
||||
$this->getAdapterTableName($table->getName()),
|
||||
$table->getOptions(),
|
||||
);
|
||||
parent::createTable($adapterTable, $columns, $indexes);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function changePrimaryKey(Table $table, $newColumns): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
|
||||
}
|
||||
|
||||
$adapterTable = new Table(
|
||||
$this->getAdapterTableName($table->getName()),
|
||||
$table->getOptions(),
|
||||
);
|
||||
$adapter->changePrimaryKey($adapterTable, $newColumns);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function changeComment(Table $table, ?string $newComment): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
|
||||
}
|
||||
|
||||
$adapterTable = new Table(
|
||||
$this->getAdapterTableName($table->getName()),
|
||||
$table->getOptions(),
|
||||
);
|
||||
$adapter->changeComment($adapterTable, $newComment);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function renameTable(string $tableName, string $newTableName): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
|
||||
}
|
||||
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
$adapterNewTableName = $this->getAdapterTableName($newTableName);
|
||||
$adapter->renameTable($adapterTableName, $adapterNewTableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function dropTable(string $tableName): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
|
||||
}
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
$adapter->dropTable($adapterTableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function truncateTable(string $tableName): void
|
||||
{
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
parent::truncateTable($adapterTableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getColumns(string $tableName): array
|
||||
{
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
|
||||
return parent::getColumns($adapterTableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasColumn(string $tableName, string $columnName): bool
|
||||
{
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
|
||||
return parent::hasColumn($adapterTableName, $columnName);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function addColumn(Table $table, Column $column): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
|
||||
}
|
||||
$adapterTableName = $this->getAdapterTableName($table->getName());
|
||||
$adapterTable = new Table($adapterTableName, $table->getOptions());
|
||||
$adapter->addColumn($adapterTable, $column);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function renameColumn(string $tableName, string $columnName, string $newColumnName): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
|
||||
}
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
$adapter->renameColumn($adapterTableName, $columnName, $newColumnName);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function changeColumn(string $tableName, string $columnName, Column $newColumn): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
|
||||
}
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
$adapter->changeColumn($adapterTableName, $columnName, $newColumn);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function dropColumn(string $tableName, string $columnName): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
|
||||
}
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
$adapter->dropColumn($adapterTableName, $columnName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasIndex(string $tableName, string|array $columns): bool
|
||||
{
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
|
||||
return parent::hasIndex($adapterTableName, $columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasIndexByName(string $tableName, string $indexName): bool
|
||||
{
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
|
||||
return parent::hasIndexByName($adapterTableName, $indexName);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function addIndex(Table $table, Index $index): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
|
||||
}
|
||||
$adapterTable = new Table($table->getName(), $table->getOptions());
|
||||
$adapter->addIndex($adapterTable, $index);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function dropIndex(string $tableName, $columns): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
|
||||
}
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
$adapter->dropIndex($adapterTableName, $columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function dropIndexByName(string $tableName, string $indexName): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
|
||||
}
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
$adapter->dropIndexByName($adapterTableName, $indexName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasPrimaryKey(string $tableName, $columns, ?string $constraint = null): bool
|
||||
{
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
|
||||
return parent::hasPrimaryKey($adapterTableName, $columns, $constraint);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasForeignKey(string $tableName, $columns, ?string $constraint = null): bool
|
||||
{
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
|
||||
return parent::hasForeignKey($adapterTableName, $columns, $constraint);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function addForeignKey(Table $table, ForeignKey $foreignKey): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
|
||||
}
|
||||
$adapterTableName = $this->getAdapterTableName($table->getName());
|
||||
$adapterTable = new Table($adapterTableName, $table->getOptions());
|
||||
$adapter->addForeignKey($adapterTable, $foreignKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function dropForeignKey(string $tableName, array $columns, ?string $constraint = null): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
|
||||
}
|
||||
$adapterTableName = $this->getAdapterTableName($tableName);
|
||||
$adapter->dropForeignKey($adapterTableName, $columns, $constraint);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function insert(Table $table, array $row): void
|
||||
{
|
||||
$adapterTableName = $this->getAdapterTableName($table->getName());
|
||||
$adapterTable = new Table($adapterTableName, $table->getOptions());
|
||||
parent::insert($adapterTable, $row);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function bulkinsert(Table $table, array $rows): void
|
||||
{
|
||||
$adapterTableName = $this->getAdapterTableName($table->getName());
|
||||
$adapterTable = new Table($adapterTableName, $table->getOptions());
|
||||
parent::bulkinsert($adapterTable, $rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the table prefix.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPrefix(): string
|
||||
{
|
||||
return (string)$this->getOption('table_prefix');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the table suffix.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSuffix(): string
|
||||
{
|
||||
return (string)$this->getOption('table_suffix');
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the prefix and suffix to the table name.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @return string
|
||||
*/
|
||||
public function getAdapterTableName(string $tableName): string
|
||||
{
|
||||
return $this->getPrefix() . $tableName . $this->getSuffix();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @return void
|
||||
*/
|
||||
public function executeActions(Table $table, array $actions): void
|
||||
{
|
||||
$adapterTableName = $this->getAdapterTableName($table->getName());
|
||||
$adapterTable = new Table($adapterTableName, $table->getOptions());
|
||||
|
||||
foreach ($actions as $k => $action) {
|
||||
switch (true) {
|
||||
case $action instanceof AddColumn:
|
||||
/** @var \Phinx\Db\Action\AddColumn $action */
|
||||
$actions[$k] = new AddColumn($adapterTable, $action->getColumn());
|
||||
break;
|
||||
|
||||
case $action instanceof AddIndex:
|
||||
/** @var \Phinx\Db\Action\AddIndex $action */
|
||||
$actions[$k] = new AddIndex($adapterTable, $action->getIndex());
|
||||
break;
|
||||
|
||||
case $action instanceof AddForeignKey:
|
||||
/** @var \Phinx\Db\Action\AddForeignKey $action */
|
||||
$foreignKey = clone $action->getForeignKey();
|
||||
$refTable = $foreignKey->getReferencedTable();
|
||||
$refTableName = $this->getAdapterTableName($refTable->getName());
|
||||
$foreignKey->setReferencedTable(new Table($refTableName, $refTable->getOptions()));
|
||||
$actions[$k] = new AddForeignKey($adapterTable, $foreignKey);
|
||||
break;
|
||||
|
||||
case $action instanceof ChangeColumn:
|
||||
/** @var \Phinx\Db\Action\ChangeColumn $action */
|
||||
$actions[$k] = new ChangeColumn($adapterTable, $action->getColumnName(), $action->getColumn());
|
||||
break;
|
||||
|
||||
case $action instanceof DropForeignKey:
|
||||
/** @var \Phinx\Db\Action\DropForeignKey $action */
|
||||
$actions[$k] = new DropForeignKey($adapterTable, $action->getForeignKey());
|
||||
break;
|
||||
|
||||
case $action instanceof DropIndex:
|
||||
/** @var \Phinx\Db\Action\DropIndex $action */
|
||||
$actions[$k] = new DropIndex($adapterTable, $action->getIndex());
|
||||
break;
|
||||
|
||||
case $action instanceof DropTable:
|
||||
/** @var \Phinx\Db\Action\DropTable $action */
|
||||
$actions[$k] = new DropTable($adapterTable);
|
||||
break;
|
||||
|
||||
case $action instanceof RemoveColumn:
|
||||
/** @var \Phinx\Db\Action\RemoveColumn $action */
|
||||
$actions[$k] = new RemoveColumn($adapterTable, $action->getColumn());
|
||||
break;
|
||||
|
||||
case $action instanceof RenameColumn:
|
||||
/** @var \Phinx\Db\Action\RenameColumn $action */
|
||||
$actions[$k] = new RenameColumn($adapterTable, $action->getColumn(), $action->getNewName());
|
||||
break;
|
||||
|
||||
case $action instanceof RenameTable:
|
||||
/** @var \Phinx\Db\Action\RenameTable $action */
|
||||
$actions[$k] = new RenameTable($adapterTable, $this->getAdapterTableName($action->getNewName()));
|
||||
break;
|
||||
|
||||
case $action instanceof ChangePrimaryKey:
|
||||
/** @var \Phinx\Db\Action\ChangePrimaryKey $action */
|
||||
$actions[$k] = new ChangePrimaryKey($adapterTable, $action->getNewColumns());
|
||||
break;
|
||||
|
||||
case $action instanceof ChangeComment:
|
||||
/** @var \Phinx\Db\Action\ChangeComment $action */
|
||||
$actions[$k] = new ChangeComment($adapterTable, $action->getNewComment());
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException(
|
||||
sprintf("Forgot to implement table prefixing for action: '%s'", get_class($action)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
parent::executeActions($adapterTable, $actions);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,424 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Adapter;
|
||||
|
||||
use BadMethodCallException;
|
||||
use Phinx\Db\Table\Column;
|
||||
use Phinx\Db\Table\ForeignKey;
|
||||
use Phinx\Db\Table\Index;
|
||||
use Phinx\Db\Table\Table;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Wraps any adapter to record the time spend executing its commands
|
||||
*/
|
||||
class TimedOutputAdapter extends AdapterWrapper implements DirectActionInterface
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getAdapterType(): string
|
||||
{
|
||||
return $this->getAdapter()->getAdapterType();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start timing a command.
|
||||
*
|
||||
* @return callable A function that is to be called when the command finishes
|
||||
*/
|
||||
public function startCommandTimer(): callable
|
||||
{
|
||||
$started = microtime(true);
|
||||
|
||||
return function () use ($started): void {
|
||||
$end = microtime(true);
|
||||
if (OutputInterface::VERBOSITY_VERBOSE <= $this->getOutput()->getVerbosity()) {
|
||||
$this->getOutput()->writeln(' -> ' . sprintf('%.4fs', $end - $started));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a Phinx command to the output.
|
||||
*
|
||||
* @param string $command Command Name
|
||||
* @param array $args Command Args
|
||||
* @return void
|
||||
*/
|
||||
public function writeCommand(string $command, array $args = []): void
|
||||
{
|
||||
if (OutputInterface::VERBOSITY_VERBOSE > $this->getOutput()->getVerbosity()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (count($args)) {
|
||||
$outArr = [];
|
||||
foreach ($args as $arg) {
|
||||
if (is_array($arg)) {
|
||||
$arg = array_map(
|
||||
function ($value) {
|
||||
return '\'' . $value . '\'';
|
||||
},
|
||||
$arg,
|
||||
);
|
||||
$outArr[] = '[' . implode(', ', $arg) . ']';
|
||||
continue;
|
||||
}
|
||||
|
||||
$outArr[] = '\'' . $arg . '\'';
|
||||
}
|
||||
$this->getOutput()->writeln(' -- ' . $command . '(' . implode(', ', $outArr) . ')');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->getOutput()->writeln(' -- ' . $command);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function insert(Table $table, array $row): void
|
||||
{
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('insert', [$table->getName()]);
|
||||
parent::insert($table, $row);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function bulkinsert(Table $table, array $rows): void
|
||||
{
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('bulkinsert', [$table->getName()]);
|
||||
parent::bulkinsert($table, $rows);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createTable(Table $table, array $columns = [], array $indexes = []): void
|
||||
{
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('createTable', [$table->getName()]);
|
||||
parent::createTable($table, $columns, $indexes);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function changePrimaryKey(Table $table, $newColumns): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
|
||||
}
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('changePrimaryKey', [$table->getName()]);
|
||||
$adapter->changePrimaryKey($table, $newColumns);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function changeComment(Table $table, ?string $newComment): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
|
||||
}
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('changeComment', [$table->getName()]);
|
||||
$adapter->changeComment($table, $newComment);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function renameTable(string $tableName, string $newTableName): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
|
||||
}
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('renameTable', [$tableName, $newTableName]);
|
||||
$adapter->renameTable($tableName, $newTableName);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function dropTable(string $tableName): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
|
||||
}
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('dropTable', [$tableName]);
|
||||
$adapter->dropTable($tableName);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function truncateTable(string $tableName): void
|
||||
{
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('truncateTable', [$tableName]);
|
||||
parent::truncateTable($tableName);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function addColumn(Table $table, Column $column): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
|
||||
}
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand(
|
||||
'addColumn',
|
||||
[
|
||||
$table->getName(),
|
||||
$column->getName(),
|
||||
$column->getType(),
|
||||
],
|
||||
);
|
||||
$adapter->addColumn($table, $column);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function renameColumn(string $tableName, string $columnName, string $newColumnName): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
|
||||
}
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('renameColumn', [$tableName, $columnName, $newColumnName]);
|
||||
$adapter->renameColumn($tableName, $columnName, $newColumnName);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function changeColumn(string $tableName, string $columnName, Column $newColumn): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
|
||||
}
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('changeColumn', [$tableName, $columnName, $newColumn->getType()]);
|
||||
$adapter->changeColumn($tableName, $columnName, $newColumn);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function dropColumn(string $tableName, string $columnName): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
|
||||
}
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('dropColumn', [$tableName, $columnName]);
|
||||
$adapter->dropColumn($tableName, $columnName);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function addIndex(Table $table, Index $index): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
|
||||
}
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('addIndex', [$table->getName(), $index->getColumns()]);
|
||||
$adapter->addIndex($table, $index);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function dropIndex(string $tableName, $columns): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
|
||||
}
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('dropIndex', [$tableName, $columns]);
|
||||
$adapter->dropIndex($tableName, $columns);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function dropIndexByName(string $tableName, string $indexName): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
|
||||
}
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('dropIndexByName', [$tableName, $indexName]);
|
||||
$adapter->dropIndexByName($tableName, $indexName);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function addForeignKey(Table $table, ForeignKey $foreignKey): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
|
||||
}
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('addForeignKey', [$table->getName(), $foreignKey->getColumns()]);
|
||||
$adapter->addForeignKey($table, $foreignKey);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* @return void
|
||||
*/
|
||||
public function dropForeignKey(string $tableName, array $columns, ?string $constraint = null): void
|
||||
{
|
||||
$adapter = $this->getAdapter();
|
||||
if (!$adapter instanceof DirectActionInterface) {
|
||||
throw new BadMethodCallException('The adapter needs to implement DirectActionInterface');
|
||||
}
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('dropForeignKey', [$tableName, $columns]);
|
||||
$adapter->dropForeignKey($tableName, $columns, $constraint);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createDatabase(string $name, array $options = []): void
|
||||
{
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('createDatabase', [$name]);
|
||||
parent::createDatabase($name, $options);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function dropDatabase(string $name): void
|
||||
{
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('dropDatabase', [$name]);
|
||||
parent::dropDatabase($name);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createSchema(string $name = 'public'): void
|
||||
{
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('createSchema', [$name]);
|
||||
parent::createSchema($name);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function dropSchema(string $name): void
|
||||
{
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand('dropSchema', [$name]);
|
||||
parent::dropSchema($name);
|
||||
$end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function executeActions(Table $table, array $actions): void
|
||||
{
|
||||
$end = $this->startCommandTimer();
|
||||
$this->writeCommand(sprintf('Altering table %s', $table->getName()));
|
||||
parent::executeActions($table, $actions);
|
||||
$end();
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Adapter;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Exception thrown when a column type doesn't match a Phinx type.
|
||||
*/
|
||||
class UnsupportedColumnTypeException extends RuntimeException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Adapter;
|
||||
|
||||
/**
|
||||
* Wrapper Interface.
|
||||
*/
|
||||
interface WrapperInterface
|
||||
{
|
||||
/**
|
||||
* Class constructor, must always wrap another adapter.
|
||||
*
|
||||
* @param \Phinx\Db\Adapter\AdapterInterface $adapter Adapter
|
||||
*/
|
||||
public function __construct(AdapterInterface $adapter);
|
||||
|
||||
/**
|
||||
* Sets the database adapter to proxy commands to.
|
||||
*
|
||||
* @param \Phinx\Db\Adapter\AdapterInterface $adapter Adapter
|
||||
* @return \Phinx\Db\Adapter\AdapterInterface
|
||||
*/
|
||||
public function setAdapter(AdapterInterface $adapter): AdapterInterface;
|
||||
|
||||
/**
|
||||
* Gets the database adapter.
|
||||
*
|
||||
* @throws \RuntimeException if the adapter has not been set
|
||||
* @return \Phinx\Db\Adapter\AdapterInterface
|
||||
*/
|
||||
public function getAdapter(): AdapterInterface;
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Plan;
|
||||
|
||||
use Phinx\Db\Action\Action;
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
/**
|
||||
* A collection of ALTER actions for a single table
|
||||
*/
|
||||
class AlterTable
|
||||
{
|
||||
/**
|
||||
* The table
|
||||
*
|
||||
* @var \Phinx\Db\Table\Table
|
||||
*/
|
||||
protected Table $table;
|
||||
|
||||
/**
|
||||
* The list of actions to execute
|
||||
*
|
||||
* @var \Phinx\Db\Action\Action[]
|
||||
*/
|
||||
protected array $actions = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to change
|
||||
*/
|
||||
public function __construct(Table $table)
|
||||
{
|
||||
$this->table = $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds another action to the collection
|
||||
*
|
||||
* @param \Phinx\Db\Action\Action $action The action to add
|
||||
* @return void
|
||||
*/
|
||||
public function addAction(Action $action): void
|
||||
{
|
||||
$this->actions[] = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the table associated to this collection
|
||||
*
|
||||
* @return \Phinx\Db\Table\Table
|
||||
*/
|
||||
public function getTable(): Table
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with all collected actions
|
||||
*
|
||||
* @return \Phinx\Db\Action\Action[]
|
||||
*/
|
||||
public function getActions(): array
|
||||
{
|
||||
return $this->actions;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Plan;
|
||||
|
||||
use Phinx\Db\Action\Action;
|
||||
|
||||
/**
|
||||
* An intent is a collection of actions for many tables
|
||||
*/
|
||||
class Intent
|
||||
{
|
||||
/**
|
||||
* List of actions to be executed
|
||||
*
|
||||
* @var \Phinx\Db\Action\Action[]
|
||||
*/
|
||||
protected array $actions = [];
|
||||
|
||||
/**
|
||||
* Adds a new action to the collection
|
||||
*
|
||||
* @param \Phinx\Db\Action\Action $action The action to add
|
||||
* @return void
|
||||
*/
|
||||
public function addAction(Action $action): void
|
||||
{
|
||||
$this->actions[] = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the full list of actions
|
||||
*
|
||||
* @return \Phinx\Db\Action\Action[]
|
||||
*/
|
||||
public function getActions(): array
|
||||
{
|
||||
return $this->actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges another Intent object with this one
|
||||
*
|
||||
* @param \Phinx\Db\Plan\Intent $another The other intent to merge in
|
||||
* @return void
|
||||
*/
|
||||
public function merge(Intent $another): void
|
||||
{
|
||||
$this->actions = array_merge($this->actions, $another->getActions());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Plan;
|
||||
|
||||
use Phinx\Db\Table\Column;
|
||||
use Phinx\Db\Table\Index;
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
/**
|
||||
* Represents the collection of actions for creating a new table
|
||||
*/
|
||||
class NewTable
|
||||
{
|
||||
/**
|
||||
* The table to create
|
||||
*
|
||||
* @var \Phinx\Db\Table\Table
|
||||
*/
|
||||
protected Table $table;
|
||||
|
||||
/**
|
||||
* The list of columns to add
|
||||
*
|
||||
* @var \Phinx\Db\Table\Column[]
|
||||
*/
|
||||
protected array $columns = [];
|
||||
|
||||
/**
|
||||
* The list of indexes to create
|
||||
*
|
||||
* @var \Phinx\Db\Table\Index[]
|
||||
*/
|
||||
protected array $indexes = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to create
|
||||
*/
|
||||
public function __construct(Table $table)
|
||||
{
|
||||
$this->table = $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a column to the collection
|
||||
*
|
||||
* @param \Phinx\Db\Table\Column $column The column description
|
||||
* @return void
|
||||
*/
|
||||
public function addColumn(Column $column): void
|
||||
{
|
||||
$this->columns[] = $column;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an index to the collection
|
||||
*
|
||||
* @param \Phinx\Db\Table\Index $index The index description
|
||||
* @return void
|
||||
*/
|
||||
public function addIndex(Index $index): void
|
||||
{
|
||||
$this->indexes[] = $index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the table object associated to this collection
|
||||
*
|
||||
* @return \Phinx\Db\Table\Table
|
||||
*/
|
||||
public function getTable(): Table
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the columns collection
|
||||
*
|
||||
* @return \Phinx\Db\Table\Column[]
|
||||
*/
|
||||
public function getColumns(): array
|
||||
{
|
||||
return $this->columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the indexes collection
|
||||
*
|
||||
* @return \Phinx\Db\Table\Index[]
|
||||
*/
|
||||
public function getIndexes(): array
|
||||
{
|
||||
return $this->indexes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,493 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Plan;
|
||||
|
||||
use ArrayObject;
|
||||
use Phinx\Db\Action\AddColumn;
|
||||
use Phinx\Db\Action\AddForeignKey;
|
||||
use Phinx\Db\Action\AddIndex;
|
||||
use Phinx\Db\Action\ChangeColumn;
|
||||
use Phinx\Db\Action\ChangeComment;
|
||||
use Phinx\Db\Action\ChangePrimaryKey;
|
||||
use Phinx\Db\Action\CreateTable;
|
||||
use Phinx\Db\Action\DropForeignKey;
|
||||
use Phinx\Db\Action\DropIndex;
|
||||
use Phinx\Db\Action\DropTable;
|
||||
use Phinx\Db\Action\RemoveColumn;
|
||||
use Phinx\Db\Action\RenameColumn;
|
||||
use Phinx\Db\Action\RenameTable;
|
||||
use Phinx\Db\Adapter\AdapterInterface;
|
||||
use Phinx\Db\Plan\Solver\ActionSplitter;
|
||||
use Phinx\Db\Table\Table;
|
||||
|
||||
/**
|
||||
* A Plan takes an Intent and transforms int into a sequence of
|
||||
* instructions that can be correctly executed by an AdapterInterface.
|
||||
*
|
||||
* The main focus of Plan is to arrange the actions in the most efficient
|
||||
* way possible for the database.
|
||||
*/
|
||||
class Plan
|
||||
{
|
||||
/**
|
||||
* List of tables to be created
|
||||
*
|
||||
* @var \Phinx\Db\Plan\NewTable[]
|
||||
*/
|
||||
protected array $tableCreates = [];
|
||||
|
||||
/**
|
||||
* List of table updates
|
||||
*
|
||||
* @var \Phinx\Db\Plan\AlterTable[]
|
||||
*/
|
||||
protected array $tableUpdates = [];
|
||||
|
||||
/**
|
||||
* List of table removals or renames
|
||||
*
|
||||
* @var \Phinx\Db\Plan\AlterTable[]
|
||||
*/
|
||||
protected array $tableMoves = [];
|
||||
|
||||
/**
|
||||
* List of index additions or removals
|
||||
*
|
||||
* @var \Phinx\Db\Plan\AlterTable[]
|
||||
*/
|
||||
protected array $indexes = [];
|
||||
|
||||
/**
|
||||
* List of constraint additions or removals
|
||||
*
|
||||
* @var \Phinx\Db\Plan\AlterTable[]
|
||||
*/
|
||||
protected array $constraints = [];
|
||||
|
||||
/**
|
||||
* List of dropped columns
|
||||
*
|
||||
* @var \Phinx\Db\Plan\AlterTable[]
|
||||
*/
|
||||
protected array $columnRemoves = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \Phinx\Db\Plan\Intent $intent All the actions that should be executed
|
||||
*/
|
||||
public function __construct(Intent $intent)
|
||||
{
|
||||
$this->createPlan($intent->getActions());
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given Intent and creates the separate steps to execute
|
||||
*
|
||||
* @param \Phinx\Db\Action\Action[] $actions The actions to use for the plan
|
||||
* @return void
|
||||
*/
|
||||
protected function createPlan(array $actions): void
|
||||
{
|
||||
$this->gatherCreates($actions);
|
||||
$this->gatherUpdates($actions);
|
||||
$this->gatherTableMoves($actions);
|
||||
$this->gatherIndexes($actions);
|
||||
$this->gatherConstraints($actions);
|
||||
$this->resolveConflicts();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a nested list of all the steps to execute
|
||||
*
|
||||
* @return \Phinx\Db\Plan\AlterTable[][]
|
||||
*/
|
||||
protected function updatesSequence(): array
|
||||
{
|
||||
return [
|
||||
$this->tableUpdates,
|
||||
$this->constraints,
|
||||
$this->indexes,
|
||||
$this->columnRemoves,
|
||||
$this->tableMoves,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a nested list of all the steps to execute in inverse order
|
||||
*
|
||||
* @return \Phinx\Db\Plan\AlterTable[][]
|
||||
*/
|
||||
protected function inverseUpdatesSequence(): array
|
||||
{
|
||||
return [
|
||||
$this->constraints,
|
||||
$this->tableMoves,
|
||||
$this->indexes,
|
||||
$this->columnRemoves,
|
||||
$this->tableUpdates,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes this plan using the given AdapterInterface
|
||||
*
|
||||
* @param \Phinx\Db\Adapter\AdapterInterface $executor The executor object for the plan
|
||||
* @return void
|
||||
*/
|
||||
public function execute(AdapterInterface $executor): void
|
||||
{
|
||||
foreach ($this->tableCreates as $newTable) {
|
||||
$executor->createTable($newTable->getTable(), $newTable->getColumns(), $newTable->getIndexes());
|
||||
}
|
||||
|
||||
foreach ($this->updatesSequence() as $updates) {
|
||||
foreach ($updates as $update) {
|
||||
$executor->executeActions($update->getTable(), $update->getActions());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the inverse plan (rollback the actions) with the given AdapterInterface:w
|
||||
*
|
||||
* @param \Phinx\Db\Adapter\AdapterInterface $executor The executor object for the plan
|
||||
* @return void
|
||||
*/
|
||||
public function executeInverse(AdapterInterface $executor): void
|
||||
{
|
||||
foreach ($this->inverseUpdatesSequence() as $updates) {
|
||||
foreach ($updates as $update) {
|
||||
$executor->executeActions($update->getTable(), $update->getActions());
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->tableCreates as $newTable) {
|
||||
$executor->createTable($newTable->getTable(), $newTable->getColumns(), $newTable->getIndexes());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes certain actions from the plan if they are found to be conflicting or redundant.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function resolveConflicts(): void
|
||||
{
|
||||
foreach ($this->tableMoves as $alterTable) {
|
||||
foreach ($alterTable->getActions() as $action) {
|
||||
if ($action instanceof DropTable) {
|
||||
$this->tableUpdates = $this->forgetTable($action->getTable(), $this->tableUpdates);
|
||||
$this->constraints = $this->forgetTable($action->getTable(), $this->constraints);
|
||||
$this->indexes = $this->forgetTable($action->getTable(), $this->indexes);
|
||||
$this->columnRemoves = $this->forgetTable($action->getTable(), $this->columnRemoves);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Renaming a column and then changing the renamed column is something people do,
|
||||
// but it is a conflicting action. Luckily solving the conflict can be done by moving
|
||||
// the ChangeColumn action to another AlterTable.
|
||||
$splitter = new ActionSplitter(
|
||||
RenameColumn::class,
|
||||
ChangeColumn::class,
|
||||
function (RenameColumn $a, ChangeColumn $b) {
|
||||
return $a->getNewName() === $b->getColumnName();
|
||||
},
|
||||
);
|
||||
$tableUpdates = [];
|
||||
foreach ($this->tableUpdates as $update) {
|
||||
$tableUpdates = array_merge($tableUpdates, $splitter($update));
|
||||
}
|
||||
$this->tableUpdates = $tableUpdates;
|
||||
|
||||
// Dropping indexes used by foreign keys is a conflict, but one we can resolve
|
||||
// if the foreign key is also scheduled to be dropped. If we can find such a a case,
|
||||
// we force the execution of the index drop after the foreign key is dropped.
|
||||
// Changing constraint properties sometimes require dropping it and then
|
||||
// creating it again with the new stuff. Unfortunately, we have already bundled
|
||||
// everything together in as few AlterTable statements as we could, so we need to
|
||||
// resolve this conflict manually.
|
||||
$splitter = new ActionSplitter(
|
||||
DropForeignKey::class,
|
||||
AddForeignKey::class,
|
||||
function (DropForeignKey $a, AddForeignKey $b) {
|
||||
return $a->getForeignKey()->getColumns() === $b->getForeignKey()->getColumns();
|
||||
},
|
||||
);
|
||||
$constraints = [];
|
||||
foreach ($this->constraints as $constraint) {
|
||||
$constraints = array_merge(
|
||||
$constraints,
|
||||
$splitter($this->remapContraintAndIndexConflicts($constraint)),
|
||||
);
|
||||
}
|
||||
$this->constraints = $constraints;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all actions related to the given table and keeps the
|
||||
* rest
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to find in the list of actions
|
||||
* @param \Phinx\Db\Plan\AlterTable[] $actions The actions to transform
|
||||
* @return \Phinx\Db\Plan\AlterTable[] The list of actions without actions for the given table
|
||||
*/
|
||||
protected function forgetTable(Table $table, array $actions): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($actions as $action) {
|
||||
if ($action->getTable()->getName() === $table->getName()) {
|
||||
continue;
|
||||
}
|
||||
$result[] = $action;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds all DropForeignKey actions in an AlterTable and moves
|
||||
* all conflicting DropIndex action in `$this->indexes` into the
|
||||
* given AlterTable.
|
||||
*
|
||||
* @param \Phinx\Db\Plan\AlterTable $alter The collection of actions to inspect
|
||||
* @return \Phinx\Db\Plan\AlterTable The updated AlterTable object. This function
|
||||
* has the side effect of changing the `$this->indexes` property.
|
||||
*/
|
||||
protected function remapContraintAndIndexConflicts(AlterTable $alter): AlterTable
|
||||
{
|
||||
$newAlter = new AlterTable($alter->getTable());
|
||||
|
||||
foreach ($alter->getActions() as $action) {
|
||||
$newAlter->addAction($action);
|
||||
if ($action instanceof DropForeignKey) {
|
||||
[$this->indexes, $dropIndexActions] = $this->forgetDropIndex(
|
||||
$action->getTable(),
|
||||
$action->getForeignKey()->getColumns(),
|
||||
$this->indexes,
|
||||
);
|
||||
foreach ($dropIndexActions as $dropIndexAction) {
|
||||
$newAlter->addAction($dropIndexAction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $newAlter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes any DropIndex actions for the given table and exact columns
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to find in the list of actions
|
||||
* @param string[] $columns The column names to match
|
||||
* @param \Phinx\Db\Plan\AlterTable[] $actions The actions to transform
|
||||
* @return array A tuple containing the list of actions without actions for dropping the index
|
||||
* and a list of drop index actions that were removed.
|
||||
*/
|
||||
protected function forgetDropIndex(Table $table, array $columns, array $actions): array
|
||||
{
|
||||
$dropIndexActions = new ArrayObject();
|
||||
$indexes = array_map(function ($alter) use ($table, $columns, $dropIndexActions) {
|
||||
if ($alter->getTable()->getName() !== $table->getName()) {
|
||||
return $alter;
|
||||
}
|
||||
|
||||
$newAlter = new AlterTable($table);
|
||||
foreach ($alter->getActions() as $action) {
|
||||
if ($action instanceof DropIndex && $action->getIndex()->getColumns() === $columns) {
|
||||
$dropIndexActions->append($action);
|
||||
} else {
|
||||
$newAlter->addAction($action);
|
||||
}
|
||||
}
|
||||
|
||||
return $newAlter;
|
||||
}, $actions);
|
||||
|
||||
return [$indexes, $dropIndexActions->getArrayCopy()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes any RemoveColumn actions for the given table and exact columns
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table to find in the list of actions
|
||||
* @param string[] $columns The column names to match
|
||||
* @param \Phinx\Db\Plan\AlterTable[] $actions The actions to transform
|
||||
* @return array A tuple containing the list of actions without actions for removing the column
|
||||
* and a list of remove column actions that were removed.
|
||||
*/
|
||||
protected function forgetRemoveColumn(Table $table, array $columns, array $actions): array
|
||||
{
|
||||
$removeColumnActions = new ArrayObject();
|
||||
$indexes = array_map(function ($alter) use ($table, $columns, $removeColumnActions) {
|
||||
if ($alter->getTable()->getName() !== $table->getName()) {
|
||||
return $alter;
|
||||
}
|
||||
|
||||
$newAlter = new AlterTable($table);
|
||||
foreach ($alter->getActions() as $action) {
|
||||
if ($action instanceof RemoveColumn && in_array($action->getColumn()->getName(), $columns, true)) {
|
||||
$removeColumnActions->append($action);
|
||||
} else {
|
||||
$newAlter->addAction($action);
|
||||
}
|
||||
}
|
||||
|
||||
return $newAlter;
|
||||
}, $actions);
|
||||
|
||||
return [$indexes, $removeColumnActions->getArrayCopy()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects all table creation actions from the given intent
|
||||
*
|
||||
* @param \Phinx\Db\Action\Action[] $actions The actions to parse
|
||||
* @return void
|
||||
*/
|
||||
protected function gatherCreates(array $actions): void
|
||||
{
|
||||
foreach ($actions as $action) {
|
||||
if ($action instanceof CreateTable) {
|
||||
$this->tableCreates[$action->getTable()->getName()] = new NewTable($action->getTable());
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($actions as $action) {
|
||||
if (
|
||||
($action instanceof AddColumn || $action instanceof AddIndex)
|
||||
&& isset($this->tableCreates[$action->getTable()->getName()])
|
||||
) {
|
||||
$table = $action->getTable();
|
||||
|
||||
if ($action instanceof AddColumn) {
|
||||
$this->tableCreates[$table->getName()]->addColumn($action->getColumn());
|
||||
}
|
||||
|
||||
if ($action instanceof AddIndex) {
|
||||
$this->tableCreates[$table->getName()]->addIndex($action->getIndex());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects all alter table actions from the given intent
|
||||
*
|
||||
* @param \Phinx\Db\Action\Action[] $actions The actions to parse
|
||||
* @return void
|
||||
*/
|
||||
protected function gatherUpdates(array $actions): void
|
||||
{
|
||||
foreach ($actions as $action) {
|
||||
if (
|
||||
!($action instanceof AddColumn)
|
||||
&& !($action instanceof ChangeColumn)
|
||||
&& !($action instanceof RemoveColumn)
|
||||
&& !($action instanceof RenameColumn)
|
||||
) {
|
||||
continue;
|
||||
} elseif (isset($this->tableCreates[$action->getTable()->getName()])) {
|
||||
continue;
|
||||
}
|
||||
$table = $action->getTable();
|
||||
$name = $table->getName();
|
||||
|
||||
if ($action instanceof RemoveColumn) {
|
||||
if (!isset($this->columnRemoves[$name])) {
|
||||
$this->columnRemoves[$name] = new AlterTable($table);
|
||||
}
|
||||
$this->columnRemoves[$name]->addAction($action);
|
||||
} else {
|
||||
if (!isset($this->tableUpdates[$name])) {
|
||||
$this->tableUpdates[$name] = new AlterTable($table);
|
||||
}
|
||||
$this->tableUpdates[$name]->addAction($action);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects all alter table drop and renames from the given intent
|
||||
*
|
||||
* @param \Phinx\Db\Action\Action[] $actions The actions to parse
|
||||
* @return void
|
||||
*/
|
||||
protected function gatherTableMoves(array $actions): void
|
||||
{
|
||||
foreach ($actions as $action) {
|
||||
if (
|
||||
!($action instanceof DropTable)
|
||||
&& !($action instanceof RenameTable)
|
||||
&& !($action instanceof ChangePrimaryKey)
|
||||
&& !($action instanceof ChangeComment)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
$table = $action->getTable();
|
||||
$name = $table->getName();
|
||||
|
||||
if (!isset($this->tableMoves[$name])) {
|
||||
$this->tableMoves[$name] = new AlterTable($table);
|
||||
}
|
||||
|
||||
$this->tableMoves[$name]->addAction($action);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects all index creation and drops from the given intent
|
||||
*
|
||||
* @param \Phinx\Db\Action\Action[] $actions The actions to parse
|
||||
* @return void
|
||||
*/
|
||||
protected function gatherIndexes(array $actions): void
|
||||
{
|
||||
foreach ($actions as $action) {
|
||||
if (!($action instanceof AddIndex) && !($action instanceof DropIndex)) {
|
||||
continue;
|
||||
} elseif (isset($this->tableCreates[$action->getTable()->getName()])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$table = $action->getTable();
|
||||
$name = $table->getName();
|
||||
|
||||
if (!isset($this->indexes[$name])) {
|
||||
$this->indexes[$name] = new AlterTable($table);
|
||||
}
|
||||
|
||||
$this->indexes[$name]->addAction($action);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects all foreign key creation and drops from the given intent
|
||||
*
|
||||
* @param \Phinx\Db\Action\Action[] $actions The actions to parse
|
||||
* @return void
|
||||
*/
|
||||
protected function gatherConstraints(array $actions): void
|
||||
{
|
||||
foreach ($actions as $action) {
|
||||
if (!($action instanceof AddForeignKey || $action instanceof DropForeignKey)) {
|
||||
continue;
|
||||
}
|
||||
$table = $action->getTable();
|
||||
$name = $table->getName();
|
||||
|
||||
if (!isset($this->constraints[$name])) {
|
||||
$this->constraints[$name] = new AlterTable($table);
|
||||
}
|
||||
|
||||
$this->constraints[$name]->addAction($action);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Plan\Solver;
|
||||
|
||||
use Phinx\Db\Plan\AlterTable;
|
||||
|
||||
/**
|
||||
* A Plan takes an Intent and transforms it into a sequence of
|
||||
* instructions that can be correctly executed by an AdapterInterface.
|
||||
*
|
||||
* The main focus of Plan is to arrange the actions in the most efficient
|
||||
* way possible for the database.
|
||||
*/
|
||||
class ActionSplitter
|
||||
{
|
||||
/**
|
||||
* The fully qualified class name of the Action class to match for conflicts
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected string $conflictClass;
|
||||
|
||||
/**
|
||||
* The fully qualified class name of the Action class to match for conflicts, which
|
||||
* is the dual of $conflictClass. For example `AddColumn` and `DropColumn` are duals.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected string $conflictClassDual;
|
||||
|
||||
/**
|
||||
* A callback used to signal the actual presence of a conflict, that will be used to
|
||||
* partition the AlterTable into non-conflicting parts.
|
||||
*
|
||||
* The callback receives as first argument amn instance of $conflictClass and as second
|
||||
* argument an instance of $conflictClassDual
|
||||
*
|
||||
* @var callable
|
||||
*/
|
||||
protected $conflictFilter;
|
||||
|
||||
/**
|
||||
* Comstructor
|
||||
*
|
||||
* @param string $conflictClass The fully qualified class name of the Action class to match for conflicts
|
||||
* @param string $conflictClassDual The fully qualified class name of the Action class to match for conflicts,
|
||||
* which is the dual of $conflictClass. For example `AddColumn` and `DropColumn` are duals.
|
||||
* @param callable $conflictFilter The collection of actions to inspect
|
||||
*/
|
||||
public function __construct(string $conflictClass, string $conflictClassDual, callable $conflictFilter)
|
||||
{
|
||||
$this->conflictClass = $conflictClass;
|
||||
$this->conflictClassDual = $conflictClassDual;
|
||||
$this->conflictFilter = $conflictFilter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returs a sequence of AlterTable instructions that are non conflicting
|
||||
* based on the constructor parameters.
|
||||
*
|
||||
* @param \Phinx\Db\Plan\AlterTable $alter The collection of actions to inspect
|
||||
* @return \Phinx\Db\Plan\AlterTable[] A list of AlterTable that can be executed without
|
||||
* this type of conflict
|
||||
*/
|
||||
public function __invoke(AlterTable $alter): array
|
||||
{
|
||||
$conflictActions = array_filter($alter->getActions(), function ($action) {
|
||||
return $action instanceof $this->conflictClass;
|
||||
});
|
||||
|
||||
$originalAlter = new AlterTable($alter->getTable());
|
||||
$newAlter = new AlterTable($alter->getTable());
|
||||
|
||||
foreach ($alter->getActions() as $action) {
|
||||
if (!$action instanceof $this->conflictClassDual) {
|
||||
$originalAlter->addAction($action);
|
||||
continue;
|
||||
}
|
||||
|
||||
$found = false;
|
||||
$matches = $this->conflictFilter;
|
||||
foreach ($conflictActions as $ca) {
|
||||
if ($matches($ca, $action)) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($found) {
|
||||
$newAlter->addAction($action);
|
||||
} else {
|
||||
$originalAlter->addAction($action);
|
||||
}
|
||||
}
|
||||
|
||||
return [$originalAlter, $newAlter];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,727 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Phinx\Config\FeatureFlags;
|
||||
use Phinx\Db\Action\AddColumn;
|
||||
use Phinx\Db\Action\AddForeignKey;
|
||||
use Phinx\Db\Action\AddIndex;
|
||||
use Phinx\Db\Action\ChangeColumn;
|
||||
use Phinx\Db\Action\ChangeComment;
|
||||
use Phinx\Db\Action\ChangePrimaryKey;
|
||||
use Phinx\Db\Action\CreateTable;
|
||||
use Phinx\Db\Action\DropForeignKey;
|
||||
use Phinx\Db\Action\DropIndex;
|
||||
use Phinx\Db\Action\DropTable;
|
||||
use Phinx\Db\Action\RemoveColumn;
|
||||
use Phinx\Db\Action\RenameColumn;
|
||||
use Phinx\Db\Action\RenameTable;
|
||||
use Phinx\Db\Adapter\AdapterInterface;
|
||||
use Phinx\Db\Plan\Intent;
|
||||
use Phinx\Db\Plan\Plan;
|
||||
use Phinx\Db\Table\Column;
|
||||
use Phinx\Db\Table\Index;
|
||||
use Phinx\Db\Table\Table as TableValue;
|
||||
use Phinx\Util\Literal;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* This object is based loosely on: https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html.
|
||||
*/
|
||||
class Table
|
||||
{
|
||||
/**
|
||||
* @var \Phinx\Db\Table\Table
|
||||
*/
|
||||
protected TableValue $table;
|
||||
|
||||
/**
|
||||
* @var \Phinx\Db\Adapter\AdapterInterface|null
|
||||
*/
|
||||
protected ?AdapterInterface $adapter = null;
|
||||
|
||||
/**
|
||||
* @var \Phinx\Db\Plan\Intent
|
||||
*/
|
||||
protected Intent $actions;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $data = [];
|
||||
|
||||
/**
|
||||
* @param string $name Table Name
|
||||
* @param array<string, mixed> $options Options
|
||||
* @param \Phinx\Db\Adapter\AdapterInterface|null $adapter Database Adapter
|
||||
*/
|
||||
public function __construct(string $name, array $options = [], ?AdapterInterface $adapter = null)
|
||||
{
|
||||
$this->table = new TableValue($name, $options);
|
||||
$this->actions = new Intent();
|
||||
|
||||
if ($adapter !== null) {
|
||||
$this->setAdapter($adapter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->table->getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the table options.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function getOptions(): array
|
||||
{
|
||||
return $this->table->getOptions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the table name and options as an object
|
||||
*
|
||||
* @return \Phinx\Db\Table\Table
|
||||
*/
|
||||
public function getTable(): TableValue
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the database adapter.
|
||||
*
|
||||
* @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter
|
||||
* @return $this
|
||||
*/
|
||||
public function setAdapter(AdapterInterface $adapter)
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the database adapter.
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
* @return \Phinx\Db\Adapter\AdapterInterface
|
||||
*/
|
||||
public function getAdapter(): AdapterInterface
|
||||
{
|
||||
if (!$this->adapter) {
|
||||
throw new RuntimeException('There is no database adapter set yet, cannot proceed');
|
||||
}
|
||||
|
||||
return $this->adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the table have pending actions?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPendingActions(): bool
|
||||
{
|
||||
return count($this->actions->getActions()) > 0 || count($this->data) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the table exist?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function exists(): bool
|
||||
{
|
||||
return $this->getAdapter()->hasTable($this->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops the database table.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function drop()
|
||||
{
|
||||
$this->actions->addAction(new DropTable($this->table));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renames the database table.
|
||||
*
|
||||
* @param string $newTableName New Table Name
|
||||
* @return $this
|
||||
*/
|
||||
public function rename(string $newTableName)
|
||||
{
|
||||
$this->actions->addAction(new RenameTable($this->table, $newTableName));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the primary key of the database table.
|
||||
*
|
||||
* @param string|string[]|null $columns Column name(s) to belong to the primary key, or null to drop the key
|
||||
* @return $this
|
||||
*/
|
||||
public function changePrimaryKey(string|array|null $columns)
|
||||
{
|
||||
$this->actions->addAction(new ChangePrimaryKey($this->table, $columns));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if a primary key exists.
|
||||
*
|
||||
* @param string|string[] $columns Column(s)
|
||||
* @param string|null $constraint Constraint names
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPrimaryKey(string|array $columns, ?string $constraint = null): bool
|
||||
{
|
||||
return $this->getAdapter()->hasPrimaryKey($this->getName(), $columns, $constraint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the comment of the database table.
|
||||
*
|
||||
* @param string|null $comment New comment string, or null to drop the comment
|
||||
* @return $this
|
||||
*/
|
||||
public function changeComment(?string $comment)
|
||||
{
|
||||
$this->actions->addAction(new ChangeComment($this->table, $comment));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an array of the table columns.
|
||||
*
|
||||
* @return \Phinx\Db\Table\Column[]
|
||||
*/
|
||||
public function getColumns(): array
|
||||
{
|
||||
return $this->getAdapter()->getColumns($this->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a table column if it exists.
|
||||
*
|
||||
* @param string $name Column name
|
||||
* @return \Phinx\Db\Table\Column|null
|
||||
*/
|
||||
public function getColumn(string $name): ?Column
|
||||
{
|
||||
$columns = array_filter(
|
||||
$this->getColumns(),
|
||||
function ($column) use ($name) {
|
||||
return $column->getName() === $name;
|
||||
},
|
||||
);
|
||||
|
||||
return array_pop($columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an array of data to be inserted.
|
||||
*
|
||||
* @param array $data Data
|
||||
* @return $this
|
||||
*/
|
||||
public function setData(array $data)
|
||||
{
|
||||
$this->data = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the data waiting to be inserted.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getData(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets all of the pending data to be inserted
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function resetData(): void
|
||||
{
|
||||
$this->setData([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets all of the pending table changes.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function reset(): void
|
||||
{
|
||||
$this->actions = new Intent();
|
||||
$this->resetData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a table column.
|
||||
*
|
||||
* Type can be: string, text, integer, float, decimal, datetime, timestamp,
|
||||
* time, date, binary, boolean.
|
||||
*
|
||||
* Valid options can be: limit, default, null, precision or scale.
|
||||
*
|
||||
* @param string|\Phinx\Db\Table\Column $columnName Column Name
|
||||
* @param string|\Phinx\Util\Literal|null $type Column Type
|
||||
* @param array<string, mixed> $options Column Options
|
||||
* @throws \InvalidArgumentException
|
||||
* @return $this
|
||||
*/
|
||||
public function addColumn(string|Column $columnName, string|Literal|null $type = null, array $options = [])
|
||||
{
|
||||
assert($columnName instanceof Column || $type !== null);
|
||||
if ($columnName instanceof Column) {
|
||||
$action = new AddColumn($this->table, $columnName);
|
||||
} elseif ($type instanceof Literal) {
|
||||
$action = AddColumn::build($this->table, $columnName, $type, $options);
|
||||
} else {
|
||||
$action = new AddColumn($this->table, $this->getAdapter()->getColumnForType($columnName, $type, $options));
|
||||
}
|
||||
|
||||
// Delegate to Adapters to check column type
|
||||
if (!$this->getAdapter()->isValidColumnType($action->getColumn())) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'An invalid column type "%s" was specified for column "%s".',
|
||||
$action->getColumn()->getType(),
|
||||
$action->getColumn()->getName(),
|
||||
));
|
||||
}
|
||||
|
||||
$this->actions->addAction($action);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a table column.
|
||||
*
|
||||
* @param string $columnName Column Name
|
||||
* @return $this
|
||||
*/
|
||||
public function removeColumn(string $columnName)
|
||||
{
|
||||
$action = RemoveColumn::build($this->table, $columnName);
|
||||
$this->actions->addAction($action);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename a table column.
|
||||
*
|
||||
* @param string $oldName Old Column Name
|
||||
* @param string $newName New Column Name
|
||||
* @return $this
|
||||
*/
|
||||
public function renameColumn(string $oldName, string $newName)
|
||||
{
|
||||
$action = RenameColumn::build($this->table, $oldName, $newName);
|
||||
$this->actions->addAction($action);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change a table column type.
|
||||
*
|
||||
* @param string $columnName Column Name
|
||||
* @param string|\Phinx\Db\Table\Column|\Phinx\Util\Literal $newColumnType New Column Type
|
||||
* @param array<string, mixed> $options Options
|
||||
* @return $this
|
||||
*/
|
||||
public function changeColumn(string $columnName, string|Column|Literal $newColumnType, array $options = [])
|
||||
{
|
||||
if ($newColumnType instanceof Column) {
|
||||
$action = new ChangeColumn($this->table, $columnName, $newColumnType);
|
||||
} else {
|
||||
$action = ChangeColumn::build($this->table, $columnName, $newColumnType, $options);
|
||||
}
|
||||
$this->actions->addAction($action);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if a column exists.
|
||||
*
|
||||
* @param string $columnName Column Name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasColumn(string $columnName): bool
|
||||
{
|
||||
return $this->getAdapter()->hasColumn($this->getName(), $columnName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an index to a database table.
|
||||
*
|
||||
* In $options you can specify unique = true/false, and name (index name).
|
||||
*
|
||||
* @param string|array|\Phinx\Db\Table\Index $columns Table Column(s)
|
||||
* @param array<string, mixed> $options Index Options
|
||||
* @return $this
|
||||
*/
|
||||
public function addIndex(string|array|Index $columns, array $options = [])
|
||||
{
|
||||
$action = AddIndex::build($this->table, $columns, $options);
|
||||
$this->actions->addAction($action);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given index from a table.
|
||||
*
|
||||
* @param string|string[] $columns Columns
|
||||
* @return $this
|
||||
*/
|
||||
public function removeIndex(string|array $columns)
|
||||
{
|
||||
$action = DropIndex::build($this->table, is_string($columns) ? [$columns] : $columns);
|
||||
$this->actions->addAction($action);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given index identified by its name from a table.
|
||||
*
|
||||
* @param string $name Index name
|
||||
* @return $this
|
||||
*/
|
||||
public function removeIndexByName(string $name)
|
||||
{
|
||||
$action = DropIndex::buildFromName($this->table, $name);
|
||||
$this->actions->addAction($action);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an index exists.
|
||||
*
|
||||
* @param string|string[] $columns Columns
|
||||
* @return bool
|
||||
*/
|
||||
public function hasIndex(string|array $columns): bool
|
||||
{
|
||||
return $this->getAdapter()->hasIndex($this->getName(), $columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an index specified by name exists.
|
||||
*
|
||||
* @param string $indexName Index name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasIndexByName(string $indexName): bool
|
||||
{
|
||||
return $this->getAdapter()->hasIndexByName($this->getName(), $indexName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a foreign key to a database table.
|
||||
*
|
||||
* In $options you can specify on_delete|on_delete = cascade|no_action ..,
|
||||
* on_update, constraint = constraint name.
|
||||
*
|
||||
* @param string|string[] $columns Columns
|
||||
* @param string|\Phinx\Db\Table\Table $referencedTable Referenced Table
|
||||
* @param string|string[] $referencedColumns Referenced Columns
|
||||
* @param array<string, mixed> $options Options
|
||||
* @return $this
|
||||
*/
|
||||
public function addForeignKey(string|array $columns, string|TableValue $referencedTable, string|array $referencedColumns = ['id'], array $options = [])
|
||||
{
|
||||
$action = AddForeignKey::build($this->table, $columns, $referencedTable, $referencedColumns, $options);
|
||||
$this->actions->addAction($action);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a foreign key to a database table with a given name.
|
||||
*
|
||||
* In $options you can specify on_delete|on_delete = cascade|no_action ..,
|
||||
* on_update, constraint = constraint name.
|
||||
*
|
||||
* @param string $name The constraint name
|
||||
* @param string|string[] $columns Columns
|
||||
* @param string|\Phinx\Db\Table\Table $referencedTable Referenced Table
|
||||
* @param string|string[] $referencedColumns Referenced Columns
|
||||
* @param array<string, mixed> $options Options
|
||||
* @return $this
|
||||
*/
|
||||
public function addForeignKeyWithName(string $name, string|array $columns, string|TableValue $referencedTable, string|array $referencedColumns = ['id'], array $options = [])
|
||||
{
|
||||
$action = AddForeignKey::build(
|
||||
$this->table,
|
||||
$columns,
|
||||
$referencedTable,
|
||||
$referencedColumns,
|
||||
$options,
|
||||
$name,
|
||||
);
|
||||
$this->actions->addAction($action);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given foreign key from the table.
|
||||
*
|
||||
* @param string|string[] $columns Column(s)
|
||||
* @param string|null $constraint Constraint names
|
||||
* @return $this
|
||||
*/
|
||||
public function dropForeignKey(string|array $columns, ?string $constraint = null)
|
||||
{
|
||||
$action = DropForeignKey::build($this->table, $columns, $constraint);
|
||||
$this->actions->addAction($action);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if a foreign key exists.
|
||||
*
|
||||
* @param string|string[] $columns Column(s)
|
||||
* @param string|null $constraint Constraint names
|
||||
* @return bool
|
||||
*/
|
||||
public function hasForeignKey(string|array $columns, ?string $constraint = null): bool
|
||||
{
|
||||
return $this->getAdapter()->hasForeignKey($this->getName(), $columns, $constraint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add timestamp columns created_at and updated_at to the table.
|
||||
*
|
||||
* @param string|false|null $createdAt Alternate name for the created_at column
|
||||
* @param string|false|null $updatedAt Alternate name for the updated_at column
|
||||
* @param bool $withTimezone Whether to set the timezone option on the added columns
|
||||
* @return $this
|
||||
*/
|
||||
public function addTimestamps(string|false|null $createdAt = 'created_at', string|false|null $updatedAt = 'updated_at', bool $withTimezone = false)
|
||||
{
|
||||
$createdAt = $createdAt ?? 'created_at';
|
||||
$updatedAt = $updatedAt ?? 'updated_at';
|
||||
|
||||
if (!$createdAt && !$updatedAt) {
|
||||
throw new RuntimeException('Cannot set both created_at and updated_at columns to false');
|
||||
}
|
||||
|
||||
$columnType = FeatureFlags::$addTimestampsUseDateTime ? 'datetime' : 'timestamp';
|
||||
|
||||
if ($createdAt) {
|
||||
$this->addColumn($createdAt, $columnType, [
|
||||
'null' => false,
|
||||
'default' => 'CURRENT_TIMESTAMP',
|
||||
'update' => '',
|
||||
'timezone' => $withTimezone,
|
||||
]);
|
||||
}
|
||||
if ($updatedAt) {
|
||||
$this->addColumn($updatedAt, $columnType, [
|
||||
'null' => true,
|
||||
'default' => null,
|
||||
'update' => 'CURRENT_TIMESTAMP',
|
||||
'timezone' => $withTimezone,
|
||||
]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias that always sets $withTimezone to true
|
||||
*
|
||||
* @see addTimestamps
|
||||
* @param string|false|null $createdAt Alternate name for the created_at column
|
||||
* @param string|false|null $updatedAt Alternate name for the updated_at column
|
||||
* @return $this
|
||||
*/
|
||||
public function addTimestampsWithTimezone(string|false|null $createdAt = null, string|false|null $updatedAt = null)
|
||||
{
|
||||
$this->addTimestamps($createdAt, $updatedAt, true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert data into the table.
|
||||
*
|
||||
* @param array $data array of data in the form:
|
||||
* array(
|
||||
* array("col1" => "value1", "col2" => "anotherValue1"),
|
||||
* array("col2" => "value2", "col2" => "anotherValue2"),
|
||||
* )
|
||||
* or array("col1" => "value1", "col2" => "anotherValue1")
|
||||
* @return $this
|
||||
*/
|
||||
public function insert(array $data)
|
||||
{
|
||||
// handle array of array situations
|
||||
$keys = array_keys($data);
|
||||
$firstKey = array_shift($keys);
|
||||
if ($firstKey !== null && is_array($data[$firstKey])) {
|
||||
foreach ($data as $row) {
|
||||
$this->data[] = $row;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (count($data) > 0) {
|
||||
$this->data[] = $data;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a table from the object instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function create(): void
|
||||
{
|
||||
$this->executeActions(false);
|
||||
$this->saveData();
|
||||
$this->reset(); // reset pending changes
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a table from the object instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function update(): void
|
||||
{
|
||||
$this->executeActions(true);
|
||||
$this->saveData();
|
||||
$this->reset(); // reset pending changes
|
||||
}
|
||||
|
||||
/**
|
||||
* Commit the pending data waiting for insertion.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function saveData(): void
|
||||
{
|
||||
$rows = $this->getData();
|
||||
if (empty($rows)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$bulk = true;
|
||||
$row = current($rows);
|
||||
$c = array_keys($row);
|
||||
foreach ($this->getData() as $row) {
|
||||
$k = array_keys($row);
|
||||
if ($k != $c) {
|
||||
$bulk = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($bulk) {
|
||||
$this->getAdapter()->bulkinsert($this->table, $this->getData());
|
||||
} else {
|
||||
foreach ($this->getData() as $row) {
|
||||
$this->getAdapter()->insert($this->table, $row);
|
||||
}
|
||||
}
|
||||
|
||||
$this->resetData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Immediately truncates the table. This operation cannot be undone
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function truncate(): void
|
||||
{
|
||||
$this->getAdapter()->truncateTable($this->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Commits the table changes.
|
||||
*
|
||||
* If the table doesn't exist it is created otherwise it is updated.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function save(): void
|
||||
{
|
||||
if ($this->exists()) {
|
||||
$this->update(); // update the table
|
||||
} else {
|
||||
$this->create(); // create the table
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes all the pending actions for this table
|
||||
*
|
||||
* @param bool $exists Whether or not the table existed prior to executing this method
|
||||
* @return void
|
||||
*/
|
||||
protected function executeActions(bool $exists): void
|
||||
{
|
||||
// Renaming a table is tricky, specially when running a reversible migration
|
||||
// down. We will just assume the table already exists if the user commands a
|
||||
// table rename.
|
||||
if (!$exists) {
|
||||
foreach ($this->actions->getActions() as $action) {
|
||||
if ($action instanceof RenameTable) {
|
||||
$exists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If the table does not exist, the last command in the chain needs to be
|
||||
// a CreateTable action.
|
||||
if (!$exists) {
|
||||
$this->actions->addAction(new CreateTable($this->table));
|
||||
}
|
||||
|
||||
$plan = new Plan($this->actions);
|
||||
$plan->execute($this->getAdapter());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,807 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Table;
|
||||
|
||||
use Phinx\Config\FeatureFlags;
|
||||
use Phinx\Db\Adapter\AdapterInterface;
|
||||
use Phinx\Db\Adapter\PostgresAdapter;
|
||||
use Phinx\Util\Literal;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* This object is based loosely on: https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html.
|
||||
*/
|
||||
class Column
|
||||
{
|
||||
public const BIGINTEGER = AdapterInterface::PHINX_TYPE_BIG_INTEGER;
|
||||
public const SMALLINTEGER = AdapterInterface::PHINX_TYPE_SMALL_INTEGER;
|
||||
public const TINYINTEGER = AdapterInterface::PHINX_TYPE_TINY_INTEGER;
|
||||
public const BINARY = AdapterInterface::PHINX_TYPE_BINARY;
|
||||
public const BOOLEAN = AdapterInterface::PHINX_TYPE_BOOLEAN;
|
||||
public const CHAR = AdapterInterface::PHINX_TYPE_CHAR;
|
||||
public const DATE = AdapterInterface::PHINX_TYPE_DATE;
|
||||
public const DATETIME = AdapterInterface::PHINX_TYPE_DATETIME;
|
||||
public const DECIMAL = AdapterInterface::PHINX_TYPE_DECIMAL;
|
||||
public const FLOAT = AdapterInterface::PHINX_TYPE_FLOAT;
|
||||
public const INTEGER = AdapterInterface::PHINX_TYPE_INTEGER;
|
||||
public const STRING = AdapterInterface::PHINX_TYPE_STRING;
|
||||
public const TEXT = AdapterInterface::PHINX_TYPE_TEXT;
|
||||
public const TIME = AdapterInterface::PHINX_TYPE_TIME;
|
||||
public const TIMESTAMP = AdapterInterface::PHINX_TYPE_TIMESTAMP;
|
||||
public const UUID = AdapterInterface::PHINX_TYPE_UUID;
|
||||
public const BINARYUUID = AdapterInterface::PHINX_TYPE_BINARYUUID;
|
||||
/** MySQL-only column type */
|
||||
public const MEDIUMINTEGER = AdapterInterface::PHINX_TYPE_MEDIUM_INTEGER;
|
||||
/** MySQL-only column type */
|
||||
public const ENUM = AdapterInterface::PHINX_TYPE_ENUM;
|
||||
/** MySQL-only column type */
|
||||
public const SET = AdapterInterface::PHINX_TYPE_STRING;
|
||||
/** MySQL-only column type */
|
||||
public const BLOB = AdapterInterface::PHINX_TYPE_BLOB;
|
||||
/** MySQL-only column type */
|
||||
public const YEAR = AdapterInterface::PHINX_TYPE_YEAR;
|
||||
/** MySQL/Postgres-only column type */
|
||||
public const JSON = AdapterInterface::PHINX_TYPE_JSON;
|
||||
/** Postgres-only column type */
|
||||
public const JSONB = AdapterInterface::PHINX_TYPE_JSONB;
|
||||
/** Postgres-only column type */
|
||||
public const CIDR = AdapterInterface::PHINX_TYPE_CIDR;
|
||||
/** Postgres-only column type */
|
||||
public const INET = AdapterInterface::PHINX_TYPE_INET;
|
||||
/** Postgres-only column type */
|
||||
public const MACADDR = AdapterInterface::PHINX_TYPE_MACADDR;
|
||||
/** Postgres-only column type */
|
||||
public const INTERVAL = AdapterInterface::PHINX_TYPE_INTERVAL;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected ?string $name = null;
|
||||
|
||||
/**
|
||||
* @var string|\Phinx\Util\Literal
|
||||
*/
|
||||
protected string|Literal $type;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
protected ?int $limit = null;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $null = true;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected mixed $default = null;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $identity = false;
|
||||
|
||||
/**
|
||||
* Postgres-only column option for identity (always|default)
|
||||
*
|
||||
* @var ?string
|
||||
*/
|
||||
protected ?string $generated = PostgresAdapter::GENERATED_BY_DEFAULT;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
protected ?int $seed = null;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
protected ?int $increment = null;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
protected ?int $scale = null;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected ?string $after = null;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected ?string $update = null;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected ?string $comment = null;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $signed = true;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $timezone = false;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $properties = [];
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected ?string $collation = null;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected ?string $encoding = null;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
protected ?int $srid = null;
|
||||
|
||||
/**
|
||||
* @var array|null
|
||||
*/
|
||||
protected ?array $values = null;
|
||||
|
||||
/**
|
||||
* Column constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->null = FeatureFlags::$columnNullDefault;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column name.
|
||||
*
|
||||
* @param string $name Name
|
||||
* @return $this
|
||||
*/
|
||||
public function setName(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column name.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column type.
|
||||
*
|
||||
* @param string|\Phinx\Util\Literal $type Column type
|
||||
* @return $this
|
||||
*/
|
||||
public function setType(string|Literal $type)
|
||||
{
|
||||
$this->type = $type;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column type.
|
||||
*
|
||||
* @return string|\Phinx\Util\Literal
|
||||
*/
|
||||
public function getType(): string|Literal
|
||||
{
|
||||
if (!isset($this->type)) {
|
||||
throw new RuntimeException('Cannot access `type` it has not been set');
|
||||
}
|
||||
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column limit.
|
||||
*
|
||||
* @param int|null $limit Limit
|
||||
* @return $this
|
||||
*/
|
||||
public function setLimit(?int $limit)
|
||||
{
|
||||
$this->limit = $limit;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column limit.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getLimit(): ?int
|
||||
{
|
||||
return $this->limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether the column allows nulls.
|
||||
*
|
||||
* @param bool $null Null
|
||||
* @return $this
|
||||
*/
|
||||
public function setNull(bool $null)
|
||||
{
|
||||
$this->null = (bool)$null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether the column allows nulls.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getNull(): bool
|
||||
{
|
||||
return $this->null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the column allow nulls?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isNull(): bool
|
||||
{
|
||||
return $this->getNull();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default column value.
|
||||
*
|
||||
* @param mixed $default Default
|
||||
* @return $this
|
||||
*/
|
||||
public function setDefault(mixed $default)
|
||||
{
|
||||
$this->default = $default;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default column value.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDefault(): mixed
|
||||
{
|
||||
return $this->default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets generated option for identity columns. Ignored otherwise.
|
||||
*
|
||||
* @param string|null $generated Generated option
|
||||
* @return $this
|
||||
*/
|
||||
public function setGenerated(?string $generated)
|
||||
{
|
||||
$this->generated = $generated;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets generated option for identity columns. Null otherwise
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getGenerated(): ?string
|
||||
{
|
||||
return $this->generated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether or not the column is an identity column.
|
||||
*
|
||||
* @param bool $identity Identity
|
||||
* @return $this
|
||||
*/
|
||||
public function setIdentity(bool $identity)
|
||||
{
|
||||
$this->identity = $identity;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether or not the column is an identity column.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getIdentity(): bool
|
||||
{
|
||||
return $this->identity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the column an identity column?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isIdentity(): bool
|
||||
{
|
||||
return $this->getIdentity();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the name of the column to add this column after.
|
||||
*
|
||||
* @param string $after After
|
||||
* @return $this
|
||||
*/
|
||||
public function setAfter(string $after)
|
||||
{
|
||||
$this->after = $after;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the column to add this column after.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getAfter(): ?string
|
||||
{
|
||||
return $this->after;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the 'ON UPDATE' mysql column function.
|
||||
*
|
||||
* @param string $update On Update function
|
||||
* @return $this
|
||||
*/
|
||||
public function setUpdate(string $update)
|
||||
{
|
||||
$this->update = $update;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of the ON UPDATE column function.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getUpdate(): ?string
|
||||
{
|
||||
return $this->update;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the number precision for decimal or float column.
|
||||
*
|
||||
* For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
|
||||
* and the column could store value from -999.99 to 999.99.
|
||||
*
|
||||
* @param int|null $precision Number precision
|
||||
* @return $this
|
||||
*/
|
||||
public function setPrecision(?int $precision)
|
||||
{
|
||||
$this->setLimit($precision);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number precision for decimal or float column.
|
||||
*
|
||||
* For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
|
||||
* and the column could store value from -999.99 to 999.99.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getPrecision(): ?int
|
||||
{
|
||||
return $this->limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column identity increment.
|
||||
*
|
||||
* @param int $increment Number increment
|
||||
* @return $this
|
||||
*/
|
||||
public function setIncrement(int $increment)
|
||||
{
|
||||
$this->increment = $increment;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column identity increment.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getIncrement(): ?int
|
||||
{
|
||||
return $this->increment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column identity seed.
|
||||
*
|
||||
* @param int $seed Number seed
|
||||
* @return $this
|
||||
*/
|
||||
public function setSeed(int $seed)
|
||||
{
|
||||
$this->seed = $seed;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column identity seed.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSeed(): ?int
|
||||
{
|
||||
return $this->seed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the number scale for decimal or float column.
|
||||
*
|
||||
* For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
|
||||
* and the column could store value from -999.99 to 999.99.
|
||||
*
|
||||
* @param int|null $scale Number scale
|
||||
* @return $this
|
||||
*/
|
||||
public function setScale(?int $scale)
|
||||
{
|
||||
$this->scale = $scale;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number scale for decimal or float column.
|
||||
*
|
||||
* For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
|
||||
* and the column could store value from -999.99 to 999.99.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getScale(): ?int
|
||||
{
|
||||
return $this->scale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the number precision and scale for decimal or float column.
|
||||
*
|
||||
* For example `DECIMAL(5,2)`, 5 is the precision and 2 is the scale,
|
||||
* and the column could store value from -999.99 to 999.99.
|
||||
*
|
||||
* @param int $precision Number precision
|
||||
* @param int $scale Number scale
|
||||
* @return $this
|
||||
*/
|
||||
public function setPrecisionAndScale(int $precision, int $scale)
|
||||
{
|
||||
$this->setLimit($precision);
|
||||
$this->scale = $scale;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column comment.
|
||||
*
|
||||
* @param string|null $comment Comment
|
||||
* @return $this
|
||||
*/
|
||||
public function setComment(?string $comment)
|
||||
{
|
||||
$this->comment = $comment;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column comment.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getComment(): ?string
|
||||
{
|
||||
return $this->comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether field should be signed.
|
||||
*
|
||||
* @param bool $signed Signed
|
||||
* @return $this
|
||||
*/
|
||||
public function setSigned(bool $signed)
|
||||
{
|
||||
$this->signed = (bool)$signed;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether field should be signed.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getSigned(): bool
|
||||
{
|
||||
return $this->signed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should the column be signed?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSigned(): bool
|
||||
{
|
||||
return $this->getSigned();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether the field should have a timezone identifier.
|
||||
* Used for date/time columns only!
|
||||
*
|
||||
* @param bool $timezone Timezone
|
||||
* @return $this
|
||||
*/
|
||||
public function setTimezone(bool $timezone)
|
||||
{
|
||||
$this->timezone = (bool)$timezone;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether field has a timezone identifier.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getTimezone(): bool
|
||||
{
|
||||
return $this->timezone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should the column have a timezone?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isTimezone(): bool
|
||||
{
|
||||
return $this->getTimezone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets field properties.
|
||||
*
|
||||
* @param array $properties Properties
|
||||
* @return $this
|
||||
*/
|
||||
public function setProperties(array $properties)
|
||||
{
|
||||
$this->properties = $properties;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets field properties
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getProperties(): array
|
||||
{
|
||||
return $this->properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets field values.
|
||||
*
|
||||
* @param string[]|string $values Value(s)
|
||||
* @return $this
|
||||
*/
|
||||
public function setValues(array|string $values)
|
||||
{
|
||||
if (!is_array($values)) {
|
||||
$values = preg_split('/,\s*/', $values) ?: [];
|
||||
}
|
||||
$this->values = $values;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets field values
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function getValues(): ?array
|
||||
{
|
||||
return $this->values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column collation.
|
||||
*
|
||||
* @param string $collation Collation
|
||||
* @return $this
|
||||
*/
|
||||
public function setCollation(string $collation)
|
||||
{
|
||||
$this->collation = $collation;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column collation.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getCollation(): ?string
|
||||
{
|
||||
return $this->collation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column character set.
|
||||
*
|
||||
* @param string $encoding Encoding
|
||||
* @return $this
|
||||
*/
|
||||
public function setEncoding(string $encoding)
|
||||
{
|
||||
$this->encoding = $encoding;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column character set.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getEncoding(): ?string
|
||||
{
|
||||
return $this->encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the column SRID.
|
||||
*
|
||||
* @param int $srid SRID
|
||||
* @return $this
|
||||
*/
|
||||
public function setSrid(int $srid)
|
||||
{
|
||||
$this->srid = $srid;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the column SRID.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getSrid(): ?int
|
||||
{
|
||||
return $this->srid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all allowed options. Each option must have a corresponding `setFoo` method.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getValidOptions(): array
|
||||
{
|
||||
return [
|
||||
'limit',
|
||||
'default',
|
||||
'null',
|
||||
'identity',
|
||||
'scale',
|
||||
'after',
|
||||
'update',
|
||||
'comment',
|
||||
'signed',
|
||||
'timezone',
|
||||
'properties',
|
||||
'values',
|
||||
'collation',
|
||||
'encoding',
|
||||
'srid',
|
||||
'seed',
|
||||
'increment',
|
||||
'generated',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all aliased options. Each alias must reference a valid option.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getAliasedOptions(): array
|
||||
{
|
||||
return [
|
||||
'length' => 'limit',
|
||||
'precision' => 'limit',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method that maps an array of column options to this objects methods.
|
||||
*
|
||||
* @param array<string, mixed> $options Options
|
||||
* @throws \RuntimeException
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
$validOptions = $this->getValidOptions();
|
||||
$aliasOptions = $this->getAliasedOptions();
|
||||
|
||||
if (isset($options['identity']) && $options['identity'] && !isset($options['null'])) {
|
||||
$options['null'] = false;
|
||||
}
|
||||
|
||||
foreach ($options as $option => $value) {
|
||||
if (isset($aliasOptions[$option])) {
|
||||
// proxy alias -> option
|
||||
$option = $aliasOptions[$option];
|
||||
}
|
||||
|
||||
if (!in_array($option, $validOptions, true)) {
|
||||
throw new RuntimeException(sprintf('"%s" is not a valid column option.', $option));
|
||||
}
|
||||
|
||||
$method = 'set' . ucfirst($option);
|
||||
$this->$method($value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Table;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use RuntimeException;
|
||||
|
||||
class ForeignKey
|
||||
{
|
||||
public const CASCADE = 'CASCADE';
|
||||
public const RESTRICT = 'RESTRICT';
|
||||
public const SET_NULL = 'SET NULL';
|
||||
public const NO_ACTION = 'NO ACTION';
|
||||
public const DEFERRED = 'DEFERRABLE INITIALLY DEFERRED';
|
||||
public const IMMEDIATE = 'DEFERRABLE INITIALLY IMMEDIATE';
|
||||
public const NOT_DEFERRED = 'NOT DEFERRABLE';
|
||||
|
||||
/**
|
||||
* @var array<string>
|
||||
*/
|
||||
protected static array $validOptions = ['delete', 'update', 'constraint', 'deferrable'];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $columns = [];
|
||||
|
||||
/**
|
||||
* @var \Phinx\Db\Table\Table
|
||||
*/
|
||||
protected Table $referencedTable;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $referencedColumns = [];
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected ?string $onDelete = null;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected ?string $onUpdate = null;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected ?string $constraint = null;
|
||||
protected ?string $deferrableMode = null;
|
||||
|
||||
/**
|
||||
* Sets the foreign key columns.
|
||||
*
|
||||
* @param string[]|string $columns Columns
|
||||
* @return $this
|
||||
*/
|
||||
public function setColumns(array|string $columns)
|
||||
{
|
||||
$this->columns = is_string($columns) ? [$columns] : $columns;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the foreign key columns.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getColumns(): array
|
||||
{
|
||||
return $this->columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the foreign key referenced table.
|
||||
*
|
||||
* @param \Phinx\Db\Table\Table $table The table this KEY is pointing to
|
||||
* @return $this
|
||||
*/
|
||||
public function setReferencedTable(Table $table)
|
||||
{
|
||||
$this->referencedTable = $table;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the foreign key referenced table.
|
||||
*
|
||||
* @return \Phinx\Db\Table\Table
|
||||
*/
|
||||
public function getReferencedTable(): Table
|
||||
{
|
||||
if (!isset($this->referencedTable)) {
|
||||
throw new RuntimeException('Cannot access `referencedTable` it has not been set');
|
||||
}
|
||||
|
||||
return $this->referencedTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the foreign key referenced columns.
|
||||
*
|
||||
* @param string[] $referencedColumns Referenced columns
|
||||
* @return $this
|
||||
*/
|
||||
public function setReferencedColumns(array $referencedColumns)
|
||||
{
|
||||
$this->referencedColumns = $referencedColumns;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the foreign key referenced columns.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getReferencedColumns(): array
|
||||
{
|
||||
return $this->referencedColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets ON DELETE action for the foreign key.
|
||||
*
|
||||
* @param string $onDelete On Delete
|
||||
* @return $this
|
||||
*/
|
||||
public function setOnDelete(string $onDelete)
|
||||
{
|
||||
$this->onDelete = $this->normalizeAction($onDelete);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets ON DELETE action for the foreign key.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getOnDelete(): ?string
|
||||
{
|
||||
return $this->onDelete;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets ON UPDATE action for the foreign key.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getOnUpdate(): ?string
|
||||
{
|
||||
return $this->onUpdate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets ON UPDATE action for the foreign key.
|
||||
*
|
||||
* @param string $onUpdate On Update
|
||||
* @return $this
|
||||
*/
|
||||
public function setOnUpdate(string $onUpdate)
|
||||
{
|
||||
$this->onUpdate = $this->normalizeAction($onUpdate);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets constraint for the foreign key.
|
||||
*
|
||||
* @param string $constraint Constraint
|
||||
* @return $this
|
||||
*/
|
||||
public function setConstraint(string $constraint)
|
||||
{
|
||||
$this->constraint = $constraint;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets constraint name for the foreign key.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getConstraint(): ?string
|
||||
{
|
||||
return $this->constraint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets deferrable mode for the foreign key.
|
||||
*
|
||||
* @param string $deferrableMode Constraint
|
||||
* @return $this
|
||||
*/
|
||||
public function setDeferrableMode(string $deferrableMode)
|
||||
{
|
||||
$this->deferrableMode = $this->normalizeDeferrable($deferrableMode);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets deferrable mode for the foreign key.
|
||||
*/
|
||||
public function getDeferrableMode(): ?string
|
||||
{
|
||||
return $this->deferrableMode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method that maps an array of index options to this objects methods.
|
||||
*
|
||||
* @param array<string, mixed> $options Options
|
||||
* @throws \RuntimeException
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
foreach ($options as $option => $value) {
|
||||
if (!in_array($option, static::$validOptions, true)) {
|
||||
throw new RuntimeException(sprintf('"%s" is not a valid foreign key option.', $option));
|
||||
}
|
||||
|
||||
// handle $options['delete'] as $options['update']
|
||||
if ($option === 'delete') {
|
||||
$this->setOnDelete($value);
|
||||
} elseif ($option === 'update') {
|
||||
$this->setOnUpdate($value);
|
||||
} elseif ($option === 'deferrable') {
|
||||
$this->setDeferrableMode($value);
|
||||
} else {
|
||||
$method = 'set' . ucfirst($option);
|
||||
$this->$method($value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* From passed value checks if it's correct and fixes if needed
|
||||
*
|
||||
* @param string $action Action
|
||||
* @throws \InvalidArgumentException
|
||||
* @return string
|
||||
*/
|
||||
protected function normalizeAction(string $action): string
|
||||
{
|
||||
$constantName = 'static::' . str_replace(' ', '_', strtoupper(trim($action)));
|
||||
if (!defined($constantName)) {
|
||||
throw new InvalidArgumentException('Unknown action passed: ' . $action);
|
||||
}
|
||||
|
||||
return constant($constantName);
|
||||
}
|
||||
|
||||
/**
|
||||
* From passed value checks if it's correct and fixes if needed
|
||||
*
|
||||
* @param string $deferrable Deferrable
|
||||
* @throws \InvalidArgumentException
|
||||
* @return string
|
||||
*/
|
||||
protected function normalizeDeferrable(string $deferrable): string
|
||||
{
|
||||
$mapping = [
|
||||
'DEFERRED' => ForeignKey::DEFERRED,
|
||||
'IMMEDIATE' => ForeignKey::IMMEDIATE,
|
||||
'NOT DEFERRED' => ForeignKey::NOT_DEFERRED,
|
||||
ForeignKey::DEFERRED => ForeignKey::DEFERRED,
|
||||
ForeignKey::IMMEDIATE => ForeignKey::IMMEDIATE,
|
||||
ForeignKey::NOT_DEFERRED => ForeignKey::NOT_DEFERRED,
|
||||
];
|
||||
$normalized = strtoupper(str_replace('_', ' ', $deferrable));
|
||||
if (array_key_exists($normalized, $mapping)) {
|
||||
return $mapping[$normalized];
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('Unknown deferrable passed: ' . $deferrable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Table;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class Index
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const UNIQUE = 'unique';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const INDEX = 'index';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const FULLTEXT = 'fulltext';
|
||||
|
||||
/**
|
||||
* @var string[]|null
|
||||
*/
|
||||
protected ?array $columns = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $type = self::INDEX;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected ?string $name = null;
|
||||
|
||||
/**
|
||||
* @var int|array|null
|
||||
*/
|
||||
protected int|array|null $limit = null;
|
||||
|
||||
/**
|
||||
* @var string[]|null
|
||||
*/
|
||||
protected ?array $order = null;
|
||||
|
||||
/**
|
||||
* @var string[]|null
|
||||
*/
|
||||
protected ?array $includedColumns = null;
|
||||
|
||||
/**
|
||||
* Sets the index columns.
|
||||
*
|
||||
* @param string|string[] $columns Columns
|
||||
* @return $this
|
||||
*/
|
||||
public function setColumns(string|array $columns)
|
||||
{
|
||||
$this->columns = is_string($columns) ? [$columns] : $columns;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the index columns.
|
||||
*
|
||||
* @return string[]|null
|
||||
*/
|
||||
public function getColumns(): ?array
|
||||
{
|
||||
return $this->columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the index type.
|
||||
*
|
||||
* @param string $type Type
|
||||
* @return $this
|
||||
*/
|
||||
public function setType(string $type)
|
||||
{
|
||||
$this->type = $type;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the index type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the index name.
|
||||
*
|
||||
* @param string $name Name
|
||||
* @return $this
|
||||
*/
|
||||
public function setName(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the index name.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the index limit.
|
||||
*
|
||||
* @param int|array $limit limit value or array of limit value
|
||||
* @return $this
|
||||
*/
|
||||
public function setLimit(int|array $limit)
|
||||
{
|
||||
$this->limit = $limit;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the index limit.
|
||||
*
|
||||
* @return int|array|null
|
||||
*/
|
||||
public function getLimit(): int|array|null
|
||||
{
|
||||
return $this->limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the index columns sort order.
|
||||
*
|
||||
* @param string[] $order column name sort order key value pair
|
||||
* @return $this
|
||||
*/
|
||||
public function setOrder(array $order)
|
||||
{
|
||||
$this->order = $order;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the index columns sort order.
|
||||
*
|
||||
* @return string[]|null
|
||||
*/
|
||||
public function getOrder(): ?array
|
||||
{
|
||||
return $this->order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the index included columns.
|
||||
*
|
||||
* @param string[] $includedColumns Columns
|
||||
* @return $this
|
||||
*/
|
||||
public function setInclude(array $includedColumns)
|
||||
{
|
||||
$this->includedColumns = $includedColumns;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the index included columns.
|
||||
*
|
||||
* @return string[]|null
|
||||
*/
|
||||
public function getInclude(): ?array
|
||||
{
|
||||
return $this->includedColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method that maps an array of index options to this objects methods.
|
||||
*
|
||||
* @param array<string, mixed> $options Options
|
||||
* @throws \RuntimeException
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
// Valid Options
|
||||
$validOptions = ['type', 'unique', 'name', 'limit', 'order', 'include'];
|
||||
foreach ($options as $option => $value) {
|
||||
if (!in_array($option, $validOptions, true)) {
|
||||
throw new RuntimeException(sprintf('"%s" is not a valid index option.', $option));
|
||||
}
|
||||
|
||||
// handle $options['unique']
|
||||
if (strcasecmp($option, self::UNIQUE) === 0) {
|
||||
if ((bool)$value) {
|
||||
$this->setType(self::UNIQUE);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$method = 'set' . ucfirst($option);
|
||||
$this->$method($value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Table;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class Table
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $name;
|
||||
|
||||
/**
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
protected array $options;
|
||||
|
||||
/**
|
||||
* @param string $name The table name
|
||||
* @param array<string, mixed> $options The creation options for this table
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __construct(string $name, array $options = [])
|
||||
{
|
||||
if (empty($name)) {
|
||||
throw new InvalidArgumentException('Cannot use an empty table name');
|
||||
}
|
||||
|
||||
$this->name = $name;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the table name.
|
||||
*
|
||||
* @param string $name The name of the table
|
||||
* @return $this
|
||||
*/
|
||||
public function setName(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the table options
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function getOptions(): array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the table options
|
||||
*
|
||||
* @param array<string, mixed> $options The options for the table creation
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
$this->options = $options;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Db\Util;
|
||||
|
||||
/**
|
||||
* Contains all the information for running an ALTER command for a table,
|
||||
* and any post-steps required after the fact.
|
||||
*/
|
||||
class AlterInstructions
|
||||
{
|
||||
/**
|
||||
* @var string[] The SQL snippets to be added to an ALTER instruction
|
||||
*/
|
||||
protected array $alterParts = [];
|
||||
|
||||
/**
|
||||
* @var (string|callable)[] The SQL commands to be executed after the ALTER instruction
|
||||
*/
|
||||
protected array $postSteps = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string[] $alterParts SQL snippets to be added to a single ALTER instruction per table
|
||||
* @param (string|callable)[] $postSteps SQL commands to be executed after the ALTER instruction
|
||||
*/
|
||||
public function __construct(array $alterParts = [], array $postSteps = [])
|
||||
{
|
||||
$this->alterParts = $alterParts;
|
||||
$this->postSteps = $postSteps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds another part to the single ALTER instruction
|
||||
*
|
||||
* @param string $part The SQL snipped to add as part of the ALTER instruction
|
||||
* @return void
|
||||
*/
|
||||
public function addAlter(string $part): void
|
||||
{
|
||||
$this->alterParts[] = $part;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a SQL command to be executed after the ALTER instruction.
|
||||
* This method allows a callable, with will get an empty array as state
|
||||
* for the first time and will pass the return value of the callable to
|
||||
* the next callable, if present.
|
||||
*
|
||||
* This allows to keep a single state across callbacks.
|
||||
*
|
||||
* @param string|callable $sql The SQL to run after, or a callable to execute
|
||||
* @return void
|
||||
*/
|
||||
public function addPostStep(string|callable $sql): void
|
||||
{
|
||||
$this->postSteps[] = $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the alter SQL snippets
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getAlterParts(): array
|
||||
{
|
||||
return $this->alterParts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the SQL commands to run after the ALTER instruction
|
||||
*
|
||||
* @return (string|callable)[]
|
||||
*/
|
||||
public function getPostSteps(): array
|
||||
{
|
||||
return $this->postSteps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges another AlterInstructions object to this one
|
||||
*
|
||||
* @param \Phinx\Db\Util\AlterInstructions $other The other collection of instructions to merge in
|
||||
* @return void
|
||||
*/
|
||||
public function merge(AlterInstructions $other): void
|
||||
{
|
||||
$this->alterParts = array_merge($this->alterParts, $other->getAlterParts());
|
||||
$this->postSteps = array_merge($this->postSteps, $other->getPostSteps());
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the ALTER instruction and all of the post steps.
|
||||
*
|
||||
* @param string $alterTemplate The template for the alter instruction
|
||||
* @param callable $executor The function to be used to execute all instructions
|
||||
* @return void
|
||||
*/
|
||||
public function execute(string $alterTemplate, callable $executor): void
|
||||
{
|
||||
if ($this->alterParts) {
|
||||
$alter = sprintf($alterTemplate, implode(', ', $this->alterParts));
|
||||
$executor($alter);
|
||||
}
|
||||
|
||||
$state = [];
|
||||
|
||||
foreach ($this->postSteps as $instruction) {
|
||||
if (is_callable($instruction)) {
|
||||
$state = $instruction($state);
|
||||
continue;
|
||||
}
|
||||
|
||||
$executor($instruction);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,396 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Migration;
|
||||
|
||||
use Cake\Database\Query;
|
||||
use Cake\Database\Query\DeleteQuery;
|
||||
use Cake\Database\Query\InsertQuery;
|
||||
use Cake\Database\Query\SelectQuery;
|
||||
use Cake\Database\Query\UpdateQuery;
|
||||
use Phinx\Db\Adapter\AdapterInterface;
|
||||
use Phinx\Db\Table;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Abstract Migration Class.
|
||||
*
|
||||
* It is expected that the migrations you write extend from this class.
|
||||
*
|
||||
* This abstract class proxies the various database methods to your specified
|
||||
* adapter.
|
||||
*/
|
||||
abstract class AbstractMigration implements MigrationInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $environment;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected int $version;
|
||||
|
||||
/**
|
||||
* @var \Phinx\Db\Adapter\AdapterInterface|null
|
||||
*/
|
||||
protected ?AdapterInterface $adapter = null;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\Console\Output\OutputInterface|null
|
||||
*/
|
||||
protected ?OutputInterface $output = null;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\Console\Input\InputInterface|null
|
||||
*/
|
||||
protected ?InputInterface $input = null;
|
||||
|
||||
/**
|
||||
* Whether this migration is being applied or reverted
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $isMigratingUp = true;
|
||||
|
||||
/**
|
||||
* List of all the table objects created by this migration
|
||||
*
|
||||
* @var array<\Phinx\Db\Table>
|
||||
*/
|
||||
protected array $tables = [];
|
||||
|
||||
/**
|
||||
* @param string $environment Environment Detected
|
||||
* @param int $version Migration Version
|
||||
* @param \Symfony\Component\Console\Input\InputInterface|null $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface|null $output Output
|
||||
*/
|
||||
final public function __construct(string $environment, int $version, ?InputInterface $input = null, ?OutputInterface $output = null)
|
||||
{
|
||||
$this->validateVersion($version);
|
||||
|
||||
$this->environment = $environment;
|
||||
$this->version = $version;
|
||||
|
||||
if ($input !== null) {
|
||||
$this->setInput($input);
|
||||
}
|
||||
|
||||
if ($output !== null) {
|
||||
$this->setOutput($output);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setAdapter(AdapterInterface $adapter): MigrationInterface
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getAdapter(): AdapterInterface
|
||||
{
|
||||
if (!isset($this->adapter)) {
|
||||
throw new RuntimeException('Cannot access `adapter` it has not been set');
|
||||
}
|
||||
|
||||
return $this->adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setInput(InputInterface $input): MigrationInterface
|
||||
{
|
||||
$this->input = $input;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getInput(): ?InputInterface
|
||||
{
|
||||
return $this->input;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setOutput(OutputInterface $output): MigrationInterface
|
||||
{
|
||||
$this->output = $output;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOutput(): ?OutputInterface
|
||||
{
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return static::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getEnvironment(): string
|
||||
{
|
||||
return $this->environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setVersion($version): MigrationInterface
|
||||
{
|
||||
$this->version = $version;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getVersion(): int
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setMigratingUp(bool $isMigratingUp): MigrationInterface
|
||||
{
|
||||
$this->isMigratingUp = $isMigratingUp;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isMigratingUp(): bool
|
||||
{
|
||||
return $this->isMigratingUp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function execute(string $sql, array $params = []): int
|
||||
{
|
||||
return $this->getAdapter()->execute($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function query(string $sql, array $params = []): mixed
|
||||
{
|
||||
return $this->getAdapter()->query($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getQueryBuilder(string $type): Query
|
||||
{
|
||||
return $this->getAdapter()->getQueryBuilder($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getSelectBuilder(): SelectQuery
|
||||
{
|
||||
return $this->getAdapter()->getSelectBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getInsertBuilder(): InsertQuery
|
||||
{
|
||||
return $this->getAdapter()->getInsertBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getUpdateBuilder(): UpdateQuery
|
||||
{
|
||||
return $this->getAdapter()->getUpdateBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getDeleteBuilder(): DeleteQuery
|
||||
{
|
||||
return $this->getAdapter()->getDeleteBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function fetchRow(string $sql): array|false
|
||||
{
|
||||
return $this->getAdapter()->fetchRow($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function fetchAll(string $sql): array
|
||||
{
|
||||
return $this->getAdapter()->fetchAll($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createDatabase(string $name, array $options): void
|
||||
{
|
||||
$this->getAdapter()->createDatabase($name, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function dropDatabase(string $name): void
|
||||
{
|
||||
$this->getAdapter()->dropDatabase($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createSchema(string $name): void
|
||||
{
|
||||
$this->getAdapter()->createSchema($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function dropSchema(string $name): void
|
||||
{
|
||||
$this->getAdapter()->dropSchema($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasTable(string $tableName): bool
|
||||
{
|
||||
return $this->getAdapter()->hasTable($tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function table(string $tableName, array $options = []): Table
|
||||
{
|
||||
$table = new Table($tableName, $options, $this->getAdapter());
|
||||
$this->tables[] = $table;
|
||||
|
||||
return $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform checks on the migration, print a warning
|
||||
* if there are potential problems.
|
||||
*
|
||||
* Right now, the only check is if there is both a `change()` and
|
||||
* an `up()` or a `down()` method.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function preFlightCheck(): void
|
||||
{
|
||||
if (method_exists($this, MigrationInterface::CHANGE)) {
|
||||
if (
|
||||
method_exists($this, MigrationInterface::UP) ||
|
||||
method_exists($this, MigrationInterface::DOWN)
|
||||
) {
|
||||
$this->output->writeln(sprintf(
|
||||
'<comment>warning</comment> Migration contains both change() and up()/down() methods. <options=bold>Ignoring up() and down()</>.',
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform checks on the migration after completion
|
||||
*
|
||||
* Right now, the only check is whether all changes were committed
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
* @return void
|
||||
*/
|
||||
public function postFlightCheck(): void
|
||||
{
|
||||
foreach ($this->tables as $table) {
|
||||
if ($table->hasPendingActions()) {
|
||||
throw new RuntimeException(sprintf('Migration %s_%s has pending actions after execution!', $this->getVersion(), $this->getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if the migration should be executed.
|
||||
*
|
||||
* Returns true by default.
|
||||
*
|
||||
* You can use this to prevent a migration from executing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldExecute(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes sure the version int is within range for valid datetime.
|
||||
* This is required to have a meaningful order in the overview.
|
||||
*
|
||||
* @param int $version Version
|
||||
* @return void
|
||||
*/
|
||||
protected function validateVersion(int $version): void
|
||||
{
|
||||
$length = strlen((string)$version);
|
||||
if ($length === 14) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new RuntimeException('Invalid version `' . $version . '`, should be in format `YYYYMMDDHHMMSS` (length of 14).');
|
||||
}
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Migration;
|
||||
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
abstract class AbstractTemplateCreation implements CreationInterface
|
||||
{
|
||||
/**
|
||||
* @var \Symfony\Component\Console\Input\InputInterface
|
||||
*/
|
||||
protected InputInterface $input;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\Console\Output\OutputInterface
|
||||
*/
|
||||
protected OutputInterface $output;
|
||||
|
||||
/**
|
||||
* @param \Symfony\Component\Console\Input\InputInterface|null $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface|null $output Output
|
||||
*/
|
||||
public function __construct(?InputInterface $input = null, ?OutputInterface $output = null)
|
||||
{
|
||||
if ($input !== null) {
|
||||
$this->setInput($input);
|
||||
}
|
||||
if ($output !== null) {
|
||||
$this->setOutput($output);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getInput(): InputInterface
|
||||
{
|
||||
return $this->input;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setInput(InputInterface $input): CreationInterface
|
||||
{
|
||||
$this->input = $input;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOutput(): OutputInterface
|
||||
{
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setOutput(OutputInterface $output): CreationInterface
|
||||
{
|
||||
$this->output = $output;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Migration;
|
||||
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Migration interface
|
||||
*/
|
||||
interface CreationInterface
|
||||
{
|
||||
/**
|
||||
* @param \Symfony\Component\Console\Input\InputInterface|null $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface|null $output Output
|
||||
*/
|
||||
public function __construct(?InputInterface $input = null, ?OutputInterface $output = null);
|
||||
|
||||
/**
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @return $this
|
||||
*/
|
||||
public function setInput(InputInterface $input);
|
||||
|
||||
/**
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @return $this
|
||||
*/
|
||||
public function setOutput(OutputInterface $output);
|
||||
|
||||
/**
|
||||
* @return \Symfony\Component\Console\Input\InputInterface
|
||||
*/
|
||||
public function getInput(): InputInterface;
|
||||
|
||||
/**
|
||||
* @return \Symfony\Component\Console\Output\OutputInterface
|
||||
*/
|
||||
public function getOutput(): OutputInterface;
|
||||
|
||||
/**
|
||||
* Get the migration template.
|
||||
*
|
||||
* This will be the content that Phinx will amend to generate the migration file.
|
||||
*
|
||||
* @return string The content of the template for Phinx to amend.
|
||||
*/
|
||||
public function getMigrationTemplate(): string;
|
||||
|
||||
/**
|
||||
* Post Migration Creation.
|
||||
*
|
||||
* Once the migration file has been created, this method will be called, allowing any additional
|
||||
* processing, specific to the template to be performed.
|
||||
*
|
||||
* @param string $migrationFilename The name of the newly created migration.
|
||||
* @param string $className The class name.
|
||||
* @param string $baseClassName The name of the base class.
|
||||
* @return void
|
||||
*/
|
||||
public function postMigrationCreation(string $migrationFilename, string $className, string $baseClassName): void;
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Migration;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Exception class thrown when migrations cannot be reversed using the 'change'
|
||||
* feature.
|
||||
*/
|
||||
class IrreversibleMigrationException extends Exception
|
||||
{
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,399 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Migration\Manager;
|
||||
|
||||
use PDO;
|
||||
use Phinx\Db\Adapter\AdapterFactory;
|
||||
use Phinx\Db\Adapter\AdapterInterface;
|
||||
use Phinx\Migration\MigrationInterface;
|
||||
use Phinx\Seed\SeedInterface;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class Environment
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $name;
|
||||
|
||||
/**
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
protected array $options;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\Console\Input\InputInterface|null
|
||||
*/
|
||||
protected ?InputInterface $input = null;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\Console\Output\OutputInterface|null
|
||||
*/
|
||||
protected ?OutputInterface $output = null;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected int $currentVersion;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $schemaTableName = 'phinxlog';
|
||||
|
||||
/**
|
||||
* @var \Phinx\Db\Adapter\AdapterInterface
|
||||
*/
|
||||
protected AdapterInterface $adapter;
|
||||
|
||||
/**
|
||||
* @param string $name Environment Name
|
||||
* @param array<string, mixed> $options Options
|
||||
*/
|
||||
public function __construct(string $name, array $options)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the specified migration on this environment.
|
||||
*
|
||||
* @param \Phinx\Migration\MigrationInterface $migration Migration
|
||||
* @param string $direction Direction
|
||||
* @param bool $fake flag that if true, we just record running the migration, but not actually do the migration
|
||||
* @return void
|
||||
*/
|
||||
public function executeMigration(MigrationInterface $migration, string $direction = MigrationInterface::UP, bool $fake = false): void
|
||||
{
|
||||
$direction = $direction === MigrationInterface::UP ? MigrationInterface::UP : MigrationInterface::DOWN;
|
||||
$migration->setMigratingUp($direction === MigrationInterface::UP);
|
||||
|
||||
$startTime = time();
|
||||
$migration->setAdapter($this->getAdapter());
|
||||
|
||||
$migration->preFlightCheck();
|
||||
|
||||
if (method_exists($migration, MigrationInterface::INIT)) {
|
||||
$migration->{MigrationInterface::INIT}();
|
||||
}
|
||||
|
||||
// begin the transaction if the adapter supports it
|
||||
if ($this->getAdapter()->hasTransactions()) {
|
||||
$this->getAdapter()->beginTransaction();
|
||||
}
|
||||
|
||||
if (!$fake) {
|
||||
// Run the migration
|
||||
if (method_exists($migration, MigrationInterface::CHANGE)) {
|
||||
if ($direction === MigrationInterface::DOWN) {
|
||||
// Create an instance of the ProxyAdapter so we can record all
|
||||
// of the migration commands for reverse playback
|
||||
|
||||
/** @var \Phinx\Db\Adapter\ProxyAdapter $proxyAdapter */
|
||||
$proxyAdapter = AdapterFactory::instance()
|
||||
->getWrapper('proxy', $this->getAdapter());
|
||||
$migration->setAdapter($proxyAdapter);
|
||||
$migration->{MigrationInterface::CHANGE}();
|
||||
$proxyAdapter->executeInvertedCommands();
|
||||
$migration->setAdapter($this->getAdapter());
|
||||
} else {
|
||||
$migration->{MigrationInterface::CHANGE}();
|
||||
}
|
||||
} else {
|
||||
$migration->{$direction}();
|
||||
}
|
||||
}
|
||||
|
||||
// Record it in the database
|
||||
$this->getAdapter()->migrated($migration, $direction, date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', time()));
|
||||
|
||||
$migration->postFlightCheck();
|
||||
|
||||
// commit the transaction if the adapter supports it
|
||||
if ($this->getAdapter()->hasTransactions()) {
|
||||
$this->getAdapter()->commitTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the specified seeder on this environment.
|
||||
*
|
||||
* @param \Phinx\Seed\SeedInterface $seed Seed
|
||||
* @return void
|
||||
*/
|
||||
public function executeSeed(SeedInterface $seed): void
|
||||
{
|
||||
$seed->setAdapter($this->getAdapter());
|
||||
if (method_exists($seed, SeedInterface::INIT)) {
|
||||
$seed->{SeedInterface::INIT}();
|
||||
}
|
||||
|
||||
// begin the transaction if the adapter supports it
|
||||
if ($this->getAdapter()->hasTransactions()) {
|
||||
$this->getAdapter()->beginTransaction();
|
||||
}
|
||||
|
||||
// Run the seeder
|
||||
if (method_exists($seed, SeedInterface::RUN)) {
|
||||
$seed->{SeedInterface::RUN}();
|
||||
}
|
||||
|
||||
// commit the transaction if the adapter supports it
|
||||
if ($this->getAdapter()->hasTransactions()) {
|
||||
$this->getAdapter()->commitTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the environment's name.
|
||||
*
|
||||
* @param string $name Environment Name
|
||||
* @return $this
|
||||
*/
|
||||
public function setName(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the environment name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the environment's options.
|
||||
*
|
||||
* @param array<string, mixed> $options Environment Options
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
$this->options = $options;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the environment's options.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function getOptions(): array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the console input.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @return $this
|
||||
*/
|
||||
public function setInput(InputInterface $input)
|
||||
{
|
||||
$this->input = $input;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the console input.
|
||||
*
|
||||
* @return \Symfony\Component\Console\Input\InputInterface|null
|
||||
*/
|
||||
public function getInput(): ?InputInterface
|
||||
{
|
||||
return $this->input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the console output.
|
||||
*
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @return $this
|
||||
*/
|
||||
public function setOutput(OutputInterface $output)
|
||||
{
|
||||
$this->output = $output;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the console output.
|
||||
*
|
||||
* @return \Symfony\Component\Console\Output\OutputInterface|null
|
||||
*/
|
||||
public function getOutput(): ?OutputInterface
|
||||
{
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all migrated version numbers.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getVersions(): array
|
||||
{
|
||||
return $this->getAdapter()->getVersions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all migration log entries, indexed by version creation time and sorted in ascending order by the configuration's
|
||||
* version_order option
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getVersionLog(): array
|
||||
{
|
||||
return $this->getAdapter()->getVersionLog();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current version of the environment.
|
||||
*
|
||||
* @param int $version Environment Version
|
||||
* @return $this
|
||||
*/
|
||||
public function setCurrentVersion(int $version)
|
||||
{
|
||||
$this->currentVersion = $version;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current version of the environment.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getCurrentVersion(): int
|
||||
{
|
||||
// We don't cache this code as the current version is pretty volatile.
|
||||
// that means they're no point in a setter then?
|
||||
// maybe we should cache and call a reset() method every time a migration is run
|
||||
$versions = $this->getVersions();
|
||||
$version = 0;
|
||||
|
||||
if (!empty($versions)) {
|
||||
$version = end($versions);
|
||||
}
|
||||
|
||||
$this->setCurrentVersion($version);
|
||||
|
||||
return $this->currentVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the database adapter.
|
||||
*
|
||||
* @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter
|
||||
* @return $this
|
||||
*/
|
||||
public function setAdapter(AdapterInterface $adapter)
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the database adapter.
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
* @return \Phinx\Db\Adapter\AdapterInterface
|
||||
*/
|
||||
public function getAdapter(): AdapterInterface
|
||||
{
|
||||
if (isset($this->adapter)) {
|
||||
return $this->adapter;
|
||||
}
|
||||
|
||||
$options = $this->getOptions();
|
||||
if (isset($options['connection'])) {
|
||||
if (!($options['connection'] instanceof PDO)) {
|
||||
throw new RuntimeException('The specified connection is not a PDO instance');
|
||||
}
|
||||
|
||||
$options['connection']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$options['adapter'] = $options['connection']->getAttribute(PDO::ATTR_DRIVER_NAME);
|
||||
}
|
||||
if (!isset($options['adapter'])) {
|
||||
throw new RuntimeException('No adapter was specified for environment: ' . $this->getName());
|
||||
}
|
||||
|
||||
$factory = AdapterFactory::instance();
|
||||
$adapter = $factory
|
||||
->getAdapter($options['adapter'], $options);
|
||||
|
||||
// Automatically time the executed commands
|
||||
$adapter = $factory->getWrapper('timed', $adapter);
|
||||
|
||||
if (isset($options['wrapper'])) {
|
||||
$adapter = $factory
|
||||
->getWrapper($options['wrapper'], $adapter);
|
||||
}
|
||||
|
||||
/** @var \Symfony\Component\Console\Input\InputInterface|null $input */
|
||||
$input = $this->getInput();
|
||||
if ($input) {
|
||||
$adapter->setInput($this->getInput());
|
||||
}
|
||||
|
||||
/** @var \Symfony\Component\Console\Output\OutputInterface|null $output */
|
||||
$output = $this->getOutput();
|
||||
if ($output) {
|
||||
$adapter->setOutput($this->getOutput());
|
||||
}
|
||||
|
||||
// Use the TablePrefixAdapter if table prefix/suffixes are in use
|
||||
if ($adapter->hasOption('table_prefix') || $adapter->hasOption('table_suffix')) {
|
||||
$adapter = AdapterFactory::instance()
|
||||
->getWrapper('prefix', $adapter);
|
||||
}
|
||||
|
||||
$this->setAdapter($adapter);
|
||||
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the schema table name.
|
||||
*
|
||||
* @param string $schemaTableName Schema Table Name
|
||||
* @return $this
|
||||
*/
|
||||
public function setSchemaTableName(string $schemaTableName)
|
||||
{
|
||||
$this->schemaTableName = $schemaTableName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the schema table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSchemaTableName(): string
|
||||
{
|
||||
return $this->schemaTableName;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
$namespaceDefinition
|
||||
use $useClassName;
|
||||
|
||||
final class $className extends $baseClassName
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change(): void
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
$namespaceDefinition
|
||||
use $useClassName;
|
||||
|
||||
final class $className extends $baseClassName
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,317 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Migration;
|
||||
|
||||
use Cake\Database\Query;
|
||||
use Cake\Database\Query\DeleteQuery;
|
||||
use Cake\Database\Query\InsertQuery;
|
||||
use Cake\Database\Query\SelectQuery;
|
||||
use Cake\Database\Query\UpdateQuery;
|
||||
use Phinx\Db\Adapter\AdapterInterface;
|
||||
use Phinx\Db\Table;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Migration interface
|
||||
*/
|
||||
interface MigrationInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const CHANGE = 'change';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const UP = 'up';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const DOWN = 'down';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const INIT = 'init';
|
||||
|
||||
/**
|
||||
* Sets the database adapter.
|
||||
*
|
||||
* @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter
|
||||
* @return $this
|
||||
*/
|
||||
public function setAdapter(AdapterInterface $adapter);
|
||||
|
||||
/**
|
||||
* Gets the database adapter.
|
||||
*
|
||||
* @return \Phinx\Db\Adapter\AdapterInterface|null
|
||||
*/
|
||||
public function getAdapter(): ?AdapterInterface;
|
||||
|
||||
/**
|
||||
* Sets the input object to be used in migration object
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @return $this
|
||||
*/
|
||||
public function setInput(InputInterface $input);
|
||||
|
||||
/**
|
||||
* Gets the input object to be used in migration object
|
||||
*
|
||||
* @return \Symfony\Component\Console\Input\InputInterface|null
|
||||
*/
|
||||
public function getInput(): ?InputInterface;
|
||||
|
||||
/**
|
||||
* Sets the output object to be used in migration object
|
||||
*
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @return $this
|
||||
*/
|
||||
public function setOutput(OutputInterface $output);
|
||||
|
||||
/**
|
||||
* Gets the output object to be used in migration object
|
||||
*
|
||||
* @return \Symfony\Component\Console\Output\OutputInterface|null
|
||||
*/
|
||||
public function getOutput(): ?OutputInterface;
|
||||
|
||||
/**
|
||||
* Gets the name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string;
|
||||
|
||||
/**
|
||||
* Gets the detected environment
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEnvironment(): string;
|
||||
|
||||
/**
|
||||
* Sets the migration version number.
|
||||
*
|
||||
* @param int $version Version
|
||||
* @return $this
|
||||
*/
|
||||
public function setVersion(int $version);
|
||||
|
||||
/**
|
||||
* Gets the migration version number.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getVersion(): int;
|
||||
|
||||
/**
|
||||
* Sets whether this migration is being applied or reverted
|
||||
*
|
||||
* @param bool $isMigratingUp True if the migration is being applied
|
||||
* @return $this
|
||||
*/
|
||||
public function setMigratingUp(bool $isMigratingUp);
|
||||
|
||||
/**
|
||||
* Gets whether this migration is being applied or reverted.
|
||||
* True means that the migration is being applied.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isMigratingUp(): bool;
|
||||
|
||||
/**
|
||||
* Executes a SQL statement and returns the number of affected rows.
|
||||
*
|
||||
* @param string $sql SQL
|
||||
* @param array $params parameters to use for prepared query
|
||||
* @return int
|
||||
*/
|
||||
public function execute(string $sql, array $params = []): int;
|
||||
|
||||
/**
|
||||
* Executes a SQL statement.
|
||||
*
|
||||
* The return type depends on the underlying adapter being used. To improve
|
||||
* IDE auto-completion possibility, you can overwrite the query method
|
||||
* phpDoc in your (typically custom abstract parent) migration class, where
|
||||
* you can set the return type by the adapter in your current use.
|
||||
*
|
||||
* @param string $sql SQL
|
||||
* @param array $params parameters to use for prepared query
|
||||
* @return mixed
|
||||
*/
|
||||
public function query(string $sql, array $params = []): mixed;
|
||||
|
||||
/**
|
||||
* Returns a new Query object that can be used to build complex SELECT, UPDATE, INSERT or DELETE
|
||||
* queries and execute them against the current database.
|
||||
*
|
||||
* Queries executed through the query builder are always sent to the database, regardless of the
|
||||
* the dry-run settings.
|
||||
*
|
||||
* @see https://api.cakephp.org/3.6/class-Cake.Database.Query.html
|
||||
* @param string $type Query
|
||||
* @return \Cake\Database\Query
|
||||
*/
|
||||
public function getQueryBuilder(string $type): Query;
|
||||
|
||||
/**
|
||||
* Returns a new SelectQuery object that can be used to build complex
|
||||
* SELECT queries and execute them against the current database.
|
||||
*
|
||||
* Queries executed through the query builder are always sent to the database, regardless of the
|
||||
* the dry-run settings.
|
||||
*
|
||||
* @return \Cake\Database\Query\SelectQuery
|
||||
*/
|
||||
public function getSelectBuilder(): SelectQuery;
|
||||
|
||||
/**
|
||||
* Returns a new InsertQuery object that can be used to build complex
|
||||
* INSERT queries and execute them against the current database.
|
||||
*
|
||||
* Queries executed through the query builder are always sent to the database, regardless of the
|
||||
* the dry-run settings.
|
||||
*
|
||||
* @return \Cake\Database\Query\InsertQuery
|
||||
*/
|
||||
public function getInsertBuilder(): InsertQuery;
|
||||
|
||||
/**
|
||||
* Returns a new UpdateQuery object that can be used to build complex
|
||||
* UPDATE queries and execute them against the current database.
|
||||
*
|
||||
* Queries executed through the query builder are always sent to the database, regardless of the
|
||||
* the dry-run settings.
|
||||
*
|
||||
* @return \Cake\Database\Query\UpdateQuery
|
||||
*/
|
||||
public function getUpdateBuilder(): UpdateQuery;
|
||||
|
||||
/**
|
||||
* Returns a new DeleteQuery object that can be used to build complex
|
||||
* DELETE queries and execute them against the current database.
|
||||
*
|
||||
* Queries executed through the query builder are always sent to the database, regardless of the
|
||||
* the dry-run settings.
|
||||
*
|
||||
* @return \Cake\Database\Query\DeleteQuery
|
||||
*/
|
||||
public function getDeleteBuilder(): DeleteQuery;
|
||||
|
||||
/**
|
||||
* Executes a query and returns only one row as an array.
|
||||
*
|
||||
* @param string $sql SQL
|
||||
* @return array|false
|
||||
*/
|
||||
public function fetchRow(string $sql): array|false;
|
||||
|
||||
/**
|
||||
* Executes a query and returns an array of rows.
|
||||
*
|
||||
* @param string $sql SQL
|
||||
* @return array
|
||||
*/
|
||||
public function fetchAll(string $sql): array;
|
||||
|
||||
/**
|
||||
* Create a new database.
|
||||
*
|
||||
* @param string $name Database Name
|
||||
* @param array<string, mixed> $options Options
|
||||
* @return void
|
||||
*/
|
||||
public function createDatabase(string $name, array $options): void;
|
||||
|
||||
/**
|
||||
* Drop a database.
|
||||
*
|
||||
* @param string $name Database Name
|
||||
* @return void
|
||||
*/
|
||||
public function dropDatabase(string $name): void;
|
||||
|
||||
/**
|
||||
* Creates schema.
|
||||
*
|
||||
* This will thrown an error for adapters that do not support schemas.
|
||||
*
|
||||
* @param string $name Schema name
|
||||
* @return void
|
||||
* @throws \BadMethodCallException
|
||||
*/
|
||||
public function createSchema(string $name): void;
|
||||
|
||||
/**
|
||||
* Drops schema.
|
||||
*
|
||||
* This will thrown an error for adapters that do not support schemas.
|
||||
*
|
||||
* @param string $name Schema name
|
||||
* @return void
|
||||
* @throws \BadMethodCallException
|
||||
*/
|
||||
public function dropSchema(string $name): void;
|
||||
|
||||
/**
|
||||
* Checks to see if a table exists.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasTable(string $tableName): bool;
|
||||
|
||||
/**
|
||||
* Returns an instance of the <code>\Table</code> class.
|
||||
*
|
||||
* You can use this class to create and manipulate tables.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @param array<string, mixed> $options Options
|
||||
* @return \Phinx\Db\Table
|
||||
*/
|
||||
public function table(string $tableName, array $options): Table;
|
||||
|
||||
/**
|
||||
* Perform checks on the migration, printing a warning
|
||||
* if there are potential problems.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function preFlightCheck(): void;
|
||||
|
||||
/**
|
||||
* Perform checks on the migration after completion
|
||||
*
|
||||
* Right now, the only check is whether all changes were committed
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function postFlightCheck(): void;
|
||||
|
||||
/**
|
||||
* Checks to see if the migration should be executed.
|
||||
*
|
||||
* Returns true by default.
|
||||
*
|
||||
* You can use this to prevent a migration from executing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldExecute(): bool;
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Seed;
|
||||
|
||||
use Phinx\Db\Adapter\AdapterInterface;
|
||||
use Phinx\Db\Table;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Abstract Seed Class.
|
||||
*
|
||||
* It is expected that the seeds you write extend from this class.
|
||||
*
|
||||
* This abstract class proxies the various database methods to your specified
|
||||
* adapter.
|
||||
*/
|
||||
abstract class AbstractSeed implements SeedInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $environment;
|
||||
|
||||
/**
|
||||
* @var \Phinx\Db\Adapter\AdapterInterface
|
||||
*/
|
||||
protected AdapterInterface $adapter;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\Console\Input\InputInterface
|
||||
*/
|
||||
protected InputInterface $input;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\Console\Output\OutputInterface
|
||||
*/
|
||||
protected OutputInterface $output;
|
||||
|
||||
/**
|
||||
* Override to specify dependencies for dependency injection from the configured PSR-11 container
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getDependencies(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setEnvironment(string $environment)
|
||||
{
|
||||
$this->environment = $environment;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getEnvironment(): string
|
||||
{
|
||||
return $this->environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setAdapter(AdapterInterface $adapter): SeedInterface
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getAdapter(): AdapterInterface
|
||||
{
|
||||
if (!isset($this->adapter)) {
|
||||
throw new RuntimeException('Cannot access `adapter` it has not been set');
|
||||
}
|
||||
|
||||
return $this->adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setInput(InputInterface $input)
|
||||
{
|
||||
$this->input = $input;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getInput(): InputInterface
|
||||
{
|
||||
return $this->input;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setOutput(OutputInterface $output): SeedInterface
|
||||
{
|
||||
$this->output = $output;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOutput(): OutputInterface
|
||||
{
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return static::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function execute(string $sql, array $params = []): int
|
||||
{
|
||||
return $this->getAdapter()->execute($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function query(string $sql, array $params = []): mixed
|
||||
{
|
||||
return $this->getAdapter()->query($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function fetchRow(string $sql): array|false
|
||||
{
|
||||
return $this->getAdapter()->fetchRow($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function fetchAll(string $sql): array
|
||||
{
|
||||
return $this->getAdapter()->fetchAll($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function insert(string $table, array $data): void
|
||||
{
|
||||
// convert to table object
|
||||
if (is_string($table)) {
|
||||
$table = new Table($table, [], $this->getAdapter());
|
||||
}
|
||||
$table->insert($data)->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasTable(string $tableName): bool
|
||||
{
|
||||
return $this->getAdapter()->hasTable($tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function table(string $tableName, array $options = []): Table
|
||||
{
|
||||
return new Table($tableName, $options, $this->getAdapter());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if the seed should be executed.
|
||||
*
|
||||
* Returns true by default.
|
||||
*
|
||||
* You can use this to prevent a seed from executing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldExecute(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
$namespaceDefinition
|
||||
use $useClassName;
|
||||
|
||||
class $className extends $baseClassName
|
||||
{
|
||||
/**
|
||||
* Run Method.
|
||||
*
|
||||
* Write your database seeder using this method.
|
||||
*
|
||||
* More information on writing seeders is available here:
|
||||
* https://book.cakephp.org/phinx/0/en/seeding.html
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Seed;
|
||||
|
||||
use Phinx\Db\Adapter\AdapterInterface;
|
||||
use Phinx\Db\Table;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Seed interface
|
||||
*/
|
||||
interface SeedInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const RUN = 'run';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const INIT = 'init';
|
||||
|
||||
/**
|
||||
* Run the seeder.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run(): void;
|
||||
|
||||
/**
|
||||
* Return seeds dependencies.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDependencies(): array;
|
||||
|
||||
/**
|
||||
* Sets the environment.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setEnvironment(string $environment);
|
||||
|
||||
/**
|
||||
* Gets the environment.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEnvironment(): string;
|
||||
|
||||
/**
|
||||
* Sets the database adapter.
|
||||
*
|
||||
* @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter
|
||||
* @return $this
|
||||
*/
|
||||
public function setAdapter(AdapterInterface $adapter);
|
||||
|
||||
/**
|
||||
* Gets the database adapter.
|
||||
*
|
||||
* @return \Phinx\Db\Adapter\AdapterInterface
|
||||
*/
|
||||
public function getAdapter(): AdapterInterface;
|
||||
|
||||
/**
|
||||
* Sets the input object to be used in migration object
|
||||
*
|
||||
* @param \Symfony\Component\Console\Input\InputInterface $input Input
|
||||
* @return $this
|
||||
*/
|
||||
public function setInput(InputInterface $input);
|
||||
|
||||
/**
|
||||
* Gets the input object to be used in migration object
|
||||
*
|
||||
* @return \Symfony\Component\Console\Input\InputInterface
|
||||
*/
|
||||
public function getInput(): InputInterface;
|
||||
|
||||
/**
|
||||
* Sets the output object to be used in migration object
|
||||
*
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
|
||||
* @return $this
|
||||
*/
|
||||
public function setOutput(OutputInterface $output);
|
||||
|
||||
/**
|
||||
* Gets the output object to be used in migration object
|
||||
*
|
||||
* @return \Symfony\Component\Console\Output\OutputInterface
|
||||
*/
|
||||
public function getOutput(): OutputInterface;
|
||||
|
||||
/**
|
||||
* Gets the name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string;
|
||||
|
||||
/**
|
||||
* Executes a SQL statement and returns the number of affected rows.
|
||||
*
|
||||
* @param string $sql SQL
|
||||
* @param array $params parameters to use for prepared query
|
||||
* @return int
|
||||
*/
|
||||
public function execute(string $sql, array $params = []): int;
|
||||
|
||||
/**
|
||||
* Executes a SQL statement.
|
||||
*
|
||||
* The return type depends on the underlying adapter being used. To improve
|
||||
* IDE auto-completion possibility, you can overwrite the query method
|
||||
* phpDoc in your (typically custom abstract parent) seed class, where
|
||||
* you can set the return type by the adapter in your current use.
|
||||
*
|
||||
* @param string $sql SQL
|
||||
* @param array $params parameters to use for prepared query
|
||||
* @return mixed
|
||||
*/
|
||||
public function query(string $sql, array $params = []): mixed;
|
||||
|
||||
/**
|
||||
* Executes a query and returns only one row as an array.
|
||||
*
|
||||
* @param string $sql SQL
|
||||
* @return array|false
|
||||
*/
|
||||
public function fetchRow(string $sql): array|false;
|
||||
|
||||
/**
|
||||
* Executes a query and returns an array of rows.
|
||||
*
|
||||
* @param string $sql SQL
|
||||
* @return array
|
||||
*/
|
||||
public function fetchAll(string $sql): array;
|
||||
|
||||
/**
|
||||
* Insert data into a table.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @param array $data Data
|
||||
* @return void
|
||||
*/
|
||||
public function insert(string $tableName, array $data): void;
|
||||
|
||||
/**
|
||||
* Checks to see if a table exists.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasTable(string $tableName): bool;
|
||||
|
||||
/**
|
||||
* Returns an instance of the <code>\Table</code> class.
|
||||
*
|
||||
* You can use this class to create and manipulate tables.
|
||||
*
|
||||
* @param string $tableName Table name
|
||||
* @param array<string, mixed> $options Options
|
||||
* @return \Phinx\Db\Table
|
||||
*/
|
||||
public function table(string $tableName, array $options): Table;
|
||||
|
||||
/**
|
||||
* Checks to see if the seed should be executed.
|
||||
*
|
||||
* Returns true by default.
|
||||
*
|
||||
* You can use this to prevent a seed from executing.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldExecute(): bool;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Util;
|
||||
|
||||
class Expression
|
||||
{
|
||||
/**
|
||||
* @var string The expression
|
||||
*/
|
||||
protected string $value;
|
||||
|
||||
/**
|
||||
* @param string $value The expression
|
||||
*/
|
||||
public function __construct(string $value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string Returns the expression
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value The expression
|
||||
* @return self
|
||||
*/
|
||||
public static function from(string $value): Expression
|
||||
{
|
||||
return new self($value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Util;
|
||||
|
||||
class Literal
|
||||
{
|
||||
/**
|
||||
* @var string The literal's value
|
||||
*/
|
||||
protected string $value;
|
||||
|
||||
/**
|
||||
* @param string $value The literal's value
|
||||
*/
|
||||
public function __construct(string $value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string Returns the literal's value
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value The literal's value
|
||||
* @return self
|
||||
*/
|
||||
public static function from(string $value): Literal
|
||||
{
|
||||
return new self($value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,394 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Util;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class Util
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const DATE_FORMAT = 'YmdHis';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected const MIGRATION_FILE_NAME_PATTERN = '/^\d+_([a-z][a-z\d]*(?:_[a-z\d]+)*)\.php$/i';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected const MIGRATION_FILE_NAME_NO_NAME_PATTERN = '/^[0-9]{14}\.php$/';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected const SEED_FILE_NAME_PATTERN = '/^([a-z][a-z\d]*)\.php$/i';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected const CLASS_NAME_PATTERN = '/^(?:[A-Z][a-z\d]*)+$/';
|
||||
|
||||
/**
|
||||
* Gets the current timestamp string, in UTC.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getCurrentTimestamp(?int $offset = null): string
|
||||
{
|
||||
$dt = new DateTime('now', new DateTimeZone('UTC'));
|
||||
if ($offset) {
|
||||
$dt->modify('+' . $offset . ' seconds');
|
||||
}
|
||||
|
||||
return $dt->format(static::DATE_FORMAT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that the given timestamp is a unique prefix for any files in the given path.
|
||||
*
|
||||
* @param string $path Path to check
|
||||
* @param string $timestamp Timestamp to check
|
||||
* @return bool
|
||||
*/
|
||||
public static function isUniqueTimestamp(string $path, string $timestamp): bool
|
||||
{
|
||||
return !count(static::glob($path . DIRECTORY_SEPARATOR . $timestamp . '*.php'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an array of all the existing migration class names.
|
||||
*
|
||||
* @param string $path Path
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getExistingMigrationClassNames(string $path): array
|
||||
{
|
||||
$classNames = [];
|
||||
|
||||
if (!is_dir($path)) {
|
||||
return $classNames;
|
||||
}
|
||||
|
||||
// filter the files to only get the ones that match our naming scheme
|
||||
$phpFiles = static::getFiles($path);
|
||||
|
||||
foreach ($phpFiles as $filePath) {
|
||||
$fileName = basename($filePath);
|
||||
if (preg_match(static::MIGRATION_FILE_NAME_PATTERN, $fileName)) {
|
||||
$classNames[] = static::mapFileNameToClassName($fileName);
|
||||
}
|
||||
}
|
||||
|
||||
return $classNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version from the beginning of a file name.
|
||||
*
|
||||
* @param string $fileName File Name
|
||||
* @return int
|
||||
*/
|
||||
public static function getVersionFromFileName(string $fileName): int
|
||||
{
|
||||
$matches = [];
|
||||
preg_match('/^[0-9]+/', basename($fileName), $matches);
|
||||
$value = (int)($matches[0] ?? null);
|
||||
if (!$value) {
|
||||
throw new RuntimeException(sprintf('Cannot get a valid version from filename `%s`', $fileName));
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a string, convert it to snake_case.
|
||||
*
|
||||
* @param string $string String to convert
|
||||
* @return string
|
||||
*/
|
||||
public static function toSnakeCase(string $string): string
|
||||
{
|
||||
$snake = function ($matches) {
|
||||
return '_' . strtolower($matches[0]);
|
||||
};
|
||||
|
||||
return preg_replace_callback('/\d+|[A-Z]/', $snake, $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn migration names like 'CreateUserTable' into file names like
|
||||
* '12345678901234_create_user_table.php' or 'LimitResourceNamesTo30Chars' into
|
||||
* '12345678901234_limit_resource_names_to_30_chars.php'.
|
||||
*
|
||||
* @deprecated Will be removed in 0.17.0
|
||||
* @param string $className Class Name
|
||||
* @return string
|
||||
*/
|
||||
public static function mapClassNameToFileName(string $className): string
|
||||
{
|
||||
trigger_error('Util::mapClassNameToFileName is deprecated since 0.16.6, and will be removed in a future release.', E_USER_DEPRECATED);
|
||||
$snake = function ($matches) {
|
||||
return '_' . strtolower($matches[0]);
|
||||
};
|
||||
$fileName = preg_replace_callback('/\d+|[A-Z]/', $snake, $className);
|
||||
$fileName = static::getCurrentTimestamp() . "$fileName.php";
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn file names like '12345678901234_create_user_table.php' into class
|
||||
* names like 'CreateUserTable'.
|
||||
*
|
||||
* @param string $fileName File Name
|
||||
* @return string
|
||||
*/
|
||||
public static function mapFileNameToClassName(string $fileName): string
|
||||
{
|
||||
$matches = [];
|
||||
if (preg_match(static::MIGRATION_FILE_NAME_PATTERN, $fileName, $matches)) {
|
||||
$fileName = $matches[1];
|
||||
} elseif (preg_match(static::MIGRATION_FILE_NAME_NO_NAME_PATTERN, $fileName)) {
|
||||
return 'V' . substr($fileName, 0, strlen($fileName) - 4);
|
||||
}
|
||||
|
||||
$className = str_replace('_', '', ucwords($fileName, '_'));
|
||||
|
||||
return $className;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a migration class name is unique regardless of the
|
||||
* timestamp.
|
||||
*
|
||||
* This method takes a class name and a path to a migrations directory.
|
||||
*
|
||||
* Migration class names must be in PascalCase format but consecutive
|
||||
* capitals are allowed.
|
||||
* e.g: AddIndexToPostsTable or CustomHTMLTitle.
|
||||
*
|
||||
* @param string $className Class Name
|
||||
* @param string $path Path
|
||||
* @return bool
|
||||
*/
|
||||
public static function isUniqueMigrationClassName(string $className, string $path): bool
|
||||
{
|
||||
$existingClassNames = static::getExistingMigrationClassNames($path);
|
||||
|
||||
return !in_array($className, $existingClassNames, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a migration/seed class name is valid.
|
||||
*
|
||||
* Migration & Seed class names must be in CamelCase format.
|
||||
* e.g: CreateUserTable, AddIndexToPostsTable or UserSeeder.
|
||||
*
|
||||
* Single words are not allowed on their own.
|
||||
*
|
||||
* @param string $className Class Name
|
||||
* @return bool
|
||||
*/
|
||||
public static function isValidPhinxClassName(string $className): bool
|
||||
{
|
||||
return (bool)preg_match(static::CLASS_NAME_PATTERN, $className);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a migration file name is valid.
|
||||
*
|
||||
* @param string $fileName File Name
|
||||
* @return bool
|
||||
*/
|
||||
public static function isValidMigrationFileName(string $fileName): bool
|
||||
{
|
||||
return (bool)preg_match(static::MIGRATION_FILE_NAME_PATTERN, $fileName)
|
||||
|| (bool)preg_match(static::MIGRATION_FILE_NAME_NO_NAME_PATTERN, $fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a seed file name is valid.
|
||||
*
|
||||
* @param string $fileName File Name
|
||||
* @return bool
|
||||
*/
|
||||
public static function isValidSeedFileName(string $fileName): bool
|
||||
{
|
||||
return (bool)preg_match(static::SEED_FILE_NAME_PATTERN, $fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Expands a set of paths with curly braces (if supported by the OS).
|
||||
*
|
||||
* @param string[] $paths Paths
|
||||
* @return string[]
|
||||
*/
|
||||
public static function globAll(array $paths): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($paths as $path) {
|
||||
$result = array_merge($result, static::glob($path));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expands a path with curly braces (if supported by the OS).
|
||||
*
|
||||
* @param string $path Path
|
||||
* @return string[]
|
||||
*/
|
||||
public static function glob(string $path): array
|
||||
{
|
||||
return glob($path, defined('GLOB_BRACE') ? GLOB_BRACE : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the path to a php file and attempts to include it if readable
|
||||
*
|
||||
* @param string $filename Filename
|
||||
* @param \Symfony\Component\Console\Input\InputInterface|null $input Input
|
||||
* @param \Symfony\Component\Console\Output\OutputInterface|null $output Output
|
||||
* @param \Phinx\Console\Command\AbstractCommand|mixed|null $context Context
|
||||
* @throws \Exception
|
||||
* @return string
|
||||
*/
|
||||
public static function loadPhpFile(string $filename, ?InputInterface $input = null, ?OutputInterface $output = null, mixed $context = null): string
|
||||
{
|
||||
$filePath = realpath($filename);
|
||||
if (!file_exists($filePath)) {
|
||||
throw new Exception(sprintf("File does not exist: %s \n", $filename));
|
||||
}
|
||||
|
||||
/**
|
||||
* I lifed this from phpunits FileLoader class
|
||||
*
|
||||
* @see https://github.com/sebastianbergmann/phpunit/pull/2751
|
||||
*/
|
||||
$isReadable = @fopen($filePath, 'r') !== false;
|
||||
|
||||
if (!$isReadable) {
|
||||
throw new Exception(sprintf("Cannot open file %s \n", $filename));
|
||||
}
|
||||
|
||||
// prevent this to be propagated to the included file
|
||||
unset($isReadable);
|
||||
|
||||
include_once $filePath;
|
||||
|
||||
return $filePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an array of paths, return all unique PHP files that are in them
|
||||
*
|
||||
* @param string|string[] $paths Path or array of paths to get .php files.
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getFiles(string|array $paths): array
|
||||
{
|
||||
$files = static::globAll(array_map(function ($path) {
|
||||
return $path . DIRECTORY_SEPARATOR . '*.php';
|
||||
}, (array)$paths));
|
||||
// glob() can return the same file multiple times
|
||||
// This will cause the migration to fail with a
|
||||
// false assumption of duplicate migrations
|
||||
// https://php.net/manual/en/function.glob.php#110340
|
||||
$files = array_unique($files);
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to remove the current working directory from a path for output.
|
||||
*
|
||||
* @param string $path Path to remove cwd prefix from
|
||||
* @return string
|
||||
*/
|
||||
public static function relativePath(string $path): string
|
||||
{
|
||||
$realpath = realpath($path);
|
||||
if ($realpath !== false) {
|
||||
$path = $realpath;
|
||||
}
|
||||
|
||||
$cwd = getcwd();
|
||||
if ($cwd !== false) {
|
||||
$cwd .= DIRECTORY_SEPARATOR;
|
||||
$cwdLen = strlen($cwd);
|
||||
|
||||
if (substr($path, 0, $cwdLen) === $cwd) {
|
||||
$path = substr($path, $cwdLen);
|
||||
}
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses DSN string into db config array.
|
||||
*
|
||||
* @param string $dsn DSN string
|
||||
* @return array
|
||||
*/
|
||||
public static function parseDsn(string $dsn): array
|
||||
{
|
||||
$pattern = <<<'REGEXP'
|
||||
{
|
||||
^
|
||||
(?:
|
||||
(?P<adapter>[\w\\\\]+)://
|
||||
)
|
||||
(?:
|
||||
(?P<user>.*?)
|
||||
(?:
|
||||
:(?P<pass>.*?)
|
||||
)?
|
||||
@
|
||||
)?
|
||||
(?:
|
||||
(?P<host>[^?#/:@]+)
|
||||
(?:
|
||||
:(?P<port>\d+)
|
||||
)?
|
||||
)?
|
||||
(?:
|
||||
/(?P<name>[^?#]*)
|
||||
)?
|
||||
(?:
|
||||
\?(?P<query>[^#]*)
|
||||
)?
|
||||
$
|
||||
}x
|
||||
REGEXP;
|
||||
|
||||
if (!preg_match($pattern, $dsn, $parsed)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// filter out everything except the matched groups
|
||||
$config = array_intersect_key($parsed, array_flip(['adapter', 'user', 'pass', 'host', 'port', 'name']));
|
||||
$config = array_filter($config);
|
||||
|
||||
parse_str($parsed['query'] ?? '', $query);
|
||||
$config = array_merge($query, $config);
|
||||
|
||||
return $config;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
* For full license information, please view the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Phinx\Wrapper;
|
||||
|
||||
use Phinx\Console\PhinxApplication;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Output\StreamOutput;
|
||||
|
||||
/**
|
||||
* Phinx text wrapper: a way to run `status`, `migrate`, and `rollback` commands
|
||||
* and get the output of the command back as plain text.
|
||||
*/
|
||||
class TextWrapper
|
||||
{
|
||||
/**
|
||||
* @var \Phinx\Console\PhinxApplication
|
||||
*/
|
||||
protected PhinxApplication $app;
|
||||
|
||||
/**
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
protected array $options;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected int $exitCode;
|
||||
|
||||
/**
|
||||
* @param \Phinx\Console\PhinxApplication $app Application
|
||||
* @param array<string, mixed> $options Options
|
||||
*/
|
||||
public function __construct(PhinxApplication $app, array $options = [])
|
||||
{
|
||||
$this->app = $app;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the application instance.
|
||||
*
|
||||
* @return \Phinx\Console\PhinxApplication
|
||||
*/
|
||||
public function getApp(): PhinxApplication
|
||||
{
|
||||
return $this->app;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the exit code from the last run command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getExitCode(): int
|
||||
{
|
||||
return $this->exitCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the output from running the "status" command.
|
||||
*
|
||||
* @param string|null $env environment name (optional)
|
||||
* @return string
|
||||
*/
|
||||
public function getStatus(?string $env = null): string
|
||||
{
|
||||
$command = ['status'];
|
||||
if ($this->hasEnvValue($env)) {
|
||||
$command['-e'] = $env ?: $this->getOption('environment');
|
||||
}
|
||||
if ($this->hasOption('configuration')) {
|
||||
$command['-c'] = $this->getOption('configuration');
|
||||
}
|
||||
if ($this->hasOption('parser')) {
|
||||
$command['-p'] = $this->getOption('parser');
|
||||
}
|
||||
if ($this->hasOption('format')) {
|
||||
$command['-f'] = $this->getOption('format');
|
||||
}
|
||||
|
||||
return $this->executeRun($command);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $env environment name
|
||||
* @return bool
|
||||
*/
|
||||
private function hasEnvValue(?string $env): bool
|
||||
{
|
||||
return $env || $this->hasOption('environment');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the output from running the "migrate" command.
|
||||
*
|
||||
* @param string|null $env environment name (optional)
|
||||
* @param string|null $target target version (optional)
|
||||
* @return string
|
||||
*/
|
||||
public function getMigrate(?string $env = null, ?string $target = null): string
|
||||
{
|
||||
$command = ['migrate'];
|
||||
if ($this->hasEnvValue($env)) {
|
||||
$command += ['-e' => $env ?: $this->getOption('environment')];
|
||||
}
|
||||
if ($this->hasOption('configuration')) {
|
||||
$command += ['-c' => $this->getOption('configuration')];
|
||||
}
|
||||
if ($this->hasOption('parser')) {
|
||||
$command += ['-p' => $this->getOption('parser')];
|
||||
}
|
||||
if ($target) {
|
||||
$command += ['-t' => $target];
|
||||
}
|
||||
|
||||
return $this->executeRun($command);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the output from running the "seed:run" command.
|
||||
*
|
||||
* @param string|null $env Environment name
|
||||
* @param string|null $target Target version
|
||||
* @param string[]|string|null $seed Array of seed names or seed name
|
||||
* @return string
|
||||
*/
|
||||
public function getSeed(?string $env = null, ?string $target = null, array|string|null $seed = null): string
|
||||
{
|
||||
$command = ['seed:run'];
|
||||
if ($this->hasEnvValue($env)) {
|
||||
$command += ['-e' => $env ?: $this->getOption('environment')];
|
||||
}
|
||||
if ($this->hasOption('configuration')) {
|
||||
$command += ['-c' => $this->getOption('configuration')];
|
||||
}
|
||||
if ($this->hasOption('parser')) {
|
||||
$command += ['-p' => $this->getOption('parser')];
|
||||
}
|
||||
if ($target) {
|
||||
$command += ['-t' => $target];
|
||||
}
|
||||
if ($seed) {
|
||||
$seed = (array)$seed;
|
||||
$command += ['-s' => $seed];
|
||||
}
|
||||
|
||||
return $this->executeRun($command);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the output from running the "rollback" command.
|
||||
*
|
||||
* @param string|null $env Environment name (optional)
|
||||
* @param mixed $target Target version, or 0 (zero) fully revert (optional)
|
||||
* @return string
|
||||
*/
|
||||
public function getRollback(?string $env = null, mixed $target = null): string
|
||||
{
|
||||
$command = ['rollback'];
|
||||
if ($this->hasEnvValue($env)) {
|
||||
$command += ['-e' => $env ?: $this->getOption('environment')];
|
||||
}
|
||||
if ($this->hasOption('configuration')) {
|
||||
$command += ['-c' => $this->getOption('configuration')];
|
||||
}
|
||||
if ($this->hasOption('parser')) {
|
||||
$command += ['-p' => $this->getOption('parser')];
|
||||
}
|
||||
if (isset($target)) {
|
||||
// Need to use isset() with rollback, because -t0 is a valid option!
|
||||
// See https://book.cakephp.org/phinx/0/en/commands.html#the-rollback-command
|
||||
$command += ['-t' => $target];
|
||||
}
|
||||
|
||||
return $this->executeRun($command);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check option from options array
|
||||
*
|
||||
* @param string $key Key
|
||||
* @return bool
|
||||
*/
|
||||
protected function hasOption(string $key): bool
|
||||
{
|
||||
return isset($this->options[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get option from options array
|
||||
*
|
||||
* @param string $key Key
|
||||
* @return string|null
|
||||
*/
|
||||
protected function getOption(string $key): ?string
|
||||
{
|
||||
if (!isset($this->options[$key])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->options[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set option in options array
|
||||
*
|
||||
* @param string $key Key
|
||||
* @param string $value Value
|
||||
* @return $this
|
||||
*/
|
||||
public function setOption(string $key, string $value)
|
||||
{
|
||||
$this->options[$key] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a command, capturing output and storing the exit code.
|
||||
*
|
||||
* @param array $command Command
|
||||
* @return string
|
||||
*/
|
||||
protected function executeRun(array $command): string
|
||||
{
|
||||
// Output will be written to a temporary stream, so that it can be
|
||||
// collected after running the command.
|
||||
$stream = fopen('php://temp', 'w+');
|
||||
|
||||
// Execute the command, capturing the output in the temporary stream
|
||||
// and storing the exit code for debugging purposes.
|
||||
$this->exitCode = $this->app->doRun(new ArrayInput($command), new StreamOutput($stream));
|
||||
|
||||
// Get the output of the command and close the stream, which will
|
||||
// destroy the temporary file.
|
||||
$result = stream_get_contents($stream, -1, 0);
|
||||
fclose($stream);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Attempts to load Composer's autoload.php as either a dependency or a
|
||||
* stand-alone package.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
return function () {
|
||||
$files = [
|
||||
__DIR__ . '/../../../autoload.php', // composer dependency
|
||||
__DIR__ . '/../vendor/autoload.php', // stand-alone package
|
||||
];
|
||||
foreach ($files as $file) {
|
||||
if (is_file($file)) {
|
||||
require_once $file;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
Reference in New Issue
Block a user