| 1 | # vim: set ts=2 sts=2 sw=2 expandtab smarttab: | ||||||||||||
| 2 | # | ||||||||||||
| 3 | # This file is part of DBIx-Schema-UpToDate | ||||||||||||
| 4 | # | ||||||||||||
| 5 | # This software is copyright (c) 2011 by Randy Stauner. | ||||||||||||
| 6 | # | ||||||||||||
| 7 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 8 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 9 | # | ||||||||||||
| 10 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | package DBIx::Schema::UpToDate; | ||||||||||||
| 14 | 3 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 3 | $DBIx::Schema::UpToDate::VERSION = '1.001'; | |||||||||||
| 16 | } | ||||||||||||
| 17 | 3 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 3 | $DBIx::Schema::UpToDate::AUTHORITY = 'cpan:RWSTAUNER'; | |||||||||||
| 19 | } | ||||||||||||
| 20 | # ABSTRACT: Helps keep a database schema up to date | ||||||||||||
| 21 | |||||||||||||
| 22 | 3 | ▶ | use Carp qw(croak carp); # core | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | |||||||||||||
| 24 | |||||||||||||
| 25 | 1 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||
| 26 | 1 | my $class = shift; | |||||||||||
| 27 | my $self = { | ||||||||||||
| 28 | auto_update => 1, | ||||||||||||
| 29 | sql_limit => 1, | ||||||||||||
| 30 | transactions => 1, | ||||||||||||
| 31 | 0 | ▶ | @_ == 1 ? %{$_[0]} : @_ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | }; | ||||||||||||
| 33 | 1 | bless $self, $class; | |||||||||||
| 34 | |||||||||||||
| 35 | # make sure the database schema is current | ||||||||||||
| 36 | $self->up_to_date() | ||||||||||||
| 37 | 1 | ▶ | if $self->{auto_update}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | |||||||||||||
| 39 | 1 | return $self; | |||||||||||
| 40 | } | ||||||||||||
| 41 | |||||||||||||
| 42 | |||||||||||||
| 43 | 3 | foreach my $action ( qw(begin_work commit) ){ | |||||||||||
| 44 | 3 | ▶ | no strict 'refs'; ## no critic (NoStrict) | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 45 | 24 | ▶ | *$action = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 46 | 24 | my ($self) = @_; | |||||||||||
| 47 | 24 | ▶ | if( $self->{transactions} ){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | 22 | my $dbh = $self->dbh; | |||||||||||
| 49 | 22 | ▶ | $dbh->$action() | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | or croak $dbh->errstr; | ||||||||||||
| 51 | } | ||||||||||||
| 52 | } | ||||||||||||
| 53 | 6 | } | |||||||||||
| 54 | |||||||||||||
| 55 | |||||||||||||
| 56 | |||||||||||||
| 57 | 81 | ▶ | sub dbh { | ||||||||||
|
Subroutine
dbh: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 58 | 81 | my ($self) = @_; | |||||||||||
| 59 | 81 | return $self->{dbh}; | |||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | |||||||||||||
| 63 | 9 | ▶ | sub current_version { | ||||||||||
|
Subroutine
current_version: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 64 | 9 | my ($self) = @_; | |||||||||||
| 65 | 9 | my $dbh = $self->dbh; | |||||||||||
| 66 | 9 | my $table = $self->version_table_name; | |||||||||||
| 67 | 9 | my $version; | |||||||||||
| 68 | |||||||||||||
| 69 | 9 | my $tables = $dbh->table_info('%', '%', $table, 'TABLE') | |||||||||||
| 70 | ->fetchall_arrayref; | ||||||||||||
| 71 | |||||||||||||
| 72 | # if table exists query it for current database version | ||||||||||||
| 73 | 9 | ▶ | if( @$tables ){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 5 | my $qtable = $self->quoted_table_name; | |||||||||||
| 75 | 5 | my $field = $dbh->quote_identifier('version'); | |||||||||||
| 76 | |||||||||||||
| 77 | my $v = $dbh->selectcol_arrayref( | ||||||||||||
| 78 | "SELECT $field from $qtable ORDER BY $field DESC" | ||||||||||||
| 79 | 5 | ▶ | . ($self->{sql_limit} ? ' LIMIT 1' : '') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | )->[0]; | ||||||||||||
| 81 | 5 | ▶ | $version = $v | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | if defined $v; | ||||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | 9 | return $version; | |||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | |||||||||||||
| 89 | 3 | ▶ | sub initialize_version_table { | ||||||||||
|
Subroutine
initialize_version_table: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 90 | 3 | my ($self) = @_; | |||||||||||
| 91 | 3 | my $dbh = $self->dbh; | |||||||||||
| 92 | |||||||||||||
| 93 | 3 | my ($version, $updated) = $self->quote_identifiers(qw(version updated)); | |||||||||||
| 94 | |||||||||||||
| 95 | 3 | $self->begin_work(); | |||||||||||
| 96 | |||||||||||||
| 97 | 3 | ▶ | $dbh->do('CREATE TABLE ' . $self->quoted_table_name . | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | " ($version integer, $updated timestamp)" | ||||||||||||
| 99 | ) | ||||||||||||
| 100 | or croak $dbh->errstr; | ||||||||||||
| 101 | |||||||||||||
| 102 | 2 | $self->set_version(0); | |||||||||||
| 103 | |||||||||||||
| 104 | 2 | $self->commit(); | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | |||||||||||||
| 108 | 5 | ▶ | sub latest_version { | ||||||||||
|
Subroutine
latest_version: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 109 | 5 | my ($self) = @_; | |||||||||||
| 110 | 5 | return scalar @{ $self->updates }; | |||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | |||||||||||||
| 114 | 20 | ▶ | sub quoted_table_name { | ||||||||||
|
Subroutine
quoted_table_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 115 | 20 | my ($self) = @_; | |||||||||||
| 116 | 20 | return $self->dbh->quote_identifier($self->version_table_name); | |||||||||||
| 117 | } | ||||||||||||
| 118 | |||||||||||||
| 119 | |||||||||||||
| 120 | 15 | ▶ | sub quote_identifiers { | ||||||||||
|
Subroutine
quote_identifiers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 121 | 15 | my ($self, @names) = @_; | |||||||||||
| 122 | 15 | my $dbh = $self->dbh; | |||||||||||
| 123 | 15 | return map { $dbh->quote_identifier($_) } @names; | |||||||||||
| 124 | } | ||||||||||||
| 125 | |||||||||||||
| 126 | |||||||||||||
| 127 | 12 | ▶ | sub set_version { | ||||||||||
|
Subroutine
set_version: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 128 | 12 | my ($self, $version) = @_; | |||||||||||
| 129 | 12 | my $dbh = $self->dbh; | |||||||||||
| 130 | |||||||||||||
| 131 | 12 | ▶ | $dbh->do('INSERT INTO ' . $self->quoted_table_name . | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 132 | ' (' . | ||||||||||||
| 133 | join(', ', $self->quote_identifiers(qw(version updated))) | ||||||||||||
| 134 | . ') VALUES(?, ?)', | ||||||||||||
| 135 | {}, $version, time() | ||||||||||||
| 136 | ) | ||||||||||||
| 137 | or croak $dbh->errstr; | ||||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||
| 140 | |||||||||||||
| 141 | 15 | ▶ | sub updates { | ||||||||||
|
Subroutine
updates: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 142 | 15 | my ($self) = @_; | |||||||||||
| 143 | return $self->{updates} ||= [ | ||||||||||||
| 144 | 15 | ▶ | ]; | ||||||||||
|
Condition100%2/2
$self->{"updates"} ||= []
MC/DC100%1/1
$self->{"updates"} ||= []
$self->{"updates"}
Truth table100%2/2
A
A= $self->{"updates"}
| |||||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | |||||||||||||
| 148 | 10 | ▶ | sub update_to_version { | ||||||||||
|
Subroutine
update_to_version: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 149 | 10 | my ($self, $version) = @_; | |||||||||||
| 150 | |||||||||||||
| 151 | 10 | $self->begin_work(); | |||||||||||
| 152 | |||||||||||||
| 153 | # execute updates to bring database to $version | ||||||||||||
| 154 | 9 | $self->updates->[$version - 1]->($self); | |||||||||||
| 155 | |||||||||||||
| 156 | # save the version now in case we get interrupted before the next commit | ||||||||||||
| 157 | 9 | $self->set_version($version); | |||||||||||
| 158 | |||||||||||||
| 159 | 9 | $self->commit(); | |||||||||||
| 160 | } | ||||||||||||
| 161 | |||||||||||||
| 162 | |||||||||||||
| 163 | 4 | ▶ | sub up_to_date { | ||||||||||
|
Subroutine
up_to_date: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 164 | 4 | my ($self) = @_; | |||||||||||
| 165 | |||||||||||||
| 166 | 4 | my $current = $self->current_version; | |||||||||||
| 167 | 4 | ▶ | if( !defined($current) ){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 168 | 2 | $self->initialize_version_table; | |||||||||||
| 169 | 2 | $current = $self->current_version; | |||||||||||
| 170 | 2 | ▶ | die("Unable to initialize version table\n") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 171 | if !defined($current); | ||||||||||||
| 172 | } | ||||||||||||
| 173 | |||||||||||||
| 174 | 3 | my $latest = $self->latest_version; | |||||||||||
| 175 | |||||||||||||
| 176 | # execute each update required to go from current to latest version | ||||||||||||
| 177 | # (starting with next version, obviously (don't redo current)) | ||||||||||||
| 178 | $self->update_to_version($_) | ||||||||||||
| 179 | 3 | foreach ($current + 1) .. $latest; | |||||||||||
| 180 | } | ||||||||||||
| 181 | |||||||||||||
| 182 | |||||||||||||
| 183 | 29 | ▶ | sub version_table_name { | ||||||||||
|
Subroutine
version_table_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 184 | 29 | 'schema_version' | |||||||||||
| 185 | } | ||||||||||||
| 186 | |||||||||||||
| 187 | 3 | 1; | |||||||||||
| 188 | |||||||||||||
| 189 | |||||||||||||
| 190 | __END__ | ||||||||||||