-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathDumpCommand.php
More file actions
148 lines (134 loc) · 4.81 KB
/
DumpCommand.php
File metadata and controls
148 lines (134 loc) · 4.81 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Migrations\Command;
use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Database\Connection;
use Cake\Datasource\ConnectionManager;
use Migrations\Config\ConfigInterface;
use Migrations\Migration\ManagerFactory;
use Migrations\Util\TableFinder;
/**
* Dump command class.
* A "dump" is a snapshot of a database at a given point in time. It is stored in a
* .lock file in the same folder as migrations files.
*/
class DumpCommand extends Command
{
protected string $connection;
/**
* The default name added to the application command list
*
* @return string
*/
public static function defaultName(): string
{
return 'migrations dump';
}
/**
* Extract options for the dump command from another migrations option parser.
*
* @param \Cake\Console\Arguments $args
* @return array<int|string, mixed>
*/
public static function extractArgs(Arguments $args): array
{
/** @var array<int|string, mixed> $newArgs */
$newArgs = [];
if ($args->getOption('connection')) {
$newArgs[] = '-c';
$newArgs[] = $args->getOption('connection');
}
if ($args->getOption('plugin')) {
$newArgs[] = '-p';
$newArgs[] = $args->getOption('plugin');
}
if ($args->getOption('source')) {
$newArgs[] = '-s';
$newArgs[] = $args->getOption('source');
}
return $newArgs;
}
/**
* Configure the option parser
*
* @param \Cake\Console\ConsoleOptionParser $parser The option parser to configure
* @return \Cake\Console\ConsoleOptionParser
*/
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
$parser->setDescription([
'Dumps the current schema of the database to be used while baking a diff',
'',
'<info>migrations dump -c secondary</info>',
])->addOption('plugin', [
'short' => 'p',
'help' => 'The plugin to dump migrations for',
])->addOption('connection', [
'short' => 'c',
'help' => 'The datasource connection to use',
'default' => 'default',
])->addOption('source', [
'short' => 's',
'help' => 'The folder under src/Config that migrations are in',
'default' => ConfigInterface::DEFAULT_MIGRATION_FOLDER,
]);
return $parser;
}
/**
* Execute the command.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIo $io The console io
* @return int|null The exit code or null for success
*/
public function execute(Arguments $args, ConsoleIo $io): ?int
{
$factory = new ManagerFactory([
'plugin' => $args->getOption('plugin'),
'source' => $args->getOption('source'),
'connection' => $args->getOption('connection'),
]);
$config = $factory->createConfig();
$path = $config->getMigrationPath();
$connectionName = (string)$config->getConnection();
$connection = ConnectionManager::get($connectionName);
assert($connection instanceof Connection);
$collection = $connection->getSchemaCollection();
$options = [
'require-table' => false,
'plugin' => $args->getOption('plugin'),
];
// The connection property is used by the trait methods.
$this->connection = $connectionName;
$finder = new TableFinder($connectionName);
$tables = $finder->getTablesToBake($collection, $options);
$dump = [];
if ($tables) {
foreach ($tables as $table) {
$schema = $collection->describe($table);
$dump[$table] = $schema;
}
}
$filePath = $path . DS . 'schema-dump-' . $connectionName . '.lock';
$io->verbose("<info>Writing dump file `{$filePath}`...</info>");
if (file_put_contents($filePath, serialize($dump))) {
$io->verbose("<info>Dump file `{$filePath}` was successfully written</info>");
return self::CODE_SUCCESS;
}
$io->err("<error>An error occurred while writing dump file `{$filePath}`</error>");
return self::CODE_ERROR;
}
}