Add rake task db:first_user.

Purges database and adds a user without development dependencies.
Closes http://bugs.joindiaspora.com/issues/548. Note error handling
for User.build...
This commit is contained in:
Alec Leamas 2010-12-02 00:23:38 +01:00
parent 1f789e5f2b
commit 1a8e0788c2
2 changed files with 101 additions and 0 deletions

77
db/seeds/first_user.rb Normal file
View file

@ -0,0 +1,77 @@
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
#
# Add a parameterized user to database.
#
#
require File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment')
require File.join(File.dirname(__FILE__), '..', '..','config', 'initializers', '_load_app_config.rb')
require 'yaml'
def read_password
begin
system('stty -echo')
while true do
printf 'Enter password: '
pw1 = $stdin.gets.chomp
puts
if pw1.length < 6
puts "Too short (minimum 6 characters)"
next
end
printf 'Again: '
pw2 = $stdin.gets.chomp
puts
break if pw1 == pw2
puts "They don't match, try again"
end
ensure
system('stty echo')
end
return pw1
end
username = ( ARGS[:username] == nil ? 'admin' : ARGS[:username].dup)
if ARGS[:email] == nil
config = YAML.load(File.read(Rails.root.join('config',
'app_config.yml')))
email = username + '@' + config['default']['pod_url']
else
email = ARGS[:email]
end
if ARGS[:password] == nil
password = read_password
else
password = ARGS[:password]
end
#printf "Building: %s, %s, '%s'\n", username, email, password
user = User.build( :email => email,
:username => username,
:password => password,
:password_confirmation => password,
:person => {
:profile => {
:first_name => username,
:last_name => "Unknown",
:image_url => "/images/user/default.png"
}
}
)
errors = user.errors
errors.delete :person
if errors.size > 0
raise "Error(s) creating user " + username + ": " + errors.to_s
end
user.save
user.person.save!
user.seed_aspects
puts "Created user " + username + ' (' + email + ')'

View file

@ -21,6 +21,12 @@ namespace :db do
create create
end end
task :first_user, :username, :password do |t, args|
puts "Setting up first user in #{Rails.env} database"
ARGS = args
require File.dirname(__FILE__) + '/../../db/seeds/first_user'
end
end end
desc 'Delete the collections in the current RAILS_ENV database' desc 'Delete the collections in the current RAILS_ENV database'
@ -53,6 +59,24 @@ namespace :db do
puts "you did it!" puts "you did it!"
end end
task :reset do
puts "making a new base user"
Rake::Task['db:purge'].invoke
Rake::Task['db:seed:dev'].invoke
puts "you did it!"
end
desc 'Purge database, add a new user'
task :first_user, :username, :password do |t, args|
puts "Purging database and adding a new user"
username = args[:username] || 'admin'
password = args[:password] if args[:password] != nil
Rake::Task['db:purge'].invoke
Rake::Task['db:seed:first_user'].invoke(username, password)
end
task :fix_diaspora_handle do task :fix_diaspora_handle do
puts "fixing the people in this seed" puts "fixing the people in this seed"
require File.dirname(__FILE__) + '/../../config/environment' require File.dirname(__FILE__) + '/../../config/environment'