Change user creation date for multiple users?


(Michael Downey) #1

Is there any not-horribly-painful way to adjust multiple users’ “date of creation”? Let’s say I have a CSV or other data set of email address and date, for example.

Thanks to anyone who has an idea of how one might do with without too much effort!


(Kane York) #2

I don’t know if there is any existing method in the UI to do that? (just verified: there does not appear to be.)

Your best bet is probably to write it as an import script intended to modify an existing database.

That is: for each record, parse username and date, find user, update created_at, save.

Then you can either do a backup, modify, restore; or apply the migration on the live site (after taking a backup).


(Michael Downey) #3

Thanks. I’d kind of presumed it’d be necessary to work with the DB. If anyone here has done so in the past and would like to share such a script, would be a nice resource! :slight_smile:


(Jay Pfaffman) #4

At the rails console:

User.where("email LIKE '%gmail.com'").update_all(created_at: Time.zone.parse('2017-02-24'))
User.where("email = 'nobody@nowhere.com'").first.update(created_at: Time.zone.parse('2017-02-24'))

Converting the CSV into a line like the one above is left as an exercise for the reader. The first line will work for multiple addresses, and generates no error if there’s no match. The second line gives an error if the email address isn’t found.


(Jay Pfaffman) #5

Yeah, but this doesn’t work. At some point emails got moved into a separate table, so in the User record email is nil. Or, if the record was created before the database migration, the email address in the user record might just be wrong, I think.

Here’s what I was trying to do when I realized that what I told you might not work.

  def make_moderators
    UserEmail.where("email LIKE '%@somedomain.com'").each do |email|
      u = User.find_by_id(email.user_id)
      u.update(moderator: true)
    end
  end

Again you’ll be on your own to make it do what you want. :slight_smile:


Drupal 7 Migration Questions