Skip to content

Commit

Permalink
Merge pull request #198 from cakephp/signed-attribute
Browse files Browse the repository at this point in the history
Include the "unsigned" attribute in snapshot
  • Loading branch information
lorenzo committed Feb 9, 2016
2 parents f5a338a + d5a4820 commit 7ac1946
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Template/Bake/config/snapshot.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@

use Cake\Database\Schema\Table;

$wantedOptions = array_flip(['length', 'limit', 'default', 'unsigned', 'null', 'comment', 'autoIncrement', 'precision']);
$wantedOptions = array_flip(['length', 'limit', 'default', 'signed', 'null', 'comment', 'autoIncrement', 'precision']);
$tableMethod = $this->Migration->tableMethod($action);
$columnMethod = $this->Migration->columnMethod($action);
$indexMethod = $this->Migration->indexMethod($action);
$constraints = $foreignKeys = $dropForeignKeys = [];
$hasUnsignedPk = $this->Migration->hasUnsignedPrimaryKey($tables);

if ($autoId && $hasUnsignedPk) {
$autoId = false;
}
%>
<?php
use Migrations\AbstractMigration;
Expand Down Expand Up @@ -59,6 +64,9 @@ class <%= $name %> extends AbstractMigration
if (empty($columnOptions['precision'])) {
unset($columnOptions['precision']);
}
if (isset($columnOptions['signed']) && $columnOptions['signed'] === true) {
unset($columnOptions['signed']);
}
echo $this->Migration->stringifyList($columnOptions, ['indent' => 4]);
%>])
<%- endforeach;
Expand All @@ -76,6 +84,9 @@ class <%= $name %> extends AbstractMigration
if (empty($columnOptions['autoIncrement'])) {
unset($columnOptions['autoIncrement']);
}
if (isset($columnOptions['signed']) && $columnOptions['signed'] === true) {
unset($columnOptions['signed']);
}
if (empty($columnOptions['precision'])) {
unset($columnOptions['precision']);
} else {
Expand Down
25 changes: 25 additions & 0 deletions src/View/Helper/MigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,31 @@ public function primaryKeys($table)
return $primaryKeys;
}

/**
* Returns whether the $tables list given as arguments contains primary keys
* unsigned.
*
* @param array $tables List of tables to check
* @return bool
*/
public function hasUnsignedPrimaryKey($tables)
{
foreach ($tables as $table) {
$collection = $this->config('collection');
$tableSchema = $collection->describe($table);
$tablePrimaryKeys = $tableSchema->primaryKey();

foreach ($tablePrimaryKeys as $primaryKey) {
$column = $tableSchema->column($primaryKey);
if (isset($column['unsigned']) && $column['unsigned'] === true) {
return true;
}
}
}

return false;
}

/**
* Returns the primary key columns name for a given table
*
Expand Down
1 change: 1 addition & 0 deletions tests/Fixture/ArticlesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ArticlesFixture extends TestFixture
'title' => ['type' => 'string', 'null' => true, 'length' => 255, 'comment' => 'Article title'],
'category_id' => ['type' => 'integer', 'length' => 11],
'product_id' => ['type' => 'integer', 'length' => 11],
'counter' => ['type' => 'integer', 'length' => 11, 'unsigned' => true],
'created' => ['type' => 'timestamp', 'null' => true, 'default' => null],
'modified' => ['type' => 'timestamp', 'null' => true, 'default' => null],
'_indexes' => [
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/Shell/Task/MigrationSnapshotTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Bake\Shell\Task\BakeTemplateTask;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Database\Schema\Table;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\StringCompareTrait;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -147,6 +148,19 @@ public function testAutoIdDisabledSnapshot()
*/
public function testPluginBlog()
{
$db = ConnectionManager::get('test');
$collection = $db->schemaCollection();
$table = new Table('parts', [
'id' => ['type' => 'integer', 'unsigned' => true],
'name' => ['type' => 'string', 'length' => 255],
'number' => ['type' => 'integer', 'null' => true, 'length' => 10, 'unsigned' => true]
]);
$table->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
$sql = $table->createSql($db);
foreach ($sql as $stmt) {
$db->execute($stmt);
}

$task = $this->getTaskMock(['in', 'err', 'dispatchShell', '_stop']);
$task->params['require-table'] = false;
$task->params['connection'] = 'test';
Expand All @@ -157,6 +171,11 @@ public function testPluginBlog()
$result = $task->bake($bakeName);

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

$sql = $table->dropSql($db);
foreach ($sql as $stmt) {
$db->execute($stmt);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function up()
'limit' => 10,
'null' => true,
])
->addColumn('counter', 'integer', [
'default' => null,
'limit' => 10,
'null' => true,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public function up()
'limit' => 10,
'null' => true,
])
->addColumn('counter', 'integer', [
'default' => null,
'limit' => 10,
'null' => true,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
Expand Down
20 changes: 20 additions & 0 deletions tests/comparisons/Migration/pgsql/test_plugin_blog_pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public function up()
'limit' => 10,
'null' => true,
])
->addColumn('counter', 'integer', [
'default' => null,
'limit' => 10,
'null' => true,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
Expand Down Expand Up @@ -85,6 +90,20 @@ public function up()
)
->create();

$table = $this->table('parts');
$table
->addColumn('name', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('number', 'integer', [
'default' => null,
'limit' => 10,
'null' => true,
])
->create();

$this->table('articles')
->addForeignKey(
'category_id',
Expand Down Expand Up @@ -120,5 +139,6 @@ public function down()

$this->dropTable('articles');
$this->dropTable('categories');
$this->dropTable('parts');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public function up()
'limit' => 11,
'null' => true,
])
->addColumn('counter', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
'signed' => false,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public function up()
'limit' => 11,
'null' => true,
])
->addColumn('counter', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
'signed' => false,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
Expand Down
22 changes: 22 additions & 0 deletions tests/comparisons/Migration/sqlite/test_plugin_blog_sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public function up()
'limit' => 11,
'null' => true,
])
->addColumn('counter', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
'signed' => false,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
Expand Down Expand Up @@ -84,6 +90,21 @@ public function up()
)
->create();

