Skip to content

Commit

Permalink
Merge pull request #94 from HavokInspiration/bake-composite-fk
Browse files Browse the repository at this point in the history
Add composite foreign key constraints generation when baking a snapshot
  • Loading branch information
lorenzo committed Jul 6, 2015
2 parents 47c86f0 + ea5b173 commit dc9c5b3
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/Template/Bake/config/snapshot.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,22 @@ class <%= $name %> extends AbstractMigration
$constraintColumns = $constraint['columns'];
sort($constraintColumns);
if ($constraint['type'] !== 'unique'):
$foreignKeys[] = $constraint['columns'][0]; %>
$foreignKeys += $constraint['columns'];

$columnsList = '\'' . $constraint['columns'][0] . '\'';
if (count($constraint['columns']) > 1):
$columnsList = '[' . $this->Migration->stringifyList($constraint['columns'], ['indent' => 5]) . ']';
endif;

if (is_array($constraint['references'][1])):
$columnsReference = '[' . $this->Migration->stringifyList($constraint['references'][1], ['indent' => 5]) . ']';
else:
$columnsReference = '\'' . $constraint['references'][1] . '\'';
endif; %>
->addForeignKey(
'<%= $constraint['columns'][0] %>',
<%= $columnsList %>,
'<%= $constraint['references'][0] %>',
'<%= $constraint['references'][1] %>',
<%= $columnsReference %>,
[
'update' => '<%= $constraint['update'] %>',
'delete' => '<%= $constraint['delete'] %>'
Expand Down
58 changes: 58 additions & 0 deletions tests/Fixture/OrdersFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

/**
* Class OrdersFixture
*
*/
class OrdersFixture extends TestFixture
{

/**
* {@inheritDoc}
*/
public $table = 'orders';

/**
* fields property
*
* @var array
*/
public $fields = [
'id' => ['type' => 'integer'],
'product_category' => ['type' => 'integer', 'null' => false, 'length' => 11],
'product_id' => ['type' => 'integer', 'null' => false, 'length' => 11],
'_indexes' => [
'product_category' => [
'type' => 'index',
'columns' => ['product_category', 'product_id']
]
],
'_constraints' => [
'primary' => [
'type' => 'primary', 'columns' => ['id']
],
'product_id_fk' => [
'type' => 'foreign',
'columns' => ['product_category', 'product_id'],
'references' => ['products', ['category_id', 'id']],
'update' => 'cascade',
'delete' => 'cascade',
]
]
];
}
33 changes: 32 additions & 1 deletion tests/TestCase/Shell/Task/MigrationSnapshotTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Migrations\Test\TestCase\Shell\Task;

use Bake\Shell\Task\BakeTemplateTask;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\TestSuite\StringCompareTrait;
use Cake\TestSuite\TestCase;
Expand All @@ -30,10 +31,13 @@ class MigrationSnapshotTaskTest extends TestCase
'plugin.migrations.special_tags',
'plugin.migrations.special_pk',
'plugin.migrations.composite_pk',
'plugin.migrations.products',
'plugin.migrations.categories',
'plugin.migrations.products'
'plugin.migrations.orders'
];

public $autoFixtures = false;

/**
* setup method
*
Expand All @@ -59,6 +63,15 @@ public function setUp()

public function testNotEmptySnapshot()
{
$this->loadFixtures(
'Users',
'SpecialTags',
'SpecialPk',
'CompositePk',
'Categories',
'Products'
);

$this->Task->params['require-table'] = false;
$this->Task->params['connection'] = 'test';
$this->Task->params['plugin'] = 'BogusPlugin';
Expand All @@ -77,4 +90,22 @@ public function testNotEmptySnapshot()
$result = $this->Task->bake('NotEmptySnapshot');
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

public function testCompositeConstraintsSnapshot()
{
$this->skipIf(
version_compare(Configure::version(), '3.0.8', '<'),
'Cannot run "testCompositeConstraintsSnapshot" because CakePHP Core feature' .
'is not implemented in this version'
);

$this->loadFixtures(
'Orders'
);

$this->Task->params['require-table'] = false;
$this->Task->params['connection'] = 'test';
$result = $this->Task->bake('CompositeConstraintsSnapshot');
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
require_once 'vendor/cakephp/cakephp/src/basics.php';
require_once 'vendor/autoload.php';

define('CORE_PATH', $root . DS . 'vendor' . DS . 'cakephp' . DS . 'cakephp' . DS);
define('ROOT', $root . DS . 'tests' . DS . 'test_app' . DS);
define('APP', ROOT . 'App' . DS);
define('TMP', sys_get_temp_dir() . DS);
Expand Down
208 changes: 208 additions & 0 deletions tests/comparisons/Migration/testCompositeConstraintsSnapshot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
<?php
use Phinx\Migration\AbstractMigration;

class CompositeConstraintsSnapshot extends AbstractMigration
{
public function up()
{
$table = $this->table('categories');
$table
->addColumn('parent_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
])
->addColumn('title', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('slug', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addColumn('modified', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addIndex(
[
'slug',
],
['unique' => true]
)
->create();
$table = $this->table('composite_pks', ['id' => false, 'primary_key' => ['id', 'name']]);
$table
->addColumn('id', 'uuid', [
'default' => '',
'limit' => null,
'null' => false,
])
->addColumn('name', 'string', [
'default' => '',
'limit' => 50,
'null' => false,
])
->create();
$table = $this->table('orders');
$table
->addColumn('product_category', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
])
->addColumn('product_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
])
->addForeignKey(
[
'product_category',
'product_id',
],
'products',
[
'category_id',
'id',
],
[
'update' => 'CASCADE',
'delete' => 'CASCADE'
]
)
->create();
$table = $this->table('products');
$table
->addColumn('title', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('slug', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('category_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(
[
'slug',
],
['unique' => true]
)
->addForeignKey(
'category_id',
'categories',
'id',
[
'update' => 'CASCADE',
'delete' => 'CASCADE'
]
)
->create();
$table = $this->table('special_pks', ['id' => false, 'primary_key' => ['id']]);
$table
->addColumn('id', 'uuid', [
'default' => '',
'limit' => null,
'null' => false,
])
->addColumn('name', 'string', [
'default' => null,
'limit' => 256,
'null' => true,
])
->create();
$table = $this->table('special_tags');
$table
->addColumn('article_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
])
->addColumn('author_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
])
->addColumn('tag_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
])
->addColumn('highlighted', 'boolean', [
'default' => null,
'limit' => null,
'null' => true,
])
->addColumn('highlighted_time', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addIndex(
[
'article_id',
],
['unique' => true]
)
->create();
$table = $this->table('users');
$table
->addColumn('username', 'string', [
'default' => null,
'limit' => 256,
'null' => true,
])
->addColumn('password', 'string', [
'default' => null,
'limit' => 256,
'null' => true,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addColumn('updated', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->create();
}

public function down()
{
$this->dropTable('categories');
$this->dropTable('composite_pks');
$this->dropTable('orders');
$this->dropTable('products');
$this->dropTable('special_pks');
$this->dropTable('special_tags');
$this->dropTable('users');
}
}

0 comments on commit dc9c5b3

Please sign in to comment.