-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb_filter_sql.pl
executable file
·60 lines (54 loc) · 1.1 KB
/
db_filter_sql.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/perl
use strict;
my $usage=q{
db_filter_sql.pl tbl_list.txt schema.sql
Filters schema.sql, which is expected to include CREATE TABLE
ALTER TABLE or CREATE SEQUENCE etc. to only let through SQL statements
relating to the tables in tbl_list.txt
};
foreach my $f (@ARGV) {
die("Error: file $f not found!\n") unless -f $f;
}
my ($ftbl, $fsql)=@ARGV;
my %th; # table => 1
open(F, $ftbl) || die("Error opening $ftbl!\n");
while(<F>) {
chomp;
my @t=split();
$th{lc($t[0])}=1
}
close(F);
open(SQL, $fsql) || die("Error opening $fsql!\n");
my $ml=0; #multiline flag
my $k=0; #keep flag
while(<SQL>) {
# check for multi-line SQL to preserve
if ($ml) { #multi-line entry
if ($k) {
print $_;
}
if (m/\);\s*$/) {
$ml=0;
$k=0;
}
next;
}
if (m/CREATE\s+TABLE\s+"public"\."([^"]+)/) {
my $tbl=lc($1);
$ml=1;
if ($th{$tbl}) {
print $_;
$k=1;
}
next;
}
if (m/ALTER\s+TABLE\s+"public"\."([^"]+)/ ||
m/OWNED BY "public"\."([^"]+)/ ) {
my $tbl=lc($1);
if ($th{$tbl}) {
print $_;
}
next;
}
}
close(SQL);