File Coverage

blib/lib/DBR/Sandbox.pm
Criterion Covered Total %
statement 96 98 97.9
branch 24 44 54.5
condition 5 14 35.7
subroutine 16 16 100.0
pod 0 1 0.0
total 141 173 81.5


line stmt bran cond sub pod time code
1             package DBR::Sandbox;
2              
3 18     18   14933 use strict;
  18         47  
  18         813  
4 18     18   116 use DBR;
  18         39  
  18         168  
5 18     18   116 use DBR::Util::Logger;
  18         41  
  18         750  
6 18     18   148 use DBR::Config::ScanDB;
  18         38  
  18         443  
7 18     18   130 use DBR::Config::SpecLoader;
  18         37  
  18         497  
8 18     18   96 use DBR::Config::Schema;
  18         44  
  18         5011  
9 18     18   327 use Scalar::Util 'blessed';
  18         66  
  18         1236  
10 18     18   101 use File::Path;
  18         44  
  18         1974  
11 18     18   119 use Carp;
  18         44  
  18         28811  
12              
13             sub import {
14 18     18   226 my $pkg = shift;
15 18         55 my %params = @_;
16 18         162 my $dbr;
17              
18 18         77 my ($callpack, $callfile, $callline) = caller;
19              
20 18 50       1263 if( $params{schema} ){
21 0         0 DBR::Sandbox->provision( %params );
22             }
23              
24             }
25              
26             my ($CONFDIR) = grep {-d $_ } ('schemas','example/schemas','../example/schemas');
27              
28             sub provision{
29 17     17 0 433 $| = 1;
30 17 50 33     3114 my $package = shift if blessed($_[0]) || $_[0] eq __PACKAGE__;
31 17         107 my %params = @_;
32 17 50       92 my $schema = $params{schema} or confess "schema is required";
33            
34 17         72 my $sandbox = '_sandbox/' . $params{schema};
35 17   33     176 my $dbrconf = $params{writeconf} || "$sandbox/DBR.conf";
36            
37 17 0 33     108 return if $params{reuse} && -e $dbrconf && -d $sandbox;
      33        
38            
39 17 50       86 print STDERR "Provisioning Sandbox... " unless $params{quiet};
40 17         84 _ready_sandbox ( $sandbox );
41            
42 17         132 my $metadb = _sqlite_connect( dbfile => "$sandbox/dbrconf.sqlite" );
43 17         133 my $maindb = _sqlite_connect( dbfile => "$sandbox/db.sqlite" );
44            
45 17         146 _load_sqlfile ( "$CONFDIR/dbr_schema_sqlite.sql", $metadb );
46 17         195 _load_sqlfile ( "$CONFDIR/$schema/sql", $maindb );
47            
48 17         251 _setup_metadb ( $sandbox, $schema, $metadb );
49            
50 17         1986 $metadb->disconnect();
51 17         1343 $maindb->disconnect();
52            
53 17         114 _write_dbrconf( $sandbox, $dbrconf );
54            
55 17 50       347 my $logger = new DBR::Util::Logger( -logpath => '_sandbox/sandbox_setup.log', -logLevel => 'debug3' ) or die "logger create failed";
56 17 50       241 my $dbr = new DBR(
57             -logger => $logger,
58             -conf => $dbrconf,
59             -admin => 1,
60             -fudge_tz => 1,
61             ) or die 'failed to create dbr object';
62            
63 17 50       142 my $conf_instance = $dbr->get_instance('dbrconf') or die "No config found for confdb";
64            
65 17 50       102 my $loader = DBR::Config::SpecLoader->new(
66             session => $dbr->session,
67             conf_instance => $conf_instance,
68             dbr => $dbr,
69             ) or die "Failed to create spec loader";
70            
71 17 50       210 my $spec = $loader->parse_file( "$CONFDIR/$schema/spec" ) or die "Failed to open $CONFDIR/$schema/spec";
72              
73 17 50       150 $loader->process_spec( $spec ) or die "Failed to process spec data";
74              
75 17 50       99 print STDERR "Done. \n\n" unless $params{quiet};
76             # returning DBR object to be used with test harnesses
77 17         3554 return $dbr;
78              
79             }
80              
81             sub _ready_sandbox{
82 17     17   44 my $sandbox = shift;
83              
84 17 100       138353 File::Path::rmtree( $sandbox ) if -e $sandbox;
85 17 50       17370 mkpath $sandbox or confess "failed to ready sandbox '$sandbox'";
86             }
87              
88             sub _sqlite_connect {
89 34     34   166 my $attr = { @_ };
90 34   50     216 my $dbfile = delete $attr->{dbfile} || ':memory:';
91 34         168 my @params = ( "dbi:SQLite:dbname=$dbfile", '', '' );
92 34 50       154 if ( %$attr ) {
93 0         0 push @params, $attr;
94             }
95 34         306 my $dbh = DBI->connect( @params );
96 34         315793 return $dbh;
97             }
98              
99             sub _load_sqlfile{
100 34     34   121 my $file = shift;
101 34         90 my $dbh = shift;
102              
103 34         118 my $fh;
104 34 50       2557 open ($fh, "<$file") || return 0;
105 34         102 my $buff;
106 34         17722 while (<$fh>){
107 2073         4653 $buff .= $_;
108             }
109              
110 34         512 foreach my $part (split(';',$buff)){
111 471 100       31073496 next unless $part =~ /\S+/;
112 438 100       3086 next if $part =~ /^\s*--/;
113 436 50       5315 $dbh->do($part) or return 0;
114             }
115              
116 34         1196 return 1;
117             }
118              
119             sub _setup_metadb{
120 17     17   73 my $sandbox = shift;
121 17         339 my $schema = shift;
122 17         68 my $dbh = shift;
123              
124 17 50       360 $dbh->do("INSERT INTO dbr_schemas (schema_id,handle) values (1,'$schema')") or return 0;
125 17 50       848687 $dbh->do("INSERT INTO dbr_instances (schema_id,handle,class,dbfile,module) values (1,'$schema','master','$sandbox/db.sqlite','SQLite')") or return 0;
126              
127 17         814828 return 1;
128             }
129              
130             sub _write_dbrconf{
131 17     17   72 my $sandbox = shift;
132 17         104 my $dbrconf = shift;
133 17         49 my $fh;
134 17 50       2131 open ($fh, "> $dbrconf") or return 0;
135 17         232 print $fh "# This DBR config file has been generated by the DBR::Sandbox library.\n";
136 17         65 print $fh "# This conf file defines DB instances, at least one of which (dbrconf) is\n";
137 17         47 print $fh "# required to have a starting point for fetching metadata. Defining other\n";
138 17         52 print $fh "# instances here is possible, but discouraged, as functionality will be\n";
139 17         53 print $fh "# dramatically degraded due to lack of metadata.\n\n";
140            
141 17         88 print $fh "name=dbrconf; class=master; dbfile=$sandbox/dbrconf.sqlite; type=SQLite; dbr_bootstrap=1\n";
142 17         1025 close $fh;
143              
144 17         114 return 1;
145             }
146              
147              
148             1;