activerecord_archive 1.0.1 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/activerecord_archive.rb +36 -4
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d3c010163dccd7143c45dbaf0680598ea4cc5dc688c389bd174d75ebaa9ae36
|
4
|
+
data.tar.gz: 2a495578d9ec68a760283ee2f40660037b433d112024818c5e6dc03292a961af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb595069da61e90ff49e74a29cfa3d04eb18460d7be9d1826253e4c1cfef29120c942dd28e6a02a9212e6f6f110fef985fab7ec5a3bff30d0d1a5412582a3e12
|
7
|
+
data.tar.gz: 77f0556795226e4204c40e844a1d788962d4f02468e07627bc0d85c118e984808ec121492309eaf03c538ac802dd96ddeee869b0e7c14ecb82826fd3270918e4
|
data/lib/activerecord_archive.rb
CHANGED
@@ -10,6 +10,7 @@ module ArchiveMethods # :nodoc:
|
|
10
10
|
end
|
11
11
|
|
12
12
|
raise 'PrefixAndTableNameTooLong - maximum is 64 characters' if "#{options[:prefix]}#{tabname}".size > 64
|
13
|
+
raise 'PrimaryKey id expected but not found' unless self.primary_key == 'id'
|
13
14
|
|
14
15
|
# do a simple query first in case to cause an exception if there is an error in conditions
|
15
16
|
ActiveRecord::Base.connection.execute("
|
@@ -23,11 +24,25 @@ module ArchiveMethods # :nodoc:
|
|
23
24
|
LIKE #{tabname}
|
24
25
|
")
|
25
26
|
|
27
|
+
# use replace into in case of duplicate inserts
|
26
28
|
ActiveRecord::Base.connection.execute("
|
27
|
-
|
29
|
+
REPLACE INTO #{options[:prefix]}#{tabname}
|
28
30
|
SELECT * FROM #{tabname} WHERE #{conditions}
|
29
31
|
")
|
30
32
|
|
33
|
+
if options[:recursive_foreign_key] && !options[:recursive_foreign_key].blank?
|
34
|
+
# for self-referencing foreign keys, delete child records first
|
35
|
+
# delete only records in parent table where ids match those in archive table
|
36
|
+
ActiveRecord::Base.connection.execute("
|
37
|
+
DELETE FROM #{tabname}
|
38
|
+
WHERE #{tabname}.#{options[:recursive_foreign_key]} IS NOT NULL AND EXISTS(
|
39
|
+
SELECT #{options[:prefix]}#{tabname}.id
|
40
|
+
FROM #{options[:prefix]}#{tabname}
|
41
|
+
WHERE #{options[:prefix]}#{tabname}.id = #{tabname}.id)
|
42
|
+
")
|
43
|
+
end
|
44
|
+
|
45
|
+
# delete only records in parent table where ids match those in archive table
|
31
46
|
ActiveRecord::Base.connection.execute("
|
32
47
|
DELETE FROM #{tabname}
|
33
48
|
WHERE EXISTS(
|
@@ -48,6 +63,7 @@ module ArchiveMethods # :nodoc:
|
|
48
63
|
end
|
49
64
|
|
50
65
|
raise 'PrefixAndTableNameTooLong - maximum is 64 characters' if "#{options[:prefix]}#{tabname}".size > 64
|
66
|
+
raise 'PrimaryKey id expected but not found' unless self.primary_key == 'id'
|
51
67
|
|
52
68
|
# do a simple query first in case to cause an exception if there is an error in conditions
|
53
69
|
ActiveRecord::Base.connection.execute("
|
@@ -56,11 +72,27 @@ module ArchiveMethods # :nodoc:
|
|
56
72
|
WHERE #{conditions}
|
57
73
|
")
|
58
74
|
|
59
|
-
|
60
|
-
|
75
|
+
if options[:recursive_foreign_key] && !options[:recursive_foreign_key].blank?
|
76
|
+
# use replace into in case of duplicate inserts
|
77
|
+
# for self-referencing foreign keys, insert parent records first
|
78
|
+
ActiveRecord::Base.connection.execute("
|
79
|
+
REPLACE INTO #{tabname}
|
80
|
+
SELECT * FROM #{options[:prefix]}#{tabname} WHERE (#{conditions}) AND #{options[:prefix]}#{tabname}.#{options[:recursive_foreign_key]} IS NULL
|
81
|
+
")
|
82
|
+
# now insert child records
|
83
|
+
ActiveRecord::Base.connection.execute("
|
84
|
+
REPLACE INTO #{tabname}
|
85
|
+
SELECT * FROM #{options[:prefix]}#{tabname} WHERE (#{conditions}) AND #{options[:prefix]}#{tabname}.#{options[:recursive_foreign_key]} IS NOT NULL
|
86
|
+
")
|
87
|
+
else
|
88
|
+
# use replace into in case of duplicate inserts
|
89
|
+
ActiveRecord::Base.connection.execute("
|
90
|
+
REPLACE INTO #{tabname}
|
61
91
|
SELECT * FROM #{options[:prefix]}#{tabname} WHERE #{conditions}
|
62
|
-
|
92
|
+
")
|
93
|
+
end
|
63
94
|
|
95
|
+
# delete only records in archive table where ids match those in parent table
|
64
96
|
ActiveRecord::Base.connection.execute("
|
65
97
|
DELETE FROM #{options[:prefix]}#{tabname}
|
66
98
|
WHERE EXISTS(
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord_archive
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vic Spanner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A simple archiving extension for ActiveRecord. Archive old records to
|
14
14
|
improve database performance. Restore old records from archive tables.
|
@@ -30,14 +30,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 2.0.0
|
34
34
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
35
|
requirements:
|
36
36
|
- - ">="
|
37
37
|
- !ruby/object:Gem::Version
|
38
38
|
version: '0'
|
39
39
|
requirements: []
|
40
|
-
rubygems_version: 3.
|
40
|
+
rubygems_version: 3.0.6
|
41
41
|
signing_key:
|
42
42
|
specification_version: 4
|
43
43
|
summary: ActiveRecord Archiving
|