Иногда нужно создать очень большое количество тестовых записей, например инцидентов. И тут есть два способа: первый через Server Script мы можем создать 300 записей, пример кода:
let i = 0;
while (i < 300) {
let record = new SimpleRecord('itsm_incident');
record.impact = '1';
record.urgency = '1';
record.assigned_user = "100000000000000001";
record.subject = `${i} test task`;
record.service = '166895900708225490';
record.description = 'Super test';
const inserterTaskID = record.insert();
ss.info(`/record/task/${inserterTaskID}`);
i++;
}
ВНИМАНИЕ! Не рекомендовано самостоятельно вносить изменения в БД. Или если нам нужно создать действительно много, можно воспользоваться SQL скриптом. Но для этого нужно иметь доступ к БД. Пример кода для генерации трех миллионов инцидентов:
\c postgres_simpleone
create or replace function random_string(length integer) returns text as
$$
declare
chars text[] := '{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}';
result text := '';
i integer := 0;
begin
if length < 0 then
raise exception 'Given length cannot be less than 0';
end if;
for i in 1..length
loop
result := result || chars[1 + random() * (array_length(chars, 1) - 1)];
end loop;
return result;
end;
$$ language plpgsql;
create or replace function random_int(low integer, high integer) returns integer as
$$
begin
return floor(random() * (high - low + 1) + low);
end;
$$ language plpgsql;
INSERT INTO public.itsm_incident (sys_id, parent_id, assigned_user, number, short_description, active, comments,
description, state, priority, urgency, impact, opened_at, closed_at, due_date,
sla_due, sys_updated_by, sys_updated_at, sys_created_by, sys_created_at,
wf_executing_activity, contact, display_name, attention_required, closed_by, subject,
opened_by, caller, assignment_group, screenshot, company, additional_comments,
work_notes, service, sys_db_table_id, followers_list, approval_state,
state_changed_at, closure_code, service_satisfaction, agent_satisfaction,
contact_type, external_task, related_cis, related_articles, level_of_dependency,
closure_notes, copy_cis_to_originators, has_breached, complete_originators,
resolved_by, planned_end_datetime, resolved_at, actual_end_datetime,
actual_start_datetime, planned_start_datetime, related_incidents, related_problems,
solved_by_changes, caused_by_changes, related_inquiry, known_error,
first_call_resolution, out_of_sla, external_company, category, subcategory, knowledge,
reject_count, steps_to_reproduce, major_incident, infrastructure_incident, chronology,
master_incident, slave_incidents, resubmission, related_request)
SELECT gs.id,
null,
null,
concat('INC00', random_int(0, 3000000)),
null,
true,
null,
'',
'-2',
'1',
'1',
'1',
'2023-06-21 10:08:24',
null,
null,
null,
155931135900000001,
'2023-06-21 10:08:24',
155931135900000001,
'2023-06-21 10:08:24',
null,
null,
concat('INC ', random_int(0, 3000000)),
false,
null,
concat('sbj ', random_string(15)),
null,
155931135900000001,
168734206214184264,
null,
null,
null,
null,
168734208212172290,
156950677111866258,
'',
'not_requested',
'2023-06-21 10:08:24',
null,
null,
null,
'20',
'',
'',
'',
null,
'',
false,
false,
false,
null,
null,
null,
null,
null,
null,
null,
'',
'',
'',
null,
null,
false,
false,
null,
null,
null,
false,
0,
'',
false,
false,
'',
null,
'',
null,
null
from generate_series(0,3000000) as gs(id)
ON CONFLICT DO NOTHING;