improve backup
This commit is contained in:
parent
db9755cad5
commit
fc71cd4a09
4 changed files with 45 additions and 17 deletions
|
|
@ -4,7 +4,14 @@ module Admin
|
|||
class BackupsController < ApplicationController
|
||||
# GET /admin/backups/1
|
||||
def show
|
||||
respond_to do |format|
|
||||
format.xlsx do
|
||||
send_file Backup.db_xlsx, filename: "backup.xlsx", disposition: :attachment
|
||||
end
|
||||
format.zip do
|
||||
send_file Backup.zip, filename: "backup.zip", disposition: :attachment
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -4,29 +4,39 @@ module Admin
|
|||
class Backup
|
||||
class << self
|
||||
def zip
|
||||
create_records_xlsx
|
||||
|
||||
# create_records_xlsx
|
||||
Dir.glob(Rails.root.join("storage", "*.sqlite3")).each do |entry|
|
||||
`sqlite3 #{entry} .dump > #{entry.sub(".sqlite3", ".dump.sql")}`
|
||||
`sqlite3 #{entry} .schema > #{entry.sub(".sqlite3", ".schema.sql")}`
|
||||
end
|
||||
ZipFileGenerator.new(Rails.root.join("storage")).write
|
||||
end
|
||||
|
||||
def db_xlsx
|
||||
xlsx = Axlsx::Package.new
|
||||
workbook = xlsx.workbook
|
||||
|
||||
ActiveRecord::Base.connection.tables.each do |table|
|
||||
klass = table.classify.safe_constantize
|
||||
next unless klass
|
||||
|
||||
workbook.add_worksheet(name: table) do |sheet|
|
||||
klass = table.classify.safe_constantize
|
||||
if klass
|
||||
attributes = klass.attribute_names
|
||||
attributes += klass.rich_text_association_names if klass.respond_to?(:rich_text_association_names)
|
||||
attributes += klass.rich_text_association_names.map(&:to_s) if klass.respond_to?(:rich_text_association_names)
|
||||
|
||||
sheet << attributes.map { _1.to_s.sub("rich_text_", "") }
|
||||
attributes = ["id"] + attributes.reject{ _1 == "id"}
|
||||
.sort { _1.sub("rich_text_", "") <=> _2.sub("rich_text_", "") }
|
||||
|
||||
sheet << attributes.map { _1.sub("rich_text_", "") }
|
||||
klass.find_each do |record|
|
||||
sheet << attributes.map { _1.starts_with?("rich_text") ? record.send(_1.to_s.sub("rich_text_", "")).body&.to_html : record.send(_1) }
|
||||
end
|
||||
else
|
||||
# TODO: Add "raw table data"
|
||||
end
|
||||
end
|
||||
path = Rails.root.join("storage/data.xls")
|
||||
end
|
||||
|
||||
path = file_path("data.xls")
|
||||
xlsx.serialize(path)
|
||||
path
|
||||
end
|
||||
|
|
|
|||
|
|
@ -19,6 +19,12 @@
|
|||
<%= link_to Link.model_name.human(count: Link.count), :links %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= link_to "Backup herunterladen", admin_backup_url, class: "btn btn-secondary", data: { turbo_prefetch: false, frame: "_top", turbo: false } %>
|
||||
</p>
|
||||
<h2 class="mt-3">Backup</h2>
|
||||
<ul class="ps-0" style="list-style-type: none">
|
||||
<li>
|
||||
<%= link_to "XLSX Backup herunterladen", admin_backup_url(format: :xlsx), class: " ", data: { turbo_prefetch: false, frame: "_top", turbo: false } %>
|
||||
</li>
|
||||
<li>
|
||||
<%= link_to "ZIP Backup herunterladen", admin_backup_url(format: :zip), class: " ", data: { turbo_prefetch: false, frame: "_top", turbo: false } %>
|
||||
</li>
|
||||
</ul>
|
||||
|
|
@ -23,8 +23,13 @@ module Admin
|
|||
login("test@example.com", "password")
|
||||
end
|
||||
|
||||
test "should show admin_backup" do
|
||||
get admin_backup_url(@admin_backup)
|
||||
test "should create admin_backup xlsx" do
|
||||
get admin_backup_url(@admin_backup, format: :xlsx)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should create admin_backup zip" do
|
||||
get admin_backup_url(@admin_backup, format: :zip)
|
||||
assert_response :success
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue