Module: ArchiveMethods

Included in:
ActiveRecord::Base
Defined in:
lib/activerecord_archive.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#do_archive(conditions, options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/activerecord_archive.rb', line 2

def do_archive(conditions, options = {})
  options[:prefix] = 'ar_archive_' unless options[:prefix]
  options[:prefix] = 'ar_archive_' if options[:prefix].blank?

  if self.respond_to?(:table_name)
    tabname = self.table_name
  else
    raise 'MissingTableName'
  end

  raise 'PrefixAndTableNameTooLong - maximum is 64 characters' if "#{options[:prefix]}#{tabname}".size > 64

  # do a simple query first in case to cause an exception if there is an error in conditions
  ActiveRecord::Base.connection.execute("
    SELECT COUNT(*)
    FROM #{tabname}
    WHERE #{conditions}
  ")

  ActiveRecord::Base.connection.execute("
    CREATE TABLE IF NOT EXISTS #{options[:prefix]}#{tabname}
      LIKE #{tabname}
  ")

  ActiveRecord::Base.connection.execute("
    INSERT INTO #{options[:prefix]}#{tabname}
      SELECT * FROM #{tabname} WHERE #{conditions}
  ")

  ActiveRecord::Base.connection.execute("
    DELETE FROM #{tabname}
    WHERE EXISTS(
      SELECT #{options[:prefix]}#{tabname}.id
      FROM #{options[:prefix]}#{tabname}
      WHERE #{options[:prefix]}#{tabname}.id = #{tabname}.id)
  ")
end

#do_restore(conditions, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/activerecord_archive.rb', line 40

def do_restore(conditions, options = {})
  options[:prefix] = 'ar_archive_' unless options[:prefix]
  options[:prefix] = 'ar_archive_' if options[:prefix].blank?

  if self.respond_to?(:table_name)
    tabname = self.table_name
  else
    raise 'MissingTableName'
  end

  raise 'PrefixAndTableNameTooLong - maximum is 64 characters' if "#{options[:prefix]}#{tabname}".size > 64

  # do a simple query first in case to cause an exception if there is an error in conditions
  ActiveRecord::Base.connection.execute("
    SELECT COUNT(*)
    FROM #{options[:prefix]}#{tabname}
    WHERE #{conditions}
  ")

  ActiveRecord::Base.connection.execute("
    INSERT INTO #{tabname}
      SELECT * FROM #{options[:prefix]}#{tabname} WHERE #{conditions}
  ")

  ActiveRecord::Base.connection.execute("
    DELETE FROM #{options[:prefix]}#{tabname}
    WHERE EXISTS(
      SELECT #{tabname}.id
      FROM #{tabname}
      WHERE #{tabname}.id = #{options[:prefix]}#{tabname}.id)
  ")
end