File Coverage

blib/lib/Database/Migrator/mysql.pm
Criterion Covered Total %
statement 11 13 84.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 16 18 88.8


line stmt bran cond sub pod time code
1             package Database::Migrator::mysql;
2             {
3             $Database::Migrator::mysql::VERSION = '0.05';
4             }
5             BEGIN {
6 1     1   895 $Database::Migrator::mysql::AUTHORITY = 'cpan:DROLSKY';
7             }
8              
9 1     1   6 use strict;
  1         2  
  1         34  
10 1     1   6 use warnings;
  1         2  
  1         42  
11 1     1   892 use namespace::autoclean;
  1         22688  
  1         8  
12              
13 1     1   445 use Database::Migrator 0.07;
  0            
  0            
14             use Database::Migrator::Types qw( Str );
15             use DBD::mysql;
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__
140              
141             =pod
142              
143             =head1 NAME
144              
145             Database::Migrator::mysql - Database::Migrator implementation for MySQL
146              
147             =head1 VERSION
148              
149             version 0.05
150              
151             =head1 SYNOPSIS
152              
153             package MyApp::Migrator;
154              
155             use Moose;
156              
157             extends 'Database::Migrator::mysql';
158              
159             has '+database' => (
160             required => 0,
161             default => 'MyApp',
162             );
163              
164             =head1 DESCRIPTION
165              
166             This module provides a L<Database::Migrator> implementation for MySQL. See
167             L<Database::Migrator> and L<Database::Migrator::Core> for more documentation.
168              
169             =head1 ATTRIBUTES
170              
171             This class adds several attributes in addition to those implemented by
172             L<Database::Migrator::Core>:
173              
174             =over 4
175              
176             =item * character_set
177              
178             The character set of the database. This is only used when creating a new
179             database. This is optional.
180              
181             =item * collation
182              
183             The collation of the database. This is only used when creating a new
184             database. This is optional.
185              
186             =back
187              
188             =head1 AUTHOR
189              
190             Dave Rolsky <autarch@urth.org>
191              
192             =head1 COPYRIGHT AND LICENSE
193              
194             This software is Copyright (c) 2013 by MaxMind, Inc..
195              
196             This is free software, licensed under:
197              
198             The Artistic License 2.0 (GPL Compatible)
199              
200             =cut