From 00bda324488503251b6a0348938ed39eaba2ffcf Mon Sep 17 00:00:00 2001 From: Daniela Feitosa Date: Fri, 17 Oct 2014 11:17:37 +0000 Subject: [PATCH] improving the runtime of migrations --- db/migrate/20140708121356_index_articles_filtered_fields.rb | 4 ++-- db/migrate/20140709212646_add_spam_comments_counter_cache_to_articles.rb | 5 ++++- db/migrate/20140709224246_create_real_relation_between_article_and_author.rb | 6 ++++-- db/migrate/20140724180943_add_index_to_blog_posts_sort.rb | 4 ++-- db/schema.rb | 6 ++++-- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/db/migrate/20140708121356_index_articles_filtered_fields.rb b/db/migrate/20140708121356_index_articles_filtered_fields.rb index 0e9982a..cfae4e0 100644 --- a/db/migrate/20140708121356_index_articles_filtered_fields.rb +++ b/db/migrate/20140708121356_index_articles_filtered_fields.rb @@ -1,8 +1,8 @@ class IndexArticlesFilteredFields < ActiveRecord::Migration def self.up %w[articles article_versions].each do |table| - add_index table, [:path] - add_index table, [:path, :profile_id] + add_index table.to_sym, [:path] + add_index table.to_sym, [:path, :profile_id] end add_index :articles, [:type] add_index :articles, [:type, :parent_id] diff --git a/db/migrate/20140709212646_add_spam_comments_counter_cache_to_articles.rb b/db/migrate/20140709212646_add_spam_comments_counter_cache_to_articles.rb index 6edd44b..8448e93 100644 --- a/db/migrate/20140709212646_add_spam_comments_counter_cache_to_articles.rb +++ b/db/migrate/20140709212646_add_spam_comments_counter_cache_to_articles.rb @@ -2,7 +2,10 @@ class AddSpamCommentsCounterCacheToArticles < ActiveRecord::Migration def self.up add_column :articles, :spam_comments_count, :integer, :default => 0 add_column :article_versions, :spam_comments_count, :integer, :default => 0 - 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');" + + 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| + execute("UPDATE articles SET spam_comments_count = '#{data['comments_count']}' WHERE id = #{data['source_id']}") + end end def self.down diff --git a/db/migrate/20140709224246_create_real_relation_between_article_and_author.rb b/db/migrate/20140709224246_create_real_relation_between_article_and_author.rb index d15f9ce..25fabfa 100644 --- a/db/migrate/20140709224246_create_real_relation_between_article_and_author.rb +++ b/db/migrate/20140709224246_create_real_relation_between_article_and_author.rb @@ -4,8 +4,10 @@ class CreateRealRelationBetweenArticleAndAuthor < ActiveRecord::Migration add_column :article_versions, :author_id, :integer # Set article's author as the first version's last_changed_by_id. - 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)" - end + execute("UPDATE article_versions SET author_id = last_changed_by_id") + + 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") + end def self.down remove_column :articles, :author_id diff --git a/db/migrate/20140724180943_add_index_to_blog_posts_sort.rb b/db/migrate/20140724180943_add_index_to_blog_posts_sort.rb index 31700b2..f3ac165 100644 --- a/db/migrate/20140724180943_add_index_to_blog_posts_sort.rb +++ b/db/migrate/20140724180943_add_index_to_blog_posts_sort.rb @@ -1,13 +1,13 @@ class AddIndexToBlogPostsSort < ActiveRecord::Migration def self.up %w[articles article_versions].each do |table| - add_index table, [:published_at, :id] + add_index table.to_sym, [:published_at, :id] end end def self.down %w[articles article_versions].each do |table| - remove_index table, [:published_at, :id] + remove_index table.to_sym, [:published_at, :id] end end end diff --git a/db/schema.rb b/db/schema.rb index efee26f..ea020db 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,4 +1,4 @@ -# This file is auto-generated from the current state of the database. Instead of editing this file, +# This file is auto-generated from the current state of the database. Instead of editing this file, # please use the migrations feature of Active Record to incrementally modify your database, and # then regenerate this schema definition. # @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140709224246) do +ActiveRecord::Schema.define(:version => 20140724180943) do create_table "abuse_reports", :force => true do |t| t.integer "reporter_id" @@ -100,6 +100,7 @@ ActiveRecord::Schema.define(:version => 20140709224246) do add_index "article_versions", ["article_id"], :name => "index_article_versions_on_article_id" add_index "article_versions", ["path", "profile_id"], :name => "index_article_versions_on_path_and_profile_id" add_index "article_versions", ["path"], :name => "index_article_versions_on_path" + add_index "article_versions", ["published_at", "id"], :name => "index_article_versions_on_published_at_and_id" create_table "articles", :force => true do |t| t.string "name" @@ -154,6 +155,7 @@ ActiveRecord::Schema.define(:version => 20140709224246) do add_index "articles", ["path", "profile_id"], :name => "index_articles_on_path_and_profile_id" add_index "articles", ["path"], :name => "index_articles_on_path" add_index "articles", ["profile_id"], :name => "index_articles_on_profile_id" + add_index "articles", ["published_at", "id"], :name => "index_articles_on_published_at_and_id" add_index "articles", ["slug"], :name => "index_articles_on_slug" add_index "articles", ["translation_of_id"], :name => "index_articles_on_translation_of_id" add_index "articles", ["type", "parent_id"], :name => "index_articles_on_type_and_parent_id" -- libgit2 0.21.2