Module: ActsAsArchive::Base::Table::ClassMethods

Defined in:
lib/acts_as_archive/base/table.rb

Instance Method Summary collapse

Instance Method Details

#archive_table_exists?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/acts_as_archive/base/table.rb', line 24

def archive_table_exists?
  connection.table_exists?("archived_#{table_name}")
end

#create_archive_indexesObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/acts_as_archive/base/table.rb', line 43

def create_archive_indexes
  if archive_table_exists?
    indexes = archive_table_indexed_columns

    (archive_indexes - indexes).each do |index|
      connection.add_index("archived_#{table_name}", index, :name => index_name_for(table_name, index))  if table_has_columns(index)
    end
    (indexes - archive_indexes).each do |index|
      connection.remove_index("archived_#{table_name}", index) if table_has_columns(index)
    end
  end
end

#create_archive_tableObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/acts_as_archive/base/table.rb', line 28

def create_archive_table
  if table_exists? && !archive_table_exists?
    connection.execute(%{
      CREATE TABLE archived_#{table_name}
        #{"ENGINE=InnoDB" if connection.class.to_s.include?('Mysql')}
        AS SELECT * from #{table_name}
        WHERE #{ connection.class.to_s.include?('SQLite') ? '1=0' : 'false' };
    })
    columns = connection.columns("archived_#{table_name}").collect(&:name)
    unless columns.include?('deleted_at')
      connection.add_column("archived_#{table_name}", :deleted_at, :datetime)
    end
  end
end

#migrate_from_acts_as_paranoidObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/acts_as_archive/base/table.rb', line 57

def migrate_from_acts_as_paranoid
  if column_names.include?('deleted_at')
    if table_exists? && archive_table_exists?
      condition = "deleted_at IS NOT NULL"
      if self.count_by_sql("SELECT COUNT(*) FROM #{table_name} WHERE #{condition}") > 0
        # Base::Destroy.copy_to_archive
        copy_to_archive(condition, true)
      end
    end
  end
end