diff --git a/lib/fill_db.rb b/lib/fill_db.rb index b36404278..87a4e62d0 100644 --- a/lib/fill_db.rb +++ b/lib/fill_db.rb @@ -5,15 +5,28 @@ module FillDB 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 - 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 " agents:#{agents}" puts " customers:#{customers}" @@ -27,14 +40,11 @@ e. g. # organizations organization_pool = [] if organizations && !organizations.zero? - - ActiveRecord::Base.transaction do - - (1..organizations).each do - organization = Organization.create( name: 'FillOrganization::' + rand(999_999).to_s, active: true ) + (1..organizations).each do + ActiveRecord::Base.transaction do + organization = Organization.create!(name: "FillOrganization::#{rand(999_999)}", active: true) organization_pool.push organization end - end else organization_pool = Organization.where(active: true) @@ -44,12 +54,11 @@ e. g. # create agents agent_pool = [] if agents && !agents.zero? - roles = Role.where( name: [ 'Agent'] ) + roles = Role.where(name: [ 'Agent']) 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 user = User.create_or_update( login: "filldb-agent-#{suffix}", @@ -61,9 +70,9 @@ e. g. roles: roles, groups: groups_all, ) + sleep nice agent_pool.push user end - end else agent_pool = Role.where(name: 'Agent').first.users.where(active: true) @@ -73,12 +82,11 @@ e. g. # create customer customer_pool = [] if customers && !customers.zero? - roles = Role.where( name: [ 'Customer'] ) + roles = Role.where(name: [ 'Customer']) 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 organization = nil if !organization_pool.empty? && rand(2) == 1 @@ -94,9 +102,9 @@ e. g. organization: organization, roles: roles, ) + sleep nice customer_pool.push user end - end else customer_pool = Role.where(name: 'Customer').first.users.where(active: true) @@ -106,21 +114,19 @@ e. g. # create groups group_pool = [] if groups && !groups.zero? - puts "1..#{groups}" - ActiveRecord::Base.transaction do - - (1..groups).each do - group = Group.create( name: 'FillGroup::' + rand(999_999).to_s, active: true ) + (1..groups).each do + ActiveRecord::Base.transaction do + group = Group.create!(name: "FillGroup::#{rand(999_999)}", active: true) group_pool.push group Role.where(name: 'Agent').first.users.where(active: true).each do |user| user_groups = user.groups user_groups.push group user.groups = user_groups - user.save + user.save! end + sleep nice end - end else group_pool = Group.where(active: true) @@ -132,14 +138,12 @@ e. g. state_pool = Ticket::State.all 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) ] agent = agent_pool[ rand(agent_pool.length - 1) ] - ticket = Ticket.create( - title: 'some title äöüß' + rand(999_999).to_s, + ticket = Ticket.create!( + title: "some title äöüß#{rand(999_999)}", group: group_pool[ rand(group_pool.length - 1) ], customer: customer, owner: agent, @@ -148,13 +152,14 @@ e. g. updated_by_id: agent.id, created_by_id: agent.id, ) + # create article - article = Ticket::Article.create( + article = Ticket::Article.create!( ticket_id: ticket.id, from: customer.email, to: 'some_recipient@example.com', - subject: 'some subject' + rand(999_999).to_s, - message_id: 'some@id-' + rand(999_999).to_s, + subject: "some subject#{rand(999_999)}", + message_id: "some@id-#{rand(999_999)}", body: 'some message ...', internal: false, sender: Ticket::Article::Sender.where(name: 'Customer').first, @@ -162,9 +167,10 @@ e. g. updated_by_id: agent.id, created_by_id: agent.id, ) + puts " Ticket #{ticket.number} created" + sleep nice end end - end end end