From fc71cd4a09dcbea2ec8f1999cd57068fb1fed773 Mon Sep 17 00:00:00 2001
From: david
Date: Sun, 27 Oct 2024 03:38:15 +0100
Subject: [PATCH] improve backup
---
app/controllers/admin/backups_controller.rb | 9 +++++-
app/models/admin/backup.rb | 32 ++++++++++++-------
app/views/backoffice/show.html.erb | 12 +++++--
.../admin/backups_controller_test.rb | 9 ++++--
4 files changed, 45 insertions(+), 17 deletions(-)
diff --git a/app/controllers/admin/backups_controller.rb b/app/controllers/admin/backups_controller.rb
index 6b501e0..fa26ee3 100644
--- a/app/controllers/admin/backups_controller.rb
+++ b/app/controllers/admin/backups_controller.rb
@@ -4,7 +4,14 @@ module Admin
class BackupsController < ApplicationController
# GET /admin/backups/1
def show
- send_file Backup.db_xlsx, filename: "backup.xlsx", disposition: :attachment
+ 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
diff --git a/app/models/admin/backup.rb b/app/models/admin/backup.rb
index f3b1c45..72649d4 100644
--- a/app/models/admin/backup.rb
+++ b/app/models/admin/backup.rb
@@ -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
diff --git a/app/views/backoffice/show.html.erb b/app/views/backoffice/show.html.erb
index 936bdb1..a2e52d1 100644
--- a/app/views/backoffice/show.html.erb
+++ b/app/views/backoffice/show.html.erb
@@ -19,6 +19,12 @@
<%= link_to Link.model_name.human(count: Link.count), :links %>
-
- <%= link_to "Backup herunterladen", admin_backup_url, class: "btn btn-secondary", data: { turbo_prefetch: false, frame: "_top", turbo: false } %>
-
\ No newline at end of file
+Backup
+
+ -
+ <%= link_to "XLSX Backup herunterladen", admin_backup_url(format: :xlsx), class: " ", data: { turbo_prefetch: false, frame: "_top", turbo: false } %>
+
+ -
+ <%= link_to "ZIP Backup herunterladen", admin_backup_url(format: :zip), class: " ", data: { turbo_prefetch: false, frame: "_top", turbo: false } %>
+
+
\ No newline at end of file
diff --git a/test/controllers/admin/backups_controller_test.rb b/test/controllers/admin/backups_controller_test.rb
index 9ae75bc..2db4ad8 100644
--- a/test/controllers/admin/backups_controller_test.rb
+++ b/test/controllers/admin/backups_controller_test.rb
@@ -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