Set default value of datetime columns to CURRENT_TIMESTAMP
This resolves an issue with MySQL 5.7 where the default SQL_MODE is set to disallow zero dates (i.e. '0000-00-00 00:00:00') Fixed thanks to Francis and Normandy from postActiv.
This commit is contained in:
parent
38f2ecefac
commit
f89c052cf8
|
@ -18,8 +18,8 @@ create table status_network (
|
||||||
|
|
||||||
tags text comment 'site meta-info tags (pipe-separated)',
|
tags text comment 'site meta-info tags (pipe-separated)',
|
||||||
|
|
||||||
created datetime not null comment 'date this record was created',
|
created datetime not null comment 'date this record was created' default now(),
|
||||||
modified timestamp comment 'date this record was modified'
|
modified timestamp comment 'date this record was modified' default now()
|
||||||
|
|
||||||
) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
|
) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
|
||||||
|
|
||||||
|
|
|
@ -1097,6 +1097,10 @@ class DB_mysqli extends DB_common
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getVersion() {
|
||||||
|
return mysqli_get_server_version($this->connection);
|
||||||
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -271,7 +271,7 @@ class MysqlSchema extends Schema
|
||||||
$engine = $this->preferredEngine($def);
|
$engine = $this->preferredEngine($def);
|
||||||
return ") ENGINE=$engine CHARACTER SET utf8mb4 COLLATE utf8mb4_bin";
|
return ") ENGINE=$engine CHARACTER SET utf8mb4 COLLATE utf8mb4_bin";
|
||||||
}
|
}
|
||||||
|
|
||||||
function preferredEngine($def)
|
function preferredEngine($def)
|
||||||
{
|
{
|
||||||
/* MyISAM is no longer required for fulltext indexes, fortunately
|
/* MyISAM is no longer required for fulltext indexes, fortunately
|
||||||
|
@ -386,7 +386,7 @@ class MysqlSchema extends Schema
|
||||||
$map = array('serial' => 'int',
|
$map = array('serial' => 'int',
|
||||||
'integer' => 'int',
|
'integer' => 'int',
|
||||||
'numeric' => 'decimal');
|
'numeric' => 'decimal');
|
||||||
|
|
||||||
$type = $column['type'];
|
$type = $column['type'];
|
||||||
if (isset($map[$type])) {
|
if (isset($map[$type])) {
|
||||||
$type = $map[$type];
|
$type = $map[$type];
|
||||||
|
@ -436,13 +436,32 @@ class MysqlSchema extends Schema
|
||||||
*/
|
*/
|
||||||
function filterDef(array $tableDef)
|
function filterDef(array $tableDef)
|
||||||
{
|
{
|
||||||
|
$version = $this->conn->getVersion();
|
||||||
foreach ($tableDef['fields'] as $name => &$col) {
|
foreach ($tableDef['fields'] as $name => &$col) {
|
||||||
if ($col['type'] == 'serial') {
|
if ($col['type'] == 'serial') {
|
||||||
$col['type'] = 'int';
|
$col['type'] = 'int';
|
||||||
$col['auto_increment'] = true;
|
$col['auto_increment'] = true;
|
||||||
}
|
}
|
||||||
if ($col['type'] == 'datetime' && isset($col['default']) && $col['default'] == 'CURRENT_TIMESTAMP') {
|
|
||||||
$col['type'] = 'timestamp';
|
// Avoid invalid date errors in MySQL 5.7+
|
||||||
|
if ($col['type'] == 'timestamp' && !isset($col['default'])
|
||||||
|
&& $version >= 50605) {
|
||||||
|
$col['default'] = 'CURRENT_TIMESTAMP';
|
||||||
|
}
|
||||||
|
if ($col['type'] == 'datetime') {
|
||||||
|
// Avoid invalid date errors in MySQL 5.7+
|
||||||
|
if (!isset($col['default']) && $version >= 50605) {
|
||||||
|
$col['default'] = 'CURRENT_TIMESTAMP';
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we are using MySQL 5.5, convert datetime to timestamp if
|
||||||
|
// default value is CURRENT_TIMESTAMP. Not needed for MySQL 5.6+
|
||||||
|
// and MariaDB 10.0+
|
||||||
|
if (isset($col['default'])
|
||||||
|
&& $col['default'] == 'CURRENT_TIMESTAMP'
|
||||||
|
&& $version < 50605) {
|
||||||
|
$col['type'] = 'timestamp';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$col['type'] = $this->mapType($col);
|
$col['type'] = $this->mapType($col);
|
||||||
unset($col['size']);
|
unset($col['size']);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user