Class: Rpush200Updates

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/templates/rpush_2_0_0_updates.rb

Defined Under Namespace

Modules: Rpush

Class Method Summary collapse

Class Method Details

.adapter_nameObject



71
72
73
74
# File 'lib/generators/templates/rpush_2_0_0_updates.rb', line 71

def self.adapter_name
  env = (defined?(Rails) && Rails.env) ? Rails.env : 'development'
  Hash[ActiveRecord::Base.configurations[env].map { |k,v| [k.to_sym,v] }][:adapter]
end

.downObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/generators/templates/rpush_2_0_0_updates.rb', line 46

def self.down
  [:Apns, :Gcm, :Wpns, :Adm].each do |service|
    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

  if ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 1
    if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi)
      remove_index :rpush_notifications, name: :index_rpush_notifications_multi
    end
  else
    if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi, true)
      remove_index :rpush_notifications, name: :index_rpush_notifications_multi
    end
  end

  add_index :rpush_notifications, [:app_id, :delivered, :failed, :deliver_after], name: 'index_rpush_notifications_multi'

  remove_column :rpush_notifications, :priority
  remove_column :rpush_notifications, :processing
end

.postgresql?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/generators/templates/rpush_2_0_0_updates.rb', line 76

def self.postgresql?
  adapter_name =~ /postgresql|postgis/
end

.upObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/generators/templates/rpush_2_0_0_updates.rb', line 16

def self.up
  add_column :rpush_notifications, :processing, :boolean, null: false, default: false
  add_column :rpush_notifications, :priority, :integer, null: true

  if ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 1
    if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi)
      remove_index :rpush_notifications, name: :index_rpush_notifications_multi
    end
  else
    if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi, true)
      remove_index :rpush_notifications, name: :index_rpush_notifications_multi
    end
  end

  add_index :rpush_notifications, [:delivered, :failed], name: 'index_rpush_notifications_multi', where: 'NOT delivered AND NOT failed'

  rename_column :rpush_feedback, :app, :app_id

  if postgresql?
    execute('ALTER TABLE rpush_feedback ALTER COLUMN app_id TYPE integer USING (trim(app_id)::integer)')
  else
    change_column :rpush_feedback, :app_id, :integer
  end

  [:Apns, :Gcm, :Wpns, :Adm].each do |service|
    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

.update_type(model, from, to) ⇒ Object



12
13
14
# File 'lib/generators/templates/rpush_2_0_0_updates.rb', line 12

def self.update_type(model, from, to)
  model.where(type: from).update_all(type: to)
end