blib/lib/Database/Migrator/mysql.pm

filter: Statementstmt 89.4% 17 / 19 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.3% 24 / 26 CC 1 SCAR 0.4
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Database::Migrator::mysql;
2 {
3 $Database::Migrator::mysql::VERSION = '0.05';
4 }
5 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 $Database::Migrator::mysql::AUTHORITY = 'cpan:DROLSKY';
7 }
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Database::Migrator 0.07;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Database::Migrator::Types qw( Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 0 ▶use DBD::mysql;
Subroutine BEGIN: called CC=1 SCAR=2.6
16 use DBI;
17 use File::Slurp qw( read_file );
18 use IPC::Run3 qw( run3 );
19  
20 use Moose;
21  
22 with 'Database::Migrator::Core';
23  
24 has character_set => (
25 is => 'ro',
26 isa => Str,
27 predicate => '_has_character_set',
28 );
29  
30 has collation => (
31 is => 'ro',
32 isa => Str,
33 predicate => '_has_collation',
34 );
35  
36 sub _create_database {
37 my $self = shift;
38  
39 my $database = $self->database();
40  
41 $self->logger()->info("Creating the $database database");
42  
43 my $create_ddl = "CREATE DATABASE $database";
44 $create_ddl .= ' CHARACTER SET = ' . $self->character_set()
45 if $self->_has_character_set();
46 $create_ddl .= ' COLLATE = ' . $self->collation()
47 if $self->_has_collation();
48  
49 $self->_run_command(
50 [ $self->_cli_args(), qw( --batch -e ), $create_ddl ] );
51  
52 return;
53 }
54  
55 sub _drop_database {
56 my $self = shift;
57  
58 my $database = $self->database();
59  
60 $self->logger()->info("Dropping the $database database");
61  
62 my $drop_ddl = "DROP DATABASE IF EXISTS $database";
63  
64 $self->_run_command(
65 [ $self->_cli_args(), qw( --batch -e ), $drop_ddl ] );
66 }
67  
68 sub _run_ddl {
69 my $self = shift;
70 my $file = shift;
71  
72 my $ddl = read_file( $file->stringify() );
73  
74 $self->_run_command(
75 [ $self->_cli_args(), '--database', $self->database(), '--batch' ],
76 $ddl,
77 );
78 }
79  
80 sub _cli_args {
81 my $self = shift;
82  
83 my @cli = 'mysql';
84 push @cli, '-u' . $self->username() if defined $self->username();
85 push @cli, '-p' . $self->password() if defined $self->password();
86 push @cli, '-h' . $self->host() if defined $self->host();
87 push @cli, '-P' . $self->port() if defined $self->port();
88  
89 return @cli;
90 }
91  
92 sub _run_command {
93 my $self = shift;
94 my $command = shift;
95 my $input = shift;
96  
97 my $stdout = q{};
98 my $stderr = q{};
99  
100 my $handle_stdout = sub {
101 $self->logger()->debug(@_);
102  
103 $stdout .= $_ for @_;
104 };
105  
106 my $handle_stderr = sub {
107 $self->logger()->debug(@_);
108  
109 $stderr .= $_ for @_;
110 };
111  
112 $self->logger()->debug("Running command: [@{$command}]");
113  
114 return if $self->dry_run();
115  
116 run3( $command, \$input, $handle_stdout, $handle_stderr );
117  
118 if ($?) {
119 my $exit = $? >> 8;
120  
121 my $msg = "@{$command} returned an exit code of $exit\n";
122 $msg .= "\nSTDOUT:\n$stdout\n\n" if length $stdout;
123 $msg .= "\nSTDERR:\n$stderr\n\n" if length $stderr;
124  
125 die $msg;
126 }
127  
128 return $stdout;
129 }
130  
131 sub _driver_name { 'mysql' }
132  
133 __PACKAGE__->meta()->make_immutable();
134  
135 1;
136  
137 #ABSTRACT: Database::Migrator implementation for MySQL
138  
139 __END__
↑ Summary