Skip to content

Commit

Permalink
Merge pull request #174 from cakephp/plugin-snapshot
Browse files Browse the repository at this point in the history
Fix baking a plugin snapshot
  • Loading branch information
lorenzo committed Dec 25, 2015
2 parents 6d395c2 + 7749dcb commit 15d246c
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 10 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Full documentation of the plugin can be found on the [CakePHP Cookbook](http://b
## Installation

You can install this plugin into your CakePHP application using
[composer](http://getcomposer.org).
[composer](http://getcomposer.org).

Run the following command
```sh
Expand Down Expand Up @@ -123,6 +123,11 @@ automatically reversible migrations.

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.

Be aware that when baking a snapshot for a plugin, your plugin must implement
model Table classes matching the database tables you want to be in the snapshot :
only those tables will be exported. This is the only way to filter your plugin's
tables from you app tables if you are using the same database for both.

#### 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 @@ -146,7 +151,7 @@ $table->addColumn('id', 'char', ['limit' => 36])
#### Collations

If you need to create a table with a different collation than the database default one, you can define it
with the ``table`` method, as an option :
with the ``table`` method, as an option :

```php
$table = $this
Expand Down
2 changes: 1 addition & 1 deletion src/Shell/Task/MigrationSnapshotTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function templateData()
$collection = $this->getCollection($this->connection);
$tables = $collection->listTables();

if ($this->params['require-table'] === true) {
if ($this->params['require-table'] === true || $this->plugin) {
$tableNamesInModel = $this->getTableNames($this->plugin);

foreach ($tableNamesInModel as $num => $table) {
Expand Down
35 changes: 28 additions & 7 deletions tests/TestCase/Shell/Task/MigrationSnapshotTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,25 @@ public function setUp()
{
parent::setUp();
$this->_compareBasePath = Plugin::path('Migrations') . 'tests' . DS . 'comparisons' . DS . 'Migration' . DS;
$this->Task = $this->getTaskMock();
}

public function getTaskMock($mockedMethods = [])
{
$mockedMethods = $mockedMethods ?: ['in', 'err', 'dispatchShell', '_stop', 'findTables', 'fetchTableName'];
$inputOutput = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);

$this->Task = $this->getMock(
$task = $this->getMock(
'Migrations\Shell\Task\MigrationSnapshotTask',
['in', 'err', 'dispatchShell', '_stop', 'findTables', 'fetchTableName'],
$mockedMethods,
[$inputOutput]
);
$this->Task->name = 'Migration';
$this->Task->connection = 'test';
$this->Task->BakeTemplate = new BakeTemplateTask($inputOutput);
$this->Task->BakeTemplate->initialize();
$this->Task->BakeTemplate->interactive = false;
$task->name = 'Migration';
$task->connection = 'test';
$task->BakeTemplate = new BakeTemplateTask($inputOutput);
$task->BakeTemplate->initialize();
$task->BakeTemplate->interactive = false;
return $task;
}

public function testGetTableNames()
Expand Down Expand Up @@ -108,6 +115,20 @@ public function testAutoIdDisabledSnapshot()
$this->assertCorrectSnapshot($bakeName, $result);
}

public function testPluginBlog()
{
$task = $this->getTaskMock(['in', 'err', 'dispatchShell', '_stop']);
$task->params['require-table'] = false;
$task->params['connection'] = 'test';
$task->params['plugin'] = 'Blog';
$task->plugin = 'Blog';

$bakeName = $this->getBakeName('TestPluginBlog');
$result = $task->bake($bakeName);

$this->assertCorrectSnapshot($bakeName, $result);
}

public function getBakeName($name)
{
$dbenv = getenv("DB");
Expand Down
83 changes: 83 additions & 0 deletions tests/comparisons/Migration/pgsql/test_plugin_blog_pgsql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
use Migrations\AbstractMigration;

class TestPluginBlogPgsql extends AbstractMigration
{
public function up()
{
$table = $this->table('articles');
$table
->addColumn('title', 'string', [
'comment' => 'Article title',
'default' => 'NULL::character varying',
'limit' => 255,
'null' => true,
])
->addColumn('category_id', 'integer', [
'default' => null,
'limit' => 10,
'null' => true,
])
->addColumn('product_id', 'integer', [
'default' => null,
'limit' => 10,
'null' => true,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addColumn('modified', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addIndex(
[
'category_id',
]
)
->addIndex(
[
'product_id',
]
)
->create();

$this->table('articles')
->addForeignKey(
'category_id',
'categories',
'id',
[
'update' => 'CASCADE',
'delete' => 'CASCADE'
]
)
->addForeignKey(
'product_id',
'products',
'id',
[
'update' => 'CASCADE',
'delete' => 'CASCADE'
]
)
->update();

}

public function down()
{
$this->table('articles')
->dropForeignKey(
'category_id'
)
->dropForeignKey(
'product_id'
);

$this->dropTable('articles');
}
}
82 changes: 82 additions & 0 deletions tests/comparisons/Migration/sqlite/test_plugin_blog_sqlite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
use Migrations\AbstractMigration;

class TestPluginBlogSqlite extends AbstractMigration
{
public function up()
{
$table = $this->table('articles');
$table
->addColumn('title', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('category_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
])
->addColumn('product_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addColumn('modified', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addIndex(
[
'category_id',
]
)
->addIndex(
[
'product_id',
]
)
->create();

$this->table('articles')
->addForeignKey(
'category_id',
'categories',
'id',
[
'update' => 'CASCADE',
'delete' => 'CASCADE'
]
)
->addForeignKey(
'product_id',
'products',
'id',
[
'update' => 'CASCADE',
'delete' => 'CASCADE'
]
)
->update();

}

public function down()
{
$this->table('articles')
->dropForeignKey(
'category_id'
)
->dropForeignKey(
'product_id'
);

$this->dropTable('articles');
}
}
83 changes: 83 additions & 0 deletions tests/comparisons/Migration/test_plugin_blog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
use Migrations\AbstractMigration;

class TestPluginBlog extends AbstractMigration
{
public function up()
{
$table = $this->table('articles');
$table
->addColumn('title', 'string', [
'comment' => 'Article title',
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('category_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
])
->addColumn('product_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addColumn('modified', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addIndex(
[
'category_id',
]
)
->addIndex(
[
'product_id',
]
)
->create();

$this->table('articles')
->addForeignKey(
'category_id',
'categories',
'id',
[
'update' => 'CASCADE',
'delete' => 'CASCADE'
]
)
->addForeignKey(
'product_id',
'products',
'id',
[
'update' => 'CASCADE',
'delete' => 'CASCADE'
]
)
->update();

}

public function down()
{
$this->table('articles')
->dropForeignKey(
'category_id'
)
->dropForeignKey(
'product_id'
);

$this->dropTable('articles');
}
}
Empty file removed tests/test_app/Plugin/Blog/empty
Empty file.
29 changes: 29 additions & 0 deletions tests/test_app/Plugin/Blog/src/Model/Entity/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Pluginapp\Model\Entity;

use Cake\ORM\Entity;

/**
* Article Entity.
*
* @property int $id
* @property string $title
* @property string $content
*/
class Article extends Entity
{

/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array
*/
protected $_accessible = [
'*' => true,
'id' => false,
];
}
Loading

0 comments on commit 15d246c

Please sign in to comment.