Class: AddSiteColumns

Inherits:
ActiveRecord::Migration
  • Object
show all
Defined in:
lib/tasks/add_site_columns.rb

Constant Summary collapse

MODELS =
config[:models]

Class Method Summary collapse

Class Method Details

.downObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tasks/add_site_columns.rb', line 29

def self.down
  MODELS.each do |model|
    begin
      # Special case for Snippets to remove index
      if model == 'Snippet'
        remove_index :snippets, [:name, :site_id]
      end
      remove_column model.tableize, :site_id
    rescue
      # Ignore errors here, they're going to happen when the user
      # does a 'remigrate'
    end
  end
  remove_index :snippets, :column => [:name, :site_id] rescue nil
end

.upObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tasks/add_site_columns.rb', line 12

def self.up
  MODELS.each do |model|
    begin
      add_column model.tableize, :site_id, :integer
      model.constantize.update_all "site_id = 1"
      # Special case for Snippets to add a proper index
      if model == 'Snippet'
        add_index :snippets, [:name, :site_id], :unique => true
      end
    rescue
      # Ignore errors here, they're going to happen when the user
      # does a 'remigrate'
    end
  end
  add_index :snippets, [:name, :site_id] rescue nil
end