Changeset 761 for trunk/cgi-bin
- Timestamp:
- 07/28/15 18:07:38 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/cgi-bin/db-update.pl
r679 r761 27 27 my $get_id = $dbh->prepare("SELECT currval('allocations_id_seq')"); 28 28 29 # master blocks move to the allocations table 29 30 my $getm = $dbh->prepare("SELECT cidr,ctime,mtime,rwhois FROM masterblocks"); 30 31 my $insm = $dbh->prepare("INSERT INTO allocations (cidr,type,createstamp,modifystamp,swip) VALUES (?,'mm',?,?,?)"); 31 # master must be its own master32 # my $selfm = $dbh->prepare("UPDATE allocations SET master_id = ? WHERE id = ?33 my $mfree = $dbh->prepare("UPDATE freeblocks SET parent_id = ?, routed='m' WHERE cidr <<= ? AND routed='n'");34 # my $setm_a = $dbh->prepare("UPDATE allocations SET master_id = ? WHERE cidr <<= ? AND NOT type='mm'");35 my $setm_a = $dbh->prepare("UPDATE allocations SET master_id = ? WHERE cidr <<= ?");36 my $setm_f = $dbh->prepare("UPDATE freeblocks SET master_id = ? WHERE cidr <<= ?");37 my $setm_p = $dbh->prepare("UPDATE poolips SET master_id = ? WHERE ip <<= ?");38 32 33 # routed blocks move to the allocations table 39 34 my $getr = $dbh->prepare("SELECT cidr,city FROM routed WHERE cidr <<= ?"); 40 35 my $insr = $dbh->prepare("INSERT INTO allocations (cidr,type,city,parent_id) VALUES (?,'rm',?,?)"); 41 36 my $rfree = $dbh->prepare("UPDATE freeblocks SET parent_id = ?, routed='m' WHERE cidr <<= ? AND routed='y'"); 42 37 43 my $updalloc = $dbh->prepare("UPDATE allocations SET parent_id = ? WHERE cidr <<= ? AND NOT (type='rm' OR type='mm')"); 44 45 # my $sth_alloc_container = $dbh->prepare("UPDATE allocations SET parent = ?,rdepth=3 WHERE cidr <<= ? AND type LIKE '_r'"); 46 # my $sth_free_container = $dbh->prepare("UPDATE freeblocks SET parent = ?,rdepth=3 WHERE cidr <<= ?"); 47 my $getc = $dbh->prepare("SELECT cidr,type,id FROM allocations WHERE cidr <<= ? AND type LIKE '_c'"); 48 my $updc = $dbh->prepare("UPDATE allocations SET parent_id = ? WHERE cidr <<= ? AND type LIKE '_r'"); 38 # update freeblocks with new parent relation info 39 my $mfree = $dbh->prepare("UPDATE freeblocks SET parent_id = ?, routed='m' WHERE cidr <<= ? AND routed='n'"); 40 my $setm_f = $dbh->prepare("UPDATE freeblocks SET master_id = ? WHERE cidr <<= ?"); 49 41 my $cfree = $dbh->prepare("UPDATE freeblocks SET parent_id = ?, routed='c' WHERE cidr <<= ?"); 50 42 51 my $getp = $dbh->prepare("SELECT cidr,id FROM allocations WHERE type LIKE '_d' OR type LIKE '_p'"); 52 my $updpool = $dbh->prepare("UPDATE poolips SET parent_id = ? WHERE ip << ?"); 43 # update allocations with new parent relation info 44 my $getc = $dbh->prepare("SELECT cidr,type,id FROM allocations WHERE cidr <<= ? AND type LIKE '_c'"); 45 my $setm_a = $dbh->prepare("UPDATE allocations SET master_id = ? WHERE cidr <<= ?"); 46 my $updalloc = $dbh->prepare("UPDATE allocations SET parent_id = ? WHERE cidr <<= ? AND NOT (type='rm' OR type='mm')"); 47 my $updc = $dbh->prepare("UPDATE allocations SET parent_id = ? WHERE cidr <<= ? AND type LIKE '_r'"); 53 48 54 # "spare" freeblocks that are technically part of a container, but whose formal container parent isn't actually 55 # present. Arguably these could autoconvert the parent to a container, but IIRC in some cases live data has a 56 # mix of types in a container. *sigh*. 49 # update poolips with new parent relation info 50 my $getp = $dbh->prepare("SELECT cidr,id,master_id FROM allocations WHERE type LIKE '_d' OR type LIKE '_p'"); 51 my $updpool = $dbh->prepare("UPDATE poolips SET parent_id = ?, master_id = ? WHERE ip << ?"); 52 53 # "spare" freeblocks that are technically part of a container, but whose formal container parent 54 # isn't actually present. Arguably these could autoconvert the parent to a container, but IIRC 55 # in some cases live data has a mix of types in a container. *sigh*. 57 56 my $sparef = $dbh->prepare("SELECT cidr,id FROM freeblocks WHERE parent_id = 0"); 58 57 my $fparent = $dbh->prepare("UPDATE freeblocks SET parent_id = ". … … 60 59 " WHERE id = ?"); 61 60 62 # Need to disable the update trigger on the allocations table, so we don't mangle the real mtimes on the data. 61 # Need to disable the update trigger on the allocations and poolips tables, 62 # so we don't mangle the real mtimes on the data. 63 63 $dbh->do("DROP TRIGGER up_modtime ON allocations"); 64 $dbh->do("DROP TRIGGER up_modtime ON poolips"); 64 65 65 66 $getm->execute; 66 67 while (my ($master,$mctime,$mmtime,$mswip) = $getm->fetchrow_array()) { 67 # next unless $master eq '10.0.0.0/8';68 # next unless $master eq '172.16.0.0/12';69 68 print "$master\t"; 69 # copy master to allocations table 70 70 $insm->execute($master, $mctime, $mmtime, $mswip); 71 71 $get_id->execute; 72 72 my ($mid) = $get_id->fetchrow_array(); 73 73 print "$mid\n"; 74 # parent relation for free blocks directly under the master 74 75 $mfree->execute($mid, $master); 75 76 … … 77 78 while (my ($routed,$rcity) = $getr->fetchrow_array()) { 78 79 print " $routed\t"; 80 # copy routed to allocations table 79 81 $insr->execute($routed, $rcity, $mid); 80 82 $get_id->execute; 81 83 my ($rid) = $get_id->fetchrow_array(); 82 84 print "$rid\n"; 85 # parent relation for free blocks directly under the routed block 83 86 $rfree->execute($rid, $routed); 87 # parent relation for allocations in the routed block 84 88 $updalloc->execute($rid, $routed); 85 89 $getc->execute($routed); 86 90 while (my ($container, $ctype, $cid) = $getc->fetchrow_array()) { 87 91 print " $container"; 92 # container blocks are now functionally equivalent to routed blocks; 93 # update the parent relations on the contained blocks to treat the 94 # container as the parent, not the routed block 88 95 $updc->execute($cid, $container); 89 96 my $c = $cfree->execute($cid, $container); 90 print " $c done?\n";91 97 } 92 98 } 99 # Just In Case. Bulk-set the master ID on all allocations, then freeblocks, 100 # within the master. This could theoretically be merged into the updates 101 # above, but edge cases kept happening. 93 102 $setm_a->execute($mid, $master); 94 103 $setm_f->execute($mid, $master); 95 104 } 96 105 106 # Update poolips with new parent and master relation info 97 107 $getp->execute(); 98 while (my ($pool, $pid ) = $getp->fetchrow_array()) {99 $updpool->execute($pid, $p ool);108 while (my ($pool, $pid, $pmaster) = $getp->fetchrow_array()) { 109 $updpool->execute($pid, $pmaster, $pool); 100 110 } 101 111 102 # "spare" freeblocks112 # clean up "spare" freeblocks from formally-incorrect use of container types 103 113 $sparef->execute(); 104 114 while (my ($free, $fid) = $sparef->fetchrow_array()) { 105 #print "$free, $fid: ";106 #my $par = $dbh->selectall_arrayref("SELECT id,cidr FROM allocations WHERE cidr >>= ? ORDER BY masklen(cidr)",107 # undef, ($free));108 #print Dumper($par);109 115 $fparent->execute($free, $fid) 110 116 } … … 120 126 # Recreate modtime trigger on allocations, now that we're done monkeying with it. 121 127 $dbh->do("CREATE TRIGGER up_modtime BEFORE UPDATE ON allocations FOR EACH ROW EXECUTE PROCEDURE up_modtime()"); 128 $dbh->do("CREATE TRIGGER up_modtime BEFORE UPDATE ON poolips FOR EACH ROW EXECUTE PROCEDURE up_modtime()"); 122 129 123 130 $dbh->commit; 124 #$dbh->rollback;125 131 }; # all wrapped up in an eval{} so we can roll it all back when we want to 126 132 if ($@) {
Note:
See TracChangeset
for help on using the changeset viewer.