Commit 00bda324488503251b6a0348938ed39eaba2ffcf

Authored by Daniela Feitosa
1 parent 4d7446b1

improving the runtime of migrations

db/migrate/20140708121356_index_articles_filtered_fields.rb
1 class IndexArticlesFilteredFields < ActiveRecord::Migration 1 class IndexArticlesFilteredFields < ActiveRecord::Migration
2 def self.up 2 def self.up
3 %w[articles article_versions].each do |table| 3 %w[articles article_versions].each do |table|
4 - add_index table, [:path]  
5 - add_index table, [:path, :profile_id] 4 + add_index table.to_sym, [:path]
  5 + add_index table.to_sym, [:path, :profile_id]
6 end 6 end
7 add_index :articles, [:type] 7 add_index :articles, [:type]
8 add_index :articles, [:type, :parent_id] 8 add_index :articles, [:type, :parent_id]
db/migrate/20140709212646_add_spam_comments_counter_cache_to_articles.rb
@@ -2,7 +2,10 @@ class AddSpamCommentsCounterCacheToArticles &lt; ActiveRecord::Migration @@ -2,7 +2,10 @@ class AddSpamCommentsCounterCacheToArticles &lt; ActiveRecord::Migration
2 def self.up 2 def self.up
3 add_column :articles, :spam_comments_count, :integer, :default => 0 3 add_column :articles, :spam_comments_count, :integer, :default => 0
4 add_column :article_versions, :spam_comments_count, :integer, :default => 0 4 add_column :article_versions, :spam_comments_count, :integer, :default => 0
5 - execute "update articles set spam_comments_count = (select count(*) from comments where comments.source_id = articles.id and comments.source_type = 'Article' and comments.spam = 't');" 5 +
  6 + execute("SELECT comments.source_id as source_id, count(comments.id) as comments_count FROM comments LEFT OUTER JOIN articles ON articles.id = source_id WHERE comments.source_type = 'Article' AND comments.spam = 't' GROUP BY comments.source_id;").each do |data|
  7 + execute("UPDATE articles SET spam_comments_count = '#{data['comments_count']}' WHERE id = #{data['source_id']}")
  8 + end
6 end 9 end
7 10
8 def self.down 11 def self.down
db/migrate/20140709224246_create_real_relation_between_article_and_author.rb
@@ -4,8 +4,10 @@ class CreateRealRelationBetweenArticleAndAuthor &lt; ActiveRecord::Migration @@ -4,8 +4,10 @@ class CreateRealRelationBetweenArticleAndAuthor &lt; ActiveRecord::Migration
4 add_column :article_versions, :author_id, :integer 4 add_column :article_versions, :author_id, :integer
5 5
6 # Set article's author as the first version's last_changed_by_id. 6 # Set article's author as the first version's last_changed_by_id.
7 - execute "update articles set author_id = (select article_versions.last_changed_by_id from article_versions where article_versions.article_id = articles.id and article_versions.version = 1 limit 1)"  
8 - end 7 + execute("UPDATE article_versions SET author_id = last_changed_by_id")
  8 +
  9 + execute("UPDATE articles SET author_id = article_versions.author_id FROM article_versions WHERE article_versions.article_id = articles.id AND article_versions.version = 1")
  10 + end
9 11
10 def self.down 12 def self.down
11 remove_column :articles, :author_id 13 remove_column :articles, :author_id
db/migrate/20140724180943_add_index_to_blog_posts_sort.rb
1 class AddIndexToBlogPostsSort < ActiveRecord::Migration 1 class AddIndexToBlogPostsSort < ActiveRecord::Migration
2 def self.up 2 def self.up
3 %w[articles article_versions].each do |table| 3 %w[articles article_versions].each do |table|
4 - add_index table, [:published_at, :id] 4 + add_index table.to_sym, [:published_at, :id]
5 end 5 end
6 end 6 end
7 7
8 def self.down 8 def self.down
9 %w[articles article_versions].each do |table| 9 %w[articles article_versions].each do |table|
10 - remove_index table, [:published_at, :id] 10 + remove_index table.to_sym, [:published_at, :id]
11 end 11 end
12 end 12 end
13 end 13 end
1 -# This file is auto-generated from the current state of the database. Instead of editing this file, 1 +# This file is auto-generated from the current state of the database. Instead of editing this file,
2 # please use the migrations feature of Active Record to incrementally modify your database, and 2 # please use the migrations feature of Active Record to incrementally modify your database, and
3 # then regenerate this schema definition. 3 # then regenerate this schema definition.
4 # 4 #
@@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
9 # 9 #
10 # It's strongly recommended to check this file into your version control system. 10 # It's strongly recommended to check this file into your version control system.
11 11
12 -ActiveRecord::Schema.define(:version => 20140709224246) do 12 +ActiveRecord::Schema.define(:version => 20140724180943) do
13 13
14 create_table "abuse_reports", :force => true do |t| 14 create_table "abuse_reports", :force => true do |t|
15 t.integer "reporter_id" 15 t.integer "reporter_id"
@@ -100,6 +100,7 @@ ActiveRecord::Schema.define(:version =&gt; 20140709224246) do @@ -100,6 +100,7 @@ ActiveRecord::Schema.define(:version =&gt; 20140709224246) do
100 add_index "article_versions", ["article_id"], :name => "index_article_versions_on_article_id" 100 add_index "article_versions", ["article_id"], :name => "index_article_versions_on_article_id"
101 add_index "article_versions", ["path", "profile_id"], :name => "index_article_versions_on_path_and_profile_id" 101 add_index "article_versions", ["path", "profile_id"], :name => "index_article_versions_on_path_and_profile_id"
102 add_index "article_versions", ["path"], :name => "index_article_versions_on_path" 102 add_index "article_versions", ["path"], :name => "index_article_versions_on_path"
  103 + add_index "article_versions", ["published_at", "id"], :name => "index_article_versions_on_published_at_and_id"
103 104
104 create_table "articles", :force => true do |t| 105 create_table "articles", :force => true do |t|
105 t.string "name" 106 t.string "name"
@@ -154,6 +155,7 @@ ActiveRecord::Schema.define(:version =&gt; 20140709224246) do @@ -154,6 +155,7 @@ ActiveRecord::Schema.define(:version =&gt; 20140709224246) do
154 add_index "articles", ["path", "profile_id"], :name => "index_articles_on_path_and_profile_id" 155 add_index "articles", ["path", "profile_id"], :name => "index_articles_on_path_and_profile_id"
155 add_index "articles", ["path"], :name => "index_articles_on_path" 156 add_index "articles", ["path"], :name => "index_articles_on_path"
156 add_index "articles", ["profile_id"], :name => "index_articles_on_profile_id" 157 add_index "articles", ["profile_id"], :name => "index_articles_on_profile_id"
  158 + add_index "articles", ["published_at", "id"], :name => "index_articles_on_published_at_and_id"
157 add_index "articles", ["slug"], :name => "index_articles_on_slug" 159 add_index "articles", ["slug"], :name => "index_articles_on_slug"
158 add_index "articles", ["translation_of_id"], :name => "index_articles_on_translation_of_id" 160 add_index "articles", ["translation_of_id"], :name => "index_articles_on_translation_of_id"
159 add_index "articles", ["type", "parent_id"], :name => "index_articles_on_type_and_parent_id" 161 add_index "articles", ["type", "parent_id"], :name => "index_articles_on_type_and_parent_id"