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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/activerecord_archive.rb +36 -4
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ac9d3219f20ca205595ab51f2d8e8828529353e18c26771689a2a5e28ca63406
4
- data.tar.gz: 50dd7d5cd6b7bdf09e92464eff4595164d82471a046051d97dee739f46b90bd7
3
+ metadata.gz: 2d3c010163dccd7143c45dbaf0680598ea4cc5dc688c389bd174d75ebaa9ae36
4
+ data.tar.gz: 2a495578d9ec68a760283ee2f40660037b433d112024818c5e6dc03292a961af
5
5
  SHA512:
6
- metadata.gz: 54e184d8cda7b6557ec32a16ab0fadc09cc39b2b3e3c0ab51e41dd6cb4b31af68c5f7c0a2ebda88d473af8291e92cb2ae1c8d9d4399ea6473bd4176c346f66fa
7
- data.tar.gz: 5a06805897fe374fdb27dfb165a7e96a2993f63f3e5619a2a0243dce6dfe8f60e18aa05ffbf1c7a1f0fa2217932e56f3e1ff1e17ad74ef2120d4bb3c81a8e758
6
+ metadata.gz: fb595069da61e90ff49e74a29cfa3d04eb18460d7be9d1826253e4c1cfef29120c942dd28e6a02a9212e6f6f110fef985fab7ec5a3bff30d0d1a5412582a3e12
7
+ data.tar.gz: 77f0556795226e4204c40e844a1d788962d4f02468e07627bc0d85c118e984808ec121492309eaf03c538ac802dd96ddeee869b0e7c14ecb82826fd3270918e4
@@ -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
- INSERT INTO #{options[:prefix]}#{tabname}
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
- ActiveRecord::Base.connection.execute("
60
- INSERT INTO #{tabname}
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.1
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: 2020-07-15 00:00:00.000000000 Z
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: '0'
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.1.2
40
+ rubygems_version: 3.0.6
41
41
  signing_key:
42
42
  specification_version: 4
43
43
  summary: ActiveRecord Archiving