rails-archiver 0.1.7 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +3 -0
- data/CHANGELOG.md +13 -0
- data/lib/rails-archiver/archiver.rb +11 -2
- data/lib/rails-archiver/unarchiver.rb +62 -13
- data/rails-archiver.gemspec +2 -2
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3b31d762ea01d15091eebffd02a581b9ae2a20fba31005ec2669b4cf0d350125
|
4
|
+
data.tar.gz: 6f553c01c3a2d02062c74384c09a8e220845907544009f8d64ea97313afba6af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11961ec8a0cacd1101792d3beade62fa79eb224a3c254edcb42a67a0a0f16ce423f7fefec0a5adb1b535ee5a33864e6dd5e02d8e8df22b83f1f490925741e9ae
|
7
|
+
data.tar.gz: 3f37961ca490f99900f3de97e0e76d4b6ae83e9fa7977ebadedfb55784102b8d33dc8975aead38af477d3732350774212b8b11f323ae3a093a05aaca34a4811f
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## UNRELEASED
|
9
|
+
|
10
|
+
# [0.2.0] - 2025-01-20
|
11
|
+
- Added support for enum columns - should be able to handle both keys and values.
|
12
|
+
- Refactored the `unarchiver` method to more easily subclass `Unarchiver`.
|
13
|
+
- For `belongs_to` associations, replace foreign key attributes with actual reference to the relevant object while archiving to avoid issues with validation.
|
@@ -128,9 +128,18 @@ module RailsArchiver
|
|
128
128
|
@logger.info("Finished deleting from #{table}")
|
129
129
|
end
|
130
130
|
|
131
|
+
# @return [Class]
|
132
|
+
def unarchiver_class
|
133
|
+
RailsArchiver::Unarchiver
|
134
|
+
end
|
135
|
+
|
131
136
|
# @return [RailsArchiver::Unarchiver]
|
132
|
-
|
133
|
-
|
137
|
+
# @param crash_on_errors [Boolean]
|
138
|
+
def unarchiver(crash_on_errors: false)
|
139
|
+
unarchiver = unarchiver_class.new(@model,
|
140
|
+
logger: @logger,
|
141
|
+
crash_on_errors: crash_on_errors
|
142
|
+
)
|
134
143
|
unarchiver.transport = self.transport
|
135
144
|
unarchiver
|
136
145
|
end
|
@@ -11,18 +11,24 @@ module RailsArchiver
|
|
11
11
|
attr_accessor :errors, :transport
|
12
12
|
|
13
13
|
# @param model [ActiveRecord::Base]
|
14
|
-
# @param
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
def initialize(model,
|
14
|
+
# @param logger [Logger]
|
15
|
+
# @param new_copy [Boolean] if true, create all new objects instead of
|
16
|
+
# replacing existing ones.
|
17
|
+
# @param crash_on_errors [Boolean] if true, do not do any imports if any
|
18
|
+
# models' validations failed.
|
19
|
+
# @param transport [Symbol|RailsArchiver::Transport::Base]
|
20
|
+
def initialize(model,
|
21
|
+
transport: :in_memory,
|
22
|
+
logger: Logger.new(STDOUT),
|
23
|
+
new_copy: false, crash_on_errors: false)
|
21
24
|
@model = model
|
22
|
-
@logger =
|
23
|
-
@options =
|
25
|
+
@logger = logger
|
26
|
+
@options = {
|
27
|
+
new_copy: new_copy,
|
28
|
+
crash_on_errors: crash_on_errors
|
29
|
+
}
|
24
30
|
# Transport for downloading
|
25
|
-
self.transport = _get_transport(
|
31
|
+
self.transport = _get_transport(transport)
|
26
32
|
self.errors = []
|
27
33
|
end
|
28
34
|
|
@@ -40,7 +46,7 @@ module RailsArchiver
|
|
40
46
|
if @model
|
41
47
|
@model.reload
|
42
48
|
if @model.attribute_names.include?('archived')
|
43
|
-
@model.
|
49
|
+
@model.class.where(id: @model.id).update_all(:archived => false)
|
44
50
|
end
|
45
51
|
end
|
46
52
|
@logger.info("#{source} load complete!")
|
@@ -51,10 +57,14 @@ module RailsArchiver
|
|
51
57
|
def load_classes(hash)
|
52
58
|
full_hash = hash.with_indifferent_access
|
53
59
|
full_hash.each do |key, vals|
|
54
|
-
|
60
|
+
begin
|
61
|
+
save_models(key.constantize, vals)
|
62
|
+
rescue NameError => e
|
63
|
+
@logger.error("Could not save models for #{key}: #{e.message}")
|
64
|
+
end
|
55
65
|
end
|
56
66
|
if @options[:crash_on_errors] && self.errors.any?
|
57
|
-
raise ImportError.new("Errors occurred during load
|
67
|
+
raise ImportError.new("Errors occurred during load: #{self.errors.join("\n")}")
|
58
68
|
end
|
59
69
|
end
|
60
70
|
|
@@ -110,13 +120,52 @@ module RailsArchiver
|
|
110
120
|
end
|
111
121
|
|
112
122
|
model
|
123
|
+
rescue => e
|
124
|
+
self.errors << "Error importing class #{klass.name}: #{e.message}"
|
113
125
|
end
|
114
126
|
|
115
127
|
private
|
116
128
|
|
129
|
+
# @param model [ActiveRecord::Base]
|
130
|
+
# @param field [String]
|
131
|
+
# @param attrs [Hash]
|
132
|
+
def assign_enum_value(model, field, attrs)
|
133
|
+
entries = model.class.public_send(field.pluralize)
|
134
|
+
is_integer = entries.values.first.is_a?(Integer)
|
135
|
+
value = attrs[field]
|
136
|
+
valid = if entries.keys.include?(value)
|
137
|
+
true
|
138
|
+
elsif is_integer && entries.values.include?(value.to_i)
|
139
|
+
true
|
140
|
+
else
|
141
|
+
entries.values.include?(value)
|
142
|
+
end
|
143
|
+
|
144
|
+
return if valid
|
145
|
+
|
146
|
+
@logger.warn("Invalid value for #{field}: #{attrs[field]}")
|
147
|
+
new_value = model.class.columns.find { |a| a.name == field }.default
|
148
|
+
new_value = new_value.to_i if is_integer && new_value
|
149
|
+
attrs[field] = new_value
|
150
|
+
end
|
151
|
+
|
117
152
|
# @param model [ActiveRecord::Base]
|
118
153
|
# @param attrs [Hash]
|
119
154
|
def _assign_attributes(model, attrs)
|
155
|
+
# Handle integer enum values that were turned into strings
|
156
|
+
if model.class.respond_to?(:attribute_types)
|
157
|
+
enums = model.class.attribute_types.select { |_, v| v.is_a?(ActiveRecord::Enum::EnumType) }
|
158
|
+
enums.keys.each do |field|
|
159
|
+
assign_enum_value(model, field, attrs)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
model.class.reflect_on_all_associations(:belongs_to).each do |assoc|
|
163
|
+
if attrs[assoc.foreign_key].present?
|
164
|
+
record = assoc.klass.find_by(assoc.association_primary_key => attrs[assoc.foreign_key])
|
165
|
+
attrs[assoc.name] = record if record
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
120
169
|
if model.method(:attributes=).arity == 1
|
121
170
|
model.attributes = attrs
|
122
171
|
else
|
data/rails-archiver.gemspec
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'rails-archiver'
|
3
3
|
s.require_paths = %w(. lib lib/rails-archiver)
|
4
|
-
s.version = '0.
|
5
|
-
s.date = '
|
4
|
+
s.version = '0.2.0'
|
5
|
+
s.date = '2025-01-20'
|
6
6
|
s.summary = 'Fully archive a Rails model'
|
7
7
|
s.description = <<-EOF
|
8
8
|
EOF
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-archiver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Orner
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -44,6 +44,8 @@ executables: []
|
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
|
+
- ".gitignore"
|
48
|
+
- CHANGELOG.md
|
47
49
|
- README.md
|
48
50
|
- lib/rails-archiver.rb
|
49
51
|
- lib/rails-archiver/archiver.rb
|
@@ -56,7 +58,7 @@ homepage: https://github.com/dorner/rails-archiver
|
|
56
58
|
licenses:
|
57
59
|
- MIT
|
58
60
|
metadata: {}
|
59
|
-
post_install_message:
|
61
|
+
post_install_message:
|
60
62
|
rdoc_options: []
|
61
63
|
require_paths:
|
62
64
|
- "."
|
@@ -73,9 +75,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
75
|
- !ruby/object:Gem::Version
|
74
76
|
version: '0'
|
75
77
|
requirements: []
|
76
|
-
|
77
|
-
|
78
|
-
signing_key:
|
78
|
+
rubygems_version: 3.4.10
|
79
|
+
signing_key:
|
79
80
|
specification_version: 4
|
80
81
|
summary: Fully archive a Rails model
|
81
82
|
test_files: []
|