Bulk creating users with rails etc

I want to add bulk usernames (to create initial dummy users to create posts).
So I was trying with creating a rake file, and loading it and then running it. But had no success. The rake file doesn’t even gets loaded sometimes, and when I use another method it gets loaded and then error thrown “I don’t know how to create …” error thrown by rails c (can’t even remember the error message as I tried thousand different methods over hours to make it work with no success)

What I did

Created users.rake file

cd /var/www/discourse  
mkdir -p lib/tasks  
touch lib/tasks/users.rake

Editing it with nano

cd /var/www/discourse/lib/tasks
sudo nano users.rake

And then pasted the following code into editor and saved

namespace :users do
  desc "Create users from a list"
  task :create, [:usernames, :password, :domain] => :environment do |task, args|
    usernames = args[:usernames].split(',')
    password = args[:password]
    domain = args[:domain]

    User.transaction do
      usernames.each do |username|
        next if User.exists?(username: username)

        email = "#{username}@#{domain}"
        user = User.new(username: username, email: email, password: password)

        unless user.save
          puts "Failed to save user #{username}: #{user.errors.full_messages.join(", ")}"
          raise ActiveRecord::Rollback
        end
      end
    end
  end
end

Loaded into rails c and loaded it into tasks

rails console
load 'lib/tasks/users.rake'
exit

Then tried to create the users

./launcher enter app
rake users:create["username1,username2", "password", "domain.com"]

Is there any other method that I can try through the terminal without using third party things.

Use rake dev:populate to seed with the out of the box test data - easy!

3 Likes

Thank you for your comment.

Your method will just fill the site with some random users and data, which I do not want. And I do not want a staged online site, I can just host it locally if I want.

This is for a production environment. Dummy users will actually have some meaningful topics. The purpose of this is not to present an empty site to the public.

So, kindly let me know of a solution related to the question I mentioned.
I just need to insert users (I have the name list for the users), into the main DB of discourse.

Thank you.

You can use this technique for local development installs too (that’s its primary purpose).

Why not read the code for that rake task and modify it how you see fit?

I recommend that you look at the import scripts in script/import_scripts and look for one that reads csv files.

I’ve been planning a script or plugin that will let your create stuff from a spreadsheet, but I’ve had other work in the way.

1 Like

I amended the code, but the production discourse in the docker container, I created rake file with touch and edited in nano, then entered the app with ./launcher enter app, then entered rails and checked rake -T and the rake file does not gets loaded. This is my primary concern. I can’t even test out the file in production.

However, in localhost, theres no issue, I just copy and pasted the file into the lib/tasks and it works fine. This docker and docker container is pain when you need to access things directly. And I can’t even find out why it doesn’t automatically gets loaded into the rake tasks list.

So, I’m still stuck in the production as the rake file does not automatically gets loaded, so I can’t run the command.

Your site won’t support me if I didn’t use the docker, or else I would have been more than happy to remove the docker and host the site directly. That would make life much more easier.

Normally rake tasks are tested in development and can be deployed as part of a plugin.

1 Like

Of you have it working locally you can backup there and restore to production.

Did you put your file in /var/www/discourse/lib/rake inside the container?

The issue is that when local backup is uploaded to the live site, it will replace all the contents I made in live site. I have a local instance, but someone in this forum suggested me to have the live site to do the development. So, I’m now developing in the live site (it’s not officially public yet though).

And I put the file in lib/tasks, local instance works, not the live instance. I’ll try the lib/rake as well.

1 Like

You shouldn’t develop on a live site.

You develop locally, package the changes in a Theme Component or plug-in and then deploy to the live site.

Here’s an example of a rake task in a plugin:

2 Likes

I think you shouldn’t develop rails or ember on a standard install, but I regularly to imports in a production container. Since it’s just a rake task you don’t even need to restart the server for changes to take effect.

Since it’s not public it should be safe to revert too a backup of something goes wrong. That’s probably where I would do it.

You could backup the live site and restore it to the dev site, do the work and then backup/restore back to the live site.

2 Likes