Passed
Branch master (95ff30)
by P.R.
04:33
created

AlterAuditTableCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SetBased\Audit\MySql\Command;
4
5
use SetBased\Audit\MySql\AlterAuditTable;
6
use SetBased\Stratum\Style\StratumStyle;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Command for comparing data tables with audit tables.
13
 */
14
class AlterAuditTableCommand extends AuditCommand
15
{
16
  //--------------------------------------------------------------------------------------------------------------------
17
  /**
18
   * Check full full and return array without new or obsolete columns if full not set.
19
   *
20
   * @param array[] $columns The metadata of the columns of a table.
21
   *
22
   * @var StratumStyle
23
   */
24
  protected $io;
25
26
  //--------------------------------------------------------------------------------------------------------------------
27
28
  /**
29
   * @inheritdoc
30
   */
31 9
  protected function configure()
32
  {
33 9
    $this->setName('alter-audit-table')
34 9
         ->setDescription('Creates alter SQL statements for audit tables')
35 9
         ->addArgument('config file', InputArgument::REQUIRED, 'The audit configuration file')
36 9
         ->addArgument('sql file', InputArgument::OPTIONAL, 'The destination file for the SQL statements');
37
38 9
    $this->setHelp(<<<EOL
39 9
Generates alter table SQL statements for aligning the audit tables with the 
40
audit configuration file and data tables.
41
42
Manual inspection of the generated SQL statements is required. For example: 
43
changing an audit column from varchar(20) character set utf8 to varchar(10)
44
character set ascii might cause problems when the audit column has values
45
longer than 10 characters or values outside the ASCII character set (even 
46
though the current data table hold only values with length 10 or less and 
47
only in the ASCII character set).
48
49
No SQL statements will be generated for missing or obsolete columns in the 
50
audit tables. Use the command 'audit' for creating missing columns in audit
51
tables.
52
EOL
53
    );
54 9
  }
55
56
  //--------------------------------------------------------------------------------------------------------------------
57
  /**
58
   * @inheritdoc
59
   */
60 8
  protected function execute(InputInterface $input, OutputInterface $output)
61
  {
62 8
    $this->io = new StratumStyle($input, $output);
63
64 8
    $sqlFilename = $input->getArgument('sql file');
65
66 8
    $this->configFileName = $input->getArgument('config file');
67 8
    $this->readConfigFile();
68
69 8
    $this->connect($this->config);
70
71 8
    $alter = new AlterAuditTable($this->config);
72 8
    $sql   = $alter->main();
73
74 8
    if ($sqlFilename!==null)
75
    {
76 8
      $this->writeTwoPhases($sqlFilename, $sql);
77
    }
78
    else
79
    {
80
      $this->io->write($sql);
81
    }
82 8
  }
83
84
  //--------------------------------------------------------------------------------------------------------------------
85
86
}
87
88
//----------------------------------------------------------------------------------------------------------------------
89