Sha256: 2591c0b3a2269778ddd8d143c053f8a7181a2e4ad4bb4388151b58e7b81b9167

Contents?: true

Size: 887 Bytes

Versions: 2

Compression:

Stored size: 887 Bytes

Contents

# frozen_string_literal: true

class AddShortNameToCoreBasedStatisticalAreas < ActiveRecord::Migration[5.0]
  def up
    return if column_exists?(:us_geo_core_based_statistical_areas, :short_name)

    add_column :us_geo_core_based_statistical_areas, :short_name, :string, null: true

    select_all("SELECT geoid, name FROM us_geo_core_based_statistical_areas").each do |row|
      city, state = row["name"].split(", ", 2)
      short_name = "#{city.split("-").first}, #{state.split("-").first}"
      update("UPDATE us_geo_core_based_statistical_areas SET short_name = ? WHERE geoid = ?", nil, [short_name, row["geoid"]])
    end

    change_column_null :us_geo_core_based_statistical_areas, :short_name, false
    add_index :us_geo_core_based_statistical_areas, :short_name, unique: true
  end

  def down
    remove_column :us_geo_core_based_statistical_areas, :short_name
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
us_geo-2.0.1 db/migrate/20230414000700_add_short_name_to_core_based_statistical_areas.rb
us_geo-2.0.0 db/migrate/20230414000700_add_short_name_to_core_based_statistical_areas.rb