Giter Site home page Giter Site logo

cakephp / phinx Goto Github PK

View Code? Open in Web Editor NEW
4.5K 138.0 895.0 8.87 MB

PHP Database Migrations for Everyone

Home Page: https://phinx.org

License: MIT License

PHP 99.88% Batchfile 0.05% Dockerfile 0.07%
php migrations database-migrations php-database-migrations

phinx's Introduction

Phinx: Simple PHP Database Migrations

Build Status Code Coverage Packagist Version Minimum PHP Version Packagist Downloads

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 (EN, ZH) for the comprehensive documentation.

phinxterm

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 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
    
  2. Require Phinx as a dependency using Composer:

    php composer.phar require robmorgan/phinx
    
  3. Install Phinx:

    php composer.phar install
    
  4. 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
    
  2. Install Composer

    curl -s https://getcomposer.org/installer | php
    
  3. Install the Phinx dependencies

    php composer.phar install
    
  4. Install Box:

    curl -LSs https://box-project.github.io/box2/installer.php | php
    
  5. Create a Phar archive

    php box.phar build
    

Documentation

Check out https://book.cakephp.org/phinx for the comprehensive documentation.

Other translations include:

Contributing

Please read the CONTRIBUTING document.

News & Updates

Follow @CakePHP on Twitter to stay up to date.

Limitations

