Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/MySql/AuditAlter.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function createSqlStatement($tableName, $columns)
{
$editCharSet = false;
$charSet = '';
$this->codeStore->append(sprintf('ALTER TABLE %s CHANGE', $tableName));
$this->codeStore->append(sprintf('ALTER TABLE %s.`%s` CHANGE', $this->config['database']['audit_schema'], $tableName));
$countMax = $columns->getNumberOfColumns();
$count = 1;
/** @var MultiSourceColumnMetadata $rowMetadata */
Expand All @@ -110,7 +110,7 @@ public function createSqlStatement($tableName, $columns)
$editCharSet = true;
$charSet = $configMetadata['character_set_name'];
}
$line = sprintf('%s %s %s', $columnName, $columnName, $configMetadata['column_type']);
$line = sprintf('`%s` `%s` %s', $columnName, $columnName, $configMetadata['column_type']);
if ($count!=$countMax) $line .= ',';
$this->codeStore->append($line);
}
Expand All @@ -121,7 +121,7 @@ public function createSqlStatement($tableName, $columns)
$editCharSet = true;
$charSet = $dataMetadata['character_set_name'];
}
$line = sprintf('%s %s %s', $columnName, $columnName, $dataMetadata['column_type']);
$line = sprintf('`%s` `%s` %s', $columnName, $columnName, $dataMetadata['column_type']);
if ($count!=$countMax) $line .= ',';
$this->codeStore->append($line);
}
Expand All @@ -130,12 +130,10 @@ public function createSqlStatement($tableName, $columns)
$this->codeStore->append(';');
if ($editCharSet)
{
$this->codeStore->append(sprintf('ALTER TABLE %s DEFAULT CHARACTER SET %s;', $tableName, $charSet));
$this->codeStore->append(sprintf('ALTER TABLE %s.`%s` DEFAULT CHARACTER SET %s;', $this->config['database']['audit_schema'], $tableName, $charSet));
}
}



//--------------------------------------------------------------------------------------------------------------------
/**
* The main method: executes the create alter table statement actions for tables.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
//----------------------------------------------------------------------------------------------------------------------
namespace SetBased\Audit\Test\MySql\AlterAuditTableCommand;

use SetBased\Audit\MySql\Command\AlterAuditTableCommand;
use SetBased\Audit\MySql\Command\AuditCommand;
use SetBased\Audit\MySql\Command\DiffCommand;
use SetBased\Audit\Test\MySql\AuditTestCase;
use SetBased\Stratum\MySql\StaticDataLayer;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

//----------------------------------------------------------------------------------------------------------------------
/**
* Parent class for testing the diff command.
*/
class AlterAuditTableCommandTestCase extends AuditTestCase
{
//--------------------------------------------------------------------------------------------------------------------
/**
* The directory of the test case.
*
* @var string
*/
protected static $dir;

//--------------------------------------------------------------------------------------------------------------------
/**
* {@inheritdoc}
*/
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();

StaticDataLayer::disconnect();
StaticDataLayer::connect('localhost', 'test', 'test', self::$dataSchema);

StaticDataLayer::multiQuery(file_get_contents(self::$dir.'/config/setup.sql'));
}

//--------------------------------------------------------------------------------------------------------------------
/**
* Runs the audit command, i.e. creates the audit table.
*/
protected function runAudit()
{
$application = new Application();
$application->add(new AuditCommand());

/** @var AuditCommand $command */
$command = $application->find('audit');
$command->setRewriteConfigFile(true);
$commandTester = new CommandTester($command);
$commandTester->execute(['command' => $command->getName(),
'config file' => self::$dir.'/config/audit.json']);

// Reconnects to the MySQL instance (because the audit command always disconnects from the MySQL instance).
StaticDataLayer::connect('localhost', 'test', 'test', self::$dataSchema);
}

