scinote-web/lib/tasks/db_users.rake

133 lines
3.8 KiB
Ruby
Raw Normal View History

2016-02-12 23:52:43 +08:00
require "#{Rails.root}/app/utilities/users_generator"
include UsersGenerator
namespace :db do
desc "Load users into database from the provided YAML file"
task :load_users, [ :file_path, :create_teams ] => :environment do |task, args|
2021-07-23 17:56:28 +08:00
if args.blank? || args[:file_path].blank?
2016-02-12 23:52:43 +08:00
puts "No file provided"
return
end
create_teams = false
if args[:create_teams].present? and args[:create_teams].downcase == "true"
create_teams = true
2016-02-12 23:52:43 +08:00
end
# Parse file
yaml = YAML.load(File.read("#{args[:file_path]}"))
begin
ActiveRecord::Base.transaction do
# Parse user & team hashes from YAML
2017-01-31 20:33:55 +08:00
teams = yaml.select { |k, v| /team_[0-9]+/ =~ k }
users = yaml.select { |k, v| /user_[0-9]+/ =~ k }
2016-02-12 23:52:43 +08:00
# Create teams
teams.each do |k, team_hash|
2017-01-31 20:33:55 +08:00
team = Team.order(created_at: :desc)
.where(name: team_hash['name'])
.first
team = Team.create(name: team_hash['name'][0..99]) if team.blank?
team_hash['id'] = team.id
2016-02-12 23:52:43 +08:00
end
# Create users
2017-01-31 20:33:55 +08:00
puts 'Created users'
2016-02-12 23:52:43 +08:00
users.each do |k, user_hash|
2017-01-31 20:33:55 +08:00
password = user_hash['password']
password = generate_user_password if password.blank?
2016-02-12 23:52:43 +08:00
2017-01-31 20:33:55 +08:00
user_teams = user_hash['teams']
user_teams = '' if user_teams.blank?
2016-02-12 23:52:43 +08:00
team_ids =
user_teams
2017-01-31 20:33:55 +08:00
.split(',')
.collect(&:strip)
2016-02-12 23:52:43 +08:00
.uniq
2017-01-31 20:33:55 +08:00
.select { |o| teams.include? o }
.collect { |o| teams[o]['id'] }
2016-02-12 23:52:43 +08:00
user = create_user(
2017-01-31 20:33:55 +08:00
user_hash['full_name'],
user_hash['email'],
2016-02-12 23:52:43 +08:00
password,
true,
create_teams ? Constants::DEFAULT_PRIVATE_TEAM_NAME : nil,
team_ids
2016-02-12 23:52:43 +08:00
)
2017-01-31 20:33:55 +08:00
if user.id.present?
puts ''
2016-02-12 23:52:43 +08:00
print_user(user, password)
end
end
2017-01-31 20:33:55 +08:00
puts ''
2016-02-12 23:52:43 +08:00
end
rescue ActiveRecord::ActiveRecordError, ArgumentError
2017-01-31 20:33:55 +08:00
puts 'Error creating all users, transaction reverted'
2016-02-12 23:52:43 +08:00
end
end
2017-01-31 20:33:55 +08:00
desc 'Add a single user to the database'
2016-02-12 23:52:43 +08:00
task :add_user => :environment do
2017-01-31 20:33:55 +08:00
puts 'Type in user\'s full name (e.g. \'Steve Johnson\')'
2016-02-12 23:52:43 +08:00
full_name = $stdin.gets.to_s.strip
2017-01-31 20:33:55 +08:00
puts 'Type in user\'s email (e.g. \'steve.johnson@gmail.com\')'
2016-02-12 23:52:43 +08:00
email = $stdin.gets.to_s.strip
2017-01-31 20:33:55 +08:00
puts 'Type in user\'s password (e.g. \'password\'), or ' \
'leave blank to let Rails generate password'
2016-02-12 23:52:43 +08:00
password = $stdin.gets.to_s.strip
2021-07-23 17:56:28 +08:00
if password.blank?
2016-02-12 23:52:43 +08:00
password = generate_user_password
end
2017-01-31 20:33:55 +08:00
puts 'Do you want Rails to create default user\'s team? (T/F)'
create_team = $stdin.gets.to_s.strip == 'T'
puts 'Type names of any additional teams you want the user ' \
'to be admin of (delimited with \',\'), or leave blank'
team_names = $stdin.gets.to_s.strip
2021-07-23 17:56:28 +08:00
if team_names.blank?
team_names = []
2016-02-12 23:52:43 +08:00
else
2017-01-31 20:33:55 +08:00
team_names = team_names.split(',').collect(&:strip)
2016-02-12 23:52:43 +08:00
end
begin
ActiveRecord::Base.transaction do
# Add/fetch teams if needed
team_ids = []
team_names.each do |team_name|
team = Team.order(created_at: :desc).where(name: team_name).first
team = Team.create(name: team_name[0..99]) if team.blank?
2016-02-12 23:52:43 +08:00
team_ids << team.id
2016-02-12 23:52:43 +08:00
end
user = create_user(
full_name,
email,
password,
true,
2017-03-09 22:37:15 +08:00
create_team ? Constants::DEFAULT_PRIVATE_TEAM_NAME : nil,
team_ids
2016-02-12 23:52:43 +08:00
)
if user.id.present? then
puts "Successfully created user"
puts ""
print_user(user, password)
else
puts "Error creating new user"
end
puts ""
end
2016-07-21 19:11:15 +08:00
rescue ActiveRecord::ActiveRecordError, ArgumentError, ActiveRecord::RecordNotSaved => e
puts "Error creating user, transaction reverted: #{$!}"
2016-02-12 23:52:43 +08:00
end
end
end