acts_as_archive 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,2 +1,9 @@
1
1
  require "#{File.dirname(__FILE__)}/require"
2
- Require.rakefile!
2
+ Require.rakefile!
3
+
4
+ desc "Generate gemspec"
5
+ task :gemspec do
6
+ File.open("#{Rake.original_dir}/acts_as_archive.gemspec", 'w') do |f|
7
+ f.write(Require.gemspec.to_ruby)
8
+ end
9
+ end
@@ -2,33 +2,39 @@ module ActsAsArchive
2
2
  module Base
3
3
  module Adapters
4
4
  module PostgreSQL
5
-
5
+
6
6
  private
7
7
 
8
8
  def archive_table_indexed_columns
9
+ # This query comes courtesy of cope360:
10
+ # http://stackoverflow.com/questions/2204058/show-which-columns-an-index-is-on-in-postgresql/2213199#2213199
9
11
  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
12
+ select
13
+ t.relname as table_name,
14
+ i.relname as index_name,
15
+ a.attname as column_name
16
+ from
17
+ pg_class t,
18
+ pg_class i,
19
+ pg_index ix,
20
+ pg_attribute a
21
+ where
22
+ t.oid = ix.indrelid
23
+ and i.oid = ix.indexrelid
24
+ and a.attrelid = t.oid
25
+ and a.attnum = ANY(ix.indkey)
26
+ and t.relkind = 'r'
27
+ and t.relname = 'archived_#{table_name}'
28
+ order by
29
+ t.relname,
30
+ i.relname
31
+ SQL
25
32
 
26
- # HACK: reverse engineer the column name
27
- indexes.map do |index|
28
- index.split("_on_").last
33
+ indexes = connection.select_all(index_query).collect do |r|
34
+ r["column_name"]
29
35
  end
30
36
  end
31
37
  end
32
38
  end
33
39
  end
34
- end
40
+ end
@@ -47,6 +47,7 @@ module ActsAsArchive
47
47
  unless new_record?
48
48
  self.class.copy_to_archive("#{self.class.primary_key} = #{id}")
49
49
  end
50
+ @destroyed = true
50
51
  freeze
51
52
  end
52
53
 
@@ -6,7 +6,7 @@ module ActsAsArchive
6
6
  unless base.included_modules.include?(InstanceMethods)
7
7
  base.send :extend, ClassMethods
8
8
  base.send :include, InstanceMethods
9
-
9
+
10
10
  if base.connection.class.to_s.include?('Mysql')
11
11
  base.send :extend, ActsAsArchive::Base::Adapters::MySQL
12
12
  elsif base.connection.class.to_s.include?('PostgreSQL')
@@ -63,6 +63,43 @@ module ActsAsArchive
63
63
  end
64
64
  end
65
65
  end
66
+
67
+ private
68
+
69
+ def archive_table_indexed_columns
70
+ case connection.class.to_s
71
+ when "ActiveRecord::ConnectionAdapters::MysqlAdapter"
72
+ index_query = "SHOW INDEX FROM archived_#{table_name}"
73
+ indexes = connection.select_all(index_query).collect do |r|
74
+ r["Column_name"]
75
+ end
76
+ when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
77
+ #postgresql is...slightly...more complicated
78
+ index_query = <<EOS
79
+ SELECT c2.relname as index_name
80
+ FROM pg_catalog.pg_class c,
81
+ pg_catalog.pg_class c2,
82
+ pg_catalog.pg_index i
83
+ WHERE c.oid = (SELECT c.oid
84
+ FROM pg_catalog.pg_class c
85
+ WHERE c.relname ~ '^(archived_#{table_name})$')
86
+ AND c.oid = i.indrelid
87
+ AND i.indexrelid = c2.oid
88
+ EOS
89
+ indexes = connection.select_all(index_query).collect do |r|
90
+ r["index_name"]
91
+ end
92
+
93
+ # HACK: reverse engineer the column name
94
+ # This sucks, but acts_as_archive only adds indexes on single columns anyway so it should work OK
95
+ # and getting the columns indexed is INCREDIBLY complicated in PostgreSQL.
96
+ indexes.map do |index|
97
+ index.split("_on_").last
98
+ end
99
+ else
100
+ raise "Unsupported Database"
101
+ end
102
+ end
66
103
  end
67
104
 
68
105
  module InstanceMethods
Binary file
data/require.rb CHANGED
@@ -17,7 +17,7 @@ Require 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.1'
20
+ version '0.2.2'
21
21
  end
22
22
 
23
23
  lib do
@@ -93,6 +93,11 @@ describe ActsAsArchive::Base::Destroy do
93
93
  article_match?(original, copy)
94
94
  end
95
95
  end
96
+
97
+ it "should mark the object as destroyed" do
98
+ @articles[3].send(d)
99
+ @articles[3].destroyed?.should == true
100
+ end
96
101
  end
97
102
  end
98
103
  end
@@ -1,69 +1,69 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
2
 
3
3
  describe ActsAsArchive::Base::Table do
4
-
4
+
5
5
  before(:all) do
6
6
  establish_test_db
7
7
  Article.create_archive_table
