| 1 | package DBIx::SimpleMigration::Client; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 2 | our $VERSION = '1.0.2'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 8 | 1 | my $self = bless {}, shift; | |||||||||
| 9 | 1 | ▶ | return unless @_ % 2 == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 10 | 1 | my %args = @_; | |||||||||
| 11 | |||||||||||
| 12 | 1 | $self->{dbh} = $args{dbh}; | |||||||||
| 13 | 1 | $self->{options} = $args{options}; | |||||||||
| 14 | |||||||||||
| 15 | 1 | return $self; | |||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | sub _create_migrations_table { | ||||||||
|
Subroutine
_create_migrations_table: called CC=2 SCAR=7.0
| |||||||||||
| 19 | 1 | my ($self) = @_; | |||||||||
| 20 | |||||||||||
| 21 | my $query = ' | ||||||||||
| 22 | 1 | CREATE TABLE IF NOT EXISTS ' . $self->{options}->{migrations_table} . ' ( | |||||||||
| 23 | name varchar(255) primary key not null, | ||||||||||
| 24 | applied datetime not null | ||||||||||
| 25 | ) | ||||||||||
| 26 | '; | ||||||||||
| 27 | |||||||||||
| 28 | 1 | ▶ | $self->{dbh}->do($query) or croak __PACKAGE__ . ': Error creating table: ' . $self->{dbh}->errstr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | |||||||||||
| 30 | 1 | return $self; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 0 | ▶ | sub _migrations_table_exists { | ||||||||
|
Subroutine
_migrations_table_exists: not called CC=1 SCAR=6.9
| |||||||||||
| 34 | 0 | my ($self) = @_; | |||||||||
| 35 | |||||||||||
| 36 | 0 | return 0; # checking seems to be largely driver-dependent | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 2 | ▶ | sub _lock_migrations_table { | ||||||||
|
Subroutine
_lock_migrations_table: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 2 | my ($self) = @_; | |||||||||
| 41 | |||||||||||
| 42 | 2 | return 1; # to be overloaded per-DB | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 1 | ▶ | sub _applied_migrations { | ||||||||
|
Subroutine
_applied_migrations: called CC=2 SCAR=7.0
| |||||||||||
| 46 | 1 | my ($self) = @_; | |||||||||
| 47 | |||||||||||
| 48 | my $query = ' | ||||||||||
| 49 | SELECT name, applied | ||||||||||
| 50 | 1 | FROM ' . $self->{options}->{migrations_table}; | |||||||||
| 51 | |||||||||||
| 52 | 1 | my $result = $self->{dbh}->selectall_arrayref($query, {Slice => {}}); | |||||||||
| 53 | |||||||||||
| 54 | 1 | my %applied_migrations; | |||||||||
| 55 | 1 | foreach my $row (@{$result}) { | |||||||||
| 56 | 0 | $applied_migrations{$row->{name}} = $row->{applied}; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 1 | return \%applied_migrations; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 2 | ▶ | sub _insert_migration { | ||||||||
|
Subroutine
_insert_migration: called CC=2 SCAR=7.3
| |||||||||||
| 63 | 2 | my ($self, $key) = @_; | |||||||||
| 64 | |||||||||||
| 65 | 2 | eval { | |||||||||
| 66 | 2 | my $query = 'INSERT INTO ' . $self->{options}->{migrations_table} . ' (name, applied) VALUES (?, CURRENT_TIMESTAMP)'; | |||||||||
| 67 | 2 | my $sth = $self->{dbh}->prepare($query); | |||||||||
| 68 | 2 | $sth->execute($key); | |||||||||
| 69 | }; | ||||||||||
| 70 | |||||||||||
| 71 | 2 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | carp __PACKAGE__ . ': Error recording migration: ' . $self->{dbh}->errstr; | |||||||||
| 73 | 0 | return; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 2 | return 1; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 2 | 1; | |||||||||
| 80 | |||||||||||
| 81 | __END__ | ||||||||||