Commit a2571ba9b982e1a02380db89bef07e274a9ad01c
1 parent
43378edc
Exists in
master
and in
28 other branches
Fix migration to run at a reasonable time
Showing
2 changed files
with
34 additions
and
2 deletions
Show diff stats
db/migrate/20100928000952_aggressive_indexing_strategy2.rb
1 | 1 | class AggressiveIndexingStrategy2 < ActiveRecord::Migration |
2 | 2 | def self.up |
3 | - execute("delete from action_tracker_notifications where id not in (select distinct(atn.id) from action_tracker_notifications as atn JOIN action_tracker_notifications as t ON (t.profile_id = atn.profile_id and t.action_tracker_id = atn.action_tracker_id and atn.id < t.id))") | |
3 | + | |
4 | + say 'Removing duplicate notification records ...' | |
5 | + buffer = '' | |
6 | + removed = 0 | |
7 | + select_all( | |
8 | + 'select min(id) as min_id, action_tracker_id, profile_id, count(*) | |
9 | + from action_tracker_notifications | |
10 | + group by action_tracker_id, profile_id | |
11 | + having count(*) > 1' | |
12 | + ).each do |duplicate| | |
13 | + buffer += ('delete from action_tracker_notifications | |
14 | + where | |
15 | + profile_id = %d AND | |
16 | + action_tracker_id = %s AND | |
17 | + id > %d; | |
18 | + ' % [duplicate['profile_id'], duplicate['action_tracker_id'], duplicate['min_id']] | |
19 | + ) | |
20 | + if removed % 100 == 0 | |
21 | + execute buffer | |
22 | + say "Deleted " + removed.to_s | |
23 | + buffer = '' | |
24 | + end | |
25 | + removed += 1 | |
26 | + end | |
27 | + | |
28 | + if !buffer.empty? | |
29 | + execute buffer | |
30 | + end | |
31 | + | |
4 | 32 | add_index(:action_tracker_notifications, :profile_id) |
5 | 33 | add_index(:action_tracker_notifications, :action_tracker_id) |
6 | 34 | add_index(:action_tracker_notifications, [:profile_id, :action_tracker_id], :unique => true) | ... | ... |
db/schema.rb
... | ... | @@ -9,7 +9,7 @@ |
9 | 9 | # |
10 | 10 | # It's strongly recommended to check this file into your version control system. |
11 | 11 | |
12 | -ActiveRecord::Schema.define(:version => 20100923160040) do | |
12 | +ActiveRecord::Schema.define(:version => 20100928000952) do | |
13 | 13 | |
14 | 14 | create_table "action_tracker", :force => true do |t| |
15 | 15 | t.integer "user_id" |
... | ... | @@ -31,6 +31,10 @@ ActiveRecord::Schema.define(:version => 20100923160040) do |
31 | 31 | t.integer "profile_id" |
32 | 32 | end |
33 | 33 | |
34 | + add_index "action_tracker_notifications", ["action_tracker_id"], :name => "index_action_tracker_notifications_on_action_tracker_id" | |
35 | + add_index "action_tracker_notifications", ["profile_id", "action_tracker_id"], :name => "index_action_tracker_notifications_on_profile_id_and_action_tracker_id", :unique => true | |
36 | + add_index "action_tracker_notifications", ["profile_id"], :name => "index_action_tracker_notifications_on_profile_id" | |
37 | + | |
34 | 38 | create_table "article_versions", :force => true do |t| |
35 | 39 | t.integer "article_id" |
36 | 40 | t.integer "version" | ... | ... |