Retrospring/db/migrate/20220909161543_rpush_2_0_0_...

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

70 lines
2.6 KiB
Ruby
Raw Normal View History

2022-09-11 14:20:10 -07:00
# frozen_string_literal: true
2022-09-09 09:29:40 -07:00
class Rpush200Updates < ActiveRecord::Migration[5.0]
module Rpush
2022-09-11 14:20:10 -07:00
class App < ApplicationRecord
self.table_name = "rpush_apps"
2022-09-09 09:29:40 -07:00
end
2022-09-11 14:20:10 -07:00
class Notification < ApplicationRecord
self.table_name = "rpush_notifications"
2022-09-09 09:29:40 -07:00
end
end
def self.update_type(model, from, to)
2022-12-26 01:59:56 -08:00
model.where(type: from).update_all(type: to) # rubocop:disable Rails/SkipsModelValidations
2022-09-09 09:29:40 -07:00
end
def self.up
add_column :rpush_notifications, :processing, :boolean, null: false, default: false
add_column :rpush_notifications, :priority, :integer, null: true
2022-09-11 14:20:10 -07:00
remove_index :rpush_notifications, name: :index_rpush_notifications_multi if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi)
2022-09-09 09:29:40 -07:00
2022-09-11 14:20:10 -07:00
add_index :rpush_notifications, %i[delivered failed], name: "index_rpush_notifications_multi", where: "NOT delivered AND NOT failed"
2022-09-09 09:29:40 -07:00
rename_column :rpush_feedback, :app, :app_id
if postgresql?
2022-09-11 14:20:10 -07:00
execute("ALTER TABLE rpush_feedback ALTER COLUMN app_id TYPE integer USING (trim(app_id)::integer)")
2022-09-09 09:29:40 -07:00
else
change_column :rpush_feedback, :app_id, :integer
end
2022-09-11 14:20:10 -07:00
%i[Apns Gcm Wpns Adm].each do |service|
2022-09-09 09:29:40 -07:00
update_type(Rpush200Updates::Rpush::App, "Rpush::#{service}::App", "Rpush::Client::ActiveRecord::#{service}::App")
update_type(Rpush200Updates::Rpush::Notification, "Rpush::#{service}::Notification", "Rpush::Client::ActiveRecord::#{service}::Notification")
end
end
def self.down
2022-09-11 14:20:10 -07:00
%i[Apns Gcm Wpns Adm].each do |service|
2022-09-09 09:29:40 -07:00
update_type(Rpush200Updates::Rpush::App, "Rpush::Client::ActiveRecord::#{service}::App", "Rpush::#{service}::App")
update_type(Rpush200Updates::Rpush::Notification, "Rpush::Client::ActiveRecord::#{service}::Notification", "Rpush::#{service}::Notification")
end
change_column :rpush_feedback, :app_id, :string
rename_column :rpush_feedback, :app_id, :app
2022-09-11 14:20:10 -07:00
remove_index :rpush_notifications, name: :index_rpush_notifications_multi if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi)
2022-09-09 09:29:40 -07:00
2022-09-11 14:20:10 -07:00
add_index :rpush_notifications, %i[app_id delivered failed deliver_after], name: "index_rpush_notifications_multi"
2022-09-09 09:29:40 -07:00
remove_column :rpush_notifications, :priority
remove_column :rpush_notifications, :processing
end
def self.adapter_name
2022-09-11 14:20:10 -07:00
env = defined?(Rails) && Rails.env ? Rails.env : "development"
2022-09-09 09:29:40 -07:00
if ActiveRecord::VERSION::MAJOR > 6
ActiveRecord::Base.configurations.configs_for(env_name: env).first.configuration_hash[:adapter]
else
2022-09-11 14:20:10 -07:00
ActiveRecord::Base.configurations[env].to_h { |k, v| [k.to_sym, v] }[:adapter]
2022-09-09 09:29:40 -07:00
end
end
def self.postgresql?
adapter_name =~ /postgresql|postgis/
end
end