| 1 | package DBIx::Diff::Schema; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | our $DATE = '2020-05-06'; # DATE | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | our $DIST = 'DBIx-Diff-Schema'; # DIST | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.097'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use strict 'subs', 'vars'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Log::ger; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use DBIx::Util::Schema qw(has_table list_tables list_columns); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use List::Util qw(first); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | diff_db_schema | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | diff_table_schema | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | db_schema_eq | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | table_schema_eq | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | our %SPEC; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | $SPEC{':package'} = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | v => 1.1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | summary => 'Compare schema of two DBI databases', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | my %arg0_dbh = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | dbh => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | schema => ['obj*'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | summary => 'DBI database handle', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | req => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | pos => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | my %arg1_table = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | table => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | schema => ['str*'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | summary => 'Table name', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | req => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | pos => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 2 | my %diff_db_args = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | dbh1 => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | schema => ['obj*'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | summary => 'DBI database handle for the first database', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | req => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | pos => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | dbh2 => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | schema => ['obj*'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | summary => 'DBI database handle for the second database', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | req => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | pos => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | my %diff_table_args = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | %diff_db_args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | table1 => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | schema => 'str*', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | summary => 'Table name', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | req => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | pos => 2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | table2 => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | schema => 'str*', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | summary => 'Second table name (assumed to be the same as first table name if unspecified)', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | pos => 3, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | sub _diff_column_schema { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 30 | ▶ | my ($c1, $c2) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_diff_column_schema: called CC=13 SCAR=30.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 30 | my $res = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 30 | ▶ | if ($c1->{TYPE_NAME} ne $c2->{TYPE_NAME}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | $res->{old_type} = $c1->{TYPE_NAME}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | $res->{new_type} = $c2->{TYPE_NAME}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 4 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 26 | ▶ | if ($c1->{NULLABLE} xor $c2->{NULLABLE}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%4/4
$c1->{"NULLABLE"} xor $c2->{"NULLABLE"}
MC/DC100%2/2
$c1->{"NULLABLE"} xor $c2->{"NULLABLE"}
$c1->{"NULLABLE"}
$c2->{"NULLABLE"}
Truth table100%4/4
A xor B
A= $c1->{"NULLABLE"}B= $c2->{"NULLABLE"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | $res->{old_nullable} = $c1->{NULLABLE}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | $res->{new_nullable} = $c2->{NULLABLE}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 26 | ▶ | if (defined $c1->{CHAR_OCTET_LENGTH}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($c1->{CHAR_OCTET_LENGTH} != $c2->{CHAR_OCTET_LENGTH}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $res->{old_length} = $c1->{CHAR_OCTET_LENGTH}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | $res->{new_length} = $c2->{CHAR_OCTET_LENGTH}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 26 | ▶ | if (defined $c1->{DECIMAL_DIGITS}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 6 | ▶ | if ($c1->{DECIMAL_DIGITS} != $c2->{DECIMAL_DIGITS}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $res->{old_digits} = $c1->{DECIMAL_DIGITS}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $res->{new_digits} = $c2->{DECIMAL_DIGITS}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 26 | ▶ | if (($c1->{mysql_is_auto_increment} // 0) != ($c2->{mysql_is_auto_increment} // 0)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$c1->{"mysql_is_auto_increment"} // 0
$c2->{"mysql_is_auto_increment"} // 0
MC/DC0%0/1
$c1->{"mysql_is_auto_increment"} // 0
$c1->{"mysql_is_auto_increment"}
MC/DC0%0/1
$c2->{"mysql_is_auto_increment"} // 0
$c2->{"mysql_is_auto_increment"}
Truth table50%1/2
A
A= $c1->{"mysql_is_auto_increment"}
Truth table50%1/2
A
A= $c2->{"mysql_is_auto_increment"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $res->{old_auto_increment} = $c1->{mysql_is_auto_increment} // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$c1->{"mysql_is_auto_increment"} // 0
MC/DC0%0/1
$c1->{"mysql_is_auto_increment"} // 0
$c1->{"mysql_is_auto_increment"}
Truth table0%0/2
A
A= $c1->{"mysql_is_auto_increment"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | $res->{new_auto_increment} = $c2->{mysql_is_auto_increment} // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$c2->{"mysql_is_auto_increment"} // 0
MC/DC0%0/1
$c2->{"mysql_is_auto_increment"} // 0
$c2->{"mysql_is_auto_increment"}
Truth table0%0/2
A
A= $c2->{"mysql_is_auto_increment"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 30 | $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | sub _diff_table_schema { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 11 | ▶ | my ($dbh1, $dbh2, $table1, $table2) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_diff_table_schema: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 11 | my @columns1 = list_columns($dbh1, $table1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 11 | my @columns2 = list_columns($dbh2, $table2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 11 | log_trace("columns1: %s ...", \@columns1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 11 | log_trace("columns2: %s ...", \@columns2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 11 | my (@added, @deleted, %modified); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 11 | for my $c1 (@columns1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 35 | my $c1n = $c1->{COLUMN_NAME}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 35 | ▶ | my $c2 = first {$c1n eq $_->{COLUMN_NAME}} @columns2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 35 | ▶ | if (defined $c2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 30 | my $tres = _diff_column_schema($c1, $c2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 30 | ▶ | $modified{$c1n} = $tres if %$tres; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 5 | push @deleted, $c1n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 11 | for my $c2 (@columns2) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 39 | my $c2n = $c2->{COLUMN_NAME}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 39 | ▶ | my $c1 = first {$c2n eq $_->{COLUMN_NAME}} @columns1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 39 | ▶ | if (defined $c1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 9 | push @added, $c2n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 11 | my $res = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 11 | ▶ | $res->{added_columns} = \@added if @added; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 11 | ▶ | $res->{deleted_columns} = \@deleted if @deleted; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 11 | ▶ | $res->{modified_columns} = \%modified if %modified; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 11 | $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | $SPEC{diff_table_schema} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 2 | v => 1.1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | summary => 'Compare schema of two DBI tables', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | This function compares schemas of two DBI tables. You supply two `DBI` database | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | handles along with table name and this function will return a hash: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | deleted_columns => [...], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | added_columns => [...], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | modified_columns => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | column1 => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | old_type => '...', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | new_type => '...', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | _ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | args => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | %diff_table_args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | args_as => "array", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | result_naked => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | "x.perinci.sub.wrapper.disable_validate_args" => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | sub diff_table_schema { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 2 | ▶ | my $dbh1 = shift; my $arg_err; { no warnings ('void');require Scalar::Util;((defined($dbh1)) ? 1 : (($arg_err //= "Required but not specified"),0)) && ((Scalar::Util::blessed($dbh1)) ? 1 : (($arg_err //= "Not of type object"),0)); if ($arg_err) { die "diff_table_schema(): " . "Invalid argument value for dbh1: $arg_err" } } # VALIDATE_ARG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.7
Subroutine
diff_table_schema: called CC=28 SCAR=43.7
Pod
documented
Branch: 4 branches
Condition0%0/4
$arg_err //= "Required but not specified"
$arg_err //= "Not of type object"
MC/DC0%0/1
$arg_err //= "Required but not specified"
$arg_err
MC/DC0%0/1
$arg_err //= "Not of type object"
$arg_err
Truth table0%0/2
A
A= $arg_err
Truth table0%0/2
A
A= $arg_err
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 2 | ▶ | my $dbh2 = shift; { no warnings ('void');((defined($dbh2)) ? 1 : (($arg_err //= "Required but not specified"),0)) && ((Scalar::Util::blessed($dbh2)) ? 1 : (($arg_err //= "Not of type object"),0)); if ($arg_err) { die "diff_table_schema(): " . "Invalid argument value for dbh2: $arg_err" } } # VALIDATE_ARG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.9
Branch: 4 branches
Condition0%0/4
$arg_err //= "Required but not specified"
$arg_err //= "Not of type object"
MC/DC0%0/1
$arg_err //= "Required but not specified"
$arg_err
MC/DC0%0/1
$arg_err //= "Not of type object"
$arg_err
Truth table0%0/2
A
A= $arg_err
Truth table0%0/2
A
A= $arg_err
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 2 | ▶ | my $table1 = shift; { no warnings ('void');((defined($table1)) ? 1 : (($arg_err //= "Required but not specified"),0)) && ((!ref($table1)) ? 1 : (($arg_err //= "Not of type text"),0)); if ($arg_err) { die "diff_table_schema(): " . "Invalid argument value for table1: $arg_err" } } # VALIDATE_ARG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.9
Branch: 4 branches
Condition0%0/4
$arg_err //= "Required but not specified"
$arg_err //= "Not of type text"
MC/DC0%0/1
$arg_err //= "Required but not specified"
$arg_err
MC/DC0%0/1
$arg_err //= "Not of type text"
$arg_err
Truth table0%0/2
A
A= $arg_err
Truth table0%0/2
A
A= $arg_err
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 2 | ▶ | my $table2 = shift // $table1; { no warnings ('void');((defined($table2)) ? 1 : (($arg_err //= "Required but not specified"),0)) && ((!ref($table2)) ? 1 : (($arg_err //= "Not of type text"),0)); if ($arg_err) { die "diff_table_schema(): " . "Invalid argument value for table2: $arg_err" } } # VALIDATE_ARG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.8
Branch: 4 branches
Condition28%2/7
shift() // $table1
$arg_err //= "Required but not specified"
$arg_err //= "Not of type text"
MC/DC0%0/2
shift() // $table1
shift()
$table1
MC/DC0%0/1
$arg_err //= "Required but not specified"
$arg_err
MC/DC0%0/1
$arg_err //= "Not of type text"
$arg_err
Truth table66%2/3
A // B
A= shift()B= $table1
Truth table0%0/2
A
A= $arg_err
Truth table0%0/2
A
A= $arg_err
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | #$log->tracef("Comparing table %s vs %s ...", $table1, $table2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 6 | ▶ | die "Table $table1 in first database does not exist" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | unless has_table($dbh1, $table1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 5 | ▶ | die "Table $table2 in second database does not exist" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | unless has_table($dbh2, $table2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 4 | _diff_table_schema($dbh1, $dbh2, $table1, $table2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | $SPEC{table_schema_eq} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 2 | v => 1.1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | summary => 'Return true if two DBI tables have the same schema', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | This is basically just a shortcut for: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | my $res = diff_table_schema(...); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | !%res; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | _ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | args => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | %diff_table_args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | args_as => "array", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | result_naked => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | "x.perinci.sub.wrapper.disable_validate_args" => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | sub table_schema_eq { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 2 | ▶ | my $res = diff_table_schema(@_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
table_schema_eq: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 2 | !%$res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | $SPEC{diff_db_schema} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 2 | v => 1.1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | summary => 'Compare schemas of two DBI databases', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | This function compares schemas of two DBI databases. You supply two `DBI` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | database handles and this function will return a hash: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | # list of tables found in first db but missing in second | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | deleted_tables => ['table1', ...], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # list of tables found only in the second db | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | added_tables => ['table2', ...], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | # list of modified tables, with details for each | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | modified_tables => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | table3 => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | deleted_columns => [...], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | added_columns => [...], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | modified_columns => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | column1 => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | old_type => '...', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | new_type => '...', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | _ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | args => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | %diff_db_args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | args_as => "array", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | result_naked => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | "x.perinci.sub.wrapper.disable_validate_args" => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | sub diff_db_schema { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 2 | ▶ | my $dbh1 = shift; my $arg_err; { no warnings ('void');require Scalar::Util;((defined($dbh1)) ? 1 : (($arg_err //= "Required but not specified"),0)) && ((Scalar::Util::blessed($dbh1)) ? 1 : (($arg_err //= "Not of type object"),0)); if ($arg_err) { die "diff_db_schema(): " . "Invalid argument value for dbh1: $arg_err" } } # VALIDATE_ARG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.7
Subroutine
diff_db_schema: called CC=21 SCAR=33.1
Pod
documented
Branch: 4 branches
Condition0%0/4
$arg_err //= "Required but not specified"
$arg_err //= "Not of type object"
MC/DC0%0/1
$arg_err //= "Required but not specified"
$arg_err
MC/DC0%0/1
$arg_err //= "Not of type object"
$arg_err
Truth table0%0/2
A
A= $arg_err
Truth table0%0/2
A
A= $arg_err
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 2 | ▶ | my $dbh2 = shift; { no warnings ('void');((defined($dbh2)) ? 1 : (($arg_err //= "Required but not specified"),0)) && ((Scalar::Util::blessed($dbh2)) ? 1 : (($arg_err //= "Not of type object"),0)); if ($arg_err) { die "diff_db_schema(): " . "Invalid argument value for dbh2: $arg_err" } } # VALIDATE_ARG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.9
Branch: 4 branches
Condition0%0/4
$arg_err //= "Required but not specified"
$arg_err //= "Not of type object"
MC/DC0%0/1
$arg_err //= "Required but not specified"
$arg_err
MC/DC0%0/1
$arg_err //= "Not of type object"
$arg_err
Truth table0%0/2
A
A= $arg_err
Truth table0%0/2
A
A= $arg_err
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 5 | my @tables1 = list_tables($dbh1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 5 | my @tables2 = list_tables($dbh2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 5 | log_trace("tables1: %s ...", \@tables1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 5 | log_trace("tables2: %s ...", \@tables2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 5 | my (@added, @deleted, %modified); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 5 | for my $t (@tables1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 10 | ▶ | if (grep {$_ eq $t} @tables2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | #$log->tracef("Comparing table %s ...", $_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 7 | my $tres = _diff_table_schema($dbh1, $dbh2, $t, $t); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 7 | ▶ | $modified{$t} = $tres if %$tres; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 3 | push @deleted, $t; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 5 | for my $t (@tables2) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 10 | ▶ | if (grep {$_ eq $t} @tables1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 3 | push @added, $t; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 5 | my $res = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 5 | ▶ | $res->{added_tables} = \@added if @added; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 5 | ▶ | $res->{deleted_tables} = \@deleted if @deleted; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 5 | ▶ | $res->{modified_tables} = \%modified if %modified; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 5 | $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | $SPEC{db_schema_eq} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 2 | v => 1.1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | summary => 'Return true if two DBI databases have the same schema', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | This is basically just a shortcut for: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | my $res = diff_db_schema(...); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | !%$res; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | _ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | args => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | %diff_db_args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | args_as => "array", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | result_naked => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | "x.perinci.sub.wrapper.disable_validate_args" => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | sub db_schema_eq { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 2 | ▶ | my $res = diff_db_schema(@_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
db_schema_eq: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 2 | !%$res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | # ABSTRACT: Compare schema of two DBI databases | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||