Improved usage of fill db.

This commit is contained in:
Martin Edenhofer 2017-10-15 08:19:57 +02:00
parent 8894374afd
commit 21588220c4

View file

@ -5,15 +5,28 @@ module FillDB
fill your database with demo records fill your database with demo records
FillDB.load(agents, customers, groups, organizations, tickets) FillDB.load(
agents: 50,
customers: 1000,
groups: 20,
organizations: 40,
tickets: 100
)
e. g. or if you only want to create 100 tickets
FillDB.load(10, 100, 5, 40, 1000) FillDB.load(tickets: 100)
=end =end
def self.load( agents, customers, groups, organizations, tickets ) def self.load(params)
nice = params[:nice] || 0.5
agents = params[:agents] || 0
customers = params[:customers] || 0
groups = params[:groups] || 0
organizations = params[:organizations] || 0
tickets = params[:tickets] || 0
puts 'load db with:' puts 'load db with:'
puts " agents:#{agents}" puts " agents:#{agents}"
puts " customers:#{customers}" puts " customers:#{customers}"
@ -27,14 +40,11 @@ e. g.
# organizations # organizations
organization_pool = [] organization_pool = []
if organizations && !organizations.zero? if organizations && !organizations.zero?
ActiveRecord::Base.transaction do
(1..organizations).each do (1..organizations).each do
organization = Organization.create( name: 'FillOrganization::' + rand(999_999).to_s, active: true ) ActiveRecord::Base.transaction do
organization = Organization.create!(name: "FillOrganization::#{rand(999_999)}", active: true)
organization_pool.push organization organization_pool.push organization
end end
end end
else else
organization_pool = Organization.where(active: true) organization_pool = Organization.where(active: true)
@ -47,9 +57,8 @@ e. g.
roles = Role.where(name: [ 'Agent']) roles = Role.where(name: [ 'Agent'])
groups_all = Group.all groups_all = Group.all
ActiveRecord::Base.transaction do
(1..agents).each do (1..agents).each do
ActiveRecord::Base.transaction do
suffix = rand(99_999).to_s suffix = rand(99_999).to_s
user = User.create_or_update( user = User.create_or_update(
login: "filldb-agent-#{suffix}", login: "filldb-agent-#{suffix}",
@ -61,9 +70,9 @@ e. g.
roles: roles, roles: roles,
groups: groups_all, groups: groups_all,
) )
sleep nice
agent_pool.push user agent_pool.push user
end end
end end
else else
agent_pool = Role.where(name: 'Agent').first.users.where(active: true) agent_pool = Role.where(name: 'Agent').first.users.where(active: true)
@ -76,9 +85,8 @@ e. g.
roles = Role.where(name: [ 'Customer']) roles = Role.where(name: [ 'Customer'])
groups_all = Group.all groups_all = Group.all
ActiveRecord::Base.transaction do
(1..customers).each do (1..customers).each do
ActiveRecord::Base.transaction do
suffix = rand(99_999).to_s suffix = rand(99_999).to_s
organization = nil organization = nil
if !organization_pool.empty? && rand(2) == 1 if !organization_pool.empty? && rand(2) == 1
@ -94,9 +102,9 @@ e. g.
organization: organization, organization: organization,
roles: roles, roles: roles,
) )
sleep nice
customer_pool.push user customer_pool.push user
end end
end end
else else
customer_pool = Role.where(name: 'Customer').first.users.where(active: true) customer_pool = Role.where(name: 'Customer').first.users.where(active: true)
@ -106,21 +114,19 @@ e. g.
# create groups # create groups
group_pool = [] group_pool = []
if groups && !groups.zero? if groups && !groups.zero?
puts "1..#{groups}"
ActiveRecord::Base.transaction do
(1..groups).each do (1..groups).each do
group = Group.create( name: 'FillGroup::' + rand(999_999).to_s, active: true ) ActiveRecord::Base.transaction do
group = Group.create!(name: "FillGroup::#{rand(999_999)}", active: true)
group_pool.push group group_pool.push group
Role.where(name: 'Agent').first.users.where(active: true).each do |user| Role.where(name: 'Agent').first.users.where(active: true).each do |user|
user_groups = user.groups user_groups = user.groups
user_groups.push group user_groups.push group
user.groups = user_groups user.groups = user_groups
user.save user.save!
end end
sleep nice
end end
end end
else else
group_pool = Group.where(active: true) group_pool = Group.where(active: true)
@ -132,14 +138,12 @@ e. g.
state_pool = Ticket::State.all state_pool = Ticket::State.all
if tickets && !tickets.zero? if tickets && !tickets.zero?
ActiveRecord::Base.transaction do
(1..tickets).each do (1..tickets).each do
ActiveRecord::Base.transaction do
customer = customer_pool[ rand(customer_pool.length - 1) ] customer = customer_pool[ rand(customer_pool.length - 1) ]
agent = agent_pool[ rand(agent_pool.length - 1) ] agent = agent_pool[ rand(agent_pool.length - 1) ]
ticket = Ticket.create( ticket = Ticket.create!(
title: 'some title äöüß' + rand(999_999).to_s, title: "some title äöüß#{rand(999_999)}",
group: group_pool[ rand(group_pool.length - 1) ], group: group_pool[ rand(group_pool.length - 1) ],
customer: customer, customer: customer,
owner: agent, owner: agent,
@ -148,13 +152,14 @@ e. g.
updated_by_id: agent.id, updated_by_id: agent.id,
created_by_id: agent.id, created_by_id: agent.id,
) )
# create article # create article
article = Ticket::Article.create( article = Ticket::Article.create!(
ticket_id: ticket.id, ticket_id: ticket.id,
from: customer.email, from: customer.email,
to: 'some_recipient@example.com', to: 'some_recipient@example.com',
subject: 'some subject' + rand(999_999).to_s, subject: "some subject#{rand(999_999)}",
message_id: 'some@id-' + rand(999_999).to_s, message_id: "some@id-#{rand(999_999)}",
body: 'some message ...', body: 'some message ...',
internal: false, internal: false,
sender: Ticket::Article::Sender.where(name: 'Customer').first, sender: Ticket::Article::Sender.where(name: 'Customer').first,
@ -162,9 +167,10 @@ e. g.
updated_by_id: agent.id, updated_by_id: agent.id,
created_by_id: agent.id, created_by_id: agent.id,
) )
end puts " Ticket #{ticket.number} created"
end sleep nice
end
end
end end
end end
end end