Replace migrations that rely on models with rake tasks
authorTom Hughes <tom@compton.nu>
Wed, 17 Jul 2019 17:55:15 +0000 (18:55 +0100)
committerTom Hughes <tom@compton.nu>
Wed, 17 Jul 2019 17:55:15 +0000 (18:55 +0100)
Replace some migrations that rely on relationships between
models to add subscriptions with rake tasks.

db/migrate/20150111192335_subscribe_old_changesets.rb
db/migrate/20161011010929_subscribe_authors_to_diary_entries.rb
lib/tasks/subscribe_diary_authors.rb [new file with mode: 0644]
lib/tasks/subscribe_old_changesets.rb [new file with mode: 0644]

index 94b3bb5..41d139c 100644 (file)
@@ -1,12 +1,5 @@
 class SubscribeOldChangesets < ActiveRecord::Migration[4.2]
-  class Changeset < ActiveRecord::Base
-  end
-
-  def up
-    Changeset.find_each do |changeset|
-      changeset.subscribers << changeset.user unless changeset.subscribers.exists?(changeset.user.id)
-    end
-  end
+  def up; end
 
   def down; end
 end
index 76fa1fa..996d2c9 100644 (file)
@@ -1,12 +1,5 @@
 class SubscribeAuthorsToDiaryEntries < ActiveRecord::Migration[4.2]
-  class DiaryEntry < ActiveRecord::Base
-  end
-
-  def up
-    DiaryEntry.find_each do |diary_entry|
-      diary_entry.subscriptions.create(:user => diary_entry.user) unless diary_entry.subscribers.exists?(diary_entry.user.id)
-    end
-  end
+  def up; end
 
   def down; end
 end
diff --git a/lib/tasks/subscribe_diary_authors.rb b/lib/tasks/subscribe_diary_authors.rb
new file mode 100644 (file)
index 0000000..107fcc5
--- /dev/null
@@ -0,0 +1,7 @@
+namespace :db do
+  task :subscribe_diary_authors => :environment do
+    DiaryEntry.find_each do |diary_entry|
+      diary_entry.subscriptions.create(:user => diary_entry.user) unless diary_entry.subscribers.exists?(diary_entry.user.id)
+    end
+  end
+end
diff --git a/lib/tasks/subscribe_old_changesets.rb b/lib/tasks/subscribe_old_changesets.rb
new file mode 100644 (file)
index 0000000..6fdd4c4
--- /dev/null
@@ -0,0 +1,7 @@
+namespace :db do
+  task :subscribe_old_changesets => :environment do
+    Changeset.find_each do |changeset|
+      changeset.subscribers << changeset.user unless changeset.subscribers.exists?(changeset.user.id)
+    end
+  end
+end