File: test_load.pl

package info (click to toggle)
libchado-perl 1.31-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 44,716 kB
  • sloc: sql: 282,721; xml: 192,553; perl: 25,524; sh: 102; python: 73; makefile: 57
file content (49 lines) | stat: -rwxr-xr-x 1,192 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env perl

#
# A simple script to create the Chado database and tables.
#

use strict;

my $dbname = shift or exit;
my @list   = `psql -l`;
my %dbs;
my $ok = 0;
for my $line ( @list ) {
    ;
    if ( $line =~ m/^\s*Name\s*|\s*Owner\s*/ ) {
        $ok = 1;
        next;
    }
    elsif ( $ok ) {
        if ( $line =~ m/^\s*(\w+)\s*\|\s*\w+\s*/ ) {
            $dbs{ $1 } = 1;
        }
    }
}

if ( $dbs{ $dbname } ) {
    print "A database called '$dbname' already exists.\n";
    print "OK to drop database '$dbname'? [Y/n] ";
    chomp( my $answer = <STDIN> );
    unless ( $answer =~ m/^[Nn]/ ) {
        print "Dropping database '$dbname'\n";
        system( "dropdb $dbname" ) == 0 or die "Cannot drop database: $?";
    }
    else {
        print "Will not drop database '$dbname'.  Exiting.\n";
        exit(0);
    }
}

print "Creating new database called '$dbname'\n";
system( "createdb $dbname" ) == 0 or die "Cannot create database: $?";

print "Creating tables\n";
system( 
    "psql -f modules/complete.sql $dbname 2>&1 | grep -E 'ERROR|FATAL|No such file or directory'"
) == 0 or die "Problem creating tables: $?";

print "Database '$dbname' created\n";
exit(0);