Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Splitting ActsAsArchive::Base::Table#archive_table_indexed_columns in…

…to modular database adapters
  • Loading branch information...
commit bac08ca86a1046e5e0614915720ce3d0a85127b8 1 parent 042f3d8
@winton winton authored
View
17 lib/acts_as_archive/base/adapters/mysql.rb
@@ -0,0 +1,17 @@
+module ActsAsArchive
+ module Base
+ module Adapters
+ module MySQL
+
+ private
+
+ def archive_table_indexed_columns
+ index_query = "SHOW INDEX FROM archived_#{table_name}"
+ indexes = connection.select_all(index_query).collect do |r|
+ r["Column_name"]
+ end
+ end
+ end
+ end
+ end
+end
View
34 lib/acts_as_archive/base/adapters/postgresql.rb
@@ -0,0 +1,34 @@
+module ActsAsArchive
+ module Base
+ module Adapters
+ module PostgreSQL
+
+ private
+
+ def archive_table_indexed_columns
+ 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
+
+ # HACK: reverse engineer the column name
+ indexes.map do |index|
+ index.split("_on_").last
+ end
+ end
+ end
+ end
+ end
+end
View
47 lib/acts_as_archive/base/table.rb
@@ -6,6 +6,14 @@ def self.included(base)
unless base.included_modules.include?(InstanceMethods)
base.send :extend, ClassMethods
base.send :include, InstanceMethods
+
+ if base.connection.class.to_s.include?('Mysql')
+ base.send :extend, ActsAsArchive::Base::Adapters::MySQL
+ elsif base.connection.class.to_s.include?('PostgreSQL')
+ base.send :extend, ActsAsArchive::Base::Adapters::PostgreSQL
+ else
+ raise 'acts_as_archive does not support this database adapter'
+ end
end
end
@@ -19,7 +27,7 @@ def create_archive_table
if table_exists? && !archive_table_exists?
connection.execute(%{
CREATE TABLE archived_#{table_name}
- #{"ENGINE=InnoDB" if connection.class.to_s == "ActiveRecord::ConnectionAdapters::MysqlAdapter"}
+ #{"ENGINE=InnoDB" if connection.class.to_s.include?('Mysql')}
AS SELECT * from #{table_name}
WHERE false;
})
@@ -55,43 +63,6 @@ def migrate_from_acts_as_paranoid
end
end
end
-
- private
-
- def archive_table_indexed_columns
- case connection.class.to_s
- when "ActiveRecord::ConnectionAdapters::MysqlAdapter"
- index_query = "SHOW INDEX FROM archived_#{table_name}"
- indexes = connection.select_all(index_query).collect do |r|
- r["Column_name"]
- end
- when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
- #postgresql is...slightly...more complicated
- index_query = <<EOS
-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
-EOS
- indexes = connection.select_all(index_query).collect do |r|
- r["index_name"]
- end
-
- # HACK: reverse engineer the column name
- # This sucks, but acts_as_archive only adds indexes on single columns anyway so it should work OK
- # and getting the columns indexed is INCREDIBLY complicated in PostgreSQL.
- indexes.map do |index|
- index.split("_on_").last
- end
- else
- raise "Unsupported Database"
- end
- end
end
module InstanceMethods
View
2  require.rb
@@ -22,6 +22,8 @@
lib do
require "lib/acts_as_archive/base"
+ require "lib/acts_as_archive/base/adapters/mysql"
+ require "lib/acts_as_archive/base/adapters/postgresql"
require "lib/acts_as_archive/base/destroy"
require "lib/acts_as_archive/base/restore"
require "lib/acts_as_archive/base/table"
Please sign in to comment.
Something went wrong with that request. Please try again.