activerecord_archive 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/activerecord_archive.rb +29 -3
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec6caa129b25d19c6f0f399c91509018130b38f4bbed884de7efebf7df1d5137
4
- data.tar.gz: f02746ec310b8ad11af8a08e2c04a4b5ee023b442e194c7d236be8ae4134bec4
3
+ metadata.gz: 2d3c010163dccd7143c45dbaf0680598ea4cc5dc688c389bd174d75ebaa9ae36
4
+ data.tar.gz: 2a495578d9ec68a760283ee2f40660037b433d112024818c5e6dc03292a961af
5
5
  SHA512:
6
- metadata.gz: dcb76d080936e53506cadf4e2d2353d10072ab8472c6d2b8d861fc5829e1a690a1e3e2fdee0b0d67c34cdd9eeab8c01d7ef87b03f98033fdcf7054ba52301a87
7
- data.tar.gz: 914075c3c87ae28ede48f1500b226cf7d213a355fd836c59a0c9e6962fe50b6ff46d123daa1b4e2934157f27a536012a0939d5deef5507fa0e1243acbf1350d1
6
+ metadata.gz: fb595069da61e90ff49e74a29cfa3d04eb18460d7be9d1826253e4c1cfef29120c942dd28e6a02a9212e6f6f110fef985fab7ec5a3bff30d0d1a5412582a3e12
7
+ data.tar.gz: 77f0556795226e4204c40e844a1d788962d4f02468e07627bc0d85c118e984808ec121492309eaf03c538ac802dd96ddeee869b0e7c14ecb82826fd3270918e4
@@ -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
- # use replace into in case of duplicate inserts
64
- ActiveRecord::Base.connection.execute("
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("
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_archive
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vic Spanner