PostgreSQL

  • Not able to set a unique constraint on a table (#1026).

Misc

Version History

Please read the release notes.

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.

phinx's People

Contributors

admad avatar archer308 avatar bartekrylko avatar cyrusboadway avatar czogori avatar dereuromark avatar firtzberg avatar gamalan avatar garas avatar jayphp avatar jbourdin avatar jkingweb avatar lorenzo avatar mackstar avatar markstory avatar martijngastkemper avatar masterodin avatar ndm2 avatar ntzm avatar ondrejmirtes avatar othercorey avatar robmorgan avatar rquadling avatar saeideng avatar seinopsys avatar shadowhand avatar stickler-ci avatar trilopin avatar twoixter avatar yashtodi94 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phinx's Issues

Pass direction to change() method

Hi

When running non-reversible functions in the change method, like drop column, you need to be aware of which way the change is going, up or down. For example, a down operation can't reverse a drop column (how would it know what to put in back in?), thus this needs to be conditional based on the direction.

Simple change to Manager/Environment.php: 103:

        if ($direction == MigrationInterface::DOWN) {
            // Create an instance of the ProxyAdapter so we can record all
            // of the migration commands for reverse playback
            $proxyAdapter = new ProxyAdapter($this->getAdapter(), $this->getOutput());
            $migration->setAdapter($proxyAdapter);
            $migration->change($direction);
            $proxyAdapter->executeInvertedCommands();
            $migration->setAdapter($this->getAdapter());
        } else {
            $migration->change($direction);
        }

Oli

A Rails-like 'change' method to describe schema changes

Along-side both up() & down() migration methods, we introduce a change() method. This method can describe a schema change in code. Phinx can automatically migrate up/down based on the description. Rails calls this 'reversible migrations'.

Phinx cmd doesn't work on all partitions in Windows

I have Phinx installed trough composer in C:\PHPtools, so I have in my path C:\PHPtools\vendor\bin.
I can use Phinx correctly on my C: drive, but as sun as I want to work on drive D: I got problem.

This is cmd output:

The system cannot find the path specified.
Could not open input file: D:\projects\test_app\app\migrations\phinx

I'm interested in using my tools system wide without installing it in projects. Any solution?

Running migrate for the first time without any migrations should not give php warnings with stack trace

When running phinx migrate -e development without any new migrations and no previous migrations, the console outputs a warning together with a strack trace:

Phinx by Rob Morgan. version 0.2.0
[...]
Warning: max(): Array must contain at least one element in
[...]\vendor\robmorgan\phinx\src\Phinx\Migration\Manager.php on line 136
[Stack trace here, i don't think it is needed to fix this issue]

All Done. Took 0.2778s

Please remove the warning.

Issue with running a migration

Hi,

This might be major PEBCAK but I've got an issue with Phinx whenever I try to run a migration or setup. I'm using MAMP Pro and PHP 5.4.3 (with the PHP path set to use MAMP"s version of PHP).

[11-Mar-2013 21:29:46 UTC] PHP Notice: Undefined property: Phinx\Db\Adapter\MysqlAdapter::$output in /Users/anthony/Sites/l.residen.se/fuel/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php on line 112
[11-Mar-2013 21:29:46 UTC] PHP Fatal error: Call to a member function writeln() on a non-object in /Users/anthony/Sites/l.residen.se/fuel/app/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php on line 186

If I create a phinxlog table in the database I specify for development I get this error in the console....

PDOException]
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'version' in 'order
clause'

My MySQL connection details appear to be being set properly based on the above. Any idea what might be causing this?

Phinx should support PHP as a configuration file parser

Phinx should support loading configuration options from a PHP file as well as from yaml.

Primary scenario:
I use PaaS providers such as Pagodabox, Dotcloud, Appfog, etc. I'd like to use Phinx to manage database versioning during deployments. However, most providers provide database credentials in the form of environment variables. These are easily read in native PHP, but cannot be accessed in yaml without writing a shim that outputs credentials to a .yml file (which negates the point of the env variables). Up until recently I could work around this as the Symfony yaml parser actually supported reading native PHP files through Yaml::parse() as long as they returned an array. However, that support is now deprecated and will be removed in future versions.

Secondary scenario:
Most frameworks provide a single settings file to store database credentials and environments. If Phinx can support reading in configuration options through a PHP file, it makes it much easier to create a lightweight, app-specific shim that allows me to maintain database credentials in a single location rather than in both the framework settings file and in phinx.yml.

Add Composer support

Allow Phinx to be installed via composer and add dependencies to the composer.json file.

Index declarations are not separated with comma when creating multiple indexes

When creating table with more the one indexes at once

$this->table('test_table')
            ->addColumn('test_id1', 'integer')
            ->addColumn('test_id2', 'integer')
            ->addIndex('test_id1')
            ->addIndex('test_id2')
            ->save();

KEY definitions are not separated by comma in final SQL statetement:

CREATE TABLE `test_table`
(`id` INT(11) NOT NULL AUTO_INCREMENT,
`test_id1` INT(11) NOT NULL,
`test_id2` INT(11) NOT NULL,
PRIMARY KEY (`id`), 
KEY (test_id1)
KEY (test_id2)
) ENGINE = InnoDB;

I've forked phinx and working on this issue now, so I hope there will be pull request soon :)

Phinx fails to migrate when default_migration_table is not set to the default 'phinxlog'

As of 0.2.1, we can't seem to run a migration with a default_migration_table set to something other than 'phinxlog'. If its left as 'phinxlog' the migration runs successfully.

My phinx.yml is set to:

default_migration_table: phinxlog_api_schools

Here's the resulting error trying to migrate to any environment:

Phinx by Rob Morgan. version 0.2.1

using config ./phinx.yml
using migration path /var/www/api/api-schools/migrations
using environment local
using adapter mysql
using database removing_db_name
PHP Notice:  Undefined property: Phinx\Db\Adapter\MysqlAdapter::$output in /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php on line 112
PHP Stack trace:
PHP   1. {main}() /var/www/api/api-schools/vendor/robmorgan/phinx/bin/phinx:0
PHP   2. Symfony\Component\Console\Application->run() /var/www/api/api-schools/vendor/robmorgan/phinx/bin/phinx:54
PHP   3. Phinx\Console\PhinxApplication->doRun() /var/www/api/api-schools/vendor/symfony/console/Symfony/Component/Console/Application.php:106
PHP   4. Symfony\Component\Console\Application->doRun() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Console/PhinxApplication.php:81
PHP   5. Symfony\Component\Console\Command\Command->run() /var/www/api/api-schools/vendor/symfony/console/Symfony/Component/Console/Application.php:193
PHP   6. Phinx\Console\Command\Migrate->execute() /var/www/api/api-schools/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:240
PHP   7. Phinx\Migration\Manager->migrate() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Console/Command/Migrate.php:94
PHP   8. Phinx\Migration\Manager\Environment->getVersions() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Migration/Manager.php:137
PHP   9. Phinx\Db\Adapter\PdoAdapter->getVersions() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Migration/Manager/Environment.php:227
PHP  10. Phinx\Db\Adapter\PdoAdapter->fetchAll() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:248
PHP  11. Phinx\Db\Adapter\PdoAdapter->query() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:234
PHP  12. Phinx\Db\Adapter\PdoAdapter->getConnection() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:216
PHP  13. Phinx\Db\Adapter\MysqlAdapter->connect() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:157
PHP  14. Phinx\Db\Adapter\PdoAdapter->createSchemaTable() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/MysqlAdapter.php:79
PHP  15. Phinx\Db\Table->save() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:333
PHP  16. Phinx\Db\Table->create() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Table.php:556
PHP  17. Phinx\Db\Adapter\MysqlAdapter->createTable() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Table.php:513
PHP  18. Phinx\Db\Adapter\PdoAdapter->writeCommand() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/MysqlAdapter.php:238
PHP  19. Phinx\Db\Adapter\PdoAdapter->getOutput() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:186
PHP Fatal error:  Call to a member function writeln() on a non-object in /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php on line 186
PHP Stack trace:
PHP   1. {main}() /var/www/api/api-schools/vendor/robmorgan/phinx/bin/phinx:0
PHP   2. Symfony\Component\Console\Application->run() /var/www/api/api-schools/vendor/robmorgan/phinx/bin/phinx:54
PHP   3. Phinx\Console\PhinxApplication->doRun() /var/www/api/api-schools/vendor/symfony/console/Symfony/Component/Console/Application.php:106
PHP   4. Symfony\Component\Console\Application->doRun() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Console/PhinxApplication.php:81
PHP   5. Symfony\Component\Console\Command\Command->run() /var/www/api/api-schools/vendor/symfony/console/Symfony/Component/Console/Application.php:193
PHP   6. Phinx\Console\Command\Migrate->execute() /var/www/api/api-schools/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:240
PHP   7. Phinx\Migration\Manager->migrate() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Console/Command/Migrate.php:94
PHP   8. Phinx\Migration\Manager\Environment->getVersions() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Migration/Manager.php:137
PHP   9. Phinx\Db\Adapter\PdoAdapter->getVersions() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Migration/Manager/Environment.php:227
PHP  10. Phinx\Db\Adapter\PdoAdapter->fetchAll() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:248
PHP  11. Phinx\Db\Adapter\PdoAdapter->query() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:234
PHP  12. Phinx\Db\Adapter\PdoAdapter->getConnection() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:216
PHP  13. Phinx\Db\Adapter\MysqlAdapter->connect() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:157
PHP  14. Phinx\Db\Adapter\PdoAdapter->createSchemaTable() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/MysqlAdapter.php:79
PHP  15. Phinx\Db\Table->save() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:333
PHP  16. Phinx\Db\Table->create() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Table.php:556
PHP  17. Phinx\Db\Adapter\MysqlAdapter->createTable() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Table.php:513
PHP  18. Phinx\Db\Adapter\PdoAdapter->writeCommand() /var/www/api/api-schools/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/MysqlAdapter.php:238

Check out 0.2.1, change the default migration table name and try to migrate. You should be able to reproduce the issue.

Phinx Export Feature

It would be nice to run phinx --export to export a json/xml/SQL representation of your database migrations. What purpose would this serve though?

The unit tests rely on the phinx.yml template

The MigrateTest unit tests rely on the standard phinx.yml config file that ships with Phinx. These tests should use their own phinx.yml file or create an instance of the config object.

To reproduce rename ./phinx.yml then execute the tests:

php vendor/bin/phpunit --configuration tests/phpunit.xml

Setting default_migration_table is ignored and phinxlog table is used anyway.

I expected that setting default_migration_table would change what the database table is called in the database. That did not happen for me. After changing this setting in phinx.yml to version and then running my first migration, the migration info was still stored in a new table phinxlog, apparantly using the old value.

Part of my phinx.yml (anonymized)

paths:
    migrations: %%PHINX_CONFIG_DIR%%/../../../migrations

environments:
    default_migration_table: version
    default_database: development
    development:
        adapter: mysql
        host: localhost
        name: dev
        user: changetoyouruserhere
        pass: 'changetoyourpasswordhere'
        port: 3306

If it matters my setup looks like this: MySQL 5.5.16, PHP 5.4.3 on Windows 7. Not using Symfony for anything else. My migrations runs just fine otherwise.

Paths should be cleaned up

Phinx should use/print clean paths.

Instead of:

using config ././/phinx.yml
using migration path /home/ubuntu/apps/app1/releases/20120611112413/./scripts/migrations

Phinx should say:

using config ./phinx.yml
using migration path /home/ubuntu/apps/app1/releases/20120611112413/scripts/migrations

Add foreign key support

Phinx should include support for foreign keys including adding and deleting them.

Methods could be like: addForeignKey() or dropForeignKey().

Add a Phinx\Db\Table::addTimestamps() method

This method should automatically create two datetime columns: created_at and updated_at.

Call the following code:

$table->addColumn('created', 'datetime')
$table->addColumn('updated', 'datetime', array('null' => true, 'default' => null))

Add a --verbose parameter to increase console output

Add a -v / --verbose parameter to increase the level of console output.

When executing migrations each command should be printed to the console.

e.g:

 == 20121013151612 CreatePostsTable migrating
 -- createTable('posts')
    -> 1.0121s
 == 20121013151612 CreatePostsTable migrated 1.1352s

This option should be added to both the migrate and the rollback command.

Phinx user/machine global preferences

We need a way to store preferences for the user or globally on their machine.

At the moment it would be nice to open migrations in your favorite text edito, straight after creating them.

Specifying the phinx.yaml path is not working

Say i place my phinx.yml in the following path: /home/ghola/migration/config/phinx.yml
Then i go to my project which is in: /home/www/project, and attempt to run the following command from CLI (assumming phinx is in PATH):
phinx status -c="/home/ghola/migration/config/phinx.yml"

It will error out saying that it can't find the yml file. So i try look inside the code, and i see it's based on the cwd, so i try a path based on the cwd (remember i'm in the project directory):
phinx status -c="../../ghola/migration/config/phinx.yml"

The same error appears. I managed to make it working by changing my cwd to /home/ghola/migration/config/ and issuing the command from there (without any other path specification for the yml file)

Support for larger BLOB types in MySQL

With the current mapping of the binary type to use the "BLOB" type in MySQL restricts the column to 64KB of data.
Is there a way to use the other blob types "mediumblob" or "longblob" ?

Allow numbers in migration class names

In our system, each task/issue has a numerical ID, and as such it's easier to refer to the ID when naming the migration class (this way you know which task it originated from).

So, class names such as Feature49 should be permitted.

It's a simple regex change inside Util.php from:
return (bool) preg_match('/\b[A-Z][a-zA-Z](?:[a-z][a-zA-Z][A-Z]|[A-Z][a-zA-Z][a-z])[a-zA-Z]\b/', $className);
to
return (bool) preg_match('/\b[A-Z][a-z0-9A-Z](?:[a-z0-9][a-z0-9A-Z][A-Z]|[A-Z][a-z0-9A-Z][a-z0-9])[a-z0-9A-Z]\b/', $className);

Interactive init

Allow to specify the migrations dir and other configurations.
Also should create the migration dir if it does not exist.

Unsigned columns

I see no support for adding unsigned columns. The list of allowed column options is: 'limit', 'length', 'default', 'null', 'precision', 'scale', 'after'. Should i just be using plain SQL for this?

Add a test command

Add a test command to quickly verify the YAML configuration file and then the database connection

Introduce Phinx\Db\Table::getPendingColumns()

Add methods to Phinx\Db\Table

getPendingColumns()
setPendingColumns()

Move the logic from getColumns() & setColumns() to these methods. These old methods should actually return the table's columns!

Support for primary key

Support for defining custom primary key may be useful in some cases.

Currently phinx add ĂŹd` column as auto increment primary key.
It should be possible to override this behavior, so we can create tables w/out primary key, w/out auto increment.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.