-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from HavokInspiration/bake-composite-fk
Add composite foreign key constraints generation when baking a snapshot
- Loading branch information
Showing
5 changed files
with
313 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
] | ||
] | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 208 additions & 0 deletions
208
tests/comparisons/Migration/testCompositeConstraintsSnapshot.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |