-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDumpSqlCommand.php
More file actions
104 lines (90 loc) · 3.4 KB
/
DumpSqlCommand.php
File metadata and controls
104 lines (90 loc) · 3.4 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
namespace CakeDumpSql\Command;
use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Database\Driver\Mysql;
use Cake\Database\Driver\Postgres;
use Cake\Database\Driver\Sqlite;
use Cake\Datasource\ConnectionManager;
use Cake\Datasource\Exception\MissingDatasourceConfigException;
use CakeDumpSql\Error\UnknownDriverException;
use CakeDumpSql\Sql\MySQL as CakeDumpMySQL;
use CakeDumpSql\Sql\PostgreSQL as CakeDumpPostgres;
use CakeDumpSql\Sql\Sqlite as CakeDumpSqlite;
/**
* DumpSQL command.
*/
class DumpSqlCommand extends Command
{
/**
* Hook method for defining this command's option parser.
*
* @see https://book.cakephp.org/4/en/console-commands/commands.html#defining-arguments-and-options
* @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
* @return \Cake\Console\ConsoleOptionParser The built parser.
*/
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
$parser->addArgument('datasource', [
'help' => 'The name of the datasource config entry. Defaults to "default"',
]);
$parser->addOption('gzip', [
'help' => 'Compress the dump using the gzip program which must be in your $PATH.',
]);
$parser->addOption('data-only', [
'help' => 'Set this option to only export data, no structure',
]);
return $parser;
}
/**
* Implement this method with your command's logic.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIo $io The console io
* @return int The exit code
* @throws \CakeDumpSql\Error\UnknownDriverException
*/
public function execute(Arguments $args, ConsoleIo $io): int
{
$datasource = $args->getArgument('datasource') ?? 'default';
$gzip = $args->getOption('gzip') !== null;
$dataOnly = $args->getOption('data-only') !== null;
try {
$connection = ConnectionManager::get($datasource);
} catch (MissingDatasourceConfigException $e) {
$io->err($e->getMessage());
return self::CODE_ERROR;
}
$driver = $connection->getDriver();
if ($driver instanceof Mysql) {
$object = new CakeDumpMySQL($connection->config(), $driver);
} elseif ($driver instanceof Sqlite) {
$object = new CakeDumpSqlite($connection->config());
} elseif ($driver instanceof Postgres) {
$object = new CakeDumpPostgres($connection->config());
} else {
$message = sprintf('Unknown driver "%s" given.', get_class($driver));
throw new UnknownDriverException($message);
}
$object->setIo($io);
$object->setDataOnly($dataOnly);
$result = $object->dump();
if ($gzip) {
if (function_exists('gzencode')) {
$result = gzencode($result, 9);
if ($result === false) {
$io->err('Failed to gzip the dump!');
return self::CODE_ERROR;
}
} else {
$io->err('Your PHP installation does not have zlib support to create a gzip file!');
return self::CODE_ERROR;
}
}
$io->out($result);
return self::CODE_SUCCESS;
}
}