$table = $this->table('parts');
$table
->addColumn('name', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('number', 'integer', [
'default' => null,
'limit' => 10,
'null' => true,
'signed' => false,
])
->create();

$this->table('articles')
->addForeignKey(
'category_id',
Expand Down Expand Up @@ -119,5 +140,6 @@ public function down()

$this->dropTable('articles');
$this->dropTable('categories');
$this->dropTable('parts');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public function up()
'limit' => 11,
'null' => true,
])
->addColumn('counter', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
'signed' => false,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
Expand Down
6 changes: 6 additions & 0 deletions tests/comparisons/Migration/test_not_empty_snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public function up()
'limit' => 11,
'null' => true,
])
->addColumn('counter', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
'signed' => false,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
Expand Down
47 changes: 47 additions & 0 deletions tests/comparisons/Migration/test_plugin_blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@

class TestPluginBlog extends AbstractMigration
{

public $autoId = false;

public function up()
{
$table = $this->table('articles');
$table
->addColumn('id', 'integer', [
'autoIncrement' => true,
'default' => null,
'limit' => 11,
'null' => false,
])
->addPrimaryKey(['id'])
->addColumn('title', 'string', [
'comment' => 'Article title',
'default' => null,
Expand All @@ -23,6 +33,12 @@ public function up()
'limit' => 11,
'null' => true,
])
->addColumn('counter', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
'signed' => false,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
Expand Down Expand Up @@ -52,6 +68,13 @@ public function up()

$table = $this->table('categories');
$table
->addColumn('id', 'integer', [
'autoIncrement' => true,
'default' => null,
'limit' => 11,
'null' => false,
])
->addPrimaryKey(['id'])
->addColumn('parent_id', 'integer', [
'default' => null,
'limit' => 11,
Expand Down Expand Up @@ -85,6 +108,29 @@ public function up()
)
->create();

$table = $this->table('parts');
$table
->addColumn('id', 'integer', [
'autoIncrement' => true,
'default' => null,
'limit' => 10,
'null' => false,
'signed' => false,
])
->addPrimaryKey(['id'])
->addColumn('name', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('number', 'integer', [
'default' => null,
'limit' => 10,
'null' => true,
'signed' => false,
])
->create();

$this->table('articles')
->addForeignKey(
'category_id',
Expand Down Expand Up @@ -120,5 +166,6 @@ public function down()

$this->dropTable('articles');
$this->dropTable('categories');
$this->dropTable('parts');
}
}
12 changes: 12 additions & 0 deletions tests/test_app/Plugin/TestBlog/src/Model/Table/PartsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace TestBlog\Model\Table;

use Cake\ORM\Table;

/**
* Articles Model
*
*/
class PartsTable extends Table
{
}

0 comments on commit 7ac1946

Please sign in to comment.