Debian Codegraph @

modules/audit/make_audit_ddl

raw
1 #!/usr/bin/env perl
2 #
3 # make_audit_ddl
4 #
5 # Perl script reads idb ddl from cvs module directories and
6 # for each table produces the corresponding audit table named
7 # tablename_audit.
8 #
9 # columns of type 'serial' are changed to 'int'
10 # primary key declarations are removed
11 # foreign key declarations are removed
12 # unique column declarations are removed
13 # additional columns added to each table:
14 # transaction_date timestamp not null
15 # transaction_type char not null
16 # all priveleges granted to public
17 #
18 #
19 #---------------------------------------------------------------------------#
20 #---------------------------------------------------------------------------#
21
22
23 #
24 ## General setup
25 #
26
27 $cvsdir = "/Users/emmert/cvs/scratch/idb/flydb/";
28 #$cvsdir = "../";
29
30 ## open the master module list
31
32 $modlist = $cvsdir . "idb-full.modules";
33 open(mlist,$modlist);
34
35 while (<mlist>) {
36 chop($_) if $_ =~ /\n$/;
37
38 ## now for each module open the .sql file for parsing
39 $sqlfile = $cvsdir . $_;
40
41 # print "\nopening $sqlfile...\n";
42 open(sfile,$sqlfile);
43
44 ## for each table we add _audit to the table name, get rid of foreign
45 ## keys, and set up grant statement.
46 ##
47 ## printing here is a little fiddly to handle correct placement of commas
48 ## in create table statements...
49 while (<sfile>) {
50 if ( ($_ !~ /^#/) && ($_ !~ /^\s*$/) && ($_ !~ /foreign key/) && ($_ !~ /unique/) && ($_ !~ /primary key/) ) {
51 chop($_) if $_ =~ /\n$/;
52 chop($_) if $_ =~ /\,$/;
53 $_ =~ s/serial/int/;
54 if ($_ =~ /(^.*create table) (.*) \(/) {
55 $tabst = 0;
56 $_ = $1 . " " . $2 . "_audit (";
57 $tname = $2 . "_audit";
58 print "$_";
59 }
60 elsif ($_ =~ /\)\;/) {
61 print ",\n\ttransaction_date timestamp not null,";
62 print "\n\ttransaction_type char not null";
63 print "\n$_\n";
64 print "GRANT ALL on $tname to PUBLIC;\n\n";
65 }
66 elsif ($tabst == 0) {
67 print "\n$_";
68 $tabst++;
69 }
70 ## when create view statements appear we can assume that the create table
71 ## statements are all handled...
72 elsif ($_ =~ /create view/) {
73 close(sfile);
74 }
75 else {
76 if ($_ =~ /timeentered|timelastmod/) {
77 $_ =~ s/ default current_timestamp//;
78 }
79 print ",\n$_";
80 $tabst++;
81 }
82 }
83 }
84 }
85