Class: VanityMigration

Inherits:
ActiveRecord::Migration
  • Object
show all
Defined in:
lib/generators/templates/vanity_migration.rb

Instance Method Summary collapse

Instance Method Details

#connectionObject Also known as: default_connection

Helper methods to ensure we’re connecting to the right database, see github.com/assaf/vanity/issues/295.



5
6
7
# File 'lib/generators/templates/vanity_migration.rb', line 5

def connection
  @connection ||= ActiveRecord::Base.connection
end

#downObject



64
65
66
67
68
69
70
71
72
# File 'lib/generators/templates/vanity_migration.rb', line 64

def down
  with_vanity_connection do
    drop_table :vanity_metrics
    drop_table :vanity_metric_values
    drop_table :vanity_experiments
    drop_table :vanity_conversions
    drop_table :vanity_participants
  end
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/generators/templates/vanity_migration.rb', line 16

def up
  with_vanity_connection do
    create_table :vanity_metrics do |t|
      t.string :metric_id
      t.datetime :updated_at
    end
    add_index :vanity_metrics, [:metric_id]

    create_table :vanity_metric_values do |t|
      t.integer :vanity_metric_id
      t.integer :index
      t.integer :value
      t.string :date
    end
    add_index :vanity_metric_values, [:vanity_metric_id, :date]

    create_table :vanity_experiments do |t|
      t.string :experiment_id
      t.integer :outcome
      t.boolean :enabled
      t.datetime :created_at
      t.datetime :completed_at
    end
    add_index :vanity_experiments, [:experiment_id]

    create_table :vanity_conversions do |t|
      t.integer :vanity_experiment_id
      t.integer :alternative
      t.integer :conversions
    end
    add_index :vanity_conversions, [:vanity_experiment_id, :alternative], :name => "by_experiment_id_and_alternative"

    create_table :vanity_participants do |t|
      t.string :experiment_id
      t.string :identity
      t.integer :shown
      t.integer :seen
      t.integer :converted
      t.timestamps
    end
    add_index :vanity_participants, [:experiment_id]
    add_index :vanity_participants, [:experiment_id, :identity], :name => "by_experiment_id_and_identity"
    add_index :vanity_participants, [:experiment_id, :shown], :name => "by_experiment_id_and_shown"
    add_index :vanity_participants, [:experiment_id, :seen], :name => "by_experiment_id_and_seen"
    add_index :vanity_participants, [:experiment_id, :converted], :name => "by_experiment_id_and_converted"
  end
end

#with_vanity_connectionObject



10
11
12
13
14
# File 'lib/generators/templates/vanity_migration.rb', line 10

def with_vanity_connection
  @connection = Vanity::Adapters::ActiveRecordAdapter::VanityRecord.connection
  yield
  @connection = default_connection
end