Skip to content

Commit

Permalink
[#13297] - Added size to datetime/time and timestamp fields
Browse files Browse the repository at this point in the history
  • Loading branch information
niden committed Feb 4, 2020
1 parent 3e8a21c commit 1174583
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
6 changes: 5 additions & 1 deletion phalcon/Db/Dialect.zep
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,11 @@ abstract class Dialect implements DialectInterface
*/
protected function getColumnSize(<ColumnInterface> column) -> string
{
return "(" . column->getSize() . ")";
if column->getSize() > 0 {
return "(" . column->getSize() . ")";
}

return "";
}

/**
Expand Down
6 changes: 6 additions & 0 deletions phalcon/Db/Dialect/Mysql.zep
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ class Mysql extends Dialect
let columnSql .= "DATETIME";
}

let columnSql .= this->getColumnSize(column);

break;

case Column::TYPE_DECIMAL:
Expand Down Expand Up @@ -556,13 +558,17 @@ class Mysql extends Dialect
let columnSql .= "TIME";
}

let columnSql .= this->getColumnSize(column);

break;

case Column::TYPE_TIMESTAMP:
if empty columnSql {
let columnSql .= "TIMESTAMP";
}

let columnSql .= this->getColumnSize(column);

break;

case Column::TYPE_TINYBLOB:
Expand Down
51 changes: 51 additions & 0 deletions tests/tocheck-database/Db/Dialect/Mysql/AddColumnCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

use Codeception\Example;
use IntegrationTester;
use Phalcon\Db\Column;
use Phalcon\Db\Dialect\Mysql;
use Phalcon\Test\Fixtures\Traits\DialectTrait;
use Phalcon\Test\Fixtures\Traits\DiTrait;

class AddColumnCest
{
use DiTrait;
use DialectTrait;

/**
Expand All @@ -44,6 +47,54 @@ public function testAddColumn(IntegrationTester $I, Example $example)
$I->assertEquals($expected, $actual);
}

/**
* Tests Dialect::addColumn
*
* @author Phalcon Team <[email protected]>
* @since 2019-12-23
*/
public function dbDialectAddColumnDateFractal(IntegrationTester $I)
{
$dialect = new Mysql();

$options = [
"type" => Column::TYPE_DATETIME,
"notNull" => false,
"size" => 2,
];
$column = new Column('ftest', $options);
$actual = $dialect->addColumn('fractal_dates', "", $column);
$I->assertEquals(
"ALTER TABLE `fractal_dates` ADD `ftest` DATETIME(2)",
$actual
);

$options = [
"type" => Column::TYPE_TIME,
"notNull" => false,
"size" => 2,
];
$column = new Column('ftest', $options);
$actual = $dialect->addColumn('fractal_dates', "", $column);
$I->assertEquals(
"ALTER TABLE `fractal_dates` ADD `ftest` TIME(2)",
$actual
);

$options = [
"type" => Column::TYPE_TIMESTAMP,
"notNull" => false,
"size" => 2,
];
$column = new Column('ftest', $options);
$actual = $dialect->addColumn('fractal_dates', "", $column);
$I->assertEquals(
"ALTER TABLE `fractal_dates` ADD `ftest` TIMESTAMP(2)",
$actual
);
}


protected function getAddColumnFixtures(): array
{
return [
Expand Down

0 comments on commit 1174583

Please sign in to comment.