Added doc, improved performance through ActiveRecord::Base.transaction.

This commit is contained in:
Martin Edenhofer 2015-08-07 11:24:47 +02:00
parent 506e2f1e04
commit e1dc0b1707

View file

@ -1,5 +1,18 @@
# rubocop:disable Rails/Output
module FillDB
=begin
fill your database with demo records
FillDB.load(agents, customers, groups, organizations, tickets)
e. g.
FillDB.load(10, 100, 5, 40, 1000)
=end
def self.load( agents, customers, groups, organizations, tickets )
puts 'load db with:'
puts " agents:#{agents}"
@ -14,10 +27,15 @@ module FillDB
# organizations
organization_pool = []
if organizations && !organizations.zero?
ActiveRecord::Base.transaction do
(1..organizations).each {
organization = Organization.create( name: 'FillOrganization::' + rand(999_999).to_s, active: true )
organization_pool.push organization
}
end
else
organization_pool = Organization.where(active: true)
end
@ -27,6 +45,9 @@ module FillDB
if agents && !agents.zero?
roles = Role.where( name: [ 'Agent'] )
groups_all = Group.all
ActiveRecord::Base.transaction do
(1..agents).each {
suffix = rand(99_999).to_s
user = User.create_or_update(
@ -41,6 +62,8 @@ module FillDB
)
agent_pool.push user
}
end
else
agent_pool = Role.where(name: 'Agent').first.users.where(active: true)
puts " take #{agent_pool.length} agents"
@ -51,6 +74,9 @@ module FillDB
if customers && !customers.zero?
roles = Role.where( name: [ 'Customer'] )
groups_all = Group.all
ActiveRecord::Base.transaction do
(1..customers).each {
suffix = rand(99_999).to_s
organization = nil
@ -69,6 +95,8 @@ module FillDB
)
customer_pool.push user
}
end
else
customer_pool = Role.where(name: 'Customer').first.users.where(active: true)
end
@ -77,6 +105,9 @@ module FillDB
group_pool = []
if groups && !groups.zero?
puts "1..#{groups}"
ActiveRecord::Base.transaction do
(1..groups).each {
group = Group.create( name: 'FillGroup::' + rand(999_999).to_s, active: true )
group_pool.push group
@ -87,6 +118,8 @@ module FillDB
user.save
}
}
end
else
group_pool = Group.where(active: true)
end
@ -94,7 +127,11 @@ module FillDB
# create tickets
priority_pool = Ticket::Priority.all
state_pool = Ticket::State.all
if tickets && !tickets.zero?
ActiveRecord::Base.transaction do
(1..tickets).each {
customer = customer_pool[ rand(customer_pool.length - 1) ]
agent = agent_pool[ rand(agent_pool.length - 1) ]
@ -124,5 +161,7 @@ module FillDB
)
}
end
end
end
end