Skip to content

Commit 94bcab5

Browse files
committed
Fix invalid default value for timestamp column.
1 parent e7c523e commit 94bcab5

File tree

7 files changed

+84
-6
lines changed

7 files changed

+84
-6
lines changed

src/MySql/Metadata/ColumnMetadata.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ public function getColumnAuditDefinition(): string
4747

4848
$parts[] = ($this->getProperty('is_nullable')=='YES') ? 'null' : 'not null';
4949

50-
if ($this->getProperty('column_default')!==null && $this->getProperty('column_default')!='NULL')
50+
if ($this->getProperty('column_default')!==null && mb_strtoupper($this->getProperty('column_default'))!=='NULL')
5151
{
5252
$parts[] = 'default '.$this->getProperty('column_default');
5353
}
54-
elseif ($this->getProperty('column_type')=='timestamp')
54+
elseif ($this->getProperty('column_type')==='timestamp')
5555
{
5656
// Prevent automatic updates of timestamp columns.
57-
$parts[] = 'default now()';
57+
$parts[] = 'default null';
5858
}
5959

6060
return implode(' ', $parts);

test/MySql/AlterAuditTableCommand/testMany/expected.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ alter table `test_audit`.`TABLE1` engine InnoDB default character set utf8 defau
22

33
alter table `test_audit`.`TABLE1`
44
change column `col1` `col1` int(8) null,
5-
change column `col10` `col10` timestamp null default now(),
5+
change column `col10` `col10` timestamp null default null,
66
change column `col100` `col100` varchar(10) character set ascii collate ascii_bin null
77
;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
alter table `test_audit`.`TABLE1`
22
change column `col1` `col1` int(8) null,
3-
change column `col10` `col10` timestamp null default now(),
3+
change column `col10` `col10` timestamp null default null,
44
change column `col100` `col100` varchar(10) character set ascii collate ascii_bin null
55
;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
alter table `test_audit`.`TABLE1`
2-
change column `col1` `col1` timestamp null default now()
2+
change column `col1` `col1` timestamp null default null
33
;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SetBased\Audit\Test\MySql\AuditCommand\TimestampColumn;
5+
6+
use SetBased\Audit\MySql\AuditDataLayer;
7+
use SetBased\Audit\Test\MySql\AuditCommand\AuditCommandTestCase;
8+
use SetBased\Stratum\Middle\Helper\RowSetHelper;
9+
10+
/**
11+
* Tests for running audit on a table with timestamp column table.
12+
*/
13+
class TimestampColumnTest extends AuditCommandTestCase
14+
{
15+
//--------------------------------------------------------------------------------------------------------------------
16+
/**
17+
* @inheritdoc
18+
*/
19+
public static function setUpBeforeClass(): void
20+
{
21+
self::$dir = __DIR__;
22+
23+
parent::setUpBeforeClass();
24+
}
25+
26+
//--------------------------------------------------------------------------------------------------------------------
27+
public function test01(): void
28+
{
29+
$this->runAudit();
30+
31+
// TABLE1 MUST exist.
32+
$tables = AuditDataLayer::$dl->getTablesNames(self::$auditSchema);
33+
self::assertNotNull(RowSetHelper::searchInRowSet($tables, 'table_name', 'TABLE1'));
34+
35+
// TABLE1 MUST have triggers.
36+
$triggers = AuditDataLayer::$dl->getTableTriggers(self::$dataSchema, 'TABLE1');
37+
self::assertNotNull(RowSetHelper::searchInRowSet($triggers, 'trigger_name', 'trg_audit_t1_insert'));
38+
self::assertNotNull(RowSetHelper::searchInRowSet($triggers, 'trigger_name', 'trg_audit_t1_update'));
39+
self::assertNotNull(RowSetHelper::searchInRowSet($triggers, 'trigger_name', 'trg_audit_t1_delete'));
40+
}
41+
42+
//--------------------------------------------------------------------------------------------------------------------
43+
}
44+
45+
//----------------------------------------------------------------------------------------------------------------------
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"database": {
3+
"host": "localhost",
4+
"user": "test",
5+
"password": "test",
6+
"data_schema": "test_data",
7+
"audit_schema": "test_audit"
8+
},
9+
"audit_columns": [
10+
{
11+
"column_name": "audit_statement",
12+
"column_type": "enum('INSERT','DELETE','UPDATE') not null",
13+
"value_type": "ACTION"
14+
},
15+
{
16+
"column_name": "audit_type",
17+
"column_type": "enum('OLD','NEW') not null",
18+
"value_type": "STATE"
19+
}
20+
],
21+
"tables": {
22+
"TABLE1": {
23+
"audit": true,
24+
"skip": null,
25+
"alias": "t1"
26+
}
27+
}
28+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
drop table if exists `TABLE1`;
2+
3+
create table `TABLE1` (
4+
`poo_rank_last_update` timestamp null default null
5+
);

0 commit comments

Comments
 (0)