Skip to content

Commit

Permalink
Merge pull request #34 from cakephp/migration-generation
Browse files Browse the repository at this point in the history
Migration generation
  • Loading branch information
markstory committed Feb 21, 2015
2 parents 70cb70b + b334a85 commit b4f5a77
Show file tree
Hide file tree
Showing 26 changed files with 2,160 additions and 349 deletions.
22 changes: 15 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ sudo: false
env:
matrix:
- DB=mysql db_dsn='mysql://travis@0.0.0.0/cakephp_test'
- DB=pgsql db_dsn='postgres://postgres@127.0.0.1/cakephp_test'
- DB=sqlite db_dsn='sqlite:///:memory:'
global:
- DEFAULT=1

matrix:
allow_failures:
- php: hhvm

- php: hhvm-nightly
- env: DB=pgsql db_dsn='postgres://postgres@127.0.0.1/cakephp_test'

fast_finish: true

Expand All @@ -30,17 +29,26 @@ matrix:
- php: 5.4
env: COVERALLS=1 DEFAULT=0

- php: 5.4
env: DB=pgsql db_dsn='postgres://postgres@127.0.0.1/cakephp_test'

- php: 5.5
env: DB=pgsql db_dsn='postgres://postgres@127.0.0.1/cakephp_test'

- php: 5.6
env: DB=pgsql db_dsn='postgres://postgres@127.0.0.1/cakephp_test'

- php: hhvm
env: HHVM=1 DB=sqlite db_class='Cake\Database\Driver\Sqlite' db_dsn='sqlite::memory:'
env: HHVM=1 DB=sqlite db_dsn='sqlite:///:memory:'

- php: hhvm
env: HHVM=1 DB=mysql db_class='Cake\Database\Driver\Mysql' db_dsn='mysql:host=0.0.0.0;dbname=cakephp_test' db_database='cakephp_test' db_login='travis' db_password=''
env: HHVM=1 DB=mysql db_dsn='mysql://travis@0.0.0.0/cakephp_test'

- php: hhvm-nightly
env: HHVM=1 DB=sqlite db_class='Cake\Database\Driver\Sqlite' db_dsn='sqlite::memory:'
env: HHVM=1 DB=sqlite db_dsn='sqlite:///:memory:'

- php: hhvm-nightly
env: HHVM=1 DB=mysql db_class='Cake\Database\Driver\Mysql' db_dsn='mysql:host=0.0.0.0;dbname=cakephp_test' db_database='cakephp_test' db_login='travis' db_password=''
env: HHVM=1 DB=mysql db_dsn='mysql://travis@0.0.0.0/cakephp_test'

before_script:
- composer self-update
Expand All @@ -61,7 +69,7 @@ before_script:

script:
- sh -c "if [ '$DEFAULT' = '1' ]; then phpunit --stderr; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -p --extensions=php --standard=psr2 --ignore=tests/bootstrap.php ./src ./tests ; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -p --extensions=php --standard=psr2 --ignore=tests/bootstrap.php,tests/comparisons/* ./src ./tests ; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then phpunit --stderr --coverage-clover build/logs/clover.xml; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then php vendor/bin/coveralls -c .coveralls.yml -v; fi"

Expand Down
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>
7 changes: 6 additions & 1 deletion src/Command/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ protected function configure()
->addOption('connection', 'c', InputArgument::OPTIONAL, 'The datasource connection to use')
->addOption('source', 's', InputArgument::OPTIONAL, 'The folder where migrations are in')
->addOption('template', 't', InputOption::VALUE_REQUIRED, 'Use an alternative template')
->addOption('class', 'l', InputOption::VALUE_REQUIRED, 'Use a class implementing "' . parent::CREATION_INTERFACE . '" to generate the template');
->addOption(
'class',
'l',
InputOption::VALUE_REQUIRED,
'Use a class implementing "' . parent::CREATION_INTERFACE . '" to generate the template'
);
}
}
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
Loading

0 comments on commit b4f5a77

Please sign in to comment.