|
1
|
|
|
<?php namespace Tequilarapido\Cli\Commands; |
|
2
|
|
|
|
|
3
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
4
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
5
|
|
|
use Tequilarapido\Cli\Commands\Base\AbstractDatabaseCommand; |
|
6
|
|
|
use Tequilarapido\Database\Database; |
|
7
|
|
|
use Tequilarapido\Database\Table; |
|
8
|
|
|
|
|
9
|
|
|
class DatabaseConvertToUtf8 extends AbstractDatabaseCommand |
|
10
|
|
|
{ |
|
11
|
|
|
const CHARSET = 'utf8'; |
|
12
|
|
|
const COLLATION = 'utf8_general_ci'; |
|
13
|
|
|
|
|
14
|
|
|
protected $databaseTables; |
|
15
|
|
|
|
|
16
|
|
|
protected function configure() |
|
17
|
|
|
{ |
|
18
|
|
|
parent::configure(); |
|
19
|
|
|
|
|
20
|
|
|
$description = ''; |
|
21
|
|
|
$description .= 'Converts database tables to utf8. ' . PHP_EOL; |
|
22
|
|
|
$description .= ' '; |
|
23
|
|
|
|
|
24
|
|
|
$this |
|
25
|
|
|
->setName('db:utf8') |
|
26
|
|
|
->setDescription($description); |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::execute($input, $output); |
|
34
|
|
|
|
|
35
|
|
|
// Database configuration |
|
36
|
|
|
$dbConfig = $this->config->getDatabase(); |
|
37
|
|
|
$databaseName = $this->config->getDatabaseName(); |
|
38
|
|
|
|
|
39
|
|
|
// Connection |
|
40
|
|
|
$this->db = new Database(); |
|
41
|
|
|
$this->db->connect($dbConfig); |
|
42
|
|
|
|
|
43
|
|
|
// Tables |
|
44
|
|
|
$this->table = new Table(); |
|
|
|
|
|
|
45
|
|
|
$this->databaseTables = $this->table->all($databaseName); |
|
46
|
|
|
|
|
47
|
|
|
// Set proper engine |
|
48
|
|
|
$this->setCharsetAndCollation(static::CHARSET, static::COLLATION); |
|
49
|
|
|
|
|
50
|
|
|
// Test out |
|
51
|
|
|
$isOK = $this->table->isCollation($databaseName, static::COLLATION); |
|
52
|
|
|
if ($isOK) { |
|
53
|
|
|
$this->output->success("Done. All table are now " . static::CHARSET . '/' . static::COLLATION); |
|
|
|
|
|
|
54
|
|
|
} else { |
|
55
|
|
|
$this->output->warn("Done. But not sure all tables are " . static::CHARSET . '/' . static::COLLATION); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $charset |
|
61
|
|
|
* @param string $collation |
|
62
|
|
|
*/ |
|
63
|
|
View Code Duplication |
protected function setCharsetAndCollation($charset, $collation) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
$this->output->title("Setting database charset to $charset and collation to $collation"); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
// Cli progress setup |
|
68
|
|
|
$progress = $this->getHelperSet()->get('progress'); |
|
69
|
|
|
$progress->start($this->output, count($this->databaseTables)); |
|
70
|
|
|
|
|
71
|
|
|
foreach ($this->databaseTables as $tableName) { |
|
72
|
|
|
|
|
73
|
|
|
$this->output->info($tableName); |
|
|
|
|
|
|
74
|
|
|
$this->table->alterCharsetAndCollation($tableName, $charset, $collation); |
|
75
|
|
|
$progress->advance(); |
|
76
|
|
|
} |
|
77
|
|
|
$progress->finish(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..