|
| 1 | +require 'optparse' |
| 2 | + |
| 3 | +module Migrate |
| 4 | + CMD_NAME = 'migrate'.freeze |
| 5 | + SUMMARY_WIDTH = 40 |
| 6 | + DESC_INDENT = 45 |
| 7 | + |
| 8 | + def self.indent(str, len) |
| 9 | + str.gsub(/%%/, ' ' * len) |
| 10 | + end |
| 11 | + |
| 12 | + USAGE ||= <<~EOS.freeze |
| 13 | + Manage migrations |
| 14 | +
|
| 15 | + Usage: |
| 16 | + gitlab-ctl registry-database migrate SUBCOMMAND [options] |
| 17 | +
|
| 18 | + Subcommands: |
| 19 | + down Apply down migrations |
| 20 | + status Show migration status |
| 21 | + up Apply up migrations |
| 22 | + version Show current migration version |
| 23 | +
|
| 24 | + Options: |
| 25 | + -h, --help help for migrate |
| 26 | + EOS |
| 27 | + |
| 28 | + UP_USAGE ||= <<~EOS.freeze |
| 29 | + Apply up migrations |
| 30 | +
|
| 31 | + Usage: |
| 32 | + gitlab-ctl registry-database migrate up [options] |
| 33 | +
|
| 34 | + Options: |
| 35 | + -d, --dry-run do not commit changes to the database |
| 36 | + -h, --help help for up |
| 37 | + -l, --limit int limit the number of migrations (all by default) |
| 38 | + -s, --skip-post-deployment do not apply post deployment migrations |
| 39 | + EOS |
| 40 | + |
| 41 | + DOWN_USAGE ||= <<~EOS.freeze |
| 42 | + Apply down migrations |
| 43 | +
|
| 44 | + Usage: |
| 45 | + gitlab-ctl registry-database migrate down [options] |
| 46 | +
|
| 47 | + Options: |
| 48 | + -d, --dry-run do not commit changes to the database |
| 49 | + -f, --force no confirmation message |
| 50 | + -h, --help help for down |
| 51 | + -l, --limit int limit the number of migrations (all by default) |
| 52 | + EOS |
| 53 | + |
| 54 | + STATUS_USAGE ||= <<~EOS.freeze |
| 55 | + Show migration status |
| 56 | +
|
| 57 | + Usage: |
| 58 | + gitlab-ctl registry-database migrate status [options] |
| 59 | +
|
| 60 | + Options: |
| 61 | + -h, --help help for status |
| 62 | + -s, --skip-post-deployment ignore post deployment migrations |
| 63 | + -u, --up-to-date check if all known migrations are applied |
| 64 | +
|
| 65 | + EOS |
| 66 | + |
| 67 | + VERSION_USAGE ||= <<~EOS.freeze |
| 68 | + Show current migration version |
| 69 | +
|
| 70 | + Usage: |
| 71 | + gitlab-ctl registry-database migrate version [options] |
| 72 | +
|
| 73 | + Flags: |
| 74 | + -h, --help help for version |
| 75 | + EOS |
| 76 | + |
| 77 | + def self.parse_options!(args, options) |
| 78 | + return unless args.include? CMD_NAME |
| 79 | + |
| 80 | + loop do |
| 81 | + break if args.shift == CMD_NAME |
| 82 | + end |
| 83 | + |
| 84 | + OptionParser.new do |opts| |
| 85 | + opts.on('-h', '--help', 'Usage help') do |
| 86 | + Kernel.puts USAGE |
| 87 | + Kernel.exit 0 |
| 88 | + end |
| 89 | + end.order! args |
| 90 | + |
| 91 | + subcommands = populate_subcommands(options) |
| 92 | + subcommand = args.shift |
| 93 | + |
| 94 | + raise OptionParser::ParseError, "migrate subcommand is not specified." \ |
| 95 | + if subcommand.nil? || subcommand.empty? |
| 96 | + |
| 97 | + raise OptionParser::ParseError, "Unknown migrate subcommand: #{subcommand}" \ |
| 98 | + unless subcommands.key?(subcommand) |
| 99 | + |
| 100 | + subcommands[subcommand].parse!(args) |
| 101 | + options[:subcommand] = subcommand |
| 102 | + needs_stop!(options) |
| 103 | + |
| 104 | + options |
| 105 | + end |
| 106 | + |
| 107 | + def self.populate_subcommands(options) |
| 108 | + database_docs_url = 'https://gitlab.com/gitlab-org/container-registry/-/blob/master/docs-gitlab/database-migrations.md?ref_type=heads#administration' |
| 109 | + |
| 110 | + { |
| 111 | + 'up' => OptionParser.new do |opts| |
| 112 | + opts.banner = "Usage gitlab-ctl registry-database migrate up [options]. See documentation at #{database_docs_url}" |
| 113 | + parse_common_options!(opts) |
| 114 | + parse_up_down_common_options!(options, opts) |
| 115 | + parse_up_options!(options, opts) |
| 116 | + end, |
| 117 | + 'down' => OptionParser.new do |opts| |
| 118 | + opts.banner = "Usage gitlab-ctl registry-database migrate down [options]. See documentation at #{database_docs_url}" |
| 119 | + parse_common_options!(opts) |
| 120 | + parse_up_down_common_options!(options, opts) |
| 121 | + parse_down_options!(options, opts) |
| 122 | + end, |
| 123 | + 'status' => OptionParser.new do |opts| |
| 124 | + opts.banner = "Usage gitlab-ctl registry-database migrate status [options]. See documentation at #{database_docs_url}" |
| 125 | + parse_common_options!(opts) |
| 126 | + parse_status_options!(options, opts) |
| 127 | + end, |
| 128 | + 'version' => OptionParser.new do |opts| |
| 129 | + opts.banner = "Usage gitlab-ctl registry-database migrate version [options]. See documentation at #{database_docs_url}" |
| 130 | + opts.on('-h', '--help', 'Usage help') do |
| 131 | + Kernel.puts VERSION_USAGE |
| 132 | + Kernel.exit 0 |
| 133 | + end |
| 134 | + |
| 135 | + parse_common_options!(opts) |
| 136 | + end, |
| 137 | + } |
| 138 | + end |
| 139 | + |
| 140 | + def self.parse_common_options!(option_parser) |
| 141 | + option_parser.on("-h", "--help", "Prints this help") do |
| 142 | + option_parser.set_summary_width(SUMMARY_WIDTH) |
| 143 | + Kernel.puts USAGE |
| 144 | + Kernel.exit 0 |
| 145 | + end |
| 146 | + end |
| 147 | + |
| 148 | + def self.parse_up_down_common_options!(options, option_parser) |
| 149 | + option_parser.on('-d', '--dry-run', indent('do not commit changes to the database', DESC_INDENT)) do |
| 150 | + options[:dry_run] = '-d' |
| 151 | + end |
| 152 | + |
| 153 | + option_parser.on('-l limit', '--limit LIMIT', indent('limit the number of migrations (all by default)', DESC_INDENT)) do |limit| |
| 154 | + raise OptionParser::ParseError, "--limit option must be a positive number" \ |
| 155 | + if limit.nil? || limit.to_i <= 0 |
| 156 | + |
| 157 | + options[:limit] = limit |
| 158 | + end |
| 159 | + end |
| 160 | + |
| 161 | + def self.parse_up_options!(options, option_parser) |
| 162 | + option_parser.on('-h', '--help', 'Usage help') do |
| 163 | + Kernel.puts UP_USAGE |
| 164 | + Kernel.exit 0 |
| 165 | + end |
| 166 | + |
| 167 | + option_parser.on('-s', '--skip-post-deployment', indent('do not apply post deployment migration', DESC_INDENT)) do |
| 168 | + options[:skip_post_deploy] = '-s' |
| 169 | + end |
| 170 | + end |
| 171 | + |
| 172 | + def self.parse_down_options!(options, option_parser) |
| 173 | + option_parser.on('-h', '--help', 'Usage help') do |
| 174 | + Kernel.puts DOWN_USAGE |
| 175 | + Kernel.exit 0 |
| 176 | + end |
| 177 | + |
| 178 | + option_parser.on('-f', '--force', indent('no confirmation message', DESC_INDENT)) do |
| 179 | + options[:force] = '-f' |
| 180 | + end |
| 181 | + end |
| 182 | + |
| 183 | + def self.parse_status_options!(options, option_parser) |
| 184 | + option_parser.on('-h', '--help', 'Usage help') do |
| 185 | + Kernel.puts STATUS_USAGE |
| 186 | + Kernel.exit 0 |
| 187 | + end |
| 188 | + |
| 189 | + option_parser.on('-u', '--up-to-date', indent('do not commit changes to the database', DESC_INDENT)) do |
| 190 | + options[:up_to_date] = '-u' |
| 191 | + end |
| 192 | + |
| 193 | + option_parser.on('-s', '--skip-post-deployment', indent('do not apply post deployment migration', DESC_INDENT)) do |
| 194 | + options[:skip_post_deploy] = '-s' |
| 195 | + end |
| 196 | + end |
| 197 | + |
| 198 | + def self.needs_stop!(options) |
| 199 | + case options[:subcommand] |
| 200 | + when 'up', 'down' |
| 201 | + options[:needs_stop] = true unless options.has_key? :dry_run |
| 202 | + else |
| 203 | + options[:needs_stop] = false |
| 204 | + end |
| 205 | + end |
| 206 | +end |
0 commit comments