This repository has been archived by the owner on Jan 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrations should use Vanity connection.
Fixes #295.
- Loading branch information
1 parent
136ebbc
commit e13e845
Showing
8 changed files
with
116 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,73 @@ | ||
class VanityMigration < ActiveRecord::Migration | ||
def self.up | ||
create_table :vanity_metrics do |t| | ||
t.string :metric_id | ||
t.datetime :updated_at | ||
end | ||
add_index :vanity_metrics, [:metric_id] | ||
# Helper methods to ensure we're connecting to the right database, see | ||
# https://github.com/assaf/vanity/issues/295. | ||
|
||
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] | ||
def connection | ||
@connection ||= ActiveRecord::Base.connection | ||
end | ||
alias_method :default_connection, :connection | ||
|
||
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 | ||
def with_vanity_connection | ||
@connection = Vanity::Adapters::ActiveRecordAdapter::VanityRecord.connection | ||
yield | ||
@connection = default_connection | ||
end | ||
|
||
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 | ||
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 | ||
|
||
def self.down | ||
drop_table :vanity_metrics | ||
drop_table :vanity_metric_values | ||
drop_table :vanity_experiments | ||
drop_table :vanity_conversions | ||
drop_table :vanity_participants | ||
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 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require "test_helper" | ||
|
||
describe Vanity::Adapters do | ||
describe ".establish_connection" do | ||
it "returns nil when not autoconnecting" do | ||
Vanity::Autoconnect.stubs(:should_connect?).returns(false) | ||
adapter = Vanity::Adapters.establish_connection({}) | ||
assert_nil adapter | ||
end | ||
|
||
it "returns nil using active record and migrating" do | ||
Rake.stubs(:application).returns(stub(:top_level_tasks => ['db:migrate'])) | ||
adapter = Vanity::Adapters.establish_connection(adapter: 'active_record') | ||
refute_nil adapter | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters