Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Wrote specs for create_archive_indexes and migrate_from_acts_as_paran…

…oid, simplified specs in general
  • Loading branch information...
commit aab6c6b15bef274678f0d242b9a9b93c233d3ae6 1 parent 5551a14
@winton authored
View
1  .gitignore
@@ -2,5 +2,4 @@
*.gem
tmp
-spec/db/config/database.yml
spec/db/log/*.log
View
27 README.markdown
@@ -3,7 +3,7 @@ ActsAsArchive
Don't delete your records, move them to a different table.
-Like <code>acts\_as\_paranoid</code>, but doesn't change all your SQL queries.
+Like <code>acts\_as\_paranoid</code>, but doesn't mess with your SQL queries.
Install
-------
@@ -35,6 +35,8 @@ class Article < ActiveRecord::Base
end
</pre>
+<a name="run_acts_as_archive"></a>
+
Run acts\_as\_archive
---------------------
@@ -45,9 +47,9 @@ cd your_rails_app
acts_as_archive
</pre>
-This command creates your archive tables (<code>archived_articles</code>, for example).
+This command creates your archive tables (<code>archived_articles</code> as per the example).
-Archive tables mirror your table's structure, with an additional <code>deleted_at</code> column.
+Archive tables mirror your table's structure, but with an additional <code>deleted_at</code> column.
Run this command every time you add <code>acts\_as\_archive</code> to a new model.
@@ -55,6 +57,7 @@ That's it!
----------
Use <code>destroy</code>, <code>delete</code>, and <code>delete_all</code> like you normally would.
+
Records move into the archive table instead of being destroyed.
What if my schema changes?
@@ -83,15 +86,18 @@ Article.restore_all([ 'id = ?', 1 ])
Auto-migrate from acts\_as\_paranoid
------------------------------------
-If you previously used the <code>acts\_as\_paranoid</code> plugin, running the <code>acts\_as\_archive</code>
-command will automatically move your deleted records to the archive table (see _Run acts\_as\_archive_).
+If you previously used <code>acts\_as\_paranoid</code>, running the <code>acts\_as\_archive</code>
+command will automatically move your deleted records to the archive table
+(see <a href="#run_acts_as_archive">_Run acts\_as\_archive_</a>).
+
+Original <code>deleted_at</code> values are preserved.
-Add indexes to your archive
----------------------------
+Add indexes to the archive table
+--------------------------------
-By default, there are no indexes on your archive table to keep insertions fast.
+To keep insertions fast, there are no indexes on your archive table by default.
-If you will be querying your archive a lot, you will want to add indexes:
+If you are querying your archive a lot, you will want to add indexes:
<pre>
class Article < ActiveRecord::Base
@@ -99,4 +105,5 @@ class Article < ActiveRecord::Base
end
</pre>
-Run the <code>acts\_as\_archive</code> command after adding new indexes.
+Run the <code>acts\_as\_archive</code> command any time you add new indexes
+(see <a href="#run_acts_as_archive">_Run acts\_as\_archive_</a>).
View
3  lib/acts_as_archive/base/table.rb
@@ -39,6 +39,9 @@ def create_archive_indexes
(archive_indexes - indexes).each do |index|
connection.add_index("archived_#{table_name}", index)
end
+ (indexes - archive_indexes).each do |index|
+ connection.remove_index("archived_#{table_name}", index)
+ end
end
end
View
17 spec/acts_as_archive/base/destroy_spec.rb
@@ -5,17 +5,12 @@
before(:all) do
establish_test_db
Article.create_archive_table
- @connection = ActiveRecord::Base.connection
end
describe 'delete_all!' do
before(:each) do
- @articles = []
- @connection.execute("TRUNCATE TABLE #{Article.table_name}")
- 5.times do |x|
- @articles << Article.create(:title => "Title #{x}", :body => "Body #{x}")
- end
+ create_records
end
it "should really delete all records" do
@@ -29,10 +24,7 @@
describe 'delete_all' do
before(:each) do
- @articles = []
- 5.times do |x|
- @articles << Article.create(:title => "Title #{x}", :body => "Body #{x}")
- end
+ @articles = create_records
end
describe 'with conditions' do
@@ -83,10 +75,7 @@
describe d do
before(:each) do
- @articles = []
- 5.times do |x|
- @articles << Article.create(:title => "Title #{x}", :body => "Body #{x}")
- end
+ @articles = create_records
Article.find(@articles[0..1].collect(&:id)).each do |a|
a.send(d)
end
View
16 spec/acts_as_archive/base/restore_spec.rb
@@ -5,25 +5,17 @@
before(:all) do
establish_test_db
Article.create_archive_table
- @connection = ActiveRecord::Base.connection
end
describe 'restore_all' do
- before(:each) do
- @articles = []
- @connection.execute("TRUNCATE TABLE #{Article.table_name}")
- 5.times do |x|
- @connection.execute(%{
- INSERT INTO archived_#{Article.table_name} (`id`, `title`, `body`) VALUES (#{x+1}, 'Title #{x}', 'Body #{x}')
- })
- @articles << Article::Archive.find(x+1)
- end
+ before(:all) do
+ @articles = create_records(Article::Archive)
end
describe 'with conditions' do
- before(:each) do
+ before(:all) do
# Mini restore parameter test
Article.restore_all [ 'id = ?', @articles[0].id ]
Article.restore_all "id = #{@articles[1].id}"
@@ -45,7 +37,7 @@
describe 'without conditions' do
- before(:each) do
+ before(:all) do
Article.restore_all
end
View
52 spec/acts_as_archive/base/table_spec.rb
@@ -4,19 +4,18 @@
before(:all) do
establish_test_db
- @connection = ActiveRecord::Base.connection
+ Article.create_archive_table
end
describe 'create_archive_table' do
before(:all) do
- Article.create_archive_table
- @article_columns = @connection.columns("articles").collect(&:name)
- @archive_columns = @connection.columns("archived_articles").collect(&:name)
+ @article_columns = connection.columns("articles").collect(&:name)
+ @archive_columns = connection.columns("archived_articles").collect(&:name)
end
it "should create an archive table" do
- @connection.table_exists?("archived_articles").should == true
+ connection.table_exists?("archived_articles").should == true
end
it "should create an archive table with the same structure as the original table" do
@@ -29,4 +28,47 @@
(@archive_columns - @article_columns).should == [ 'deleted_at' ]
end
end
+
+ describe 'create_archive_indexes' do
+
+ before(:all) do
+ Article.create_archive_indexes
+ end
+
+ it "should create archive indexes" do
+ indexes.should == [ "id", "deleted_at" ]
+ end
+
+ it "should destroy archive indexes" do
+ Article.class_eval { acts_as_archive }
+ Article.create_archive_indexes
+ indexes.should == []
+ end
+ end
+
+ describe 'migrate_from_acts_as_paranoid' do
+
+ before(:all) do
+ connection.add_column(:articles, :deleted_at, :datetime)
+ Article.reset_column_information
+ end
+
+ before(:each) do
+ connection.execute("DELETE FROM #{Article::Archive.table_name}")
+ end
+
+ it "should move deleted records to the archive" do
+ create_records(Article, :deleted_at => Time.now)
+ Article.migrate_from_acts_as_paranoid
+ Article.count.should == 0
+ Article::Archive.count.should == 5
+ end
+
+ it "should not move non-deleted records to the archive" do
+ create_records
+ Article.migrate_from_acts_as_paranoid
+ Article.count.should == 5
+ Article::Archive.count.should == 0
+ end
+ end
end
View
13 spec/acts_as_archive/migration_spec.rb
@@ -5,17 +5,16 @@
before(:each) do
establish_test_db
Article.create_archive_table
- @connection = ActiveRecord::Base.connection
end
describe 'method_missing_with_archive' do
before(:each) do
- @old_article_columns = @connection.columns("articles").collect(&:name)
- @old_archive_columns = @connection.columns("archived_articles").collect(&:name)
+ @old_article_columns = columns("articles")
+ @old_archive_columns = columns("archived_articles")
ActiveRecord::Migrator.migrate("#{SPEC}/db/migrate")
- @new_article_columns = @connection.columns("articles").collect(&:name)
- @new_archive_columns = @connection.columns("archived_articles").collect(&:name)
+ @new_article_columns = columns("articles")
+ @new_archive_columns = columns("archived_articles")
end
it 'should migrate both tables up' do
@@ -27,8 +26,8 @@
@old_article_columns = @new_article_columns
@old_archive_columns = @new_archive_columns
ActiveRecord::Migrator.migrate("#{SPEC}/db/migrate", 0)
- @new_article_columns = @connection.columns("articles").collect(&:name)
- @new_archive_columns = @connection.columns("archived_articles").collect(&:name)
+ @new_article_columns = columns("articles")
+ @new_archive_columns = columns("archived_articles")
(@old_article_columns - @new_article_columns).should == [ 'permalink' ]
(@old_archive_columns - @new_archive_columns).should == [ 'permalink' ]
end
View
0  spec/db/config/database.yml.example → spec/db/config/database.yml
File renamed without changes
View
2  spec/db/models/article.rb
@@ -1,3 +1,3 @@
class Article < ActiveRecord::Base
- acts_as_archive
+ acts_as_archive :indexes => [ :id, :deleted_at ]
end
View
53 spec/spec_helper.rb
@@ -16,13 +16,44 @@ def article_match?(original, copy)
copy.id.should == original.id
copy.title.should == original.title
copy.body.should == original.body
- copy.created_at.to_s.should == original.created_at.to_s
- copy.updated_at.to_s.should == original.updated_at.to_s
if copy.respond_to?(:deleted_at)
copy.deleted_at.strftime('%j%H%M').should == Time.now.strftime('%j%H%M')
end
end
+def columns(table)
+ connection.columns(table).collect(&:name)
+end
+
+def connection
+ ActiveRecord::Base.connection
+end
+
+def create_records(klass=Article, values={})
+ articles = []
+ table = klass.table_name
+ cols = columns(table)
+ connection.execute("DELETE FROM #{table}")
+ (1..5).collect do |x|
+ vals = cols.collect do |c|
+ if values.keys.include?(c.intern)
+ values[c.intern] ? "'#{values[c.intern]}'" : "NULL"
+ else
+ case c.intern
+ when :id; x
+ when :deleted_at; 'NULL'
+ else "'#{c.capitalize} #{x}'"
+ end
+ end
+ end
+ connection.execute(%{
+ INSERT INTO #{table} (#{cols.collect { |c| "`#{c}`" }.join(', ')})
+ VALUES (#{vals.join(', ')})
+ })
+ klass.find(x)
+ end
+end
+
def debug(object)
puts "<pre>"
pp object
@@ -39,18 +70,24 @@ def establish_test_db
# Establish logger
logger_file = File.open("#{SPEC}/db/log/test.log", 'a')
logger_file.sync = true
- @logger = Logger.new logger_file
+ @logger = Logger.new(logger_file)
ActiveRecord::Base.logger = @logger
# Drop articles and archived_articles
- ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS articles")
- ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS archived_articles")
- ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS schema_migrations")
+ connection.execute("DROP TABLE IF EXISTS articles")
+ connection.execute("DROP TABLE IF EXISTS archived_articles")
+ connection.execute("DROP TABLE IF EXISTS schema_migrations")
# Create articles table
- ActiveRecord::Base.connection.create_table(:articles) do |t|
+ connection.create_table(:articles) do |t|
t.string :title
t.string :body
- t.timestamps
end
# Load model
require "#{SPEC}/db/models/article"
end
+
+def indexes
+ query = "SHOW INDEX FROM archived_#{Article.table_name}"
+ connection.select_all(query).collect do |r|
+ r["Column_name"]
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.