Sha256: ca559ab5e8723bdac0f6bc21fa6e41a2af55ce9c32a2814834e81cd7cb8606c7

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

class CreateUsers < ActiveRecord::Migration
  def self.up
    if !table_exists? :users
      create_table :users do |t|
        t.string :provider
        t.string :uid
        t.string :email
        t.bigint :created
        t.string :username
        t.boolean :verified
        t.boolean :admin
        t.string :referred_by
        t.string :token
        t.text :facebook
        t.text :tags
        t.text :referred
        t.text :google
        t.text :twitter
        t.text :github
        t.text :access_tokens
        t.boolean :subscribed
        t.string :display
        t.text :identities

        t.timestamps
      end
    else
      safe_column :users, :provider, :string
      safe_column :users, :uid, :string
      safe_column :users, :email, :string
      safe_column :users, :created, :integer
      safe_column :users, :username, :string
      safe_column :users, :verified, :booleanied
      safe_column :users, :admin, :boolean
      safe_column :users, :referred, :string
      safe_column :users, :token, :string
      safe_column :users, :facebook, :text
      safe_column :users, :tags, :text
      safe_column :users, :referred, :text
      safe_column :users, :google, :text
      safe_column :users, :twitter, :text
      safe_column :users, :github, :text
      safe_column :users, :access_tokens, :text
      safe_column :users, :display, :string
      safe_column :users, :subscribed, :boolean
      safe_column :users, :identities, :text
    end
  end

  def self.down
    drop_table :users
  end

  private

  def table_exists? table
    ActiveRecord::Base.connection.table_exists?(table.to_s)
  end

  def column_exists?(table, column)
    begin
      ActiveRecord::Base.connection.columns(table.to_s).map(&:name).include?(column.to_s)
    rescue NoMethodError #actual_columns is empty and it doesn't respond to the map method
      false
    end
  end

  def safe_column table, name, type
    add_column table.to_sym, name.to_sym, type.to_sym unless column_exists? table, name
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dailycred-0.1.42 lib/generators/templates/migration_create_user.rb