0fc9f9d3e7
DB schema generated by a migration may look different in rails 4 and 5 (because rails 5 may use different default values). For this reason it's important to explicitly set for which rails version a migration was written for. See https://stackoverflow.com/questions/35929869/activerecordmigration-deprecation-warning-asks-for-rails-version-but-im-no/35930912#35930912
25 lines
807 B
Ruby
25 lines
807 B
Ruby
class MigrateUsersNotificationLevel < ActiveRecord::Migration[4.2]
|
|
DOWNTIME = false
|
|
|
|
# Migrates only users who changed their default notification level :participating
|
|
# creating a new record on notification settings table
|
|
|
|
DOWNTIME = false
|
|
|
|
def up
|
|
execute(%Q{
|
|
INSERT INTO notification_settings
|
|
(user_id, level, created_at, updated_at)
|
|
(SELECT id, notification_level, created_at, updated_at FROM users WHERE notification_level != 1)
|
|
})
|
|
end
|
|
|
|
# Migrates from notification settings back to user notification_level
|
|
# If no value is found the default level of 1 will be used
|
|
def down
|
|
execute(%Q{
|
|
UPDATE users u SET
|
|
notification_level = COALESCE((SELECT level FROM notification_settings WHERE user_id = u.id AND source_type IS NULL), 1)
|
|
})
|
|
end
|
|
end
|