activerecord_archive 1.0.3 → 1.0.4
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 +4 -4
- data/lib/activerecord_archive.rb +29 -3
- metadata +1 -1
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
@@ -30,6 +30,18 @@ module ArchiveMethods # :nodoc:
|
|
30
30
|
SELECT * FROM #{tabname} WHERE #{conditions}
|
31
31
|
")
|
32
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
|
+
|
33
45
|
# delete only records in parent table where ids match those in archive table
|
34
46
|
ActiveRecord::Base.connection.execute("
|
35
47
|
DELETE FROM #{tabname}
|
@@ -60,11 +72,25 @@ module ArchiveMethods # :nodoc:
|
|
60
72
|
WHERE #{conditions}
|
61
73
|
")
|
62
74
|
|
63
|
-
|
64
|
-
|
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("
|
65
90
|
REPLACE INTO #{tabname}
|
66
91
|
SELECT * FROM #{options[:prefix]}#{tabname} WHERE #{conditions}
|
67
|
-
|
92
|
+
")
|
93
|
+
end
|
68
94
|
|
69
95
|
# delete only records in archive table where ids match those in parent table
|
70
96
|
ActiveRecord::Base.connection.execute("
|