Sha256: 72afe9045dfb15c9d4cb669a8bc64b5b582a653ed20b20c52d78666c10c95901

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

class AddSiteColumns < ActiveRecord::Migration
  
  config = VhostExtension.read_config

  MODELS = config[:models]
  
  # Declare the models so we can use them.
  MODELS.each do |model|
    eval "class #{model} < ActiveRecord::Base; end"
  end
  
  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

  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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
radiant-vhost-extension-2.3.1 lib/tasks/add_site_columns.rb
radiant-vhost-extension-2.3.0 lib/tasks/add_site_columns.rb
radiant-vhost-extension-2.2.0 lib/tasks/add_site_columns.rb
radiant-vhost-extension-2.1.0 lib/tasks/add_site_columns.rb