Skip to content

Commit

Permalink
Merge aa937f6 into 70cb70b
Browse files Browse the repository at this point in the history
  • Loading branch information
josegonzalez committed Feb 15, 2015
2 parents 70cb70b + aa937f6 commit aaf0665
Show file tree
Hide file tree
Showing 24 changed files with 2,026 additions and 343 deletions.
94 changes: 41 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# Database migrations plugin for CakePHP

[![Build Status](https://api.travis-ci.org/cakephp/migrations.png)](https://travis-ci.org/cakephp/migrations)
[![License](https://poser.pugx.org/cakephp/migrations/license.svg)](https://packagist.org/packages/cakephp/migrations)
# cakephp/migrations [![Build Status](https://travis-ci.org/cakephp/migrations.svg?branch=master)](https://travis-ci.org/cakephp/migrations) [![License](https://poser.pugx.org/cakephp/migrations/license.svg)](https://packagist.org/packages/cakephp/migrations)

This is a pre-alpha version of a Database Migrations system for CakePHP 3.0. It is currently under development and should be considered experimental.

Expand Down Expand Up @@ -33,86 +30,75 @@ Additionally, you will need to configure the `default` database configuration in

## Usage

This plugins provides the Migrations shell that you can invoke from your application's root folder:
After creating/modifying a migration file, you can run your changes in the database by executing:

```bash
$ bin/cake migrations
```
# The following will run migrations against the default database connection
bin/cake migrations migrate

The command above will display a list of available options. Make sure you read [the official phinx documentation](http://docs.phinx.org/en/latest/migrations.html) to understand how migrations are created and executed in your database.
# Rolling back migrations. If a `change()` method is defined, it will be reversed.
# Otherwise, the `down()` method will be invoked
bin/cake migrations rollback

### Create a migration file with tables from your database
# Retrieve the status of executed and pending migrations
bin/cake migrations status

The [bake](https://github.com/cakephp/bake) command can be used to create a populated migration file based on the tables in your database:
# All console commands can take a `--plugin` or `-p` option
bin/cake migrations status -p PluginName

```bash
$ bin/cake bake migration Initial [-p PluginName] [-c connection]
# You can also scope a command to a connection via the `--connection` or `-c` option
bin/cake migrations status -c my_datasource
```

This will create a phinx file with tables found in your database. By default,
this will just add tables that have model files, but you can create a file with
all tables by adding the option `--checkModel false`.
### Creating Migrations

### Create an empty migration file
This plugin provides two interfaces to creating migrations: a passthru to the Phinx library and a way to use the `bake` utility.

To create an empty migration file, execute:
#### Phinx interface

The Phinx Migrations shell can be invoked via the following command from your application's root folder:

```bash
$ bin/cake migrations create Name
$ bin/cake migrations
```

This will create a file under `config/Migrations` that you can edit to complete
the migration steps as documented in phinx's manual.

Please note that you will need to learn how to write your own migrations, you
need to fill in the up() and down() or change() methods if you want
automatically reversible migrations.
The command above will display a list of available options. Make sure you read [the official phinx documentation](http://docs.phinx.org/en/latest/migrations.html) to understand how migrations are created and executed in your database.

Once again, please make sure you read [the official phinx
documentation](http://docs.phinx.org/en/latest/migrations.html) to understand
how migrations are created and executed in your database.
Please note that you need to learn how to write your own migrations.

### Run the migration
Empty migrations files will be created leaving you to fill in the up() and down() or change() if you want automatically reversible migrations.

After modifying the migration file, you can run your changes in the database by executing:
Once again, please make sure you read [the official phinx documentation](http://docs.phinx.org/en/latest/migrations.html) to understand how migrations are created and executed in your database.

```bash
$ bin/cake migrations migrate
```

### Rollback a migration
#### Bake interface

If you added any steps to revert a migration in the `down()` callback, you can execute this command and have that function executed:
You can also use the `bake` command to generate migrations.

```bash
$ bin/cake migrations rollback
```

### Watch migrations status
# The following will create an initial snapshot migration file:
bin/cake bake migration Initial --snapshot

By executing this command you will have an overview of the migrations that have been executed and those still pending to be run:

```bash
$ bin/cake migrations status
```
# Create an empty migration file
bin/cake bake migration AddFieldToTable

### Usage for plugins
# You can specify a plugin to bake into
bin/cake bake migration AddFieldToTable --plugin PluginName

All the commands from above support the `--plugin` or `-p` option:
# You can specify an alternative connection when generating a migration.
bin/cake bake migration AddFieldToTable --connection connection

```bash
$ bin/cake migrations status -p PluginName
# Require that the table class exists before creating a migration
bin/cake bake migration AddFieldToTable --require-table
```

### Usage for connections
These commands will create a file under `config/Migrations` with the current database snapshot as the contents of the `change()` method. You may edit this as desired.

All the commands from above support the `--connection` or `-c` option:
Please note that you will need to learn how to write your own migrations, you need to fill in the up() and down() or change() methods if you want automatically reversible migrations.

```bash
$ bin/cake migrations migrate -c my_datasource
```
Once again, please make sure you read [the official phinx documentation](http://docs.phinx.org/en/latest/migrations.html) to understand how migrations are created and executed in your database.

### Usage for custom primary key id in tables
#### Usage for custom primary key id in tables

To create a table called `statuses` and use a CHAR (36) for the `id` field, this requires you to turn off the id.

Expand All @@ -129,3 +115,5 @@ $table->addColumn('id', 'char', ['limit' => 36])
->addColumn('model', 'string', ['limit' => 128])
->create();
```

> Phinx automatically creates an auto-increment `id` field for *every* table. This will hopefully be fixed in the future.
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
"homepage": "https://github.com/cakephp/migrations/graphs/contributors"
}
],
"extra": {
"installer-name": "Migrations"
},
"support": {
"issues": "https://github.com/cakephp/migrations/issues",
"forum": "http://stackoverflow.com/tags/cakephp",
Expand All @@ -36,6 +33,7 @@
},
"autoload-dev": {
"psr-4": {
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests",
"Migrations\\Test\\": "tests"
}
},
Expand Down
50 changes: 35 additions & 15 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
syntaxCheck="false">
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php"
>

<testsuites>
<testsuite name="Migrations">
<directory>./tests/</directory>
<testsuite name="Migrations Test Suite">
<directory>./tests/TestCase</directory>
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener
class="\Cake\TestSuite\Fixture\FixtureInjector"
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
<arguments>
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
</arguments>
</listener>
</listeners>

<!-- Prevent coverage reports from looking in tests and vendors -->
<filter>
<blacklist>
<directory suffix=".php">./vendor/</directory>
<directory suffix=".ctp">./vendor/</directory>

<directory suffix=".php">./tests/</directory>
<directory suffix=".ctp">./tests/</directory>
</blacklist>
</filter>

</phpunit>
2 changes: 1 addition & 1 deletion src/ConfigurationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getConfig()
'pass' => isset($config['password']) ? $config['password'] : null,
'port' => isset($config['port']) ? $config['port'] : null,
'name' => $config['database'],
'charset' => $config['encoding']
'charset' => isset($config['encoding']) ? $config['encoding'] : null,
]
]
]);
Expand Down
145 changes: 145 additions & 0 deletions src/Shell/Task/MigrationSnapshotTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Migrations\Shell\Task;

use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Filesystem\File;
use Migrations\Shell\Task\SimpleMigrationTask;

/**
* Task class for generating migration snapshot files.
*/
class MigrationSnapshotTask extends SimpleMigrationTask
{
/**
* Tables to skip
*
* @var array
*/
public $skipTables = ['i18n', 'phinxlog'];

/**
* Regex of Table name to skip
*
* @var string
*/
public $skipTablesRegex = '_phinxlog';

/**
* {@inheritDoc}
*/
public function template()
{
return 'Migrations.config/snapshot';
}

public function templateData()
{
$ns = Configure::read('App.namespace');
$pluginPath = '';
if ($this->plugin) {
$ns = $this->plugin;
$pluginPath = $this->plugin . '.';
}

$collection = $this->getCollection($this->connection);

$tables = $collection->listTables();
foreach ($tables as $num => $table) {
if ((in_array($table, $this->skipTables)) || (strpos($table, $this->skipTablesRegex) !== false)) {
unset($tables[$num]);
continue;
}

if (!$this->tableToAdd($table, $this->plugin)) {
unset($tables[$num]);
continue;
}
}

return [
'plugin' => $this->plugin,
'pluginPath' => $pluginPath,
'namespace' => $ns,
'collection' => $collection,
'tables' => $tables,
'action' => 'create_table',
'name' => $this->Template->viewVars['name'],
];
}

/**
* To check if a Table Model is to be added in the migration file
*
* @param string $tableName Table name in underscore case
* @param string $pluginName Plugin name if exists
* @return bool true if the model is to be added
*/
public function tableToAdd($tableName, $pluginName = null)
{
if ($this->params['require-table'] === true) {
return $this->tableExists(Inflector::camelize($tableName), $pluginName);
}

return true;
}

/**
* To check if a Table Model exists in the path of model
*
* @param string $tableName Table name in underscore case
* @param string $pluginName Plugin name if exists
* @return bool
*/
public function tableExists($tableName, $pluginName = null)
{
$file = new File($this->getModelPath($pluginName) . $tableName . 'Table.php');
return $file->exists();
}

/**
* Path for Table folder
*
* @param string $pluginName Plugin name if exists
* @return string : path to Table Folder. Default to App Table Path
*/
public function getModelPath($pluginName = null)
{
if (!is_null($pluginName) && Plugin::loaded($pluginName)) {
return Plugin::classPath($pluginName) . 'Model' . DS . 'Table' . DS;
}
return APP . 'Model' . DS . 'Table' . DS;
}

/**
* Gets the option parser instance and configures it.
*
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser()
{
$parser = parent::getOptionParser();

$parser->description(
'Bake migration snapshot class.'
)->addOption('require-table', [
'boolean' => true,
'default' => false,
'help' => 'If require-table is set to true, check also that the table class exists.'
]);

return $parser;
}
}
Loading

0 comments on commit aaf0665

Please sign in to comment.