acts_as_archive 0.2.0 → 0.2.1

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.
data/README.markdown CHANGED
@@ -29,22 +29,23 @@ class Article < ActiveRecord::Base
29
29
  end
30
30
  </pre>
31
31
 
32
- <a name="run_acts_as_archive"></a>
32
+ <a name="create_archive_tables"></a>
33
33
 
34
- Run acts\_as\_archive
34
+ Create archive tables
35
35
  ---------------------
36
36
 
37
+ Add this line to a migration:
38
+
37
39
  <pre>
38
- cd your_rails_app
39
- acts_as_archive Article
40
+ ActsAsArchive.update Article, Comment
40
41
  </pre>
41
42
 
42
- Run this command every time you add <code>acts\_as\_archive</code> to a new model.
43
-
44
- This command creates your archive tables (<code>archived_articles</code> as per the example).
43
+ Replace <code>Article, Comment</code> with your own models that use <code>acts_as_archive</code>.
45
44
 
46
45
  Archive tables mirror your table's structure, but with an additional <code>deleted_at</code> column.
47
46
 
47
+ There is an [alternate way to create archive tables](http://wiki.github.com/winton/acts_as_archive/alternatives-to-migrations) if you don't like migrations.
48
+
48
49
  That's it!
49
50
  ----------
50
51
 
@@ -57,6 +58,8 @@ What if my schema changes?
57
58
 
58
59
  New migrations are automatically applied to the archive table.
59
60
 
61
+ No action is necessary on your part.
62
+
60
63
  Query the archive
61
64
  -----------------
62
65
 
@@ -78,9 +81,9 @@ Article.restore_all([ 'id = ?', 1 ])
78
81
  Auto-migrate from acts\_as\_paranoid
79
82
  ------------------------------------
80
83
 
81
- If you previously used <code>acts\_as\_paranoid</code>, the <code>acts\_as\_archive</code>
82
- command will automatically move your deleted records to the archive table
83
- (see <a href="#run_acts_as_archive">_Run acts\_as\_archive_</a>).
84
+ If you previously used <code>acts\_as\_paranoid</code>, the <code>ActsAsArchive.update</code>
85
+ call will automatically move your deleted records to the archive table
86
+ (see <a href="#create_archive_tables">_Create archive tables_</a>).
84
87
 
85
88
  Original <code>deleted_at</code> values are preserved.
86
89
 
@@ -97,5 +100,5 @@ class Article < ActiveRecord::Base
97
100
  end
98
101
  </pre>
99
102
 
100
- Run the <code>acts\_as\_archive</code> command upon adding new indexes
101
- (see <a href="#run_acts_as_archive">_Run acts\_as\_archive_</a>).
103
+ Call <code>ActsAsArchive.update</code> upon adding new indexes
104
+ (see <a href="#create_archive_tables">_Create archive tables_</a>).
@@ -0,0 +1,17 @@
1
+ module ActsAsArchive
2
+ module Base
3
+ module Adapters
4
+ module MySQL
5
+
6
+ private
7
+
8
+ def archive_table_indexed_columns
9
+ index_query = "SHOW INDEX FROM archived_#{table_name}"
10
+ indexes = connection.select_all(index_query).collect do |r|
11
+ r["Column_name"]
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,34 @@
1
+ module ActsAsArchive
2
+ module Base
3
+ module Adapters
4
+ module PostgreSQL
5
+
6
+ private
7
+
8
+ def archive_table_indexed_columns
9
+ index_query = <<-SQL
10
+ SELECT c2.relname as index_name
11
+ FROM pg_catalog.pg_class c,
12
+ pg_catalog.pg_class c2,
13
+ pg_catalog.pg_index i
14
+ WHERE c.oid = (SELECT c.oid
15
+ FROM pg_catalog.pg_class c
16
+ WHERE c.relname ~ '^(archived_#{table_name})$')
17
+ AND c.oid = i.indrelid
18
+ AND i.indexrelid = c2.oid
19
+ SQL
20
+
21
+ index_query.gsub!(/\s+/, ' ').strip!
22
+ indexes = connection.select_all(index_query).collect do |r|
23
+ r["index_name"]
24
+ end
25
+
26
+ # HACK: reverse engineer the column name
27
+ indexes.map do |index|
28
+ index.split("_on_").last
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -6,20 +6,28 @@ module ActsAsArchive
6
6
  unless base.included_modules.include?(InstanceMethods)
7
7
  base.send :extend, ClassMethods
8
8
  base.send :include, InstanceMethods
9
+
10
+ if base.connection.class.to_s.include?('Mysql')
11
+ base.send :extend, ActsAsArchive::Base::Adapters::MySQL
12
+ elsif base.connection.class.to_s.include?('PostgreSQL')
13
+ base.send :extend, ActsAsArchive::Base::Adapters::PostgreSQL
14
+ else
15
+ raise 'acts_as_archive does not support this database adapter'
16
+ end
9
17
  end
10
18
  end
11
19
 
12
20
  module ClassMethods
13
-
21
+
14
22
  def archive_table_exists?
15
23
  connection.table_exists?("archived_#{table_name}")
16
24
  end
17
-
25
+
18
26
  def create_archive_table
19
27
  if table_exists? && !archive_table_exists?
20
28
  connection.execute(%{
21
29
  CREATE TABLE archived_#{table_name}
22
- ENGINE=InnoDB
30
+ #{"ENGINE=InnoDB" if connection.class.to_s.include?('Mysql')}
23
31
  AS SELECT * from #{table_name}
24
32
  WHERE false;
25
33
  })
@@ -29,13 +37,11 @@ module ActsAsArchive
29
37
  end
30
38
  end
31
39
  end
32
-
40
+
33
41
  def create_archive_indexes
34
42
  if archive_table_exists?
35
- indexes = "SHOW INDEX FROM archived_#{table_name}"
36
- indexes = connection.select_all(indexes).collect do |r|
37
- r["Column_name"]
38
- end
43
+ indexes = archive_table_indexed_columns
44
+
39
45
  (archive_indexes - indexes).each do |index|
40
46
  connection.add_index("archived_#{table_name}", index)
41
47
  end
@@ -44,7 +50,8 @@ module ActsAsArchive
44
50
  end
45
51
  end
46
52
  end
47
-
53
+
54
+
48
55
  def migrate_from_acts_as_paranoid
49
56
  if column_names.include?('deleted_at')
50
57
  if table_exists? && archive_table_exists?
@@ -6,10 +6,13 @@ module ActsAsArchive
6
6
  def self.update(*models)
7
7
  models.each do |klass|
8
8
  if klass.respond_to?(:acts_as_archive?) && klass.acts_as_archive?
9
- klass.create_archive_table
10
- klass.migrate_from_acts_as_paranoid
11
- klass.create_archive_indexes
12
- puts 'Success!'
9
+ time = Benchmark.measure do
10
+ klass.create_archive_table
11
+ klass.migrate_from_acts_as_paranoid
12
+ klass.create_archive_indexes
13
+ end
14
+ $stdout.puts "-- ActsAsArchive.update(#{models.join(', ')})"
15
+ $stdout.puts " -> #{"%.4fs" % time.real}"
13
16
  end
14
17
  end
15
18
  end
data/require.rb CHANGED
@@ -2,9 +2,9 @@ require 'rubygems'
2
2
  gem 'require'
3
3
  require 'require'
4
4
 
5
- Require File.dirname(__FILE__) do
5
+ Require do
6
6
  gem(:activerecord) { require 'active_record' }
7
- gem :require, '=0.1.8'
7
+ gem :require, '=0.2.1'
8
8
  gem(:rake, '=0.8.7') { require 'rake' }
9
9
  gem :rspec, '=1.3.0'
10
10
 
@@ -17,11 +17,13 @@ Require File.dirname(__FILE__) do
17
17
  name 'acts_as_archive'
18
18
  homepage "http://github.com/winton/#{name}"
19
19
  summary "Don't delete your records, move them to a different table"
20
- version '0.2.0'
20
+ version '0.2.1'
21
21
  end
22
22
 
23
23
  lib do
24
24
  require "lib/acts_as_archive/base"
25
+ require "lib/acts_as_archive/base/adapters/mysql"
26
+ require "lib/acts_as_archive/base/adapters/postgresql"
25
27
  require "lib/acts_as_archive/base/destroy"
26
28
  require "lib/acts_as_archive/base/restore"
27
29
  require "lib/acts_as_archive/base/table"
data/spec/spec_helper.rb CHANGED
@@ -39,7 +39,7 @@ def create_records(klass=Article, values={})
39
39
  end
40
40
  end
41
41
  connection.execute(%{
42
- INSERT INTO #{table} (#{cols.collect { |c| "`#{c}`" }.join(', ')})
42
+ INSERT INTO #{table} (#{cols.collect { |c| "#{c}" }.join(', ')})
43
43
  VALUES (#{vals.join(', ')})
44
44
  })
45
45
  klass.find(x)
@@ -71,10 +71,7 @@ def establish_test_db
71
71
  end
72
72
 
73
73
  def indexes
74
- query = "SHOW INDEX FROM archived_#{Article.table_name}"
75
- connection.select_all(query).collect do |r|
76
- r["Column_name"]
77
- end
74
+ Article.send(:archive_table_indexed_columns)
78
75
  end
79
76
 
80
77
  def migrate_up(directory='migrate')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_archive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Winton Welsh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-03 00:00:00 -08:00
12
+ date: 2010-02-09 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - "="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.1.8
23
+ version: 0.2.1
24
24
  version:
25
25
  description:
26
26
  email: mail@wintoni.us
@@ -33,6 +33,8 @@ extra_rdoc_files:
33
33
  files:
34
34
  - bin/acts_as_archive
35
35
  - init.rb
36
+ - lib/acts_as_archive/base/adapters/mysql.rb
37
+ - lib/acts_as_archive/base/adapters/postgresql.rb
36
38
  - lib/acts_as_archive/base/destroy.rb
37
39
  - lib/acts_as_archive/base/restore.rb
38
40
  - lib/acts_as_archive/base/table.rb