0

I have a seed file to create the 'root/sysadmin' user. It works locally and for other devs locally.

But when we run it on heroku, it fails:

cat seed_sa.rb | heroku run rails console --no-tty

This is what I see in the log:

sa = User.new(
  email: 'mickey@mouse.com', 
  nickname: 'sysadmin',
  name: 'Mickey Mouse',
  password: ‘testing2020!’,
  is_onboarded: true,
  role: User.roles[:admin],
  confirmed_at: DateTime.now,
  retailer_id: @retailer.id
)
D, [2020-11-03T13:51:41.529625 #4] DEBUG -- :   Retailer Load (1.2ms)  SELECT  "retailers".* FROM "retailers" WHERE "retailers"."id" = $1 LIMIT $2  [["id", "4d696e0f-7c63-44e1-9521-d75da2debd7e"], ["LIMIT", 1]]

#<User id: nil, retailer_id: "4d696e0f-7c63-44e1-9521-d75da2debd7e", provider: "email", uid: "", allow_password_change: false, name: "Mickey Mouse", nickname: "sysadmin", image: nil, email: "mickey@mouse.com", created_at: nil, updated_at: nil, role: "admin", is_onboarded: true>

sa.save
D, [2020-11-03T13:51:41.532197 #4] DEBUG -- :    (1.0ms)  BEGIN
D, [2020-11-03T13:51:41.541785 #4] DEBUG -- :   User Exists (1.2ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "mickey@mouse.com"], ["LIMIT", 1]]
D, [2020-11-03T13:51:41.543825 #4] DEBUG -- :   User Exists (1.2ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 AND "users"."provider" = $2 LIMIT $3  [["email", "mickey@mouse.com"], ["provider", "email"], ["LIMIT", 1]]
D, [2020-11-03T13:51:41.545052 #4] DEBUG -- :    (1.0ms)  ROLLBACK
false

I have nuked the database, recreated it (running all migrations), restarted Heroku. But it always fails creating that user....but not locally?!

This is the user model:

class User < ActiveRecord::Base
  # Relationships
  belongs_to :retailer
  has_many :customers, foreign_key: :created_by
  
  enum role: {admin: 0, owner: 1, user: 2}

  # Validations
  validates_presence_of :password, on: :create
  validates_uniqueness_of :email, allow_nil: false

  # Hooks
  after_initialize :create_retailer, if: :new_record?
  
  # Include default devise modules. Others available are:
  # :lockable, :timeoutable, :omniauthable
  devise :invitable, :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable
  include DeviseTokenAuth::Concerns::User

  def token_validation_response
    UserSerializer.new(self, root: false).as_json(include: '**')
  end

  def as_json(options={})
    # see https://stackoverflow.com/questions/17730121/include-associated-model-when-rendering-json-in-rails
    super(include: [:retailer])
  end

  def inviter_name
    inviter = User.find(self.invited_by_id)
    inviter.name || "Someone"
  end

  def reassign_proposals_to(user)
    proposals = Proposal.where('created_by = ?', self.id)
    proposals.each do |proposal|
      proposal.created_by = user.id
      proposal.updated_by = '(deleted user)'
      proposal.save
    end
  end

  private
  def create_retailer
    # When a new user is created (via registration) we auto-create their account
    # using ||= to ensure users created other ways (eg. seeding, invitation) do not also
    # result in a new account being created
    self.retailer ||= Retailer.new(name: 'New Account')
    self.role ||= User.roles[:owner]
  end

rmcsharry
  • 5,363
  • 6
  • 65
  • 108
  • It is saying the user exists. Have you queried the DB to see if that user is in there before running this? Maybe there is something wrong with the seed code that it is trying to reuse that email? ether way the error is pretty clear that the user already exists, can you verify that it indeed does not before running this? – Rockwell Rice Nov 03 '20 at 14:29
  • I have verified that it does not exist, by entering the Rails console and doing User.count -> returns 0 – rmcsharry Nov 03 '20 at 14:38
  • If you use `sa.save!` you might get more information on what exactly is happening, but that error seems to think otherwise. Is there anything being created above this in the seed file that runs previously? – Rockwell Rice Nov 03 '20 at 14:38
  • You're a life saver @RockwellRice, that did it. There was a validation method that I removed from the code but forgot to deploy that new code to Heroku! GRRR – rmcsharry Nov 03 '20 at 14:40
  • @RockwellRice Post an answer suggesting the `sa.save!` and I'll edit it to include the details and mark it as answer. – rmcsharry Nov 03 '20 at 14:41
  • 1
    Thats ok, you can just go ahead and put in the answer. Glade you got it figured out – Rockwell Rice Nov 03 '20 at 14:42

1 Answers1

0

Thanks to the comments from @Rockwell Rice, I used sa.save! to see what else was going on.

There was a validation error on a custom method that checked for password complexity. This method had been changed locally but NOT deployed to production, hence why saving the user was failing.

DOH!

rmcsharry
  • 5,363
  • 6
  • 65
  • 108