assaf / vanity

Experiment Driven Development for Ruby
http://vanity.labnotes.org
MIT License
1.55k stars 269 forks source link

Getting uninitialized constant Vanity::Adapters::ActiveRecordAdapter in Rails 5 #321

Closed alexvbush closed 6 years ago

alexvbush commented 7 years ago

Hi, I'm trying to setup Vanity with a brand new Rails 5 app with Postgres. I've added gem "vanity" to my Gemfile, launched bundle exec rails generate vanity which created the following migration file:

class VanityMigration < ActiveRecord::Migration
  # Helper methods to ensure we're connecting to the right database, see
  # https://github.com/assaf/vanity/issues/295.

  def connection
    @connection ||= ActiveRecord::Base.connection
  end
  alias_method :default_connection, :connection

  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 null: false
      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

  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

But when I next run be rails db:migrate it throws the following error:

/Users/alex/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:51: warning: constant ::Fixnum is deprecated
/Users/alex/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:52: warning: constant ::Bignum is deprecated
/Users/alex/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/core_ext/numeric/conversions.rb:138: warning: constant ::Fixnum is deprecated
== 20170202042511 VanityMigration: migrating ==================================
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

uninitialized constant Vanity::Adapters::ActiveRecordAdapter
/Users/alex/workspace/ios-interview-guide-website/db/migrate/20170202042511_vanity_migration.rb:11:in `with_vanity_connection'
/Users/alex/workspace/ios-interview-guide-website/db/migrate/20170202042511_vanity_migration.rb:17:in `up'
/Users/alex/workspace/ios-interview-guide-website/bin/rails:9:in `require'
/Users/alex/workspace/ios-interview-guide-website/bin/rails:9:in `<top (required)>'
/Users/alex/workspace/ios-interview-guide-website/bin/spring:15:in `require'
/Users/alex/workspace/ios-interview-guide-website/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
NameError: uninitialized constant Vanity::Adapters::ActiveRecordAdapter
/Users/alex/workspace/ios-interview-guide-website/db/migrate/20170202042511_vanity_migration.rb:11:in `with_vanity_connection'
/Users/alex/workspace/ios-interview-guide-website/db/migrate/20170202042511_vanity_migration.rb:17:in `up'
/Users/alex/workspace/ios-interview-guide-website/bin/rails:9:in `require'
/Users/alex/workspace/ios-interview-guide-website/bin/rails:9:in `<top (required)>'
/Users/alex/workspace/ios-interview-guide-website/bin/spring:15:in `require'
/Users/alex/workspace/ios-interview-guide-website/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Seems like Vanity::Adapters::ActiveRecordAdapter isn't loaded, although, when I run rails consolse I am able to call Vanity::Adapters::ActiveRecordAdapter. Can anyone suggest a solution?

alexvbush commented 7 years ago

my config/vanity.yml looks like this

default: &default
#adapter: postgresql
  adapter: active_record
  active_record_adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: ios-interview-guide-website_development
  username: ios-interview-guide-website

test:
  <<: *default
  database: ios-interview-guide-website_test
  username: ios-interview-guide-website
  collecting: false

production:
  <<: *default
  database: ios-interview-guide-website_production
  username: ios-interview-guide-website
  password: <%= ENV['IOS-INTERVIEW-GUIDE-WEBSITE_DATABASE_PASSWORD'] %>
phillbaker commented 7 years ago

Hey, sorry for the slow response on this. As a workaround, does adding require Vanity::Adapters::ActiveRecordAdapter at the top of the file by hand fix the problem?

alexvbush commented 7 years ago

Hey, I'll get to test it in a few days On Tue, Feb 7, 2017 at 6:40 PM Phillip Baker notifications@github.com wrote:

Hey, sorry for the slow response on this. As a workaround, does adding require Vanity::Adapters::ActiveRecordAdapter at the top of the file by hand fix the problem?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/assaf/vanity/issues/321#issuecomment-278212688, or mute the thread https://github.com/notifications/unsubscribe-auth/AAS_Jew-m8qPBhu7-wSzN0Y6CdHwuhoDks5raSswgaJpZM4L0tjS .

shanearrowsmith commented 7 years ago

anyone any luck with this?

Same issue, loading fine in console, breaking migration with no including or requiring fixing it.

borama commented 7 years ago

Using require "vanity/adapters/active_record_adapter" at the top of the migration seems to fix this issue for me.