From 53f73ac3069e88199d3a0dc208b82bff7bde034c Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Thu, 7 Jan 2016 18:09:51 -0300 Subject: [PATCH] article-followers: fast migration to move followers from settings to table --- db/migrate/20151105175041_create_article_followers.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+), 0 deletions(-) create mode 100644 db/migrate/20151105175041_create_article_followers.rb diff --git a/db/migrate/20151105175041_create_article_followers.rb b/db/migrate/20151105175041_create_article_followers.rb new file mode 100644 index 0000000..9c9f97a --- /dev/null +++ b/db/migrate/20151105175041_create_article_followers.rb @@ -0,0 +1,12 @@ +class CreateArticleFollowers < ActiveRecord::Migration + def self.up + execute("CREATE TABLE article_followers AS (SELECT profiles.id AS person_id, t.id AS article_id, clock_timestamp() AS since FROM (SELECT articles.id, regexp_split_to_table(replace(replace(substring(articles.setting FROM ':followers:[^:]*'), ':followers:', ''), '- ', ''), '\n') AS follower FROM articles) t INNER JOIN users ON users.email = follower INNER JOIN profiles ON users.id = profiles.user_id WHERE follower != '');") + add_index :article_followers, :person_id + add_index :article_followers, :article_id + add_index :article_followers, [:person_id, :article_id], :unique => true + end + + def self.down + drop_table :article_followers + end +end -- libgit2 0.21.2