File Coverage

blib/lib/Test/DBIx/Class/SchemaManager/Trait/SQLite.pm
Criterion Covered Total %
statement 23 24 95.8
branch 5 6 83.3
condition n/a
subroutine 6 6 100.0
pod 0 2 0.0
total 34 38 89.4


line stmt bran cond sub pod time code
1             package Test::DBIx::Class::SchemaManager::Trait::SQLite; {
2            
3 14     14   36155 use Moose::Role;
  14         26  
  14         158  
4 14     14   52515 use MooseX::Attribute::ENV;
  14         18  
  14         335  
5 14     14   150 use Test::DBIx::Class::Types qw(ConnectInfo);
  14         20  
  14         154  
6              
7             sub dbname {
8 26     26 0 40 my ($self) = @_;
9              
10 26         65 my $env_path = $ENV{DBNAME};
11 26         56 my $dsn = $self->{connect_info}{dsn};
12              
13 26 100       94 if($env_path) {
    100          
14 1         28 return $env_path;
15             }
16             elsif($dsn) {
17 14         51 my ($dbname) = $self->_extract_dbname_from_dsn($dsn);
18 14 50       33 if($dbname) {
19 14         208 return $dbname;
20             }
21             else {
22 0         0 die("Couldn't find dbname in sqlite dsn '$dsn'");
23             }
24             }
25             else {
26 11         331 return ':memory:';
27             }
28             }
29              
30             sub _extract_dbname_from_dsn
31             {
32 17     17   28 my ($self, $dsn) = @_;
33 17         122 my ($dbname) = $dsn =~ m/dbi:[^:]+:(?:dbname=)?(.+)/i;
34 17         48 return $dbname;
35             }
36              
37             sub get_default_connect_info {
38 12     12 0 19 my ($self) = @_;
39 12         43 return ["dbi:SQLite:dbname=".$self->dbname,'',''];
40             }
41              
42             before 'setup' => sub {
43             my ($self) = @_;
44             if(my $path = $ENV{DBNAME}) {
45             if(-e $path) {
46             $self->builder->ok(-w $path, "Path $path is accessible, forcing 'force_drop_table'");
47             $self->force_drop_table(1);
48             }
49             }
50             };
51              
52             after 'cleanup' => sub {
53             my ($self) = @_;
54             if(!$self->keep_db && lc $self->dbname ne ':memory:') {
55             unlink $self->dbname;
56             }
57             };
58             } 1;
59              
60             __END__
61              
62             =head1 NAME
63              
64             Test::DBIx::Class::SchemaManager::Trait::SQLite - The Default Role
65              
66             =head1 DESCRIPTION
67              
68             The default Storage trait which provides the ability to deploy to a SQLite
69             database. It also sets some %ENV and or configuration options that you can
70             use to specify alternative database setup.
71              
72             In addition to the documented %ENV settings, this Trait adds the following:
73              
74             =over 4
75              
76             =item DBNAME
77              
78             Defaults to ':memory:' to create an in memory database. Provide a string
79             suitable for the "dbname=XXX" part of your connect string. Typically this
80             should be the path to a location on the filesystem you want the datbase file
81             to be stored.
82              
83             Please note that this file will automatically be deleted unless you have
84             specified to 'keep_db' in the config or via the $ENV{KEEP_DB} setting.
85              
86             Also note that if you specify a path that already exists, we will automatically
87             add the option 'force_drop_table', on the assumption you are roundtripping
88             tests to the same database file. This way you can avoid having to specifically
89             tell the system to delete the file each time.
90              
91             =back
92              
93             =head1 AUTHOR
94              
95             John Napiorkowski C<< <jjnapiork@cpan.org> >>
96              
97             =head1 CONTRIBUTORS
98              
99             Tristan Pratt
100              
101             =head1 COPYRIGHT & LICENSE
102              
103             Copyright 2009, John Napiorkowski C<< <jjnapiork@cpan.org> >>
104              
105             This program is free software; you can redistribute it and/or modify
106             it under the same terms as Perl itself.
107              
108             =cut