DEV: ignore `flair_url` column in group model. (#9873)

DEV: ignore flair_url column in group model. (#9873)

diff --git a/app/jobs/onceoff/migrate_group_flair_images.rb b/app/jobs/onceoff/migrate_group_flair_images.rb
index 75616fd..2b862cd 100644
--- a/app/jobs/onceoff/migrate_group_flair_images.rb
+++ b/app/jobs/onceoff/migrate_group_flair_images.rb
@@ -4,11 +4,20 @@ require 'uri'
 module Jobs
   class MigrateGroupFlairImages < ::Jobs::Onceoff
     def execute_onceoff(args)
-      return if Group.column_names.exclude?("flair_url")
-
-      Group.where.not(flair_url: nil).each do |group|
+      column_exists = DB.exec(<<~SQL) == 1
+        SELECT 1
+        FROM INFORMATION_SCHEMA.COLUMNS
+        WHERE
+          table_schema = 'public' AND
+          table_name = 'groups' AND
+          column_name = 'flair_url'
+      SQL
+      return unless column_exists
+
+      groups = Group.where.not(flair_url: nil).select(:id, :flair_url, :flair_upload_id, :name)
+      groups.each do |group|
         if group.flair_upload.present?
-          g.update_column(:flair_url, nil)
+          DB.exec("UPDATE groups SET flair_url = NULL WHERE id = #{group.id}")
           next
         end
 
@@ -68,7 +77,7 @@ module Jobs
           origin: UrlHelper.absolute(old_url)
         ).create_for(Discourse.system_user.id)
 
-        group.update_columns(flair_upload_id: upload.id, flair_url: nil) if upload.present?
+        DB.exec("UPDATE groups SET flair_url = NULL, flair_upload_id = #{upload.id} WHERE id = #{group.id}") if upload.present?
       end
     end
 
diff --git a/app/models/group.rb b/app/models/group.rb
index b71906b..6448f46 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -1,9 +1,10 @@
 # frozen_string_literal: true
 
 class Group < ActiveRecord::Base
-  # TODO(2021-04-22): remove
+  # TODO(2021-05-26): remove
   self.ignored_columns = %w{
     automatic_membership_retroactive
+    flair_url
   }
 
   include HasCustomFields
diff --git a/spec/jobs/migrate_group_flair_images_spec.rb b/spec/jobs/migrate_group_flair_images_spec.rb
index a1c0c51..ff3428f 100644
--- a/spec/jobs/migrate_group_flair_images_spec.rb
+++ b/spec/jobs/migrate_group_flair_images_spec.rb
@@ -4,6 +4,7 @@ require 'rails_helper'
 
 RSpec.describe Jobs::MigrateGroupFlairImages do
   let(:image_url) { "https://omg.aws.somestack/test.png" }
+  let(:group) { Fabricate(:group) }
 
   before do
     stub_request(:get, image_url).to_return(
@@ -18,7 +19,9 @@ RSpec.describe Jobs::MigrateGroupFlairImages do
   end
 
   it 'should migrate to the new group `flair_upload_id` column correctly' do
-    group = Fabricate(:group, flair_url: image_url)
+    DB.exec(<<~SQL, flair_url: image_url)
+      UPDATE groups SET flair_url = :flair_url WHERE id = #{group.id}
+    SQL
 
     expect do
       described_class.new.execute_onceoff({})
@@ -30,7 +33,7 @@ RSpec.describe Jobs::MigrateGroupFlairImages do
   end
 
   it 'should skip groups with invalid flair URLs' do
-    group = Fabricate(:group, flair_url: "abc")
+    DB.exec("UPDATE groups SET flair_url = 'abc' WHERE id = #{group.id}")
     described_class.new.execute_onceoff({})
     expect(Rails.logger.warnings.count).to eq(0)
   end

GitHub sha: 5fb92718

1 Like

This commit appears in #9873 which was merged by vinothkannans.

We can’t use AR queries in migrations.

Group.where.not(flair_url: nil).select(:id, :flair_url, :flair_upload_id, :name)

Can you replace that with a SQL query please?

2 Likes