Class: Mole::Db::Migrate

Inherits:
Object
  • Object
show all
Defined in:
lib/mole/db/migrate.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Migrate

Returns a new instance of Migrate.



13
14
15
16
17
# File 'lib/mole/db/migrate.rb', line 13

def initialize( opts )
  @direction = opts.direction
  @config    = opts.configuration
  @env       = opts.environment
end

Instance Method Details

#applyObject

Creates a MOle migration by creating or dropping the MOle related tables



20
21
22
23
# File 'lib/mole/db/migrate.rb', line 20

def apply #:nodoc:
  setup
  @direction == :up ? migrate_up : migrate_down
end

#migrate_downObject


Destroys mole persistence tables



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mole/db/migrate.rb', line 74

def migrate_down
  # Delete the mole_feature table
  if ActiveRecord::Schema.tables.include?( 'mole_features' )                                                             
    ActiveRecord::Schema.remove_index( 'mole_features', :name => 'feature_idx' )              
    ActiveRecord::Schema.drop_table( 'mole_features' )
  end
    
  # Delete the mole_logs table
  if ActiveRecord::Schema.tables.include?( 'mole_logs' )
    ActiveRecord::Schema.remove_index( 'mole_logs', :name => 'log_feature_idx' )        
    ActiveRecord::Schema.remove_index( 'mole_logs', :name =>'log_date_idx' )              
    ActiveRecord::Schema.drop_table( 'mole_logs' )
  end
end

#migrate_upObject


Create mole persistence tables ( 2 tables mole_features/mole_logs )



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mole/db/migrate.rb', line 36

def migrate_up
  # Create the mole_features table if it doesn't exist
  unless ActiveRecord::Schema.tables.include?('mole_features')
    ActiveRecord::Schema.create_table('mole_features') do |t|
      t.column :name,             :string     
      t.column :context,          :string
      t.column :app_name,         :string
      t.column :created_at,       :datetime
      t.column :updated_at,       :datetime
    end
    ActiveRecord::Schema.add_index( 'mole_features', 
                                    ['name', 'context', 'app_name'], 
                                    :name   => 'feature_idx')
  end
  # Create the mole_logs table if it doesn't exist
  unless ActiveRecord::Schema.tables.include?('mole_logs')
    ActiveRecord::Schema.create_table('mole_logs') do |t|
      t.column :mole_feature_id,  :integer
      t.column :user_id,          :integer
      t.column :params,           :string, :limit => 1024    
      t.column :ip_address,       :string
      t.column :browser_type,     :string  
      t.column :host_name,        :string
      t.column :created_at,       :datetime
      t.column :updated_at,       :datetime
    end     
    ActiveRecord::Schema.add_index( 'mole_logs', 
                                    ['mole_feature_id','user_id'], 
                                    :name   => "log_feature_idx" )
    ActiveRecord::Schema.add_index( 'mole_logs', 
                                    ['mole_feature_id','created_at'], 
                                    :name   => "log_date_idx",
                                    :unique => true )
  end
end

#setupObject

Setup database connection prior to applying migrations



26
27
28
29
30
31
32
# File 'lib/mole/db/migrate.rb', line 26

def setup #:nodoc:
  require 'rubygems'
  gem "activerecord"
  require 'active_record'  
  db_config = YAML.load_file( File.expand_path( @config ) )[@env]
  ::ActiveRecord::Base.establish_connection(db_config)
end