I've written a migration to clean up some items in the database rails g migration FixSourceNames, which generates the migration:
class FixSourceNames < ActiveRecord::Migration[7.0]
def change
end
end
I modify it:
class FixSourceNames < ActiveRecord::Migration[7.0]
def self.up
Dataset.where(source: "U.S. Census Bureau American Community Survey").update_all(source: "US Census Bureau American Community Survey")
Dataset.where(source: "US Census - American Community Survey").update_all(source: "US Census Bureau American Community Survey")
Dataset.where(source: "US Census Bureau - American Community Survey").update_all(source: "US Census Bureau American Community Survey")
Dataset.where(source: "HUD Office of Policy Development & Research").update_all(source: "HUD")
Dataset.where(source: "CDC - Behavioral Risk Factors Survey").update_all(source: "CDC")
Dataset.where(source: "County Health Rankings").update_all(source: "Robert Wood Johnson Foundation/University of Wisconsin")
end
def self.down
end
end
When I run rake db:migrate I see that the migration runs without incident:
% rake db:migrate
== 20221216160630 FixSourceNames: migrating ===================================
== 20221216160630 FixSourceNames: migrated (0.1593s) ==========================
but none of the values in my data change. If I go into the console to run the commands individually though things get cleaned up.
What am I doing wrong here?
I've also tried something like rake db:migrate VERSION=20221216160630 but still no joy. I had also just tried to modify the default migration:
class FixSourceNames < ActiveRecord::Migration[7.0]
def change
Dataset.where(source: "U.S. Census Bureau American Community Survey").update_all(source: "US Census Bureau American Community Survey")
...
end
end
but that also didn't work.
Another way that can you achieve what are you looking for, it's running a migration executing the SQL query for update each of datasets. Something like this:
I think that it's a valid way to do it, give it a try