8
8
  end
9
-
9
+
10
10
  describe 'create_archive_table' do
11
-
11
+
12
12
  before(:all) do
13
13
  @article_columns = connection.columns("articles").collect(&:name)
14
14
  @archive_columns = connection.columns("archived_articles").collect(&:name)
15
15
  end
16
-
16
+
17
17
  it "should create an archive table" do
18
18
  connection.table_exists?("archived_articles").should == true
19
19
  end
20
-
20
+
21
21
  it "should create an archive table with the same structure as the original table" do
22
22
  @article_columns.each do |col|
23
23
  @archive_columns.include?(col).should == true
24
24
  end
25
25
  end
26
-
26
+
27
27
  it "should add a deleted_at column to the archive table" do
28
28
  (@archive_columns - @article_columns).should == [ 'deleted_at' ]
29
29
  end
30
30
  end
31
-
31
+
32
32
  describe 'create_archive_indexes' do
33
-
33
+
34
34
  before(:all) do
35
35
  Article.create_archive_indexes
36
36
  end
37
-
37
+
38
38
  it "should create archive indexes" do
39
- indexes.should == [ "id", "deleted_at" ]
39
+ indexes.to_set.should == [ "id", "deleted_at" ].to_set
40
40
  end
41
-
41
+
42
42
  it "should destroy archive indexes" do
43
43
  Article.class_eval { acts_as_archive }
44
44
  Article.create_archive_indexes
45
45
  indexes.should == []
46
46
  end
47
47
  end
48
-
48
+
49
49
  describe 'migrate_from_acts_as_paranoid' do
50
-
50
+
51
51
  before(:all) do
52
52
  connection.add_column(:articles, :deleted_at, :datetime)
53
53
  Article.reset_column_information
54
54
  end
55
-
55
+
56
56
  before(:each) do
57
57
  connection.execute("DELETE FROM #{Article::Archive.table_name}")
58
58
  end
59
-
59
+
60
60
  it "should move deleted records to the archive" do
61
61
  create_records(Article, :deleted_at => Time.now.utc)
62
62
  Article.migrate_from_acts_as_paranoid
63
63
  Article.count.should == 0
64
64
  Article::Archive.count.should == 5
65
65
  end
66
-
66
+
67
67
  it "should not move non-deleted records to the archive" do
68
68
  create_records
69
69
  Article.migrate_from_acts_as_paranoid
@@ -71,4 +71,4 @@ describe ActsAsArchive::Base::Table do
71
71
  Article::Archive.count.should == 0
72
72
  end
73
73
  end
74
- end
74
+ end
File without changes
@@ -0,0 +1,6 @@
1
+ test:
2
+ adapter: postgresql
3
+ database: acts_as_archive
4
+ username: postgres
5
+ password:
6
+ host: localhost
data/spec/spec_helper.rb CHANGED
@@ -4,6 +4,10 @@ Require.spec_helper!
4
4
  Spec::Runner.configure do |config|
5
5
  end
6
6
 
7
+ def db_type
8
+ ENV['DB_TYPE'] ? ENV['DB_TYPE'] : 'mysql'
9
+ end
10
+
7
11
  def article_match?(original, copy)
8
12
  copy.id.should == original.id
9
13
  copy.title.should == original.title
@@ -49,7 +53,7 @@ end
49
53
  def establish_test_db
50
54
  # Establish connection
51
55
  unless ActiveRecord::Base.connected?
52
- config = YAML::load(File.open("#{SPEC}/db/config/database.yml"))
56
+ config = YAML::load(File.open("#{SPEC}/db/config/database.#{db_type}.yml"))
53
57
  ActiveRecord::Base.configurations = config
54
58
  ActiveRecord::Base.establish_connection(config['test'])
55
59
  end
@@ -80,4 +84,4 @@ def migrate_up(directory='migrate')
80
84
  ActiveRecord::Migrator.migrate("#{SPEC}/db/#{directory}")
81
85
  @new_article_columns = columns("articles")
82
86
  @new_archive_columns = columns("archived_articles")
83
- end
87
+ end
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.1
4
+ version: 0.2.2
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-09 00:00:00 -08:00
12
+ date: 2010-02-21 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,6 +42,7 @@ files:
42
42
  - lib/acts_as_archive/migration.rb
43
43
  - lib/acts_as_archive.rb
44
44
  - MIT-LICENSE
45
+ - pkg/acts_as_archive-0.2.1.gem
45
46
  - rails/init.rb
46
47
  - Rakefile
47
48
  - README.markdown
@@ -51,7 +52,8 @@ files:
51
52
  - spec/acts_as_archive/base/table_spec.rb
52
53
  - spec/acts_as_archive/base_spec.rb
53
54
  - spec/acts_as_archive/migration_spec.rb
54
- - spec/db/config/database.yml
55
+ - spec/db/config/database.mysql.yml
56
+ - spec/db/config/database.postgresql.yml
55
57
  - spec/db/migrate/001_add_to_articles.rb
56
58
  - spec/db/migrate_2/001_add_to_articles.rb
57
59
  - spec/db/models/article.rb