Skip to content

Commit

Permalink
Safer & cleaner database updates
Browse files Browse the repository at this point in the history
  • Loading branch information
lmrodriguezr committed Jun 17, 2024
1 parent 6829496 commit 95af6b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
25 changes: 20 additions & 5 deletions lib/miga/cli/action/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ def perform
cli.puts "# Host: #{manif[:host]}"
cli.puts "# Manifest last update: #{manif[:last_update]}"
list_databases(manif) and return
db = db_requested(manif)
db_sym, db = db_requested(manif)
list_versions(db) and return
ver = version_requested(db)
check_target and return

# Download and expand
file = download_file(@ftp, ver[:path], cli[:reuse_archive])
check_digest(ver, file)
unarchive(file)
unarchive(file, db_sym.to_s)
register_database(manif, db, ver)
end

Expand Down Expand Up @@ -168,7 +168,7 @@ def db_requested(manif)
db = manif[:databases][cli[:database]]
raise 'Cannot find database in this host' if db.nil?

db
[cli[:database], db]
end

def version_requested(db)
Expand Down Expand Up @@ -240,13 +240,28 @@ def version_size(ver)
cli.num_suffix(ver[:size_unarchived], true) + ')'
end

def unarchive(file)
def unarchive(file, db_name)
cli.say "Unarchiving #{file}"
tmp_archive = File.join(cli[:local], '.TMP_ARCHIVE')
FileUtils.mkdir_p(tmp_archive)
MiGA::MiGA.run_cmd <<~CMD
cd #{cli[:local].shellescape} \
cd #{tmp_archive.shellescape} \
&& tar -zxf #{file.shellescape} \
&& rm #{file.shellescape}
CMD

target = File.join(cli[:local], db_name)
source = File.join(tmp_archive, db_name)
if Dir.exist?(target)
cli.say 'Replacing previous version'
tmp = File.join(tmp_archive, "#{db_name}.old")
File.rename(target, tmp)
File.rename(source, target)
File.rm_rf(tmp)
else
File.rename(source, target)
end
Dir.unlink(tmp_archive) if Dir.empty?(tmp_archive)
end

def register_database(manif, db, ver)
Expand Down
4 changes: 2 additions & 2 deletions lib/miga/cli/action/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def parse_cli

def perform
puts <<~BASH
MIGA="#{MiGA::MiGA.root_path}"
MIGA_HOME=${MIGA_HOME:-"$HOME"}
export MIGA="#{MiGA::MiGA.root_path}"
export MIGA_HOME=${MIGA_HOME:-"$HOME"}
. "$MIGA_HOME/.miga_rc"
# Ensure MiGA & submodules are first in PATH
export PATH="$MIGA/bin:$PATH"
Expand Down
4 changes: 2 additions & 2 deletions lib/miga/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ module MiGA
# - String indicating release status:
# - rc* release candidate, not released as gem
# - [0-9]+ stable release, released as gem
VERSION = [1.3, 15, 6].freeze
VERSION = [1.3, 15, 7].freeze

##
# Nickname for the current major.minor version.
VERSION_NAME = 'mezzotint'

##
# Date of the current gem relese.
VERSION_DATE = Date.new(2024, 6, 5)
VERSION_DATE = Date.new(2024, 6, 17)

##
# References of MiGA
Expand Down

0 comments on commit 95af6b3

Please sign in to comment.