source: trunk/cgi-bin/db-update.pl@ 855

Last change on this file since 855 was 853, checked in by Kris Deugau, 9 years ago

/trunk

Fix hack workaround from r844 to slurp the DB connect info into db-update.pl

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 5.9 KB
RevLine 
[658]1#!/usr/bin/perl
2# Quick sort of hack to populate new columns in DB
3
4use strict;
5use warnings;
6use Data::Dumper;
7
8use MyIPDB;
9
[679]10# run the tabledef update first. This is "safe"; IPDB < 3.0 can still happily munch away at it.
[844]11##fixme
12my $sqlcmd = "PGPASSWORD=".dbpass()." psql -h ".dbhost()." -U ".dbuser()." ".dbname()." <ipdb-2.7-3.0.sql";
[853]13print qx { $sqlcmd };
[658]14
15my $dbh;
16my $errstr;
17($dbh,$errstr) = connectDB_My;
18my $verbose = 0;
19
20die "db conn failed: $errstr\n" if !$dbh;
21
22initIPDBGlobals($dbh);
23
24local $dbh->{AutoCommit} = 0;
25local $dbh->{RaiseError} = 1;
26
27eval {
28
29 my $get_id = $dbh->prepare("SELECT currval('allocations_id_seq')");
30
[761]31 # master blocks move to the allocations table
[658]32 my $getm = $dbh->prepare("SELECT cidr,ctime,mtime,rwhois FROM masterblocks");
[822]33 my $insm = $dbh->prepare("INSERT INTO allocations (cidr,type,vrf,createstamp,modifystamp,swip,custid) VALUES (?,'mm','DEFAULT',?,?,?,?)");
[658]34
[761]35 # routed blocks move to the allocations table
[658]36 my $getr = $dbh->prepare("SELECT cidr,city FROM routed WHERE cidr <<= ?");
[772]37 my $insr = $dbh->prepare("INSERT INTO allocations (cidr,type,city,parent_id,custid) VALUES (?,'rm',?,?,?)");
[658]38 my $rfree = $dbh->prepare("UPDATE freeblocks SET parent_id = ?, routed='m' WHERE cidr <<= ? AND routed='y'");
39
[761]40 # update freeblocks with new parent relation info
41 my $mfree = $dbh->prepare("UPDATE freeblocks SET parent_id = ?, routed='m' WHERE cidr <<= ? AND routed='n'");
42 my $setm_f = $dbh->prepare("UPDATE freeblocks SET master_id = ? WHERE cidr <<= ?");
43 my $cfree = $dbh->prepare("UPDATE freeblocks SET parent_id = ?, routed='c' WHERE cidr <<= ?");
[658]44
[761]45 # update allocations with new parent relation info
[658]46 my $getc = $dbh->prepare("SELECT cidr,type,id FROM allocations WHERE cidr <<= ? AND type LIKE '_c'");
[761]47 my $setm_a = $dbh->prepare("UPDATE allocations SET master_id = ? WHERE cidr <<= ?");
48 my $updalloc = $dbh->prepare("UPDATE allocations SET parent_id = ? WHERE cidr <<= ? AND NOT (type='rm' OR type='mm')");
[658]49 my $updc = $dbh->prepare("UPDATE allocations SET parent_id = ? WHERE cidr <<= ? AND type LIKE '_r'");
50
[761]51 # update poolips with new parent relation info
52 my $getp = $dbh->prepare("SELECT cidr,id,master_id FROM allocations WHERE type LIKE '_d' OR type LIKE '_p'");
53 my $updpool = $dbh->prepare("UPDATE poolips SET parent_id = ?, master_id = ? WHERE ip << ?");
[658]54
[761]55 # "spare" freeblocks that are technically part of a container, but whose formal container parent
56 # isn't actually present. Arguably these could autoconvert the parent to a container, but IIRC
57 # in some cases live data has a mix of types in a container. *sigh*.
[658]58 my $sparef = $dbh->prepare("SELECT cidr,id FROM freeblocks WHERE parent_id = 0");
59 my $fparent = $dbh->prepare("UPDATE freeblocks SET parent_id = ".
60 "(SELECT id FROM allocations WHERE cidr >>= ? ORDER BY masklen(cidr) DESC LIMIT 1)".
61 " WHERE id = ?");
62
[761]63 # Need to disable the update trigger on the allocations and poolips tables,
64 # so we don't mangle the real mtimes on the data.
[679]65 $dbh->do("DROP TRIGGER up_modtime ON allocations");
[761]66 $dbh->do("DROP TRIGGER up_modtime ON poolips");
[679]67
[658]68 $getm->execute;
69 while (my ($master,$mctime,$mmtime,$mswip) = $getm->fetchrow_array()) {
70 print "$master\t";
[761]71 # copy master to allocations table
[772]72 $insm->execute($master, $mctime, $mmtime, $mswip, $def_custids{'mm'});
[658]73 $get_id->execute;
74 my ($mid) = $get_id->fetchrow_array();
75 print "$mid\n";
[761]76 # parent relation for free blocks directly under the master
[658]77 $mfree->execute($mid, $master);
78
79 $getr->execute($master);
80 while (my ($routed,$rcity) = $getr->fetchrow_array()) {
81 print " $routed\t";
[761]82 # copy routed to allocations table
[772]83 $insr->execute($routed, $rcity, $mid, $def_custids{'rm'});
[658]84 $get_id->execute;
85 my ($rid) = $get_id->fetchrow_array();
86 print "$rid\n";
[761]87 # parent relation for free blocks directly under the routed block
[658]88 $rfree->execute($rid, $routed);
[761]89 # parent relation for allocations in the routed block
[658]90 $updalloc->execute($rid, $routed);
91 $getc->execute($routed);
92 while (my ($container, $ctype, $cid) = $getc->fetchrow_array()) {
93 print " $container";
[761]94 # container blocks are now functionally equivalent to routed blocks;
95 # update the parent relations on the contained blocks to treat the
96 # container as the parent, not the routed block
[658]97 $updc->execute($cid, $container);
98 my $c = $cfree->execute($cid, $container);
99 }
100 }
[761]101 # Just In Case. Bulk-set the master ID on all allocations, then freeblocks,
102 # within the master. This could theoretically be merged into the updates
103 # above, but edge cases kept happening.
[658]104 $setm_a->execute($mid, $master);
105 $setm_f->execute($mid, $master);
106 }
107
[761]108 # Update poolips with new parent and master relation info
[658]109 $getp->execute();
[761]110 while (my ($pool, $pid, $pmaster) = $getp->fetchrow_array()) {
111 $updpool->execute($pid, $pmaster, $pool);
[658]112 }
113
[761]114 # clean up "spare" freeblocks from formally-incorrect use of container types
[658]115 $sparef->execute();
116 while (my ($free, $fid) = $sparef->fetchrow_array()) {
117 $fparent->execute($free, $fid)
118 }
119
120 if ($ARGV[0] && $ARGV[0] == 'fetchdns') {
121 # weesa gotsa BIIIIG job, gotta fetch all the current rDNS. note this relies
122 # on having had the DNS data imported and munged into the new pseudotypes.
[679]123 # ... never mind. this will be opportunistic, since DNS data isn't viewed
124 # unless adding (not present, we should hope!) or updating (where it should
125 # be retrieved from the authoritative API in real time)
[658]126 }
127
[679]128 # Recreate modtime trigger on allocations, now that we're done monkeying with it.
129 $dbh->do("CREATE TRIGGER up_modtime BEFORE UPDATE ON allocations FOR EACH ROW EXECUTE PROCEDURE up_modtime()");
[761]130 $dbh->do("CREATE TRIGGER up_modtime BEFORE UPDATE ON poolips FOR EACH ROW EXECUTE PROCEDURE up_modtime()");
[679]131
[658]132 $dbh->commit;
133}; # all wrapped up in an eval{} so we can roll it all back when we want to
134if ($@) {
135 print "ebbeh? $@\n";
136}
Note: See TracBrowser for help on using the repository browser.