| 1 | =encoding utf8 | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | DBIx::Class::Storage::DBI::mysql::backup | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | package MyApp::Schema; | ||||||||||
| 10 | use base qw/DBIx::Class::Schema/; | ||||||||||
| 11 | |||||||||||
| 12 | our $VERSION = 0.001; | ||||||||||
| 13 | |||||||||||
| 14 | __PACKAGE__->load_classes(qw/CD Book DVD/); | ||||||||||
| 15 | __PACKAGE__->load_components(qw/ | ||||||||||
| 16 | Schema::Versioned | ||||||||||
| 17 | Storage::DBI::mysql::backup | ||||||||||
| 18 | /); | ||||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||
| 22 | |||||||||||
| 23 | Adds C<backup> method to L<DBIx::Class::Storage::DBI::mysql>. | ||||||||||
| 24 | |||||||||||
| 25 | This plugin enables L<DBIx::Class::Schema::Versioned/backup> when using MySQL. | ||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | =head1 FUNCTIONS | ||||||||||
| 29 | |||||||||||
| 30 | =cut | ||||||||||
| 31 | |||||||||||
| 32 | package DBIx::Class::Storage::DBI::mysql::backup; | ||||||||||
| 33 | |||||||||||
| 34 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | 1 | ▶ | use DBIx::Class::Storage::DBI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 1 | ▶ | use MySQL::Backup; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 1 | ▶ | use File::Path qw/mkpath/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 1 | ▶ | use Symbol; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | 1 | ▶ | use vars qw( $VERSION ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 1 | $VERSION = '0.04'; | |||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | =head2 backup ( $to_dir ) | ||||||||||
| 47 | |||||||||||
| 48 | writes SQL file as L</backup_filename> to $to_dir. returns SQL filename. | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | sub _backup { | ||||||||
|
Subroutine
_backup: not called CC=2 SCAR=17.9
| |||||||||||
| 53 | 0 | my ( $self, $dir ) = @_; | |||||||||
| 54 | 0 | ▶ | mkpath([$dir]) unless -d $dir; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 0 | my $filename = $self->backup_filename; | |||||||||
| 56 | 0 | my $fh = Symbol::gensym(); | |||||||||
| 57 | 0 | open $fh, ">$dir/$filename"; | |||||||||
| 58 | 0 | print $fh $self->dump; | |||||||||
| 59 | 0 | close $fh; | |||||||||
| 60 | 0 | $filename | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | |||||||||||
| 64 | =head2 backup_filename | ||||||||||
| 65 | |||||||||||
| 66 | returns filename of backup I<$DB_NAME-YYYYMMDD-hhmmss.sql> | ||||||||||
| 67 | |||||||||||
| 68 | =cut | ||||||||||
| 69 | |||||||||||
| 70 | 0 | ▶ | sub _backup_filename { | ||||||||
|
Subroutine
_backup_filename: not called CC=7 SCAR=40.3
| |||||||||||
| 71 | 0 | my $self = shift; | |||||||||
| 72 | 0 | my $dsn = $self->_dbi_connect_info->[0]; | |||||||||
| 73 | 0 | ▶ | my $dbname = $1 if($dsn =~ /^dbi:mysql:database=([^;]+)/i); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | ▶ | unless($dbname) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 0 | ▶ | $dbname = $1 if($dsn =~ /^dbi:mysql:dbname=([^;]+)/i); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | } | ||||||||||
| 77 | 0 | ▶ | unless($dbname) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 0 | ▶ | $dbname = $1 if($dsn =~ /^dbi:mysql:([^;]+)/i); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | } | ||||||||||
| 80 | 0 | ▶ | $self->throw_exception("Cannot determine name of mysql database") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | unless $dbname; | ||||||||||
| 82 | 0 | my @lt = localtime; | |||||||||
| 83 | 0 | my $filename = sprintf( | |||||||||
| 84 | "%s-%04d%02d%02d-%02d%02d%02d.sql", | ||||||||||
| 85 | $dbname, | ||||||||||
| 86 | $lt[5]+1900, $lt[4]+1, $lt[3], | ||||||||||
| 87 | $lt[2], $lt[1]+1, $lt[0], | ||||||||||
| 88 | ); | ||||||||||
| 89 | 0 | $filename | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | =head2 dump | ||||||||||
| 93 | |||||||||||
| 94 | returns dumped SQL | ||||||||||
| 95 | |||||||||||
| 96 | =cut | ||||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | sub _dump { | ||||||||
|
Subroutine
_dump: not called CC=1 SCAR=6.9
| |||||||||||
| 99 | 0 | my $self = shift; | |||||||||
| 100 | 0 | my $mb = MySQL::Backup->new_from_DBH( $self->dbh ,{'USE_REPLACE' => 1, 'SHOW_TABLE_NAMES' => 1}); | |||||||||
| 101 | 0 | $mb->create_structure() . $mb->data_backup() | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 1 | *DBIx::Class::Storage::DBI::dump = \&_dump; | |||||||||
| 105 | 1 | *DBIx::Class::Storage::DBI::backup = \&_backup; | |||||||||
| 106 | 1 | *DBIx::Class::Storage::DBI::backup_filename = \&_backup_filename; | |||||||||
| 107 | |||||||||||
| 108 | |||||||||||
| 109 | 1 | 1; | |||||||||
| 110 | __END__ | ||||||||||