Differential D14982 Diff 36196 src/applications/files/management/PhabricatorFilesManagementMigrateWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/management/PhabricatorFilesManagementMigrateWorkflow.php
| Show All 13 Lines | $this | ||||
| 'param' => 'storage_engine', | 'param' => 'storage_engine', | ||||
| 'help' => pht('Migrate to the named storage engine.'), | 'help' => pht('Migrate to the named storage engine.'), | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'name' => 'dry-run', | 'name' => 'dry-run', | ||||
| 'help' => pht('Show what would be migrated.'), | 'help' => pht('Show what would be migrated.'), | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'name' => 'min-size', | |||||
| 'param' => 'bytes', | |||||
| 'help' => pht( | |||||
| 'Do not migrate data for files which are smaller than a given '. | |||||
| 'filesize.'), | |||||
| ), | |||||
| array( | |||||
| 'name' => 'max-size', | |||||
| 'param' => 'bytes', | |||||
| 'help' => pht( | |||||
| 'Do not migrate data for files which are larger than a given '. | |||||
| 'filesize.'), | |||||
| ), | |||||
| array( | |||||
| 'name' => 'all', | 'name' => 'all', | ||||
| 'help' => pht('Migrate all files.'), | 'help' => pht('Migrate all files.'), | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'name' => 'names', | 'name' => 'names', | ||||
| 'wildcard' => true, | 'wildcard' => true, | ||||
| ), | ), | ||||
| )); | )); | ||||
| Show All 18 Lines | if (!$iterator) { | ||||
| pht( | pht( | ||||
| 'Either specify a list of files to migrate, or use `%s` '. | 'Either specify a list of files to migrate, or use `%s` '. | ||||
| 'to migrate all files.', | 'to migrate all files.', | ||||
| '--all')); | '--all')); | ||||
| } | } | ||||
| $is_dry_run = $args->getArg('dry-run'); | $is_dry_run = $args->getArg('dry-run'); | ||||
| $min_size = (int)$args->getArg('min-size'); | |||||
| $max_size = (int)$args->getArg('max-size'); | |||||
| $failed = array(); | $failed = array(); | ||||
| $engines = PhabricatorFileStorageEngine::loadAllEngines(); | $engines = PhabricatorFileStorageEngine::loadAllEngines(); | ||||
| $total_bytes = 0; | |||||
| $total_files = 0; | |||||
| foreach ($iterator as $file) { | foreach ($iterator as $file) { | ||||
| $monogram = $file->getMonogram(); | $monogram = $file->getMonogram(); | ||||
| $engine_key = $file->getStorageEngine(); | $engine_key = $file->getStorageEngine(); | ||||
| $engine = idx($engines, $engine_key); | $engine = idx($engines, $engine_key); | ||||
| if (!$engine) { | if (!$engine) { | ||||
| echo tsprintf( | echo tsprintf( | ||||
| Show All 20 Lines | foreach ($iterator as $file) { | ||||
| "%s\n", | "%s\n", | ||||
| pht( | pht( | ||||
| '%s: Already stored in engine "%s".', | '%s: Already stored in engine "%s".', | ||||
| $monogram, | $monogram, | ||||
| $target_key)); | $target_key)); | ||||
| continue; | continue; | ||||
| } | } | ||||
| if ($is_dry_run) { | $byte_size = $file->getByteSize(); | ||||
| if ($min_size && ($byte_size < $min_size)) { | |||||
| echo tsprintf( | echo tsprintf( | ||||
| "%s\n", | "%s\n", | ||||
| pht( | pht( | ||||
| '%s: Would migrate from "%s" to "%s" (dry run).', | '%s: File size (%s) is smaller than minimum size (%s).', | ||||
| $monogram, | $monogram, | ||||
| $engine_key, | phutil_format_bytes($byte_size), | ||||
| $target_key)); | phutil_format_bytes($min_size))); | ||||
| continue; | continue; | ||||
| } | } | ||||
| if ($max_size && ($byte_size > $max_size)) { | |||||
| echo tsprintf( | echo tsprintf( | ||||
| "%s\n", | "%s\n", | ||||
| pht( | pht( | ||||
| '%s: Migrating from "%s" to "%s"...', | '%s: File size (%s) is larger than maximum size (%s).', | ||||
| $monogram, | $monogram, | ||||
| phutil_format_bytes($byte_size), | |||||
| phutil_format_bytes($max_size))); | |||||
| continue; | |||||
| } | |||||
| if ($is_dry_run) { | |||||
| echo tsprintf( | |||||
| "%s\n", | |||||
| pht( | |||||
| '%s: (%s) Would migrate from "%s" to "%s" (dry run)...', | |||||
| $monogram, | |||||
| phutil_format_bytes($byte_size), | |||||
| $engine_key, | $engine_key, | ||||
| $target_key)); | $target_key)); | ||||
| } else { | |||||
| echo tsprintf( | |||||
| "%s\n", | |||||
| pht( | |||||
| '%s: (%s) Migrating from "%s" to "%s"...', | |||||
| $monogram, | |||||
| phutil_format_bytes($byte_size), | |||||
| $engine_key, | |||||
| $target_key)); | |||||
| } | |||||
| try { | try { | ||||
| if ($is_dry_run) { | |||||
| // Do nothing, this is a dry run. | |||||
| } else { | |||||
| $file->migrateToEngine($target_engine); | $file->migrateToEngine($target_engine); | ||||
| } | |||||
| $total_files += 1; | |||||
| $total_bytes += $byte_size; | |||||
| echo tsprintf( | echo tsprintf( | ||||
| "%s\n", | "%s\n", | ||||
| pht('Done.')); | pht('Done.')); | ||||
| } catch (Exception $ex) { | } catch (Exception $ex) { | ||||
| echo tsprintf( | echo tsprintf( | ||||
| "%s\n", | "%s\n", | ||||
| pht('Failed! %s', (string)$ex)); | pht('Failed! %s', (string)$ex)); | ||||
| $failed[] = $file; | $failed[] = $file; | ||||
| throw $ex; | throw $ex; | ||||
| } | } | ||||
| } | } | ||||
| echo tsprintf( | |||||
| "%s\n", | |||||
| pht( | |||||
| 'Total Migrated Files: %s', | |||||
| new PhutilNumber($total_files))); | |||||
| echo tsprintf( | |||||
| "%s\n", | |||||
| pht( | |||||
| 'Total Migrated Bytes: %s', | |||||
| phutil_format_bytes($total_bytes))); | |||||
| if ($is_dry_run) { | |||||
| echo tsprintf( | |||||
| "%s\n", | |||||
| pht( | |||||
| 'This was a dry run, so no real migrations were performed.')); | |||||
| } | |||||
| if ($failed) { | if ($failed) { | ||||
| $monograms = mpull($failed, 'getMonogram'); | $monograms = mpull($failed, 'getMonogram'); | ||||
| echo tsprintf( | echo tsprintf( | ||||
| "%s\n", | "%s\n", | ||||
| pht('Failures: %s.', implode(', ', $monograms))); | pht('Failures: %s.', implode(', ', $monograms))); | ||||
| return 1; | return 1; | ||||
| } | } | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| } | } | ||||