mirror of
https://github.com/openSUSE/osem.git
synced 2026-08-13 19:54:02 +00:00
Many migrations currently fail to run with:
> Directly inheriting from ActiveRecord::Migration is not supported.
> Please specify the Rails release the migration was written for:
>
> class Example < ActiveRecord::Migration[4.2]
I've annotated those that I need to run with the Rails version at the
time each was committed:
rake db:migrate:status \
| grep --perl-regexp --only-matching '(?<=^ down )\d{14}' \
| while read -r id; do
path="$(ls -1 db/migrate/${id}_*.rb)"
version="$(git show "$(git log --diff-filter=A --pretty=format:%H -- "$path")":Gemfile.lock \
| grep --perl-regexp --only-matching '(?<=^ rails \()\d+\.\d+(?=(\.\d+)+\))')"
sed --in-place -e "s/\(< ActiveRecord::Migration\)$/\\1[$version]/" "$path"
done
41 lines
1.2 KiB
Ruby
41 lines
1.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class AddShortNameToTracks < ActiveRecord::Migration[4.2]
|
|
class TmpProgram < ActiveRecord::Base
|
|
self.table_name = 'programs'
|
|
end
|
|
|
|
class TmpTrack < ActiveRecord::Base
|
|
self.table_name = 'tracks'
|
|
end
|
|
|
|
def change
|
|
add_column :tracks, :short_name, :string
|
|
|
|
TmpTrack.reset_column_information
|
|
|
|
TmpProgram.find_each do |program|
|
|
# Keeps count of how many times we've encountered a short_name
|
|
track_name_counter = {}
|
|
|
|
TmpTrack.where(program_id: program.id).find_each do |track|
|
|
# Replace spaces with undercores and remove the non alphanumeric characters that aren't underscores or dashes
|
|
short_name = track.name.tr(' ', '_').tr('^a-zA-Z0-9_-', '')
|
|
|
|
# If we've seen that short_name before then add the counter in the end to avoid collisions
|
|
if track_name_counter[short_name]
|
|
track_name_counter[short_name] += 1
|
|
short_name += "_#{track_name_counter[short_name]}"
|
|
else
|
|
# Initialize the counter
|
|
track_name_counter[short_name] = 0 unless track_name_counter[short_name]
|
|
end
|
|
|
|
track.short_name = short_name
|
|
track.save!
|
|
end
|
|
end
|
|
|
|
change_column_null :tracks, :short_name, false
|
|
end
|
|
end
|