archive_hook 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +176 -0
- data/LICENSE.txt +21 -0
- data/README.md +162 -0
- data/Rakefile +6 -0
- data/archive_hook.gemspec +38 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/archive_hook/version.rb +3 -0
- data/lib/archive_hook.rb +116 -0
- data/lib/generators/archive_hook/USAGE +8 -0
- data/lib/generators/archive_hook/archive_hook_generator.rb +27 -0
- data/lib/generators/archive_hook/templates/migration.rb +11 -0
- metadata +106 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 19b341cbc8d91d83b80ff2c777b6c2abba0218ac6ad5e2ff1babee26a9f39502
|
|
4
|
+
data.tar.gz: a1c5c5ff85989613db345b5bb5a173e164e96a1d55776a7bf6a199c7d63604e7
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c3f0b6235c99999eb379cd8985902464b568f0cf5c68e8453aa88226cca508fb3a4a398671ef70b5d6124dc048bcf0ef897344eb3f3f8f5316fab87c39ff0efd
|
|
7
|
+
data.tar.gz: aeb121668ac39e8f9b2b95e69a4a4332371657ad0b1e554e834d06cb2738fcc71bc3454fa3c11b95e54a6aaa086f726bcd52650f98232df7a4c44ba33f4a857c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at shurik.v.r@gmail.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
archive_hook (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
actioncable (6.1.4.7)
|
|
10
|
+
actionpack (= 6.1.4.7)
|
|
11
|
+
activesupport (= 6.1.4.7)
|
|
12
|
+
nio4r (~> 2.0)
|
|
13
|
+
websocket-driver (>= 0.6.1)
|
|
14
|
+
actionmailbox (6.1.4.7)
|
|
15
|
+
actionpack (= 6.1.4.7)
|
|
16
|
+
activejob (= 6.1.4.7)
|
|
17
|
+
activerecord (= 6.1.4.7)
|
|
18
|
+
activestorage (= 6.1.4.7)
|
|
19
|
+
activesupport (= 6.1.4.7)
|
|
20
|
+
mail (>= 2.7.1)
|
|
21
|
+
actionmailer (6.1.4.7)
|
|
22
|
+
actionpack (= 6.1.4.7)
|
|
23
|
+
actionview (= 6.1.4.7)
|
|
24
|
+
activejob (= 6.1.4.7)
|
|
25
|
+
activesupport (= 6.1.4.7)
|
|
26
|
+
mail (~> 2.5, >= 2.5.4)
|
|
27
|
+
rails-dom-testing (~> 2.0)
|
|
28
|
+
actionpack (6.1.4.7)
|
|
29
|
+
actionview (= 6.1.4.7)
|
|
30
|
+
activesupport (= 6.1.4.7)
|
|
31
|
+
rack (~> 2.0, >= 2.0.9)
|
|
32
|
+
rack-test (>= 0.6.3)
|
|
33
|
+
rails-dom-testing (~> 2.0)
|
|
34
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
|
35
|
+
actiontext (6.1.4.7)
|
|
36
|
+
actionpack (= 6.1.4.7)
|
|
37
|
+
activerecord (= 6.1.4.7)
|
|
38
|
+
activestorage (= 6.1.4.7)
|
|
39
|
+
activesupport (= 6.1.4.7)
|
|
40
|
+
nokogiri (>= 1.8.5)
|
|
41
|
+
actionview (6.1.4.7)
|
|
42
|
+
activesupport (= 6.1.4.7)
|
|
43
|
+
builder (~> 3.1)
|
|
44
|
+
erubi (~> 1.4)
|
|
45
|
+
rails-dom-testing (~> 2.0)
|
|
46
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
|
47
|
+
activejob (6.1.4.7)
|
|
48
|
+
activesupport (= 6.1.4.7)
|
|
49
|
+
globalid (>= 0.3.6)
|
|
50
|
+
activemodel (6.1.4.7)
|
|
51
|
+
activesupport (= 6.1.4.7)
|
|
52
|
+
activerecord (6.1.4.7)
|
|
53
|
+
activemodel (= 6.1.4.7)
|
|
54
|
+
activesupport (= 6.1.4.7)
|
|
55
|
+
activestorage (6.1.4.7)
|
|
56
|
+
actionpack (= 6.1.4.7)
|
|
57
|
+
activejob (= 6.1.4.7)
|
|
58
|
+
activerecord (= 6.1.4.7)
|
|
59
|
+
activesupport (= 6.1.4.7)
|
|
60
|
+
marcel (~> 1.0.0)
|
|
61
|
+
mini_mime (>= 1.1.0)
|
|
62
|
+
activesupport (6.1.4.7)
|
|
63
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
64
|
+
i18n (>= 1.6, < 2)
|
|
65
|
+
minitest (>= 5.1)
|
|
66
|
+
tzinfo (~> 2.0)
|
|
67
|
+
zeitwerk (~> 2.3)
|
|
68
|
+
builder (3.2.4)
|
|
69
|
+
concurrent-ruby (1.2.2)
|
|
70
|
+
crass (1.0.6)
|
|
71
|
+
date (3.3.3)
|
|
72
|
+
diff-lcs (1.5.0)
|
|
73
|
+
erubi (1.12.0)
|
|
74
|
+
globalid (1.1.0)
|
|
75
|
+
activesupport (>= 5.0)
|
|
76
|
+
i18n (1.14.1)
|
|
77
|
+
concurrent-ruby (~> 1.0)
|
|
78
|
+
loofah (2.21.3)
|
|
79
|
+
crass (~> 1.0.2)
|
|
80
|
+
nokogiri (>= 1.12.0)
|
|
81
|
+
mail (2.8.1)
|
|
82
|
+
mini_mime (>= 0.1.1)
|
|
83
|
+
net-imap
|
|
84
|
+
net-pop
|
|
85
|
+
net-smtp
|
|
86
|
+
marcel (1.0.2)
|
|
87
|
+
method_source (1.0.0)
|
|
88
|
+
mini_mime (1.1.2)
|
|
89
|
+
minitest (5.18.1)
|
|
90
|
+
net-imap (0.3.6)
|
|
91
|
+
date
|
|
92
|
+
net-protocol
|
|
93
|
+
net-pop (0.1.2)
|
|
94
|
+
net-protocol
|
|
95
|
+
net-protocol (0.2.1)
|
|
96
|
+
timeout
|
|
97
|
+
net-smtp (0.3.3)
|
|
98
|
+
net-protocol
|
|
99
|
+
nio4r (2.5.9)
|
|
100
|
+
nokogiri (1.13.10-x86_64-darwin)
|
|
101
|
+
racc (~> 1.4)
|
|
102
|
+
pg (1.5.3)
|
|
103
|
+
racc (1.7.1)
|
|
104
|
+
rack (2.2.7)
|
|
105
|
+
rack-test (2.1.0)
|
|
106
|
+
rack (>= 1.3)
|
|
107
|
+
rails (6.1.4.7)
|
|
108
|
+
actioncable (= 6.1.4.7)
|
|
109
|
+
actionmailbox (= 6.1.4.7)
|
|
110
|
+
actionmailer (= 6.1.4.7)
|
|
111
|
+
actionpack (= 6.1.4.7)
|
|
112
|
+
actiontext (= 6.1.4.7)
|
|
113
|
+
actionview (= 6.1.4.7)
|
|
114
|
+
activejob (= 6.1.4.7)
|
|
115
|
+
activemodel (= 6.1.4.7)
|
|
116
|
+
activerecord (= 6.1.4.7)
|
|
117
|
+
activestorage (= 6.1.4.7)
|
|
118
|
+
activesupport (= 6.1.4.7)
|
|
119
|
+
bundler (>= 1.15.0)
|
|
120
|
+
railties (= 6.1.4.7)
|
|
121
|
+
sprockets-rails (>= 2.0.0)
|
|
122
|
+
rails-dom-testing (2.1.1)
|
|
123
|
+
activesupport (>= 5.0.0)
|
|
124
|
+
minitest
|
|
125
|
+
nokogiri (>= 1.6)
|
|
126
|
+
rails-html-sanitizer (1.5.0)
|
|
127
|
+
loofah (~> 2.19, >= 2.19.1)
|
|
128
|
+
railties (6.1.4.7)
|
|
129
|
+
actionpack (= 6.1.4.7)
|
|
130
|
+
activesupport (= 6.1.4.7)
|
|
131
|
+
method_source
|
|
132
|
+
rake (>= 0.13)
|
|
133
|
+
thor (~> 1.0)
|
|
134
|
+
rake (10.5.0)
|
|
135
|
+
rspec (3.12.0)
|
|
136
|
+
rspec-core (~> 3.12.0)
|
|
137
|
+
rspec-expectations (~> 3.12.0)
|
|
138
|
+
rspec-mocks (~> 3.12.0)
|
|
139
|
+
rspec-core (3.12.2)
|
|
140
|
+
rspec-support (~> 3.12.0)
|
|
141
|
+
rspec-expectations (3.12.3)
|
|
142
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
143
|
+
rspec-support (~> 3.12.0)
|
|
144
|
+
rspec-mocks (3.12.6)
|
|
145
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
146
|
+
rspec-support (~> 3.12.0)
|
|
147
|
+
rspec-support (3.12.1)
|
|
148
|
+
sprockets (4.2.0)
|
|
149
|
+
concurrent-ruby (~> 1.0)
|
|
150
|
+
rack (>= 2.2.4, < 4)
|
|
151
|
+
sprockets-rails (3.4.2)
|
|
152
|
+
actionpack (>= 5.2)
|
|
153
|
+
activesupport (>= 5.2)
|
|
154
|
+
sprockets (>= 3.0.0)
|
|
155
|
+
thor (1.2.2)
|
|
156
|
+
timeout (0.4.0)
|
|
157
|
+
tzinfo (2.0.6)
|
|
158
|
+
concurrent-ruby (~> 1.0)
|
|
159
|
+
websocket-driver (0.7.5)
|
|
160
|
+
websocket-extensions (>= 0.1.0)
|
|
161
|
+
websocket-extensions (0.1.5)
|
|
162
|
+
zeitwerk (2.6.8)
|
|
163
|
+
|
|
164
|
+
PLATFORMS
|
|
165
|
+
x86_64-darwin-20
|
|
166
|
+
|
|
167
|
+
DEPENDENCIES
|
|
168
|
+
archive_hook!
|
|
169
|
+
bundler (~> 2.3)
|
|
170
|
+
pg
|
|
171
|
+
rails (~> 6.1)
|
|
172
|
+
rake (~> 10.0)
|
|
173
|
+
rspec (~> 3.0)
|
|
174
|
+
|
|
175
|
+
BUNDLED WITH
|
|
176
|
+
2.3.26
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 alvir
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# ArchiveHook
|
|
2
|
+
|
|
3
|
+
`ArchiveHook` moves old `ActiveRecord` records out of their table and into a parallel
|
|
4
|
+
`_archive` table, following a dependency tree so related child records are archived (or
|
|
5
|
+
restored) together. It's designed for pruning large, fast-growing tables (reservations,
|
|
6
|
+
payments, logs, etc.) without losing the data — everything just moves to `<table>_archive`.
|
|
7
|
+
|
|
8
|
+
Archiving is done with raw `INSERT INTO ... SELECT` + `DELETE`/`delete_all` SQL run inside a
|
|
9
|
+
transaction, in batches, so it stays reasonably efficient on large tables.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Add this line to your application's Gemfile:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
gem 'archive_hook'
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
And then execute:
|
|
20
|
+
|
|
21
|
+
$ bundle
|
|
22
|
+
|
|
23
|
+
Or install it yourself as:
|
|
24
|
+
|
|
25
|
+
$ gem install archive_hook
|
|
26
|
+
|
|
27
|
+
## Setup
|
|
28
|
+
|
|
29
|
+
For every model you want to archive, create a sibling `<table_name>_archive` table with the
|
|
30
|
+
same columns. A Rails generator is included to scaffold the migration for you:
|
|
31
|
+
|
|
32
|
+
$ rails generate archive_hook Board
|
|
33
|
+
|
|
34
|
+
which creates `db/migrate/..._create_boards_archive.rb`:
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
class CreateBoardsArchive < ActiveRecord::Migration[7.0]
|
|
38
|
+
def up
|
|
39
|
+
execute("CREATE TABLE boards_archive (LIKE boards)")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def down
|
|
43
|
+
drop_table :boards_archive
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Run the generator for the root model and for every model listed under a `children:` entry in
|
|
49
|
+
your dependency mapping (see below) — each one needs its own `_archive` table.
|
|
50
|
+
|
|
51
|
+
`ArchiveHook` reads the columns off the model at call time, so the archive table just needs to
|
|
52
|
+
have matching column names — it doesn't need matching constraints, indexes, or an FK to the
|
|
53
|
+
original table.
|
|
54
|
+
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
### Archiving everything older than a date (`.archive`)
|
|
58
|
+
|
|
59
|
+
`ArchiveHook.archive(model, date, dependencies)` finds root records older than `date` (by
|
|
60
|
+
`created_at`, or a custom column — see below) and archives them together with their declared
|
|
61
|
+
children:
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
ArchiveHook.archive(Board, 1.year.ago, Board => { children: [Card] }, Card => { children: [Tag] })
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Typically run from a scheduled job:
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
class ArchiveOldBoardsJob
|
|
71
|
+
MAPPING = {
|
|
72
|
+
Board => { children: [Card] },
|
|
73
|
+
Card => { children: [Tag] }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
def self.perform
|
|
77
|
+
ArchiveHook.archive(Board, 1.year.ago, MAPPING)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
To archive by a column other than `created_at`, set `column:` on the root model's entry:
|
|
83
|
+
|
|
84
|
+
```ruby
|
|
85
|
+
ArchiveHook.archive(Board, 1.year.ago, Board => { children: [Card], column: :published_at })
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Only root records matching the date filter are selected — children are archived because they
|
|
89
|
+
belong to an archived root, not because they're independently outdated.
|
|
90
|
+
|
|
91
|
+
### Archiving an arbitrary scope (`.archive_scope`)
|
|
92
|
+
|
|
93
|
+
`ArchiveHook.archive_scope(scope, dependencies = {})` archives whatever `ActiveRecord::Relation`
|
|
94
|
+
you pass in, applying the same dependency tree to its children. This is handy when the set of
|
|
95
|
+
records to archive isn't simply "older than X":
|
|
96
|
+
|
|
97
|
+
```ruby
|
|
98
|
+
ArchiveHook.archive_scope(Board.where(id: stale_ids), Board => { children: [Card] })
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
`dependencies` can be omitted (or `nil`) when the scope has no children to cascade to:
|
|
102
|
+
|
|
103
|
+
```ruby
|
|
104
|
+
ArchiveHook.archive_scope(Tag.where(id: stale_tag_ids), nil)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Restoring archived records (`.restore_scope`)
|
|
108
|
+
|
|
109
|
+
`ArchiveHook.restore_scope(scope, dependencies = {})` does the reverse: it reads from
|
|
110
|
+
`<table>_archive` (aliased to the original table name, so you can scope/query it normally),
|
|
111
|
+
re-inserts into the original table, and deletes the archived rows — again cascading through
|
|
112
|
+
`dependencies`:
|
|
113
|
+
|
|
114
|
+
```ruby
|
|
115
|
+
ArchiveHook.restore_scope(Board.where(id: board_ids), Board => { children: [Card] })
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Dependency mapping
|
|
119
|
+
|
|
120
|
+
`dependencies` is a `{ model => { children: [...], column: :... } }` hash:
|
|
121
|
+
|
|
122
|
+
- `children` — models that belong to this one (matched by the inferred foreign key, e.g.
|
|
123
|
+
`Card` is expected to have a `board_id` column when nested under `Board`) and that should be
|
|
124
|
+
archived/restored along with it.
|
|
125
|
+
- `column` — the timestamp column used by `.archive` to decide what's "old"; defaults to
|
|
126
|
+
`:created_at`. Only relevant for the root model passed to `.archive`.
|
|
127
|
+
|
|
128
|
+
Every level of the tree that has further descendants must have its own `children` entry — a
|
|
129
|
+
model that's missing from `dependencies` is treated as a leaf. If it's actually a leaf that's
|
|
130
|
+
fine, but if it has its own children that aren't declared, deleting it will violate the foreign
|
|
131
|
+
key from its undeclared children and raise an `ActiveRecordError`.
|
|
132
|
+
|
|
133
|
+
## Notes / gotchas
|
|
134
|
+
|
|
135
|
+
- All archiving/restoring runs in batches (`in_batches`), each batch wrapped in its own
|
|
136
|
+
transaction — a failure partway through can leave earlier batches archived.
|
|
137
|
+
- `default_scope` on a model doesn't hide it from `ArchiveHook.archive` — it operates on
|
|
138
|
+
`unscoped` relations internally.
|
|
139
|
+
- Because `.archive` only filters the root by date, make sure children are only ever reachable
|
|
140
|
+
through an archived root (or archive/restore them explicitly via `.archive_scope`).
|
|
141
|
+
|
|
142
|
+
## Development
|
|
143
|
+
|
|
144
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to
|
|
145
|
+
run the tests (they need a local Postgres instance; the suite creates and drops an
|
|
146
|
+
`archive_hook_test` database automatically). You can also run `bin/console` for an interactive
|
|
147
|
+
prompt that will allow you to experiment.
|
|
148
|
+
|
|
149
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new
|
|
150
|
+
version, update the version number in `version.rb`, and then run `bundle exec rake release`,
|
|
151
|
+
which will create a git tag for the version, push git commits and tags, and push the `.gem`
|
|
152
|
+
file to [rubygems.org](https://rubygems.org).
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
157
|
+
|
|
158
|
+
## Code of Conduct
|
|
159
|
+
|
|
160
|
+
Everyone interacting in the ArchiveHook project's codebases, issue trackers, chat rooms and
|
|
161
|
+
mailing lists is expected to follow the
|
|
162
|
+
[code of conduct](https://github.com/alvir/archive_hook/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "archive_hook/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "archive_hook"
|
|
8
|
+
spec.version = ArchiveHook::VERSION
|
|
9
|
+
spec.authors = ["alvir"]
|
|
10
|
+
spec.email = ["shurik.v.r@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Archive ActiveRecord models (and their dependents) to parallel _archive tables}
|
|
13
|
+
spec.description = %q{Moves old ActiveRecord records into sibling <table>_archive tables, cascading through a declared parent/child dependency tree, with support for restoring them back.}
|
|
14
|
+
spec.homepage = "https://github.com/alvir/archive_hook"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
if spec.respond_to?(:metadata)
|
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
19
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
20
|
+
# spec.metadata["changelog_uri"] = ""
|
|
21
|
+
else
|
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
|
23
|
+
"public gem pushes."
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
28
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
30
|
+
end
|
|
31
|
+
spec.bindir = "exe"
|
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
33
|
+
spec.require_paths = ["lib"]
|
|
34
|
+
|
|
35
|
+
spec.add_development_dependency "bundler", "~> 2.3"
|
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
38
|
+
end
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "archive_hook"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/archive_hook.rb
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
require "archive_hook/version"
|
|
2
|
+
|
|
3
|
+
module ArchiveHook
|
|
4
|
+
class ScopeArchiver
|
|
5
|
+
def initialize(dependencies: {})
|
|
6
|
+
@dependencies = dependencies
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def call(scope)
|
|
10
|
+
parent = scope.model
|
|
11
|
+
parent_id_groups = scope.in_batches.map { |relation| relation.pluck(:id) }
|
|
12
|
+
return if parent_id_groups.empty?
|
|
13
|
+
|
|
14
|
+
archive_children(parent, parent_id_groups)
|
|
15
|
+
parent_id_groups.each do |parent_ids|
|
|
16
|
+
archive_by_scope(parent.unscoped.where(id: parent_ids))
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def archive_children(parent, parent_id_groups)
|
|
23
|
+
return unless @dependencies[parent] && @dependencies[parent][:children].present?
|
|
24
|
+
|
|
25
|
+
@dependencies[parent][:children].each do |child|
|
|
26
|
+
parent_id_groups.each do |parent_ids|
|
|
27
|
+
call(child.unscoped.where(parent.to_s.foreign_key => parent_ids))
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def archive_by_scope(scope)
|
|
33
|
+
ActiveRecord::Base.transaction do
|
|
34
|
+
ActiveRecord::Base.connection.execute(Arel.sql(archive_records_sql(scope)))
|
|
35
|
+
scope.delete_all
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def archive_records_sql(scope)
|
|
40
|
+
table_name = scope.table_name
|
|
41
|
+
attributes_list = scope.column_names.join(",")
|
|
42
|
+
<<-SQL
|
|
43
|
+
INSERT INTO #{table_name}_archive (#{attributes_list})
|
|
44
|
+
#{scope.select(attributes_list).to_sql}
|
|
45
|
+
SQL
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
class ScopeRestorer
|
|
50
|
+
def initialize(dependencies: {})
|
|
51
|
+
@dependencies = dependencies
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def call(scope)
|
|
55
|
+
parent = scope.model
|
|
56
|
+
table_name = "#{scope.table_name}_archive as #{scope.table_name}"
|
|
57
|
+
parent_id_groups = scope.from(table_name).in_batches.map { |relation| relation.pluck(:id) }
|
|
58
|
+
parent_id_groups.each do |parent_ids|
|
|
59
|
+
restore_by_ids(parent, parent_ids)
|
|
60
|
+
end
|
|
61
|
+
restore_children(parent, parent_id_groups)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
def restore_children(parent, parent_id_groups)
|
|
67
|
+
return unless @dependencies[parent] && @dependencies[parent][:children].present?
|
|
68
|
+
|
|
69
|
+
@dependencies[parent][:children].each do |child|
|
|
70
|
+
if parent_id_groups.present?
|
|
71
|
+
parent_id_groups.each do |parent_ids|
|
|
72
|
+
call(child.unscoped.where(parent.to_s.foreign_key => parent_ids))
|
|
73
|
+
end
|
|
74
|
+
else
|
|
75
|
+
call(child.none)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def restore_by_ids(model, ids)
|
|
81
|
+
table_name = model.table_name
|
|
82
|
+
ActiveRecord::Base.transaction do
|
|
83
|
+
ActiveRecord::Base.connection.execute(Arel.sql(restore_records_sql(model, ids)))
|
|
84
|
+
ActiveRecord::Base.connection.execute(Arel.sql <<-SQL
|
|
85
|
+
DELETE FROM #{table_name}_archive WHERE id IN (#{ids.join(', ')})
|
|
86
|
+
SQL
|
|
87
|
+
)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def restore_records_sql(model, ids)
|
|
92
|
+
table_name = model.table_name
|
|
93
|
+
attributes_list = model.column_names.join(",")
|
|
94
|
+
<<-SQL
|
|
95
|
+
INSERT INTO #{table_name} (#{attributes_list})
|
|
96
|
+
SELECT #{attributes_list} FROM #{table_name}_archive WHERE id IN (#{ids.join(', ')})
|
|
97
|
+
SQL
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
class << self
|
|
102
|
+
def archive(root, archive_date, dependencies)
|
|
103
|
+
column = dependencies[root] && dependencies[root][:column] || :created_at
|
|
104
|
+
base_scope = root.where("#{column} < ?", archive_date)
|
|
105
|
+
ScopeArchiver.new(dependencies: dependencies).call(base_scope)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def archive_scope(scope, dependencies = {})
|
|
109
|
+
ScopeArchiver.new(dependencies: dependencies).call(scope)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def restore_scope(scope, dependencies = {})
|
|
113
|
+
ScopeRestorer.new(dependencies: dependencies).call(scope)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
class ArchiveHookGenerator < Rails::Generators::NamedBase
|
|
2
|
+
include Rails::Generators::Migration
|
|
3
|
+
|
|
4
|
+
source_root File.expand_path('templates', __dir__)
|
|
5
|
+
|
|
6
|
+
def copy_archive_hook_migration
|
|
7
|
+
migration_template "migration.rb", migration_file_name, migration_version: migration_version, archive_table_name: archive_table_name
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def self.next_migration_number(_dir_name)
|
|
11
|
+
Time.now.utc.to_s.tr('^0-9', '')[0..13]
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def migration_file_name
|
|
15
|
+
"db/migrate/create_#{archive_table_name}.rb"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def archive_table_name
|
|
19
|
+
"#{table_name}_archive"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def migration_version
|
|
23
|
+
if Rails::VERSION::MAJOR >= 5
|
|
24
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Create<%= archive_table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
|
|
4
|
+
def up
|
|
5
|
+
execute("CREATE TABLE <%= archive_table_name %> (LIKE <%= table_name %>)")
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def down
|
|
9
|
+
drop_table :<%= archive_table_name %>
|
|
10
|
+
end
|
|
11
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: archive_hook
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- alvir
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-08-27 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.3'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '2.3'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
description: Moves old ActiveRecord records into sibling <table>_archive tables, cascading
|
|
56
|
+
through a declared parent/child dependency tree, with support for restoring them
|
|
57
|
+
back.
|
|
58
|
+
email:
|
|
59
|
+
- shurik.v.r@gmail.com
|
|
60
|
+
executables: []
|
|
61
|
+
extensions: []
|
|
62
|
+
extra_rdoc_files: []
|
|
63
|
+
files:
|
|
64
|
+
- ".gitignore"
|
|
65
|
+
- ".rspec"
|
|
66
|
+
- ".travis.yml"
|
|
67
|
+
- CODE_OF_CONDUCT.md
|
|
68
|
+
- Gemfile
|
|
69
|
+
- Gemfile.lock
|
|
70
|
+
- LICENSE.txt
|
|
71
|
+
- README.md
|
|
72
|
+
- Rakefile
|
|
73
|
+
- archive_hook.gemspec
|
|
74
|
+
- bin/console
|
|
75
|
+
- bin/setup
|
|
76
|
+
- lib/archive_hook.rb
|
|
77
|
+
- lib/archive_hook/version.rb
|
|
78
|
+
- lib/generators/archive_hook/USAGE
|
|
79
|
+
- lib/generators/archive_hook/archive_hook_generator.rb
|
|
80
|
+
- lib/generators/archive_hook/templates/migration.rb
|
|
81
|
+
homepage: https://github.com/alvir/archive_hook
|
|
82
|
+
licenses:
|
|
83
|
+
- MIT
|
|
84
|
+
metadata:
|
|
85
|
+
homepage_uri: https://github.com/alvir/archive_hook
|
|
86
|
+
source_code_uri: https://github.com/alvir/archive_hook
|
|
87
|
+
post_install_message:
|
|
88
|
+
rdoc_options: []
|
|
89
|
+
require_paths:
|
|
90
|
+
- lib
|
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
|
+
requirements:
|
|
98
|
+
- - ">="
|
|
99
|
+
- !ruby/object:Gem::Version
|
|
100
|
+
version: '0'
|
|
101
|
+
requirements: []
|
|
102
|
+
rubygems_version: 3.0.3.1
|
|
103
|
+
signing_key:
|
|
104
|
+
specification_version: 4
|
|
105
|
+
summary: Archive ActiveRecord models (and their dependents) to parallel _archive tables
|
|
106
|
+
test_files: []
|