Skip to content

Commit

Permalink
Fix int, bigint limit and decimal precision
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed May 4, 2019
1 parent ecc105f commit b032226
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 18 deletions.
53 changes: 36 additions & 17 deletions src/Migration/Adapter/Generator/PhinxMySqlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ protected function getPhinxColumnType(array $columnData): string
'smallint' => 'integer',
'int' => 'integer',
'mediumint' => 'integer',
'bigint' => 'integer',
'bigint' => 'biginteger',
'tinytext' => 'text',
'mediumtext' => 'text',
'longtext' => 'text',
Expand Down Expand Up @@ -812,25 +812,44 @@ protected function getColumnLimit(array $columnData): string
*/
protected function getPhinxColumnOptionsNumeric(array $attributes, array $columnData): array
{
// For decimal columns
if (!empty($columnData['NUMERIC_PRECISION'])) {
$attributes['precision'] = $columnData['NUMERIC_PRECISION'];
}
if (!empty($columnData['NUMERIC_SCALE'])) {
$attributes['scale'] = $columnData['NUMERIC_SCALE'];
}
$dataType = $columnData['DATA_TYPE'];

// signed enable or disable the unsigned option (only applies to MySQL)
$match = null;
$pattern = '/\(\d+\) unsigned$/';
if (preg_match($pattern, $columnData['COLUMN_TYPE'], $match) === 1) {
$attributes['signed'] = false;
$intDefaultLimits = [
'int' => '11',
'bigint' => '20',
];

// For integer and biginteger columns
if ($dataType === 'int' || $dataType === 'bigint') {
$match = null;
if (preg_match('/\((\d+)\)/', $columnData['COLUMN_TYPE'], $match) === 1) {
if ($match[1] !== $intDefaultLimits[$dataType]) {
$attributes['limit'] = $match[1];
}
}

// signed enable or disable the unsigned option (only applies to MySQL)
$match = null;
$pattern = '/\(\d+\) unsigned$/';
if (preg_match($pattern, $columnData['COLUMN_TYPE'], $match) === 1) {
$attributes['signed'] = false;
}

// identity enable or disable automatic incrementing
if ($columnData['EXTRA'] == 'auto_increment') {
$attributes['identity'] = 'enable';
}
}

// For integer and biginteger columns:
// identity enable or disable automatic incrementing
if ($columnData['EXTRA'] == 'auto_increment') {
$attributes['identity'] = 'enable';
// For decimal columns
if ($dataType === 'decimal') {
// Set decimal accuracy
if (!empty($columnData['NUMERIC_PRECISION'])) {
$attributes['precision'] = $columnData['NUMERIC_PRECISION'];
}
if (!empty($columnData['NUMERIC_SCALE'])) {
$attributes['scale'] = $columnData['NUMERIC_SCALE'];
}
}

return $attributes;
Expand Down
53 changes: 53 additions & 0 deletions tests/PhinxGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,59 @@ public function testCreateTableWithNonNullColumnAsDefault(): void
$this->assertSame($oldSchema, $newSchema);
}

/**
* Test.
*
* @return void
*/
public function testCreateTableWithDecimalColumn(): void
{
$this->execSql("CREATE TABLE `table3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`value1` decimal(10,0) DEFAULT NULL,
`value2` decimal(15,2) DEFAULT NULL,
`value3` decimal(19,4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPACT");

$oldSchema = $this->getTableSchema('table3');
$this->generate();

$this->dropTables();
$this->migrate();
$newSchema = $this->getTableSchema('table3');
$this->assertSame($oldSchema, $newSchema);
}

/**
* Test.
*
* @return void
*/
public function testCreateTableWithIntColumns(): void
{
$this->execSql("CREATE TABLE `table_int` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`value1` int(10) DEFAULT NULL,
`value2` int(1) DEFAULT NULL,
`value3` tinyint(4) DEFAULT 0,
`value4` tinyint(1) DEFAULT 0,
`value5` bigint(20) DEFAULT NULL,
# not supported in phinx
# `value6` bigint(19) DEFAULT NULL,
# `value7` bigint(21) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPACT");

$oldSchema = $this->getTableSchema('table_int');
$this->generate();

$this->dropTables();
$this->migrate();
$newSchema = $this->getTableSchema('table_int');
$this->assertSame($oldSchema, $newSchema);
}

/**
* Test.
*
Expand Down
1 change: 0 additions & 1 deletion tests/diffs/newtable_expected.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public function change()
->addColumn('id', 'integer', [
'null' => false,
'limit' => MysqlAdapter::INT_REGULAR,
'precision' => '10',
'identity' => 'enable',
])
->create();
Expand Down

0 comments on commit b032226

Please sign in to comment.