Skip to content

Commit 1eb5daf

Browse files
Merge b87ee5d into 9fd9c92
2 parents 9fd9c92 + b87ee5d commit 1eb5daf

File tree

8 files changed

+220
-6
lines changed

8 files changed

+220
-6
lines changed

src/MySql/AuditAlter.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function createSqlStatement($tableName, $columns)
8888
{
8989
$editCharSet = false;
9090
$charSet = '';
91-
$this->codeStore->append(sprintf('ALTER TABLE %s CHANGE', $tableName));
91+
$this->codeStore->append(sprintf('ALTER TABLE %s.`%s` CHANGE', $this->config['database']['audit_schema'], $tableName));
9292
$countMax = $columns->getNumberOfColumns();
9393
$count = 1;
9494
/** @var MultiSourceColumnMetadata $rowMetadata */
@@ -110,7 +110,7 @@ public function createSqlStatement($tableName, $columns)
110110
$editCharSet = true;
111111
$charSet = $configMetadata['character_set_name'];
112112
}
113-
$line = sprintf('%s %s %s', $columnName, $columnName, $configMetadata['column_type']);
113+
$line = sprintf('`%s` `%s` %s', $columnName, $columnName, $configMetadata['column_type']);
114114
if ($count!=$countMax) $line .= ',';
115115
$this->codeStore->append($line);
116116
}
@@ -121,7 +121,7 @@ public function createSqlStatement($tableName, $columns)
121121
$editCharSet = true;
122122
$charSet = $dataMetadata['character_set_name'];
123123
}
124-
$line = sprintf('%s %s %s', $columnName, $columnName, $dataMetadata['column_type']);
124+
$line = sprintf('`%s` `%s` %s', $columnName, $columnName, $dataMetadata['column_type']);
125125
if ($count!=$countMax) $line .= ',';
126126
$this->codeStore->append($line);
127127
}
@@ -130,12 +130,10 @@ public function createSqlStatement($tableName, $columns)
130130
$this->codeStore->append(';');
131131
if ($editCharSet)
132132
{
133-
$this->codeStore->append(sprintf('ALTER TABLE %s DEFAULT CHARACTER SET %s;', $tableName, $charSet));
133+
$this->codeStore->append(sprintf('ALTER TABLE %s.`%s` DEFAULT CHARACTER SET %s;', $this->config['database']['audit_schema'], $tableName, $charSet));
134134
}
135135
}
136136

