| 1 | package DBIO::MySQL::Diff; | ||||||||||||||
| 2 | # ABSTRACT: Compare two introspected MySQL/MariaDB models | ||||||||||||||
| 3 | |||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 4 | ▶ | use base 'DBIO::Diff::Base'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 4 | ▶ | use DBIO::MySQL::Diff::Table; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 4 | ▶ | use DBIO::MySQL::Diff::Column; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 4 | ▶ | use DBIO::MySQL::Diff::Index; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 4 | ▶ | use DBIO::MySQL::Diff::ForeignKey; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 4 | ▶ | use DBIO::MySQL::Adapter; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 4 | my $ADAPTER = DBIO::MySQL::Adapter->new; | |||||||||||||
| 16 | |||||||||||||||
| 17 | |||||||||||||||
| 18 | |||||||||||||||
| 19 | # Parse adapter native_type (e.g. "BIGINT", "CHAR(32)", "DECIMAL(10,2)") into | ||||||||||||||
| 20 | # the lowercase data_type + column_type pair that information_schema reports. | ||||||||||||||
| 21 | 0 | ▶ | sub _native_to_mysql_types { | ||||||||||||
|
Subroutine
_native_to_mysql_types: not called CC=10 SCAR=47.0
| |||||||||||||||
| 22 | 0 | my ($native) = @_; | |||||||||||||
| 23 | 0 | my $lc = lc $native; | |||||||||||||
| 24 | |||||||||||||||
| 25 | # BIGINT → bigint / bigint(20) | ||||||||||||||
| 26 | 0 | ▶ | return ('bigint', 'bigint(20)') if $lc eq 'bigint'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 27 | |||||||||||||||
| 28 | # TINYINT(1) → tinyint / tinyint(1) | ||||||||||||||
| 29 | 0 | ▶ | return ('tinyint', 'tinyint(1)') if $lc eq 'tinyint(1)'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | |||||||||||||||
| 31 | # DOUBLE → double / double | ||||||||||||||
| 32 | 0 | ▶ | return ('double', 'double') if $lc eq 'double'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | # LONGTEXT → longtext / longtext | ||||||||||||||
| 35 | 0 | ▶ | return ('longtext', 'longtext') if $lc eq 'longtext'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | |||||||||||||||
| 37 | # LONGBLOB → longblob / longblob | ||||||||||||||
| 38 | 0 | ▶ | return ('longblob', 'longblob') if $lc eq 'longblob'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | |||||||||||||||
| 40 | # DATETIME → datetime / datetime | ||||||||||||||
| 41 | 0 | ▶ | return ('datetime', 'datetime') if $lc eq 'datetime'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | |||||||||||||||
| 43 | # CHAR(n) → char / char(n) | ||||||||||||||
| 44 | 0 | ▶ | if ($lc =~ /\Achar\((\d+)\)\z/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 0 | return ('char', "char($1)"); | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | # DECIMAL(p,s) → decimal / decimal(p,s) | ||||||||||||||
| 49 | 0 | ▶ | if ($lc =~ /\Adecimal\((\d+),(\d+)\)\z/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | 0 | return ('decimal', "decimal($1,$2)"); | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | |||||||||||||||
| 53 | # Bare DECIMAL (no precision) | ||||||||||||||
| 54 | 0 | ▶ | return ('decimal', 'decimal') if $lc eq 'decimal'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | |||||||||||||||
| 56 | # Fallback: lowercase as-is for both | ||||||||||||||
| 57 | 0 | (my $bare = $lc) =~ s/\(.*//; | |||||||||||||
| 58 | 0 | return ($bare, $lc); | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | # Types whose charset/collation is NULL in information_schema (binary/numeric/datetime). | ||||||||||||||
| 62 | # Lives in DBIO::MySQL::Adapter as %NO_CHARSET, exposed via $ADAPTER->no_charset_for. | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 0 | ▶ | sub target_from_compiled { | ||||||||||||
|
Subroutine
target_from_compiled: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||
| 65 | 0 | my ($class, $compiled) = @_; | |||||||||||||
| 66 | 0 | my (%tables, %columns); | |||||||||||||
| 67 | |||||||||||||||
| 68 | 0 | ▶ | for my $tname (keys %{ $compiled->{tables} // {} }) { | ||||||||||||
|
Condition0%0/2
$compiled->{"tables"} // {}
MC/DC0%0/1
$compiled->{"tables"} // {}
$compiled->{"tables"}
Truth table0%0/2
A
A= $compiled->{"tables"}
| |||||||||||||||
| 69 | 0 | my $t = $compiled->{tables}{$tname}; | |||||||||||||
| 70 | 0 | $tables{$tname} = { table_name => $tname }; | |||||||||||||
| 71 | |||||||||||||||
| 72 | 0 | my @cols; | |||||||||||||
| 73 | 0 | ▶ | for my $c (@{ $t->{columns} // [] }) { | ||||||||||||
|
Condition0%0/2
$t->{"columns"} // []
MC/DC0%0/1
$t->{"columns"} // []
$t->{"columns"}
Truth table0%0/2
A
A= $t->{"columns"}
| |||||||||||||||
| 74 | 0 | my ($data_type, $column_type) = _native_to_mysql_types($c->{native_type}); | |||||||||||||
| 75 | |||||||||||||||
| 76 | # charset/collation: leave undef for binary/numeric types so that the | ||||||||||||||
| 77 | # comparison (_norm returns '') equals the undef in the live introspect. | ||||||||||||||
| 78 | # For text/char types the live DB will have a server-assigned charset; | ||||||||||||||
| 79 | # we also leave undef here — see ESCALATION NOTE below. | ||||||||||||||
| 80 | 0 | my ($charset, $collation) = (undef, undef); | |||||||||||||
| 81 | 0 | ▶ | if ($ADAPTER->no_charset_for($c->{native_type})) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 0 | $charset = $collation = undef; # already undef; explicit for clarity | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | # A portable schema prescribes a default only when it declares one; when it | ||||||||||||||
| 86 | # does not, leave default_value undef so the always-on desired-state | ||||||||||||||
| 87 | # contract in DBIO::Diff::Compare skips it and leaves whatever the live | ||||||||||||||
| 88 | # column reports alone. information_schema reports SQL NULL (-> undef) as | ||||||||||||||
| 89 | # column_default for a nullable column with no explicit DEFAULT clause, so | ||||||||||||||
| 90 | # synthesising a literal string 'NULL' here produced a phantom MODIFY of | ||||||||||||||
| 91 | # every such column against that undef. | ||||||||||||||
| 92 | 0 | ▶ | my $not_null = ($c->{not_null} ? 1 : 0); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | 0 | my $default = $c->{default}; | |||||||||||||
| 94 | |||||||||||||||
| 95 | push @cols, { | ||||||||||||||
| 96 | column_name => $c->{column_name}, | ||||||||||||||
| 97 | data_type => $data_type, | ||||||||||||||
| 98 | column_type => $column_type, | ||||||||||||||
| 99 | not_null => $not_null, | ||||||||||||||
| 100 | default_value => $default, | ||||||||||||||
| 101 | is_pk => ($c->{is_pk} ? 1 : 0), | ||||||||||||||
| 102 | 0 | ▶ | is_auto_increment => ($c->{auto_increment} ? 1 : 0), | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 103 | character_set => $charset, | ||||||||||||||
| 104 | collation => $collation, | ||||||||||||||
| 105 | }; | ||||||||||||||
| 106 | } | ||||||||||||||
| 107 | 0 | $columns{$tname} = \@cols; | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | # ESCALATION NOTE: MySQL/MariaDB assigns character_set and collation from the | ||||||||||||||
| 111 | # database/server defaults to every text-family column (CHAR, VARCHAR, TEXT, | ||||||||||||||
| 112 | # LONGTEXT, …). A portable schema does not prescribe charset/collation, so | ||||||||||||||
| 113 | # target_from_compiled cannot derive these values from the schema alone. | ||||||||||||||
| 114 | # changed_column_fields() compares character_set and collation; leaving them undef in | ||||||||||||||
| 115 | # the target produces phantom diffs for text columns whenever the live DB | ||||||||||||||
| 116 | # reports a non-null charset (e.g. utf8mb4 / utf8mb4_uca1400_ai_ci on MariaDB | ||||||||||||||
| 117 | # 11.8). This is a design question for the controller: should the desired-state | ||||||||||||||
| 118 | # diff ignore attributes the target leaves unspecified? | ||||||||||||||
| 119 | |||||||||||||||
| 120 | return { | ||||||||||||||
| 121 | 0 | tables => \%tables, | |||||||||||||
| 122 | columns => \%columns, | ||||||||||||||
| 123 | indexes => {}, | ||||||||||||||
| 124 | foreign_keys => {}, | ||||||||||||||
| 125 | }; | ||||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | 4 | ▶ | sub _build_operations { | ||||||||||||
|
Subroutine
_build_operations: called CC=1 SCAR=0.0
| |||||||||||||||
| 129 | 4 | my ($self) = @_; | |||||||||||||
| 130 | 4 | my @ops; | |||||||||||||
| 131 | |||||||||||||||
| 132 | push @ops, DBIO::MySQL::Diff::Table->diff( | ||||||||||||||
| 133 | $self->source->{tables}, $self->target->{tables}, | ||||||||||||||
| 134 | $self->target->{columns}, $self->target->{foreign_keys}, | ||||||||||||||
| 135 | 4 | ); | |||||||||||||
| 136 | push @ops, DBIO::MySQL::Diff::Column->diff( | ||||||||||||||
| 137 | $self->source->{columns}, $self->target->{columns}, | ||||||||||||||
| 138 | $self->source->{tables}, $self->target->{tables}, | ||||||||||||||
| 139 | 4 | ); | |||||||||||||
| 140 | push @ops, DBIO::MySQL::Diff::Index->diff( | ||||||||||||||
| 141 | $self->source->{indexes}, $self->target->{indexes}, | ||||||||||||||
| 142 | $self->source->{tables}, $self->target->{tables}, | ||||||||||||||
| 143 | 4 | ); | |||||||||||||
| 144 | push @ops, DBIO::MySQL::Diff::ForeignKey->diff( | ||||||||||||||
| 145 | $self->source->{foreign_keys}, $self->target->{foreign_keys}, | ||||||||||||||
| 146 | $self->source->{tables}, $self->target->{tables}, | ||||||||||||||
| 147 | 4 | ); | |||||||||||||
| 148 | |||||||||||||||
| 149 | 4 | return \@ops; | |||||||||||||
| 150 | } | ||||||||||||||
| 151 | |||||||||||||||
| 152 | 4 | 1; | |||||||||||||
| 153 | |||||||||||||||
| 154 | __END__ | ||||||||||||||
| 155 | |||||||||||||||
| 156 | =pod | ||||||||||||||
| 157 | |||||||||||||||
| 158 | =encoding UTF-8 | ||||||||||||||
| 159 | |||||||||||||||
| 160 | =head1 NAME | ||||||||||||||
| 161 | |||||||||||||||
| 162 | DBIO::MySQL::Diff - Compare two introspected MySQL/MariaDB models | ||||||||||||||
| 163 | |||||||||||||||
| 164 | =head1 VERSION | ||||||||||||||
| 165 | |||||||||||||||
| 166 | version 0.900001 | ||||||||||||||
| 167 | |||||||||||||||
| 168 | =head1 DESCRIPTION | ||||||||||||||
| 169 | |||||||||||||||
| 170 | C<DBIO::MySQL::Diff> compares two introspected MySQL/MariaDB models | ||||||||||||||
| 171 | (as produced by L<DBIO::MySQL::Introspect>) and produces a list of | ||||||||||||||
| 172 | structured diff operations. | ||||||||||||||
| 173 | |||||||||||||||
| 174 | my $diff = DBIO::MySQL::Diff->new( | ||||||||||||||
| 175 | source => $current_model, | ||||||||||||||
| 176 | target => $desired_model, | ||||||||||||||
| 177 | ); | ||||||||||||||
| 178 | |||||||||||||||
| 179 | if ($diff->has_changes) { | ||||||||||||||
| 180 | print $diff->as_sql; | ||||||||||||||
| 181 | print $diff->summary; | ||||||||||||||
| 182 | } | ||||||||||||||
| 183 | |||||||||||||||
| 184 | Operations are emitted in dependency order: tables, columns, indexes, | ||||||||||||||
| 185 | foreign keys. | ||||||||||||||
| 186 | |||||||||||||||
| 187 | =head1 METHODS | ||||||||||||||
| 188 | |||||||||||||||
| 189 | =head2 target_from_compiled | ||||||||||||||
| 190 | |||||||||||||||
| 191 | my $target = DBIO::MySQL::Diff->target_from_compiled($compiled_model); | ||||||||||||||
| 192 | |||||||||||||||
| 193 | Translates the neutral model from L<DBIO::Schema::ModelCompiler> into the | ||||||||||||||
| 194 | MySQL/MariaDB introspect-shaped model that C<diff> consumes. | ||||||||||||||
| 195 | |||||||||||||||
| 196 | The compiled model uses C<native_type> (as produced by L<DBIO::MySQL::Adapter>, | ||||||||||||||
| 197 | e.g. C<BIGINT>, C<CHAR(32)>, C<TINYINT(1)>). This method maps those into the | ||||||||||||||
| 198 | exact lowercase C<data_type> and parameterised C<column_type> strings that | ||||||||||||||
| 199 | L<DBIO::MySQL::Introspect::Columns> returns from C<information_schema.columns>. | ||||||||||||||
| 200 | |||||||||||||||
| 201 | C<character_set> and C<collation> are left C<undef> on non-text columns (numeric, | ||||||||||||||
| 202 | binary, datetime) to match what MariaDB/MySQL reports for those types. For text | ||||||||||||||
| 203 | and char columns the server assigns a charset/collation from the database | ||||||||||||||
| 204 | default; since a portable schema does not prescribe a charset, those fields are | ||||||||||||||
| 205 | also left C<undef> in the target so the diff engine sees both sides as empty and | ||||||||||||||
| 206 | does not emit phantom changes. See ESCALATION NOTE in the source. | ||||||||||||||
| 207 | |||||||||||||||
| 208 | =head1 AUTHOR | ||||||||||||||
| 209 | |||||||||||||||
| 210 | DBIO & DBIx::Class Authors | ||||||||||||||
| 211 | |||||||||||||||
| 212 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 213 | |||||||||||||||
| 214 | Copyright (C) 2026 DBIO Authors | ||||||||||||||
| 215 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||
| 216 | Based on DBIx::Class, heavily modified. | ||||||||||||||
| 217 | |||||||||||||||
| 218 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 219 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 220 | |||||||||||||||
| 221 | =cut | ||||||||||||||