4 $root = dirname(dirname(dirname(__FILE__
)));
5 require_once $root.'/scripts/init/init-setup.php';
7 $args = new PhutilArgumentParser($argv);
8 $args->setTagline(pht('manage Phabricator storage and schemata'));
9 $args->setSynopsis(<<<EOHELP
10 **storage** __workflow__ [__options__]
11 Manage Phabricator database storage and schema versioning.
14 Initialize or upgrade Phabricator storage.
16 **storage** upgrade --user __root__ --password __hunter2__
17 Use administrative credentials for schema changes.
20 $args->parseStandardArguments();
22 $default_namespace = PhabricatorLiskDAO
::getDefaultStorageNamespace();
31 'Do not prompt before performing dangerous operations.'),
35 'param' => 'hostname',
37 'Operate on the database server identified by __hostname__.'),
43 'Operate on the database identified by __ref__.'),
48 'param' => 'username',
50 'Connect with __username__ instead of the configured default.'),
55 'param' => 'password',
56 'help' => pht('Use __password__ instead of the configured default.'),
59 'name' => 'namespace',
61 'default' => $default_namespace,
63 "Use namespace __namespace__ instead of the configured ".
64 "default ('%s'). This is an advanced feature used by unit tests; ".
65 "you should not normally use this flag.",
71 'Do not actually change anything, just show what would be changed.'),
74 'name' => 'disable-utf8mb4',
76 'Disable %s, even if the database supports it. This is an '.
77 'advanced feature used for testing changes to Phabricator; you '.
78 'should not normally use this flag.',
82 } catch (PhutilArgumentUsageException
$ex) {
83 $args->printUsageException($ex);
87 // First, test that the Phabricator configuration is set up correctly. After
88 // we know this works we'll test any administrative credentials specifically.
90 $refs = PhabricatorDatabaseRef
::getActiveDatabaseRefs();
92 throw new PhutilArgumentUsageException(
93 pht('No databases are configured.'));
96 $host = $args->getArg('host');
97 $ref_key = $args->getArg('ref');
98 if (strlen($host) ||
strlen($ref_key)) {
99 if ($host && $ref_key) {
100 throw new PhutilArgumentUsageException(
102 'Use "--host" or "--ref" to select a database, but not both.'));
105 $refs = PhabricatorDatabaseRef
::getActiveDatabaseRefs();
107 $possible_refs = array();
108 foreach ($refs as $possible_ref) {
109 if ($host && ($possible_ref->getHost() == $host)) {
110 $possible_refs[] = $possible_ref;
113 if ($ref_key && ($possible_ref->getRefKey() == $ref_key)) {
114 $possible_refs[] = $possible_ref;
119 if (!$possible_refs) {
121 throw new PhutilArgumentUsageException(
123 'There is no configured database on host "%s". This command can '.
124 'only interact with configured databases.',
127 throw new PhutilArgumentUsageException(
129 'There is no configured database with ref "%s". This command can '.
130 'only interact with configured databases.',
135 if (count($possible_refs) > 1) {
136 throw new PhutilArgumentUsageException(
138 'Host "%s" identifies more than one database. Use "--ref" to select '.
139 'a specific database.',
143 $refs = $possible_refs;
147 foreach ($refs as $ref) {
148 $default_user = $ref->getUser();
149 $default_host = $ref->getHost();
150 $default_port = $ref->getPort();
152 $test_api = id(new PhabricatorStorageManagementAPI())
153 ->setUser($default_user)
154 ->setHost($default_host)
155 ->setPort($default_port)
156 ->setPassword($ref->getPass())
157 ->setNamespace($args->getArg('namespace'));
161 $test_api->getConn(null),
163 } catch (AphrontQueryException
$ex) {
164 $message = phutil_console_format(
165 "**%s**\n\n%s\n\n%s\n\n%s\n\n**%s**: %s\n",
166 pht('MySQL Credentials Not Configured'),
168 'Unable to connect to MySQL using the configured credentials. '.
169 'You must configure standard credentials before you can upgrade '.
170 'storage. Run these commands to set up credentials:'),
171 " phabricator/ $ ./bin/config set mysql.host __host__\n".
172 " phabricator/ $ ./bin/config set mysql.user __username__\n".
173 " phabricator/ $ ./bin/config set mysql.pass __password__",
175 'These standard credentials are separate from any administrative '.
176 'credentials provided to this command with __%s__ or '.
177 '__%s__, and must be configured correctly before you can proceed.',
180 pht('Raw MySQL Error'),
182 echo phutil_console_wrap($message);
186 if ($args->getArg('password') === null) {
187 // This is already a PhutilOpaqueEnvelope.
188 $password = $ref->getPass();
190 // Put this in a PhutilOpaqueEnvelope.
191 $password = new PhutilOpaqueEnvelope($args->getArg('password'));
192 PhabricatorEnv
::overrideConfig('mysql.pass', $args->getArg('password'));
195 $selected_user = $args->getArg('user');
196 if ($selected_user === null) {
197 $selected_user = $default_user;
200 $api = id(new PhabricatorStorageManagementAPI())
201 ->setUser($selected_user)
202 ->setHost($default_host)
203 ->setPort($default_port)
204 ->setPassword($password)
205 ->setNamespace($args->getArg('namespace'))
206 ->setDisableUTF8MB4($args->getArg('disable-utf8mb4'));
207 PhabricatorEnv
::overrideConfig('mysql.user', $api->getUser());
209 $ref->setUser($selected_user);
210 $ref->setPass($password);
216 } catch (AphrontQueryException
$ex) {
217 $message = phutil_console_format(
218 "**%s**\n\n%s\n\n**%s**: %s\n",
219 pht('Bad Administrative Credentials'),
221 'Unable to connect to MySQL using the administrative credentials '.
222 'provided with the __%s__ and __%s__ flags. Check that '.
223 'you have entered them correctly.',
226 pht('Raw MySQL Error'),
228 echo phutil_console_wrap($message);
236 $workflows = id(new PhutilClassMapQuery())
237 ->setAncestorClass('PhabricatorStorageManagementWorkflow')
240 $patches = PhabricatorSQLPatchList
::buildAllPatches();
242 foreach ($workflows as $workflow) {
243 $workflow->setAPIs($apis);
244 $workflow->setPatches($patches);
247 $workflows[] = new PhutilHelpArgumentWorkflow();
249 $args->parseWorkflows($workflows);