//--------------------------------------------------------------------------------------------------------------------
/**
* Runs the alter-audit-table command, i.e. creates the alter table sql statement.
*
* @param int $statusCode The expected status code of the command.
* @param bool $rewriteConfigFile If true the config file will be rewritten.
*/
protected function runAlter($statusCode = 0, $rewriteConfigFile = false)
{
$application = new Application();
$application->add(new AlterAuditTableCommand());

/** @var AlterAuditTableCommand $command */
$command = $application->find('alter-audit-table');
$command->setRewriteConfigFile($rewriteConfigFile);
$commandTester = new CommandTester($command);
$commandTester->execute(['command' => $command->getName(),
'config file' => self::$dir.'/config/audit.json',
'sql file' => self::$dir.'/config/alter-table-sql-result.sql']);

$this->assertSame($statusCode, $commandTester->getStatusCode(), 'status_code');

// Reconnects to the MySQL instance (because the audit command always disconnects from the MySQL instance).
StaticDataLayer::connect('localhost', 'test', 'test', self::$dataSchema);
}

//--------------------------------------------------------------------------------------------------------------------
}

//----------------------------------------------------------------------------------------------------------------------
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
//----------------------------------------------------------------------------------------------------------------------
namespace SetBased\Audit\Test\MySql\AlterAuditTableCommand\AlterDataToAudit;

use SetBased\Audit\MySql\AuditDataLayer;
use SetBased\Audit\Test\MySql\AlterAuditTableCommand\AlterAuditTableCommandTestCase;
use SetBased\Stratum\MySql\StaticDataLayer;

//----------------------------------------------------------------------------------------------------------------------
/**
* Tests for running audit with a new table column.
*/
class AlterDataToAuditTest extends AlterAuditTableCommandTestCase
{
//--------------------------------------------------------------------------------------------------------------------
/**
* {@inheritdoc}
*/
public static function setUpBeforeClass()
{
self::$dir = __DIR__;

parent::setUpBeforeClass();
}

//--------------------------------------------------------------------------------------------------------------------
public function test01()
{
// Run audit.
$this->runAudit();

// TABLE1 MUST exist.
$tables = AuditDataLayer::getTablesNames(self::$auditSchema);
$this->assertNotNull(StaticDataLayer::searchInRowSet('table_name', 'TABLE1', $tables));

// Edit column type in data schema.
StaticDataLayer::multiQuery(file_get_contents(__DIR__.'/config/edit_column_type.sql'));

// TABLE1 must have column c4 - int(12).
$actual = AuditDataLayer::getTableColumns(self::$auditSchema, 'TABLE1');

$expected = [];
$expected[] = ['column_name' => 'c1',
'column_type' => 'tinyint(4)',
'is_nullable' => 'YES',
'character_set_name' => null,
'collation_name' => null];
$expected[] = ['column_name' => 'c2',
'column_type' => 'smallint(6)',
'is_nullable' => 'YES',
'character_set_name' => null,
'collation_name' => null];
$expected[] = ['column_name' => 'c4',
'column_type' => 'int(11)',
'is_nullable' => 'YES',
'character_set_name' => null,
'collation_name' => null];

$this->assertSame($expected, $actual);

$this->runAlter();

$tableOptions = AuditDataLayer::getTableOptions(self::$dataSchema, 'TABLE1');
$this->assertSame(file_get_contents(__DIR__.'/config/alter-table-sql-result.sql'),
sprintf('ALTER TABLE `test_audit`.`TABLE1` CHANGE
`c4` `c4` int(12)
;
ALTER TABLE `test_audit`.`TABLE1` DEFAULT CHARACTER SET %s', $tableOptions['character_set_name']));
}

//--------------------------------------------------------------------------------------------------------------------
}

//----------------------------------------------------------------------------------------------------------------------
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE test_audit.`TABLE1` CHANGE
`c4` `c4` int(12)
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"database": {
"host": "localhost",
"user": "test",
"password": "test",
"data_schema": "test_data",
"audit_schema": "test_audit"
},
"metadata": "audit_metadata.json",
"tables": {
"TABLE1": {
"audit": true,
"skip": null,
"alias": "t1"
}
},
"audit_columns": [

],
"additional_sql": [

]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"TABLE1": [
{
"column_name": "c1",
"column_type": "tinyint(4)",
"is_nullable": "YES"
},
{
"column_name": "c2",
"column_type": "smallint(6)",
"is_nullable": "YES"
},
{
"column_name": "c4",
"column_type": "int(11)",
"is_nullable": "YES"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table `TABLE1` modify `c4` int(12);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE `TABLE1` (
c1 tinyint,
c2 smallint,
c4 int
);