Test Failed
Push — master ( d02081...898276 )
by P.R.
04:01
created

AlterAuditTableCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A execute() 0 21 2
1
<?php
2
3
namespace SetBased\Audit\Command;
4
5
use SetBased\Audit\Audit\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
   * @inheritdoc
29
   */
30
  protected function configure()
31
  {
32
    $this->setName('alter-audit-table')
33
         ->setDescription('Creates alter SQL statements for audit tables')
34
         ->addArgument('config file', InputArgument::REQUIRED, 'The audit configuration file')
35
         ->addArgument('sql file', InputArgument::OPTIONAL, 'The destination file for the SQL statements');
36
37
    $this->setHelp(<<<EOL
38
Generates alter table SQL statements for aligning the audit tables with the 
39
audit configuration file and data tables.
40
41
Manual inspection of the generated SQL statements is required. For example: 
42
changing an audit column from varchar(20) character set utf8 to varchar(10)
43
character set ascii might cause problems when the audit column has values
44
longer than 10 characters or values outside the ASCII character set (even 
45
though the current data table hold only values with length 10 or less and 
46
only in the ASCII character set).
47
48
No SQL statements will be generated for missing or obsolete columns in the 
49
audit tables. Use the command 'audit' for creating missing columns in audit
50
tables.
51
EOL
52
    );
53
  }
54
55
  //--------------------------------------------------------------------------------------------------------------------
56
  /**
57
   * @inheritdoc
58
   */
59
  protected function execute(InputInterface $input, OutputInterface $output)
60
  {
61
    $this->io = new StratumStyle($input, $output);
62
63
    $sqlFilename = $input->getArgument('sql file');
64
65
    $this->configFileName = $input->getArgument('config file');
66
    $this->readConfigFile();
67
68
    $this->connect($this->config);
69
70
    $alter = new AlterAuditTable($this->config);
71
    $sql   = $alter->main();
72
73
    if ($sqlFilename!==null)
74
    {
75
      $this->writeTwoPhases($sqlFilename, $sql);
76
    }
77
    else
78
    {
79
      $this->io->write($sql);
80
    }
81
  }
82
83
  //--------------------------------------------------------------------------------------------------------------------
84
85
}
86
87
//----------------------------------------------------------------------------------------------------------------------
88