Home > Blockchain >  Rails migration to modify some values not working
Rails migration to modify some values not working

Time:12-26

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.

CodePudding user response:

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:

class FixSourceNames < ActiveRecord::Migration[7.0]

  def self.up
    execute <<-SQL
        UPDATE datasets SET source = 'US Census Bureau American Community Survey' WHERE source = 'U.S. Census Bureau American Community Survey';
        UPDATE datasets SET source = 'US Census Bureau American Community Survey' WHERE source = 'US Census - American Community Survey';
        UPDATE datasets SET source = 'US Census Bureau American Community Survey' WHERE source = 'US Census Bureau - American Community Survey';
        etc
    SQL
  end

  def self.down
  end

end

I think that it's a valid way to do it, give it a try

CodePudding user response:

Remember that you can use ruby code inside a migration. Use a hash to store the values, the key value for the where clause and the value for the new data to be updated.

class FixSourceNames < ActiveRecord::Migration[7.0]

  def values
    {
      'U.S. Census Bureau American Community Survey' => 'US Census Bureau American Community Survey',
      'US Census - American Community Survey' => 'US Census Bureau American Community Survey'
    }.freeze
  end
  
  def self.up
    values.map {|k,v| Dataset.where(source: k).update_all(source: v)}
  end

  def self.down
  end

end
  • Related