File: DBStagTest.pm

package info (click to toggle)
libdbix-dbstag-perl 0.12-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, trixie
  • size: 1,420 kB
  • sloc: perl: 6,152; sql: 588; xml: 221; lisp: 59; makefile: 20
file content (116 lines) | stat: -rw-r--r-- 2,210 bytes parent folder | download | duplicates (3)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package DBStagTest;
use strict;
use base qw(Exporter);
use DBIx::DBStag;

BEGIN {
    use Test;
    if (0) {
        plan tests=>1;
        skip(1, 1);
        exit 0;
    }
    # this file defines sub connect_args()
    unless (defined(do 'db.config')) {
        die $@ if $@;
        die "Could not reade db.config: $!\n";
    }
}

use vars qw(@EXPORT);

our $driver;
#our $dbname = "dbistagtest";
#our $testdb = "dbi:Pg:dbname=$dbname;host=localhost";


@EXPORT = qw(connect_to_cleandb dbh drop cvtddl);


sub dbh {
    # this file defines sub connect_args()
#    unless (defined(do 'db.config')) {
#        die $@ if $@;
#        die "Could not reade db.config: $!\n";
#    }
    my $dbh;
    my @conn = connect_args();

    eval {
        $dbh = DBIx::DBStag->connect(@conn);    
    };
    if (!$dbh) {
        printf STDERR "COULD NOT CONNECT USING DBI->connect(@conn)\n\n";
        die;
    }
    $driver = $dbh->{_driver};
    $dbh;
}

*connect_to_cleandb = \&dbh;

sub ddl {
    my $dbh = dbh();
    my $ddl = shift;
    
}

sub cvtddl {
    my $ddl = shift;
    if ($driver eq 'mysql') {
        $ddl =~ s/ serial / INTEGER AUTO_INCREMENT /i;
    }
    return $ddl;
}

sub alltbl {
    qw(person2address person address );
}

sub drop {
#    unless (defined(do 'db.config')) {
#        die $@ if $@;
#        die "Could not reade db.config: $!\n";
#    }
    # this sub is defined in config file
    my $cmd = recreate_cmd();
    $cmd =~ s/\;/\;sleep 2\;/g;
#    if (system($cmd)) {
#        # allowed to fail first time...
#        # (pg sometimes won't let you create a db immediately after dropping)
#        sleep(2);
#    }
    if (system($cmd)) {
        # must pass 2nd time
	print STDERR "PROBLEM recreating using: $cmd\n";
    }
}

sub zdrop {
#    my @t = @_;
    my @t = alltbl;
    my $dbh = dbh();
    my %created = ();
    if (1) {
	use DBIx::DBSchema;
	my $s = DBIx::DBSchema->new_native($dbh->dbh);
	use Data::Dumper;
	%created = map {$_=>1} $s->tables;
    }
    
#    foreach (@t) {
#        eval {
#            $dbh->do("DROP TABLE $_");
#        };
    
    foreach (@t) {
	if ($created{$_}) {
	    eval {
		$dbh->do("DROP TABLE $_");
	    };
	}
    }
    $dbh->disconnect;
}

1;