137-
138-
139137
//--------------------------------------------------------------------------------------------------------------------
140138
/**
141139
* The main method: executes the create alter table statement actions for tables.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
//----------------------------------------------------------------------------------------------------------------------
3+
namespace SetBased\Audit\Test\MySql\AlterAuditTableCommand;
4+
5+
use SetBased\Audit\MySql\Command\AlterAuditTableCommand;
6+
use SetBased\Audit\MySql\Command\AuditCommand;
7+
use SetBased\Audit\MySql\Command\DiffCommand;
8+
use SetBased\Audit\Test\MySql\AuditTestCase;
9+
use SetBased\Stratum\MySql\StaticDataLayer;
10+
use Symfony\Component\Console\Application;
11+
use Symfony\Component\Console\Tester\CommandTester;
12+
13+
//----------------------------------------------------------------------------------------------------------------------
14+
/**
15+
* Parent class for testing the diff command.
16+
*/
17+
class AlterAuditTableCommandTestCase extends AuditTestCase
18+
{
19+
//--------------------------------------------------------------------------------------------------------------------
20+
/**
21+
* The directory of the test case.
22+
*
23+
* @var string
24+
*/
25+
protected static $dir;
26+
27+
//--------------------------------------------------------------------------------------------------------------------
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public static function setUpBeforeClass()
32+
{
33+
parent::setUpBeforeClass();
34+
35+
StaticDataLayer::disconnect();
36+
StaticDataLayer::connect('localhost', 'test', 'test', self::$dataSchema);
37+
38+
StaticDataLayer::multiQuery(file_get_contents(self::$dir.'/config/setup.sql'));
39+
}
40+
41+
//--------------------------------------------------------------------------------------------------------------------
42+
/**
43+
* Runs the audit command, i.e. creates the audit table.
44+
*/
45+
protected function runAudit()
46+
{
47+
$application = new Application();
48+
$application->add(new AuditCommand());
49+
50+
/** @var AuditCommand $command */
51+
$command = $application->find('audit');
52+
$command->setRewriteConfigFile(true);
53+
$commandTester = new CommandTester($command);
54+
$commandTester->execute(['command' => $command->getName(),
55+
'config file' => self::$dir.'/config/audit.json']);
56+
57+
// Reconnects to the MySQL instance (because the audit command always disconnects from the MySQL instance).
58+
StaticDataLayer::connect('localhost', 'test', 'test', self::$dataSchema);
59+
}
60+
61+
//--------------------------------------------------------------------------------------------------------------------
62+
/**
63+
* Runs the alter-audit-table command, i.e. creates the alter table sql statement.
64+
*
65+
* @param int $statusCode The expected status code of the command.
66+
* @param bool $rewriteConfigFile If true the config file will be rewritten.
67+
*/
68+
protected function runAlter($statusCode = 0, $rewriteConfigFile = false)
69+
{
70+
$application = new Application();
71+
$application->add(new AlterAuditTableCommand());
72+
73+
/** @var AlterAuditTableCommand $command */
74+
$command = $application->find('alter-audit-table');
75+
$command->setRewriteConfigFile($rewriteConfigFile);
76+
$commandTester = new CommandTester($command);
77+
$commandTester->execute(['command' => $command->getName(),
78+
'config file' => self::$dir.'/config/audit.json',
79+
'sql file' => self::$dir.'/config/alter-table-sql-result.sql']);
80+
81+
$this->assertSame($statusCode, $commandTester->getStatusCode(), 'status_code');
82+
83+
// Reconnects to the MySQL instance (because the audit command always disconnects from the MySQL instance).
84+
StaticDataLayer::connect('localhost', 'test', 'test', self::$dataSchema);
85+
}
86+
87+
//--------------------------------------------------------------------------------------------------------------------
88+
}
89+
90+
//----------------------------------------------------------------------------------------------------------------------
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
//----------------------------------------------------------------------------------------------------------------------
3+
namespace SetBased\Audit\Test\MySql\AlterAuditTableCommand\AlterDataToAudit;
4+
5+
use SetBased\Audit\MySql\AuditDataLayer;
6+
use SetBased\Audit\Test\MySql\AlterAuditTableCommand\AlterAuditTableCommandTestCase;
7+
use SetBased\Stratum\MySql\StaticDataLayer;
8+
9+
//----------------------------------------------------------------------------------------------------------------------
10+
/**
11+
* Tests for running audit with a new table column.
12+
*/
13+
class AlterDataToAuditTest extends AlterAuditTableCommandTestCase
14+
{
15+
//--------------------------------------------------------------------------------------------------------------------
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public static function setUpBeforeClass()
20+
{
21+
self::$dir = __DIR__;
22+
23+
parent::setUpBeforeClass();
24+
}
25+
26+
//--------------------------------------------------------------------------------------------------------------------
27+
public function test01()
28+
{
29+
// Run audit.
30+
$this->runAudit();
31+
32+
// TABLE1 MUST exist.
33+
$tables = AuditDataLayer::getTablesNames(self::$auditSchema);
34+
$this->assertNotNull(StaticDataLayer::searchInRowSet('table_name', 'TABLE1', $tables));
35+
36+
// Edit column type in data schema.
37+
StaticDataLayer::multiQuery(file_get_contents(__DIR__.'/config/edit_column_type.sql'));
38+
39+
// TABLE1 must have column c4 - int(12).
40+
$actual = AuditDataLayer::getTableColumns(self::$auditSchema, 'TABLE1');
41+
42+
$expected = [];
43+
$expected[] = ['column_name' => 'c1',
44+
'column_type' => 'tinyint(4)',
45+
'is_nullable' => 'YES',
46+
'character_set_name' => null,
47+
'collation_name' => null];
48+
$expected[] = ['column_name' => 'c2',
49+
'column_type' => 'smallint(6)',
50+
'is_nullable' => 'YES',
51+
'character_set_name' => null,
52+
'collation_name' => null];
53+
$expected[] = ['column_name' => 'c4',
54+
'column_type' => 'int(11)',
55+
'is_nullable' => 'YES',
56+
'character_set_name' => null,
57+
'collation_name' => null];
58+
59+
$this->assertSame($expected, $actual);
60+
61+
$this->runAlter();
62+
63+
$tableOptions = AuditDataLayer::getTableOptions(self::$dataSchema, 'TABLE1');
64+
$this->assertSame(file_get_contents(__DIR__.'/config/alter-table-sql-result.sql'),
65+
sprintf('ALTER TABLE `test_audit`.`TABLE1` CHANGE
66+
`c4` `c4` int(12)
67+
;
68+
ALTER TABLE `test_audit`.`TABLE1` DEFAULT CHARACTER SET %s', $tableOptions['character_set_name']));
69+
}
70+
71+
//--------------------------------------------------------------------------------------------------------------------
72+
}
73+
74+
//----------------------------------------------------------------------------------------------------------------------
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE test_audit.`TABLE1` CHANGE
2+
`c4` `c4` int(12)
3+
;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"database": {
3+
"host": "localhost",
4+
"user": "test",
5+
"password": "test",
6+
"data_schema": "test_data",
7+
"audit_schema": "test_audit"
8+
},
9+
"metadata": "audit_metadata.json",
10+
"tables": {
11+
"TABLE1": {
12+
"audit": true,
13+
"skip": null,
14+
"alias": "t1"
15+
}
16+
},
17+
"audit_columns": [
18+
19+
],
20+
"additional_sql": [
21+
22+
]
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"TABLE1": [
3+
{
4+
"column_name": "c1",
5+
"column_type": "tinyint(4)",
6+
"is_nullable": "YES"
7+
},
8+
{
9+
"column_name": "c2",
10+
"column_type": "smallint(6)",
11+
"is_nullable": "YES"
12+
},
13+
{
14+
"column_name": "c4",
15+
"column_type": "int(11)",
16+
"is_nullable": "YES"
17+
}
18+
]
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
alter table `TABLE1` modify `c4` int(12);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE `TABLE1` (
2+
c1 tinyint,
3+
c2 smallint,
4+
c4 int
5+
);
6+

0 commit comments

Comments
 (0)