| 33 | 
           rodolico | 
           1 | 
           /*
  | 
        
        
            | 
            | 
           2 | 
              copy camp data to camp2 database
  | 
        
        
            | 
            | 
           3 | 
              Note: we have removed_date set to null so we don't copy anything
  | 
        
        
            | 
            | 
           4 | 
              that is old
  | 
        
        
            | 
            | 
           5 | 
           */
  | 
        
        
            | 
            | 
           6 | 
              | 
        
        
            | 
            | 
           7 | 
           /*
  | 
        
        
            | 
            | 
           8 | 
              | 
        
        
           | 47 | 
           rodolico | 
           9 | 
           Monster table of owner, location, device and device_type
  | 
        
        
           | 33 | 
           rodolico | 
           10 | 
              | 
        
        
            | 
            | 
           11 | 
           */
  | 
        
        
            | 
            | 
           12 | 
              | 
        
        
            | 
            | 
           13 | 
           drop table if exists temp;
  | 
        
        
            | 
            | 
           14 | 
           create table temp as 
  | 
        
        
            | 
            | 
           15 | 
           select 
  | 
        
        
            | 
            | 
           16 | 
              camp.device.device_id old_device_id,
  | 
        
        
            | 
            | 
           17 | 
              camp.device.name device_name,
  | 
        
        
            | 
            | 
           18 | 
              camp.device.serial device_serial,
  | 
        
        
            | 
            | 
           19 | 
              camp.device.part_of,
  | 
        
        
            | 
            | 
           20 | 
              date( camp.device.added_date ) device_added,
  | 
        
        
            | 
            | 
           21 | 
              date( camp.device.removed_date ) device_removed,
  | 
        
        
            | 
            | 
           22 | 
              camp.device_type.name device_type,
  | 
        
        
            | 
            | 
           23 | 
              if( camp.device_type.show_as_system = 'Y', true, false ) show_as_system,
  | 
        
        
            | 
            | 
           24 | 
              camp.site.site_id old_site_id,
  | 
        
        
            | 
            | 
           25 | 
              camp.site.name site_name,
  | 
        
        
            | 
            | 
           26 | 
              camp.client.name site_owner,
  | 
        
        
            | 
            | 
           27 | 
              date( camp.site.added_date ) site_added,
  | 
        
        
            | 
            | 
           28 | 
              date( camp.site.removed_date ) site_removed,
  | 
        
        
            | 
            | 
           29 | 
              camp.client.client_id old_client_id,
  | 
        
        
            | 
            | 
           30 | 
              camp.client.name client_name,
  | 
        
        
            | 
            | 
           31 | 
              date(camp.client.added_date) client_added,
  | 
        
        
            | 
            | 
           32 | 
              date( camp.client.removed_date ) client_removed
  | 
        
        
            | 
            | 
           33 | 
           from
  | 
        
        
            | 
            | 
           34 | 
              camp.device
  | 
        
        
            | 
            | 
           35 | 
              join camp.site using (site_id)
  | 
        
        
            | 
            | 
           36 | 
              join camp.client using (client_id)
  | 
        
        
            | 
            | 
           37 | 
              join camp.device_type using (device_type_id)
  | 
        
        
            | 
            | 
           38 | 
           where
  | 
        
        
            | 
            | 
           39 | 
              device_type.show_as_system = 'Y'
  | 
        
        
           | 53 | 
           rodolico | 
           40 | 
           /*   and device.removed_date is null
  | 
        
        
           | 33 | 
           rodolico | 
           41 | 
              and site.removed_date is null
  | 
        
        
            | 
            | 
           42 | 
              and client.removed_date is null
  | 
        
        
           | 53 | 
           rodolico | 
           43 | 
           */
  | 
        
        
           | 33 | 
           rodolico | 
           44 | 
           ;
  | 
        
        
            | 
            | 
           45 | 
              | 
        
        
           | 40 | 
           rodolico | 
           46 | 
              | 
        
        
           | 33 | 
           rodolico | 
           47 | 
           /*
  | 
        
        
           | 40 | 
           rodolico | 
           48 | 
             uuid and serial number are stored A) in the table and B) in the attrib_device table.
  | 
        
        
            | 
            | 
           49 | 
             We are doing a kludge. If the value is 36 characters long, we assume it is a UUID. If 
  | 
        
        
            | 
            | 
           50 | 
             it is not, we assume it is a serial number
  | 
        
        
            | 
            | 
           51 | 
           */
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
           | 48 | 
           rodolico | 
           53 | 
           alter table temp add device_uuid varchar(36);
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
           | 40 | 
           rodolico | 
           55 | 
           update temp a join camp.attrib_device b on (a.old_device_id = b.device_id and b.attrib_id = 23 )
  | 
        
        
            | 
            | 
           56 | 
             set
  | 
        
        
            | 
            | 
           57 | 
                a.device_uuid = b.value
  | 
        
        
            | 
            | 
           58 | 
              where a.device_uuid is null;
  | 
        
        
            | 
            | 
           59 | 
              | 
        
        
            | 
            | 
           60 | 
           update temp a join camp.attrib_device b on (a.old_device_id = b.device_id and b.attrib_id = 3 )
  | 
        
        
            | 
            | 
           61 | 
             set
  | 
        
        
            | 
            | 
           62 | 
                a.device_serial = b.value
  | 
        
        
            | 
            | 
           63 | 
              where a.device_serial is null
  | 
        
        
            | 
            | 
           64 | 
                 and length( b.value ) <> 36;
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
           update temp a join camp.attrib_device b on (a.old_device_id = b.device_id and b.attrib_id = 3 )
  | 
        
        
            | 
            | 
           67 | 
             set
  | 
        
        
            | 
            | 
           68 | 
                a.device_uuid = b.value
  | 
        
        
            | 
            | 
           69 | 
              where a.device_uuid is null
  | 
        
        
            | 
            | 
           70 | 
                 and length( b.value ) = 36;
  | 
        
        
           | 48 | 
           rodolico | 
           71 | 
              | 
        
        
            | 
            | 
           72 | 
           update temp set device_uuid = device_serial where length( device_serial ) = 36 and device_uuid is null;
  | 
        
        
            | 
            | 
           73 | 
           update temp set device_serial = null where lower( device_serial ) = lower( device_uuid);
  | 
        
        
           | 40 | 
           rodolico | 
           74 | 
              | 
        
        
            | 
            | 
           75 | 
           update temp set device_uuid = trim(device_uuid);
  | 
        
        
            | 
            | 
           76 | 
           update temp set device_serial = trim(device_serial);
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
           update temp set device_uuid = null where length( device_uuid ) = 0;
  | 
        
        
            | 
            | 
           79 | 
           update temp set device_serial = null where length( device_serial ) = 0;
  | 
        
        
            | 
            | 
           80 | 
              | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
           /*
  | 
        
        
           | 47 | 
           rodolico | 
           83 | 
              in the old system, there was no deliniation between location and client, so we created new 'locations' with 'NOC' at the end
  | 
        
        
           | 33 | 
           rodolico | 
           84 | 
              so we could have ownership and locations. So, fivestar equipment located at the NOC would have been put at the fictitious
  | 
        
        
           | 47 | 
           rodolico | 
           85 | 
              location 'fivestar NOC'. Modified original data so every NOC location has NOC in the name.
  | 
        
        
            | 
            | 
           86 | 
              Following query puts them all in 'Colocation NOC' (Daily Data's location) without losing ownership information
  | 
        
        
           | 33 | 
           rodolico | 
           87 | 
           */
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
           update temp a
  | 
        
        
            | 
            | 
           90 | 
              join temp b
  | 
        
        
            | 
            | 
           91 | 
           set
  | 
        
        
            | 
            | 
           92 | 
              a.old_site_id = b.old_site_id,
  | 
        
        
            | 
            | 
           93 | 
              a.site_name = b.site_name,
  | 
        
        
            | 
            | 
           94 | 
              a.site_added = b.site_added,
  | 
        
        
            | 
            | 
           95 | 
              a.site_owner = b.site_owner
  | 
        
        
            | 
            | 
           96 | 
           where
  | 
        
        
            | 
            | 
           97 | 
              b.site_name = 'Colocation NOC'
  | 
        
        
            | 
            | 
           98 | 
              and a.site_name like '%NOC%' and a.site_name <> 'Colocation NOC';
  | 
        
        
            | 
            | 
           99 | 
              | 
        
        
            | 
            | 
           100 | 
              | 
        
        
            | 
            | 
           101 | 
           /* we have some stuff at Lori Bryant's office also */
  | 
        
        
            | 
            | 
           102 | 
           update temp a
  | 
        
        
            | 
            | 
           103 | 
              join temp b
  | 
        
        
            | 
            | 
           104 | 
           set
  | 
        
        
            | 
            | 
           105 | 
              a.old_site_id = b.old_site_id,
  | 
        
        
            | 
            | 
           106 | 
              a.site_name = b.site_name,
  | 
        
        
            | 
            | 
           107 | 
              a.site_added = b.site_added,
  | 
        
        
            | 
            | 
           108 | 
              a.site_owner = b.site_owner
  | 
        
        
            | 
            | 
           109 | 
           where
  | 
        
        
            | 
            | 
           110 | 
              b.site_name = 'Stemmons Towers'
  | 
        
        
            | 
            | 
           111 | 
              and a.site_name like 'Lori Bryant''s Office';
  | 
        
        
            | 
            | 
           112 | 
              | 
        
        
            | 
            | 
           113 | 
           /* and Lakewood Title */
  | 
        
        
            | 
            | 
           114 | 
           update temp a
  | 
        
        
            | 
            | 
           115 | 
              join temp b
  | 
        
        
            | 
            | 
           116 | 
           set
  | 
        
        
            | 
            | 
           117 | 
              a.old_site_id = b.old_site_id,
  | 
        
        
            | 
            | 
           118 | 
              a.site_name = b.site_name,
  | 
        
        
            | 
            | 
           119 | 
              a.site_added = b.site_added,
  | 
        
        
            | 
            | 
           120 | 
              a.site_owner = b.site_owner
  | 
        
        
            | 
            | 
           121 | 
           where
  | 
        
        
            | 
            | 
           122 | 
              b.site_name = 'Matilda and Prospect'
  | 
        
        
            | 
            | 
           123 | 
              and a.site_name like 'Lakewood Title Office';
  | 
        
        
            | 
            | 
           124 | 
              | 
        
        
           | 47 | 
           rodolico | 
           125 | 
           /* populate owner table */
  | 
        
        
            | 
            | 
           126 | 
           truncate table owner;
  | 
        
        
            | 
            | 
           127 | 
           insert into owner( name,created, removed)
  | 
        
        
           | 33 | 
           rodolico | 
           128 | 
              select distinct
  | 
        
        
            | 
            | 
           129 | 
                 client_name,
  | 
        
        
            | 
            | 
           130 | 
                 client_added,
  | 
        
        
            | 
            | 
           131 | 
                 client_removed
  | 
        
        
            | 
            | 
           132 | 
              from temp;
  | 
        
        
            | 
            | 
           133 | 
              | 
        
        
           | 47 | 
           rodolico | 
           134 | 
           /* populate location */
  | 
        
        
            | 
            | 
           135 | 
           truncate table location;
  | 
        
        
            | 
            | 
           136 | 
           insert into location( name,created,removed) 
  | 
        
        
           | 33 | 
           rodolico | 
           137 | 
              select distinct 
  | 
        
        
            | 
            | 
           138 | 
                 site_name,
  | 
        
        
            | 
            | 
           139 | 
                 site_added,
  | 
        
        
            | 
            | 
           140 | 
                 site_removed
  | 
        
        
            | 
            | 
           141 | 
              from 
  | 
        
        
            | 
            | 
           142 | 
                 temp;
  | 
        
        
            | 
            | 
           143 | 
              | 
        
        
            | 
            | 
           144 | 
           /* get device_type directly from camp, but ignore anything not set as show_as_system */
  | 
        
        
            | 
            | 
           145 | 
           truncate table device_type;
  | 
        
        
           | 47 | 
           rodolico | 
           146 | 
           insert into device_type (name,is_system, created, removed)
  | 
        
        
           | 33 | 
           rodolico | 
           147 | 
           select 
  | 
        
        
            | 
            | 
           148 | 
              name,
  | 
        
        
            | 
            | 
           149 | 
              true,
  | 
        
        
            | 
            | 
           150 | 
              date( added_date ),
  | 
        
        
            | 
            | 
           151 | 
              date( removed_date )
  | 
        
        
            | 
            | 
           152 | 
           from
  | 
        
        
            | 
            | 
           153 | 
              camp.device_type
  | 
        
        
            | 
            | 
           154 | 
           where
  | 
        
        
            | 
            | 
           155 | 
              show_as_system = 'Y';
  | 
        
        
            | 
            | 
           156 | 
              | 
        
        
            | 
            | 
           157 | 
           /* Now, we're ready to get some devices */
  | 
        
        
            | 
            | 
           158 | 
           truncate table device;
  | 
        
        
           | 40 | 
           rodolico | 
           159 | 
           insert into device( uuid,serial,name,created,removed)
  | 
        
        
           | 48 | 
           rodolico | 
           160 | 
              select distinct
  | 
        
        
           | 40 | 
           rodolico | 
           161 | 
                 device_uuid,
  | 
        
        
           | 48 | 
           rodolico | 
           162 | 
                 left(device_serial,32),
  | 
        
        
           | 33 | 
           rodolico | 
           163 | 
                 device_name,
  | 
        
        
            | 
            | 
           164 | 
                 device_added,
  | 
        
        
            | 
            | 
           165 | 
                 device_removed
  | 
        
        
            | 
            | 
           166 | 
              from
  | 
        
        
            | 
            | 
           167 | 
                 temp
  | 
        
        
           | 40 | 
           rodolico | 
           168 | 
                 join device_type on ( temp.device_type = device_type.name );
  | 
        
        
           | 33 | 
           rodolico | 
           169 | 
              | 
        
        
           | 40 | 
           rodolico | 
           170 | 
              | 
        
        
            | 
            | 
           171 | 
           /* get all the device types set up */
  | 
        
        
            | 
            | 
           172 | 
              | 
        
        
            | 
            | 
           173 | 
           truncate table device_device_type;
  | 
        
        
            | 
            | 
           174 | 
           insert into device_device_type (device_id,device_type_id )
  | 
        
        
            | 
            | 
           175 | 
              select 
  | 
        
        
            | 
            | 
           176 | 
                 device.device_id,
  | 
        
        
            | 
            | 
           177 | 
                 device_type_id
  | 
        
        
            | 
            | 
           178 | 
              from 
  | 
        
        
            | 
            | 
           179 | 
                 temp 
  | 
        
        
            | 
            | 
           180 | 
                 join device on (temp.device_name = device.name) 
  | 
        
        
            | 
            | 
           181 | 
                 join device_type on (temp.device_type = device_type.name);
  | 
        
        
            | 
            | 
           182 | 
              | 
        
        
            | 
            | 
           183 | 
              | 
        
        
           | 47 | 
           rodolico | 
           184 | 
           /* link a owner owner to a location */
  | 
        
        
            | 
            | 
           185 | 
           truncate table owner_location;
  | 
        
        
            | 
            | 
           186 | 
           insert into owner_location ( owner_id,location_id,created, removed )
  | 
        
        
           | 33 | 
           rodolico | 
           187 | 
              select 
  | 
        
        
           | 47 | 
           rodolico | 
           188 | 
                 owner.owner_id,
  | 
        
        
            | 
            | 
           189 | 
                 location.location_id,
  | 
        
        
           | 33 | 
           rodolico | 
           190 | 
                 oldTable.site_added,
  | 
        
        
            | 
            | 
           191 | 
                 oldTable.site_removed
  | 
        
        
            | 
            | 
           192 | 
              from 
  | 
        
        
            | 
            | 
           193 | 
                 ( 
  | 
        
        
            | 
            | 
           194 | 
                    select distinct 
  | 
        
        
            | 
            | 
           195 | 
                       site_name,
  | 
        
        
            | 
            | 
           196 | 
                       site_owner,
  | 
        
        
            | 
            | 
           197 | 
                       site_added, 
  | 
        
        
            | 
            | 
           198 | 
                       site_removed 
  | 
        
        
            | 
            | 
           199 | 
                    from temp 
  | 
        
        
            | 
            | 
           200 | 
                    ) oldTable
  | 
        
        
           | 47 | 
           rodolico | 
           201 | 
                 join owner on ( owner.name = oldTable.site_owner )
  | 
        
        
            | 
            | 
           202 | 
                 join location on ( location.name = oldTable.site_name );
  | 
        
        
           | 33 | 
           rodolico | 
           203 | 
              | 
        
        
            | 
            | 
           204 | 
              | 
        
        
            | 
            | 
           205 | 
           /* verify linkage with 
  | 
        
        
           | 47 | 
           rodolico | 
           206 | 
              select owner.name,location.name from owner join owner_location using ( owner_id ) join location using (location_id );
  | 
        
        
           | 33 | 
           rodolico | 
           207 | 
           */
  | 
        
        
            | 
            | 
           208 | 
              | 
        
        
            | 
            | 
           209 | 
           /* link a client owner to a device */
  | 
        
        
           | 47 | 
           rodolico | 
           210 | 
           truncate table owner_device;
  | 
        
        
            | 
            | 
           211 | 
           insert into owner_device ( owner_id,device_id,created, removed )
  | 
        
        
           | 33 | 
           rodolico | 
           212 | 
              select 
  | 
        
        
           | 47 | 
           rodolico | 
           213 | 
                 owner.owner_id,
  | 
        
        
           | 33 | 
           rodolico | 
           214 | 
                 device.device_id,
  | 
        
        
            | 
            | 
           215 | 
                 oldTable.device_added,
  | 
        
        
            | 
            | 
           216 | 
                 oldTable.device_removed
  | 
        
        
            | 
            | 
           217 | 
              from 
  | 
        
        
            | 
            | 
           218 | 
                 ( 
  | 
        
        
            | 
            | 
           219 | 
                    select
  | 
        
        
            | 
            | 
           220 | 
                       device_name,
  | 
        
        
            | 
            | 
           221 | 
                       client_name,
  | 
        
        
            | 
            | 
           222 | 
                       device_added, 
  | 
        
        
            | 
            | 
           223 | 
                       device_removed 
  | 
        
        
            | 
            | 
           224 | 
                    from temp 
  | 
        
        
            | 
            | 
           225 | 
                    ) oldTable
  | 
        
        
           | 47 | 
           rodolico | 
           226 | 
                 join owner on ( owner.name = oldTable.client_name )
  | 
        
        
           | 33 | 
           rodolico | 
           227 | 
                 join device on ( device.name = oldTable.device_name );
  | 
        
        
            | 
            | 
           228 | 
              | 
        
        
           | 47 | 
           rodolico | 
           229 | 
           /* link a device to a location */
  | 
        
        
            | 
            | 
           230 | 
           truncate table location_device;
  | 
        
        
            | 
            | 
           231 | 
           insert into location_device ( location_id,device_id,created, removed )
  | 
        
        
           | 33 | 
           rodolico | 
           232 | 
              select 
  | 
        
        
           | 47 | 
           rodolico | 
           233 | 
                 location.location_id,
  | 
        
        
           | 33 | 
           rodolico | 
           234 | 
                 device.device_id,
  | 
        
        
            | 
            | 
           235 | 
                 oldTable.device_added,
  | 
        
        
            | 
            | 
           236 | 
                 oldTable.device_removed
  | 
        
        
            | 
            | 
           237 | 
              from 
  | 
        
        
            | 
            | 
           238 | 
                 ( 
  | 
        
        
            | 
            | 
           239 | 
                    select
  | 
        
        
            | 
            | 
           240 | 
                       device_name,
  | 
        
        
            | 
            | 
           241 | 
                       site_name,
  | 
        
        
            | 
            | 
           242 | 
                       device_added, 
  | 
        
        
            | 
            | 
           243 | 
                       device_removed 
  | 
        
        
            | 
            | 
           244 | 
                    from temp 
  | 
        
        
            | 
            | 
           245 | 
                    ) oldTable
  | 
        
        
           | 47 | 
           rodolico | 
           246 | 
                 join location on ( location.name = oldTable.site_name )
  | 
        
        
           | 33 | 
           rodolico | 
           247 | 
                 join device on ( device.name = oldTable.device_name );
  | 
        
        
            | 
            | 
           248 | 
              | 
        
        
            | 
            | 
           249 | 
           /* link a device to a parent device */
  | 
        
        
            | 
            | 
           250 | 
           truncate table device_device;
  | 
        
        
            | 
            | 
           251 | 
           insert into device_device ( device_id,parent_id,created, removed )
  | 
        
        
            | 
            | 
           252 | 
              select 
  | 
        
        
            | 
            | 
           253 | 
                 device.device_id,
  | 
        
        
            | 
            | 
           254 | 
                 parent_device.device_id,
  | 
        
        
            | 
            | 
           255 | 
                 oldTable.device_added,
  | 
        
        
            | 
            | 
           256 | 
                 oldTable.device_removed
  | 
        
        
            | 
            | 
           257 | 
              from 
  | 
        
        
            | 
            | 
           258 | 
                 ( 
  | 
        
        
            | 
            | 
           259 | 
                    select
  | 
        
        
            | 
            | 
           260 | 
                       dev.device_name,
  | 
        
        
            | 
            | 
           261 | 
                       parent.device_name parent,
  | 
        
        
            | 
            | 
           262 | 
                       dev.device_added, 
  | 
        
        
            | 
            | 
           263 | 
                       dev.device_removed 
  | 
        
        
            | 
            | 
           264 | 
                    from temp dev join temp parent on ( dev.part_of = parent.old_device_id )
  | 
        
        
            | 
            | 
           265 | 
                    where dev.part_of is not null 
  | 
        
        
            | 
            | 
           266 | 
                    ) oldTable
  | 
        
        
            | 
            | 
           267 | 
                 join device on ( device.name = oldTable.device_name )
  | 
        
        
            | 
            | 
           268 | 
                 join device parent_device on ( parent_device.name = oldTable.parent );
  | 
        
        
           | 40 | 
           rodolico | 
           269 | 
              | 
        
        
           | 33 | 
           rodolico | 
           270 | 
              | 
        
        
           | 48 | 
           rodolico | 
           271 | 
           -- get rid of the temp table
  | 
        
        
            | 
            | 
           272 | 
           drop table if exists temp;
  |