activerecord_archive 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/activerecord_archive.rb +132 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ac9d3219f20ca205595ab51f2d8e8828529353e18c26771689a2a5e28ca63406
|
4
|
+
data.tar.gz: 50dd7d5cd6b7bdf09e92464eff4595164d82471a046051d97dee739f46b90bd7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 54e184d8cda7b6557ec32a16ab0fadc09cc39b2b3e3c0ab51e41dd6cb4b31af68c5f7c0a2ebda88d473af8291e92cb2ae1c8d9d4399ea6473bd4176c346f66fa
|
7
|
+
data.tar.gz: 5a06805897fe374fdb27dfb165a7e96a2993f63f3e5619a2a0243dce6dfe8f60e18aa05ffbf1c7a1f0fa2217932e56f3e1ff1e17ad74ef2120d4bb3c81a8e758
|
@@ -0,0 +1,132 @@
|
|
1
|
+
module ArchiveMethods # :nodoc:
|
2
|
+
def do_archive(conditions, options = {})
|
3
|
+
options[:prefix] = 'ar_archive_' unless options[:prefix]
|
4
|
+
options[:prefix] = 'ar_archive_' if options[:prefix].blank?
|
5
|
+
|
6
|
+
if self.respond_to?(:table_name)
|
7
|
+
tabname = self.table_name
|
8
|
+
else
|
9
|
+
raise 'MissingTableName'
|
10
|
+
end
|
11
|
+
|
12
|
+
raise 'PrefixAndTableNameTooLong - maximum is 64 characters' if "#{options[:prefix]}#{tabname}".size > 64
|
13
|
+
|
14
|
+
# do a simple query first in case to cause an exception if there is an error in conditions
|
15
|
+
ActiveRecord::Base.connection.execute("
|
16
|
+
SELECT COUNT(*)
|
17
|
+
FROM #{tabname}
|
18
|
+
WHERE #{conditions}
|
19
|
+
")
|
20
|
+
|
21
|
+
ActiveRecord::Base.connection.execute("
|
22
|
+
CREATE TABLE IF NOT EXISTS #{options[:prefix]}#{tabname}
|
23
|
+
LIKE #{tabname}
|
24
|
+
")
|
25
|
+
|
26
|
+
ActiveRecord::Base.connection.execute("
|
27
|
+
INSERT INTO #{options[:prefix]}#{tabname}
|
28
|
+
SELECT * FROM #{tabname} WHERE #{conditions}
|
29
|
+
")
|
30
|
+
|
31
|
+
ActiveRecord::Base.connection.execute("
|
32
|
+
DELETE FROM #{tabname}
|
33
|
+
WHERE EXISTS(
|
34
|
+
SELECT #{options[:prefix]}#{tabname}.id
|
35
|
+
FROM #{options[:prefix]}#{tabname}
|
36
|
+
WHERE #{options[:prefix]}#{tabname}.id = #{tabname}.id)
|
37
|
+
")
|
38
|
+
end
|
39
|
+
|
40
|
+
def do_restore(conditions, options = {})
|
41
|
+
options[:prefix] = 'ar_archive_' unless options[:prefix]
|
42
|
+
options[:prefix] = 'ar_archive_' if options[:prefix].blank?
|
43
|
+
|
44
|
+
if self.respond_to?(:table_name)
|
45
|
+
tabname = self.table_name
|
46
|
+
else
|
47
|
+
raise 'MissingTableName'
|
48
|
+
end
|
49
|
+
|
50
|
+
raise 'PrefixAndTableNameTooLong - maximum is 64 characters' if "#{options[:prefix]}#{tabname}".size > 64
|
51
|
+
|
52
|
+
# do a simple query first in case to cause an exception if there is an error in conditions
|
53
|
+
ActiveRecord::Base.connection.execute("
|
54
|
+
SELECT COUNT(*)
|
55
|
+
FROM #{options[:prefix]}#{tabname}
|
56
|
+
WHERE #{conditions}
|
57
|
+
")
|
58
|
+
|
59
|
+
ActiveRecord::Base.connection.execute("
|
60
|
+
INSERT INTO #{tabname}
|
61
|
+
SELECT * FROM #{options[:prefix]}#{tabname} WHERE #{conditions}
|
62
|
+
")
|
63
|
+
|
64
|
+
ActiveRecord::Base.connection.execute("
|
65
|
+
DELETE FROM #{options[:prefix]}#{tabname}
|
66
|
+
WHERE EXISTS(
|
67
|
+
SELECT #{tabname}.id
|
68
|
+
FROM #{tabname}
|
69
|
+
WHERE #{tabname}.id = #{options[:prefix]}#{tabname}.id)
|
70
|
+
")
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
if Rails::VERSION::MAJOR >= 3
|
75
|
+
require 'active_support/concern'
|
76
|
+
|
77
|
+
module ActiveRecordArchive
|
78
|
+
extend ActiveSupport::Concern
|
79
|
+
|
80
|
+
class_methods do
|
81
|
+
include ArchiveMethods
|
82
|
+
|
83
|
+
# Archive database records
|
84
|
+
#
|
85
|
+
# Caveats: where foreign keys are involved, child records must be archived first
|
86
|
+
#
|
87
|
+
# Examples:
|
88
|
+
# >> Model.archive('created_at < DATE_SUB(NOW(), INTERVAL 6 MONTH)')
|
89
|
+
# >> Model.archive('created_at < DATE_SUB(NOW(), INTERVAL 6 MONTH)', prefix: 'arch_')
|
90
|
+
#
|
91
|
+
# Arguments:
|
92
|
+
# conditions: (String)
|
93
|
+
# options:
|
94
|
+
# prefix: (String) - default "ar_archive_"
|
95
|
+
def archive(conditions, options = {})
|
96
|
+
do_archive(conditions, options)
|
97
|
+
end
|
98
|
+
|
99
|
+
# Restore database records
|
100
|
+
#
|
101
|
+
# Caveats: if you used a custom prefix to archive, make sure you use the same prefix to restore
|
102
|
+
#
|
103
|
+
# Examples:
|
104
|
+
# >> Model.archive('created_at < DATE_SUB(NOW(), INTERVAL 6 MONTH)')
|
105
|
+
# >> Model.archive('created_at < DATE_SUB(NOW(), INTERVAL 6 MONTH)', prefix: 'arch_')
|
106
|
+
#
|
107
|
+
# Arguments:
|
108
|
+
# conditions: (String)
|
109
|
+
# options:
|
110
|
+
# prefix: (String) - default "ar_archive_"
|
111
|
+
def restore(conditions, options = {})
|
112
|
+
do_restore(conditions, options)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
# include the extension
|
118
|
+
ActiveRecord::Base.send(:include, ActiveRecordArchive)
|
119
|
+
else
|
120
|
+
# Rails 2
|
121
|
+
class ActiveRecord::Base # :nodoc:
|
122
|
+
extend ArchiveMethods
|
123
|
+
|
124
|
+
def self.archive(conditions, options = {})
|
125
|
+
do_archive(conditions, options)
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.restore(conditions, options = {})
|
129
|
+
do_restore(conditions, options)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord_archive
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vic Spanner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple archiving extension for ActiveRecord. Archive old records to
|
14
|
+
improve database performance. Restore old records from archive tables.
|
15
|
+
email: vic@spannersoftware.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/activerecord_archive.rb
|
21
|
+
homepage: https://github.com/vspar/activerecord_archive
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubygems_version: 3.1.2
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: ActiveRecord Archiving
|
44
|
+
test_files: []
|