One time Fix Script to create relationship

Bijay Kumar Sha
Giga Guru

Hi,

I wanted to write a one time fix script to create relationship between cmdb_ci_ec2_instance table and corresponding cmdb_ci_aws_account ci (this is a reference field in the form of ec2 instance record) if the install status of ec2 instance record is 'Installed''. I've prepared a fix script as below -

 

var ec2 = new GlideRecord('cmdb_ci_ec2_instance');
ec2.addEncodedQuery('install_status=1');
ec2.query();
while (ec2.next()){
    var ciRelationship_add = new GlideRecord('cmdb_rel_ci');
    ciRelationship_add.initialize();
    ciRelationship_add.child = ec2.sys_id;
    ciRelationship_add.parent = ec2.u_aws_account_id;   //reference field for cmdb_ci_aws_account
    ciRelationship_add.type = 'bf83653c0ab30150761028c73a4de0f4';  //Relationship Type = Managed by::Manages
    ciRelationship_add.insert();
}
 
While running this script, it's keep running and running. I don't know if there is any impact in performance for this.
 
Can anyone please let me know what might be the issue or what modification do i need to do?
5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

How many installed ec2 instance records do you have?  When running the script do you suddenly have many new relationship records?  If so, are they correct or the same parent/child?

Hi @Brad Bowman  ,

Total I've 2.5 lac of records are there in cmdb_ci_ec2_instance table and out of which around 7k are having install status = installed.

Are you seeing the start of 7K relationship records get created when you attempted to run this, or what happened while it was running for awhile? You can add a line to test prior to the ec2.query() if you are uncertain:

ec2.setLimit(10);

There are probably Business Rules that are running with each insert, and you probably want to allow those to run.  This will likely cause a performance hit with 7K insertions, so best to schedule it during off hours.

Community Alums
Not applicable

Hi @Bijay Kumar Sha,

 

When you run this script, is it creating the relationships as you want? and It's running continuously because your 'cmdb_ci_ec2_instance' table has many records with 'install_status=1'. When the condition is no longer satisfied, it will automatically stop running. Your code is very simple, so I don't see any issues with it, and it doesn't impact performance either.