Class: Nvoi::Cli::Db::Command
- Inherits:
-
Object
- Object
- Nvoi::Cli::Db::Command
- Defined in:
- lib/nvoi/cli/db/command.rb
Overview
Command handles database branch operations (backup/restore)
Constant Summary collapse
- BRANCHES_DIR =
"/mnt/db-branches"
Instance Method Summary collapse
- #branch_create(name = nil) ⇒ Object
- #branch_download(branch_id) ⇒ Object
- #branch_list ⇒ Object
- #branch_restore(branch_id, new_db_name = nil) ⇒ Object
-
#initialize(options) ⇒ Command
constructor
A new instance of Command.
Constructor Details
Instance Method Details
#branch_create(name = nil) ⇒ Object
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 |
# File 'lib/nvoi/cli/db/command.rb', line 15 def branch_create(name = nil) init_db_context name ||= generate_branch_id @log.info "Creating database branch: %s", name @log.separator with_ssh do |ssh| opts = @log.step "Dumping database" dump_data = @db_provider.dump(ssh, opts) @log.ok "Dump complete (%d bytes)", dump_data.bytesize @log.step "Saving branch" app_name = @config.deploy.application.name branches_path = "#{BRANCHES_DIR}/#{app_name}" ssh.execute("mkdir -p #{branches_path}") dump_file = "#{branches_path}/#{name}.#{@db_provider.extension}" write_file_via_ssh(ssh, dump_file, dump_data) (ssh, branches_path, name, dump_data.bytesize) @log.ok "Branch saved: %s", dump_file end @log.separator @log.success "Branch created: %s", name name end |
#branch_download(branch_id) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/nvoi/cli/db/command.rb', line 127 def branch_download(branch_id) init_db_context output_path = @options[:path] || "#{branch_id}.#{@db_provider.extension}" @log.info "Downloading branch: %s", branch_id dump_data = nil with_ssh do |ssh| app_name = @config.deploy.application.name branches_path = "#{BRANCHES_DIR}/#{app_name}" dump_file = "#{branches_path}/#{branch_id}.#{@db_provider.extension}" unless ssh.execute("test -f #{dump_file} && echo yes || echo no").strip == "yes" raise Errors::ServiceError, "Branch not found: #{branch_id}" end dump_data = ssh.execute("cat #{dump_file}") end File.write(output_path, dump_data) @log.success "Downloaded to: %s (%d bytes)", output_path, dump_data.bytesize output_path end |
#branch_list ⇒ Object
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 |
# File 'lib/nvoi/cli/db/command.rb', line 50 def branch_list init_db_context branches = [] with_ssh do |ssh| app_name = @config.deploy.application.name branches_path = "#{BRANCHES_DIR}/#{app_name}" = "#{branches_path}/branches.json" result = ssh.execute("test -f #{} && cat #{} || echo '{}'") begin = External::Database::Types::BranchMetadata.from_json(result) branches = .branches rescue JSON::ParserError branches = [] end end if branches.empty? @log.info "No branches found" else @log.info "Database branches:" @log.info "" @log.info "%-20s %-20s %-12s %-10s", "ID", "Created", "Size", "Adapter" @log.info "-" * 70 branches.each do |b| size_str = format_size(b.size) created = b.created_at[0, 19].gsub("T", " ") rescue b.created_at @log.info "%-20s %-20s %-12s %-10s", b.id, created, size_str, b.adapter end end branches end |
#branch_restore(branch_id, new_db_name = nil) ⇒ Object
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 |
# File 'lib/nvoi/cli/db/command.rb', line 88 def branch_restore(branch_id, new_db_name = nil) init_db_context new_db_name ||= "#{@creds.database}_#{branch_id.gsub('-', '_')}" @log.info "Restoring branch %s to database %s", branch_id, new_db_name @log.separator with_ssh do |ssh| app_name = @config.deploy.application.name branches_path = "#{BRANCHES_DIR}/#{app_name}" dump_file = "#{branches_path}/#{branch_id}.#{@db_provider.extension}" unless ssh.execute("test -f #{dump_file} && echo yes || echo no").strip == "yes" raise Errors::ServiceError, "Branch not found: #{branch_id}" end @log.step "Reading branch data" dump_data = ssh.execute("cat #{dump_file}") opts = (new_db_name) @log.step "Restoring to new database" result = @db_provider.restore(ssh, dump_data, opts) @log.ok "Restore complete" if @db_provider.is_a?(External::Database::Sqlite) @log.info "New database path: %s", result end end @log.separator @log.success "Branch restored: %s -> %s", branch_id, new_db_name output_credentials_helper(new_db_name) new_db_name end |