improve backup
This commit is contained in:
parent
db9755cad5
commit
fc71cd4a09
4 changed files with 45 additions and 17 deletions
|
|
@ -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|
|
||||
attributes = klass.attribute_names
|
||||
attributes += klass.rich_text_association_names if klass.respond_to?(:rich_text_association_names)
|
||||
klass = table.classify.safe_constantize
|
||||
if klass
|
||||
attributes = klass.attribute_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_", "") }
|
||||
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) }
|
||||
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
|
||||
end
|
||||
path = Rails.root.join("storage/data.xls")
|
||||
|
||||
path = file_path("data.xls")
|
||||
xlsx.serialize(path)
|
||||
path
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue