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

Last change on this file since 684 was 679, checked in by Kris Deugau, 10 years ago

/trunk

  • Add a bit more detail to a couple of docucomments in db-update.pl
  • Drop and recreate the modtime trigger on the allocations table when doing the database update. This preserves the *real* modtimes, instead of generating an entirely artificial one that has nothing to do with changes in the actual IP assignment data.
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 5.3 KB
Line 
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
10# run the tabledef update first. This is "safe"; IPDB < 3.0 can still happily munch away at it.
11print qx { PGPASSWORD=ipdbpwd psql -h localhost -U ipdb ipdb <ipdb-2.7-3.0.sql };
12
13my $dbh;
14my $errstr;
15($dbh,$errstr) = connectDB_My;
16my $verbose = 0;
17
18die "db conn failed: $errstr\n" if !$dbh;
19
20initIPDBGlobals($dbh);
21
22local $dbh->{AutoCommit} = 0;
23local $dbh->{RaiseError} = 1;
24
25eval {
26
27 my $get_id = $dbh->prepare("SELECT currval('allocations_id_seq')");
28
29 my $getm = $dbh->prepare("SELECT cidr,ctime,mtime,rwhois FROM masterblocks");
30 my $insm = $dbh->prepare("INSERT INTO allocations (cidr,type,createstamp,modifystamp,swip) VALUES (?,'mm',?,?,?)");
31 # master must be its own master
32# 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
39 my $getr = $dbh->prepare("SELECT cidr,city FROM routed WHERE cidr <<= ?");
40 my $insr = $dbh->prepare("INSERT INTO allocations (cidr,type,city,parent_id) VALUES (?,'rm',?,?)");
41 my $rfree = $dbh->prepare("UPDATE freeblocks SET parent_id = ?, routed='m' WHERE cidr <<= ? AND routed='y'");
42
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'");
49 my $cfree = $dbh->prepare("UPDATE freeblocks SET parent_id = ?, routed='c' WHERE cidr <<= ?");
50
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 << ?");
53
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*.
57 my $sparef = $dbh->prepare("SELECT cidr,id FROM freeblocks WHERE parent_id = 0");
58 my $fparent = $dbh->prepare("UPDATE freeblocks SET parent_id = ".
59 "(SELECT id FROM allocations WHERE cidr >>= ? ORDER BY masklen(cidr) DESC LIMIT 1)".
60 " WHERE id = ?");
61
62 # Need to disable the update trigger on the allocations table, so we don't mangle the real mtimes on the data.
63 $dbh->do("DROP TRIGGER up_modtime ON allocations");
64
65 $getm->execute;
66 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 print "$master\t";
70 $insm->execute($master, $mctime, $mmtime, $mswip);
71 $get_id->execute;
72 my ($mid) = $get_id->fetchrow_array();
73 print "$mid\n";
74 $mfree->execute($mid, $master);
75
76 $getr->execute($master);
77 while (my ($routed,$rcity) = $getr->fetchrow_array()) {
78 print " $routed\t";
79 $insr->execute($routed, $rcity, $mid);
80 $get_id->execute;
81 my ($rid) = $get_id->fetchrow_array();
82 print "$rid\n";
83 $rfree->execute($rid, $routed);
84 $updalloc->execute($rid, $routed);
85 $getc->execute($routed);
86 while (my ($container, $ctype, $cid) = $getc->fetchrow_array()) {
87 print " $container";
88 $updc->execute($cid, $container);
89 my $c = $cfree->execute($cid, $container);
90print " $c done?\n";
91 }
92 }
93 $setm_a->execute($mid, $master);
94 $setm_f->execute($mid, $master);
95 }
96
97 $getp->execute();
98 while (my ($pool, $pid) = $getp->fetchrow_array()) {
99 $updpool->execute($pid, $pool);
100 }
101
102 # "spare" freeblocks
103 $sparef->execute();
104 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 $fparent->execute($free, $fid)
110 }
111
112 if ($ARGV[0] && $ARGV[0] == 'fetchdns') {
113 # weesa gotsa BIIIIG job, gotta fetch all the current rDNS. note this relies
114 # on having had the DNS data imported and munged into the new pseudotypes.
115 # ... never mind. this will be opportunistic, since DNS data isn't viewed
116 # unless adding (not present, we should hope!) or updating (where it should
117 # be retrieved from the authoritative API in real time)
118 }
119
120 # Recreate modtime trigger on allocations, now that we're done monkeying with it.
121 $dbh->do("CREATE TRIGGER up_modtime BEFORE UPDATE ON allocations FOR EACH ROW EXECUTE PROCEDURE up_modtime()");
122
123 $dbh->commit;
124#$dbh->rollback;
125}; # all wrapped up in an eval{} so we can roll it all back when we want to
126if ($@) {
127 print "ebbeh? $@\n";
128}
Note: See TracBrowser for help on using the repository browser.