blib/lib/JiftyX/Fixtures/Script/Load.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 96 CC 4 SCAR 14.8
CC
4
Cov
0%
drop_db
12.0
before_run
6.0
options
2.0
CRAP 4.4
1 package JiftyX::Fixtures::Script::Load;
2 our $VERSION = '0.07';
3  
4 # ABSTRACT: load subcommand, primary function of this module
5  
6 use warnings;
7 use strict;
8  
9 use Jifty;
10 use Jifty::Everything;
11  
12 use File::Basename;
13 use File::Spec;
14 use YAML qw(Dump LoadFile);
15  
16 use base qw(
17 App::CLI::Command
18 );
19  
20 my $super = 'JiftyX::Fixtures::Script';
21  
22 our $help_msg = qq{
23 Usage:
24  
25 jiftyx-fixtures load [options]
26  
27 Options:
28  
29 -d, --drop-database: drop database before loading fixtures, default is true
30 -e, --environment: specify environment, default is development
31 -h, --help: show help
32  
33 };
34  
35 sub options {
36 my ($self) = @_;
37 return (
38 $super->options,
39 'd|drop-database=s' => 'drop-database',
40 'e|environment=s' => 'environment',
41 );
42 }
43  
44 sub before_run {
45 my ($self) = @_;
46  
47 $super->before_run($self);
48  
49 $self->{environment} ||= "development";
50 $self->{'drop-database'} ||= "true";
51 $self->drop_db() if ($self->{"drop-database"} eq "true");
52  
53 return;
54 }
55  
56 sub run {
57 my ($self) = @_;
58 $self->before_run();
59  
60 Jifty->new;
61  
62 for ($self->fixtures_files) {
63 my $filename = basename($_);
64 $filename =~ s/\.yml//;
65  
66 my $fixtures = LoadFile($_);
67  
68 my $model = Jifty->app_class("Model",$filename)->new;
69  
70 for my $entity (@{ $fixtures }) {
71 $model->create( %{$entity} );
72 }
73 }
74 }
75  
76 sub fixtures_files {
77 my $self = shift;
78 return glob(
79 File::Spec->catfile(
80 $self->{config}->{app_root},
81 "etc",
82 "fixtures",
83 $self->{environment},
84 "*"
85 )
86 );
87 }
88  
89 sub drop_db {
90 my $self = shift;
91 my $dbconfig = $self->{config}->{framework}->{Database};
92  
93 if ( $dbconfig->{Driver} eq "SQLite" && -e $dbconfig->{Database} ) {
94 print "WARN - SQLite Database has existed, delete file now.\n";
95 unlink $dbconfig->{Database};
96 }
97  
98 if ($dbconfig->{Driver} eq "MySQL") {
99 print "WARN - MySQL Database has existed, delete file now.\n";
100 unlink $dbconfig->{Database};
101 my $dbh = DBI->connect("dbi:mysql:database=".$dbconfig->{Database}, $dbconfig->{User}, $dbconfig->{Password});
102 $dbh->prepare("drop database ". $dbconfig->{Database});
103 $dbh->disconnect;
104 }
105 }
106  
107  
108 1;
109  
110 __END__