| 1 | package DBIx::Migration; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use base qw/Class::Accessor::Fast/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use File::Slurp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | __PACKAGE__->mk_accessors(qw/debug dir dsn password username/); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | DBIx::Migration - Seamless DB schema up- and downgrades | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # migrate.pl | ||||||||||||||||||||||||||||
| 20 | my $m = DBIx::Migration->new( | ||||||||||||||||||||||||||||
| 21 | { | ||||||||||||||||||||||||||||
| 22 | dsn => 'dbi:SQLite:/Users/sri/myapp/db/sqlite/myapp.db', | ||||||||||||||||||||||||||||
| 23 | dir => '/Users/sri/myapp/db/sqlite' | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | my $version = $m->version; # Get current version from database | ||||||||||||||||||||||||||||
| 28 | $m->migrate(2); # Migrate database to version 2 | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # /Users/sri/myapp/db/sqlite/schema_1_up.sql | ||||||||||||||||||||||||||||
| 31 | CREATE TABLE foo ( | ||||||||||||||||||||||||||||
| 32 | id INTEGER PRIMARY KEY, | ||||||||||||||||||||||||||||
| 33 | bar TEXT | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # /Users/sri/myapp/db/sqlite/schema_1_down.sql | ||||||||||||||||||||||||||||
| 37 | DROP TABLE foo; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # /Users/sri/myapp/db/sqlite/schema_2_up.sql | ||||||||||||||||||||||||||||
| 40 | CREATE TABLE bar ( | ||||||||||||||||||||||||||||
| 41 | id INTEGER PRIMARY KEY, | ||||||||||||||||||||||||||||
| 42 | baz TEXT | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # /Users/sri/myapp/db/sqlite/schema_2_down.sql | ||||||||||||||||||||||||||||
| 46 | DROP TABLE bar; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Seamless DB schema up- and downgrades. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head1 METHODS | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =over 4 | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =item $self->debug($debug) | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Enable/Disable debug messages. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =item $self->dir($dir) | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | Get/Set directory. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =item $self->dsn($dsn) | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Get/Set dsn. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =item $self->migrate($version) | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Migrate database to version. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub migrate { | ||||||||||||||||||||||||||||
| 75 | 6 | ▶ | my ( $self, $wanted ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
migrate: called CC=19 SCAR=33.4
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 6 | $self->_connect; | |||||||||||||||||||||||||||
| 77 | 6 | ▶ | $wanted = $self->_newest unless defined $wanted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 6 | my $version = $self->_version; | |||||||||||||||||||||||||||
| 79 | 6 | ▶ | if ( defined $version && ( $wanted eq $version ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $version and $wanted eq $version
MC/DC0%0/2
defined $version and $wanted eq $version
defined $version
$wanted eq $version
Truth table66%2/3
A and B
A= defined $versionB= $wanted eq $version
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | print "Database is already at version $wanted\n" if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | return 1; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 6 | ▶ | unless ( defined $version ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 1 | $self->_create_migration_table; | |||||||||||||||||||||||||||
| 86 | 1 | $version = 0; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # Up- or downgrade | ||||||||||||||||||||||||||||
| 90 | 6 | my @need; | |||||||||||||||||||||||||||
| 91 | 6 | my $type = 'down'; | |||||||||||||||||||||||||||
| 92 | 6 | ▶ | if ( $wanted > $version ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 3 | $type = 'up'; | |||||||||||||||||||||||||||
| 94 | 3 | $version += 1; | |||||||||||||||||||||||||||
| 95 | 3 | @need = $version .. $wanted; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||
| 98 | 3 | $wanted += 1; | |||||||||||||||||||||||||||
| 99 | 3 | @need = reverse( $wanted .. $version ); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 6 | my $files = $self->_files( $type, \@need ); | |||||||||||||||||||||||||||
| 102 | 6 | ▶ | if ( defined $files ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 6 | for my $file (@$files) { | |||||||||||||||||||||||||||
| 104 | 8 | my $name = $file->{name}; | |||||||||||||||||||||||||||
| 105 | 8 | my $ver = $file->{version}; | |||||||||||||||||||||||||||
| 106 | 8 | ▶ | print qq/Processing "$name"\n/ if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 8 | ▶ | next unless $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 8 | my $text = read_file($name); | |||||||||||||||||||||||||||
| 109 | 8 | $text =~ s/\s*--.*$//g; | |||||||||||||||||||||||||||
| 110 | 8 | for my $sql ( split /;/, $text ) { | |||||||||||||||||||||||||||
| 111 | 20 | ▶ | next unless $sql =~ /\w/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 12 | ▶ | print "$sql\n" if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 12 | $self->{_dbh}->do($sql); | |||||||||||||||||||||||||||
| 114 | 12 | ▶ | if ( $self->{_dbh}->err ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | die "Database error: " . $self->{_dbh}->errstr; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 8 | ▶ | $ver -= 1 if ( ( $ver > 0 ) && ( $type eq 'down' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ver > 0 and $type eq "down"
MC/DC50%1/2
$ver > 0 and $type eq "down"
$ver > 0
$type eq "down"
Truth table66%2/3
A and B
A= $ver > 0B= $type eq "down"
| |||||||||||||||||||||||||||||
| 119 | 8 | $self->_update_migration_table($ver); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||
| 123 | 0 | my $newver = $self->_version; | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | print "Database is at version $newver, couldn't migrate to $wanted\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->debug and $wanted != $newver
MC/DC0%0/2
$self->debug and $wanted != $newver
$self->debug
$wanted != $newver
Truth table0%0/3
A and B
A= $self->debugB= $wanted != $newver
| |||||||||||||||||||||||||||||
| 125 | if ( $self->debug && ( $wanted != $newver ) ); | ||||||||||||||||||||||||||||
| 126 | 0 | return 0; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 6 | $self->_disconnect; | |||||||||||||||||||||||||||
| 129 | 6 | return 1; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =item $self->password | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Get/Set database password. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =item $self->username($username) | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Get/Set database username. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =item $self->version | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Get migration version from database. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =cut | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | sub version { | ||||||||||||||||||||||||||||
| 147 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
version: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 7 | $self->_connect; | |||||||||||||||||||||||||||
| 149 | 7 | my $version = $self->_version; | |||||||||||||||||||||||||||
| 150 | 7 | $self->_disconnect; | |||||||||||||||||||||||||||
| 151 | 7 | return $version; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub _connect { | ||||||||||||||||||||||||||||
| 155 | 13 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_connect: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 156 | 13 | ▶ | $self->{_dbh} = DBI->connect( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | $self->dsn, | ||||||||||||||||||||||||||||
| 158 | $self->username, | ||||||||||||||||||||||||||||
| 159 | $self->password, | ||||||||||||||||||||||||||||
| 160 | { | ||||||||||||||||||||||||||||
| 161 | RaiseError => 0, | ||||||||||||||||||||||||||||
| 162 | PrintError => 0, | ||||||||||||||||||||||||||||
| 163 | AutoCommit => 1 | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | ) | ||||||||||||||||||||||||||||
| 166 | or die qq/Couldn't connect to database, "$!"/; | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub _create_migration_table { | ||||||||||||||||||||||||||||
| 170 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_create_migration_table: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 171 | 1 | $self->{_dbh}->do(<<"EOF"); | |||||||||||||||||||||||||||
| 172 | CREATE TABLE dbix_migration ( | ||||||||||||||||||||||||||||
| 173 | name CHAR(64) PRIMARY KEY, | ||||||||||||||||||||||||||||
| 174 | value CHAR(64) | ||||||||||||||||||||||||||||
| 175 | ); | ||||||||||||||||||||||||||||
| 176 | EOF | ||||||||||||||||||||||||||||
| 177 | 1 | $self->{_dbh}->do(<<"EOF"); | |||||||||||||||||||||||||||
| 178 | INSERT INTO dbix_migration ( name, value ) VALUES ( 'version', '0' ); | ||||||||||||||||||||||||||||
| 179 | EOF | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | sub _disconnect { | ||||||||||||||||||||||||||||
| 183 | 13 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_disconnect: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 184 | 13 | $self->{_dbh}->disconnect; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | sub _files { | ||||||||||||||||||||||||||||
| 188 | 6 | ▶ | my ( $self, $type, $need ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_files: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||
| 189 | 6 | my @files; | |||||||||||||||||||||||||||
| 190 | 6 | for my $i (@$need) { | |||||||||||||||||||||||||||
| 191 | 8 | ▶ | opendir(DIR, $self->dir) or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 8 | ▶ | while (my $file = readdir(DIR)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 48 | ▶ | next unless $file =~ /(^|\D)${i}_$type\.sql$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 8 | $file = File::Spec->catdir($self->dir, $file); | |||||||||||||||||||||||||||
| 195 | 8 | push @files, { name => $file, version => $i }; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | 8 | closedir(DIR); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | 6 | ▶ | return undef unless @$need == @files; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 6 | ▶ | return @files ? \@files : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | sub _newest { | ||||||||||||||||||||||||||||
| 204 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_newest: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 205 | 0 | my $newest = 0; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | ▶ | opendir(DIR, $self->dir) or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 0 | ▶ | while (my $file = readdir(DIR)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | next unless $file =~ /_up\.sql$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | $file =~ /\D*(\d+)_up.sql$/; | |||||||||||||||||||||||||||
| 211 | 0 | ▶ | $newest = $1 if $1 > $newest; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | 0 | closedir(DIR); | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 0 | return $newest; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | sub _update_migration_table { | ||||||||||||||||||||||||||||
| 219 | 8 | ▶ | my ( $self, $version ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_update_migration_table: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 220 | 8 | $self->{_dbh}->do(<<"EOF"); | |||||||||||||||||||||||||||
| 221 | UPDATE dbix_migration SET value = '$version' WHERE name = 'version'; | ||||||||||||||||||||||||||||
| 222 | EOF | ||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | sub _version { | ||||||||||||||||||||||||||||
| 226 | 13 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_version: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 227 | 13 | my $version = undef; | |||||||||||||||||||||||||||
| 228 | 13 | eval { | |||||||||||||||||||||||||||
| 229 | 13 | my $sth = $self->{_dbh}->prepare(<<"EOF"); | |||||||||||||||||||||||||||
| 230 | SELECT value FROM dbix_migration WHERE name = ?; | ||||||||||||||||||||||||||||
| 231 | EOF | ||||||||||||||||||||||||||||
| 232 | 13 | $sth->execute('version'); | |||||||||||||||||||||||||||
| 233 | 11 | for my $val ( $sth->fetchrow_arrayref ) { | |||||||||||||||||||||||||||
| 234 | 11 | $version = $val->[0]; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | }; | ||||||||||||||||||||||||||||
| 237 | 13 | return $version; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =back | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | Sebastian Riedel, C<sri@oook.de> | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | This program is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 249 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =cut | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 2 | 1; | |||||||||||||||||||||||||||