This repository was archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.php
More file actions
66 lines (54 loc) · 2.38 KB
/
Copy pathconsole.php
File metadata and controls
66 lines (54 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
require __DIR__.'/vendor/autoload.php';
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Console\Output\OutputInterface;
$console = new Application();
$console
->register('database:replace')
->setDefinition([
new InputArgument('search', InputArgument::REQUIRED, 'The value being searched for'),
new InputArgument('replace', InputArgument::REQUIRED, 'The replacement value that replaces found search values'),
])
->setDescription('Replace all occurrences of the search string with the replacement string')
->setCode(function (InputInterface $input, OutputInterface $output) {
$search = $input->getArgument('search');
$replace = $input->getArgument('replace');
$progress = new ProgressBar($output, 100);
$progress->start();
try {
$parameters = Yaml::parse(file_get_contents('parameters.yml'));
$mysql = $parameters['mysql'];
} catch (ParseException $e) {
$progress->finish();
$output->writeln(sprintf('Unable to find the parameters: %s', $e->getMessage()));
return;
}
try {
$bdd = new PDO(sprintf('mysql:host=%s;dbname=%s', $mysql['host'], $mysql['database']), $mysql['user'], $mysql['password']);
$allTables = $bdd->query("SHOW TABLES");
$allTables = $allTables->fetchAll(PDO::FETCH_COLUMN);
foreach ($allTables as $table) {
$result = $bdd->query(sprintf('SELECT * FROM %s LIMIT 1', $table));
$fields = array_keys($result->fetch(PDO::FETCH_ASSOC));
foreach ($fields as $field) {
$query = $bdd->query(sprintf('UPDATE %s SET %s = REPLACE(%s, "%s", "%s")', $table, $field, $field, $search, $replace));
$query->execute();
$progress->advance();
}
}
}
catch (Exception $e) {
$progress->finish();
$output->writeln(sprintf('Erreur : %s', $e->getMessage()));
return;
}
$progress->finish();
})
;
$console->run();