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

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

/trunk

Add SQL base update fragment and database rearranging script

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 4.7 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
10# run the tabledef update first
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 $getm->execute;
63 while (my ($master,$mctime,$mmtime,$mswip) = $getm->fetchrow_array()) {
64# next unless $master eq '10.0.0.0/8';
65# next unless $master eq '172.16.0.0/12';
66 print "$master\t";
67 $insm->execute($master, $mctime, $mmtime, $mswip);
68 $get_id->execute;
69 my ($mid) = $get_id->fetchrow_array();
70 print "$mid\n";
71 $mfree->execute($mid, $master);
72
73 $getr->execute($master);
74 while (my ($routed,$rcity) = $getr->fetchrow_array()) {
75 print " $routed\t";
76 $insr->execute($routed, $rcity, $mid);
77 $get_id->execute;
78 my ($rid) = $get_id->fetchrow_array();
79 print "$rid\n";
80 $rfree->execute($rid, $routed);
81 $updalloc->execute($rid, $routed);
82 $getc->execute($routed);
83 while (my ($container, $ctype, $cid) = $getc->fetchrow_array()) {
84 print " $container";
85 $updc->execute($cid, $container);
86 my $c = $cfree->execute($cid, $container);
87print " $c done?\n";
88 }
89 }
90 $setm_a->execute($mid, $master);
91 $setm_f->execute($mid, $master);
92 }
93
94 $getp->execute();
95 while (my ($pool, $pid) = $getp->fetchrow_array()) {
96 $updpool->execute($pid, $pool);
97 }
98
99 # "spare" freeblocks
100 $sparef->execute();
101 while (my ($free, $fid) = $sparef->fetchrow_array()) {
102#print "$free, $fid: ";
103#my $par = $dbh->selectall_arrayref("SELECT id,cidr FROM allocations WHERE cidr >>= ? ORDER BY masklen(cidr)",
104# undef, ($free));
105#print Dumper($par);
106 $fparent->execute($free, $fid)
107 }
108
109 if ($ARGV[0] && $ARGV[0] == 'fetchdns') {
110 # weesa gotsa BIIIIG job, gotta fetch all the current rDNS. note this relies
111 # on having had the DNS data imported and munged into the new pseudotypes.
112 }
113
114 $dbh->commit;
115#$dbh->rollback;
116}; # all wrapped up in an eval{} so we can roll it all back when we want to
117if ($@) {
118 print "ebbeh? $@\n";
119}
Note: See TracBrowser for help on using the repository browser.