Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refine query for getting indexes from postgresql

  • Loading branch information...
commit 26a1639b879a7537eafe43231e5544cae31a9553 1 parent 1f096a9
@look look authored
View
46 lib/acts_as_archive/base/adapters/postgresql.rb
@@ -2,33 +2,39 @@ module ActsAsArchive
module Base
module Adapters
module PostgreSQL
-
+
private
def archive_table_indexed_columns
+ # This query comes courtesy of cope360:
+ # http://stackoverflow.com/questions/2204058/show-which-columns-an-index-is-on-in-postgresql/2213199#2213199
index_query = <<-SQL
- SELECT c2.relname as index_name
- FROM pg_catalog.pg_class c,
- pg_catalog.pg_class c2,
- pg_catalog.pg_index i
- WHERE c.oid = (SELECT c.oid
- FROM pg_catalog.pg_class c
- WHERE c.relname ~ '^(archived_#{table_name})$')
- AND c.oid = i.indrelid
- AND i.indexrelid = c2.oid
- SQL
-
- index_query.gsub!(/\s+/, ' ').strip!
- indexes = connection.select_all(index_query).collect do |r|
- r["index_name"]
- end
+select
+ t.relname as table_name,
+ i.relname as index_name,
+ a.attname as column_name
+from
+ pg_class t,
+ pg_class i,
+ pg_index ix,
+ pg_attribute a
+where
+ t.oid = ix.indrelid
+ and i.oid = ix.indexrelid
+ and a.attrelid = t.oid
+ and a.attnum = ANY(ix.indkey)
+ and t.relkind = 'r'
+ and t.relname = 'archived_#{table_name}'
+order by
+ t.relname,
+ i.relname
+SQL
- # HACK: reverse engineer the column name
- indexes.map do |index|
- index.split("_on_").last
+ indexes = connection.select_all(index_query).collect do |r|
+ r["column_name"]
end
end
end
end
end
-end
+end
View
34 spec/acts_as_archive/base/table_spec.rb
@@ -1,69 +1,69 @@
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
describe ActsAsArchive::Base::Table do
-
+
before(:all) do
establish_test_db
Article.create_archive_table
end
-
+
describe 'create_archive_table' do
-
+
before(:all) do
@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
end
-
+
it "should create an archive table with the same structure as the original table" do
@article_columns.each do |col|
@archive_columns.include?(col).should == true
end
end
-
+
it "should add a deleted_at column to the archive table" do
(@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" ]
+ indexes.to_set.should == [ "id", "deleted_at" ].to_set
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.utc)
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
@@ -71,4 +71,4 @@
Article::Archive.count.should == 0
end
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.