| 1 | package DBIO::MySQL::Diff::Table; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for MySQL/MariaDB tables | ||||||||||||||||||||||||||||
| 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::Op'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | __PACKAGE__->mk_diff_accessors(qw( | |||||||||||||||||||||||||||
| 10 | table_name table_info columns foreign_keys | ||||||||||||||||||||||||||||
| 11 | )); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 6 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 6 | my ($class, $source, $target, $target_columns, $target_fks) = @_; | |||||||||||||||||||||||||||
| 17 | 6 | ▶ | $target_columns //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$target_columns //= {}
MC/DC100%1/1
$target_columns //= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
| |||||||||||||||||||||||||||||
| 18 | 6 | ▶ | $target_fks //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$target_fks //= {}
MC/DC100%1/1
$target_fks //= {}
$target_fks
Truth table100%2/2
A
A= $target_fks
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | return $class->diff_toplevel( | ||||||||||||||||||||||||||||
| 21 | $source, $target, | ||||||||||||||||||||||||||||
| 22 | 2 | ▶ | create => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 23 | 2 | my ($name) = @_; | |||||||||||||||||||||||||||
| 24 | $class->new( | ||||||||||||||||||||||||||||
| 25 | action => 'create', | ||||||||||||||||||||||||||||
| 26 | table_name => $name, | ||||||||||||||||||||||||||||
| 27 | table_info => $target->{$name}, | ||||||||||||||||||||||||||||
| 28 | columns => $target_columns->{$name} // [], | ||||||||||||||||||||||||||||
| 29 | 2 | ▶ | foreign_keys => $target_fks->{$name} // [], | ||||||||||||||||||||||||||
|
Condition75%3/4
$target_columns->{$name} // []
$target_fks->{$name} // []
MC/DC0%0/1
$target_columns->{$name} // []
$target_columns->{$name}
MC/DC100%1/1
$target_fks->{$name} // []
$target_fks->{$name}
Truth table50%1/2
A
A= $target_columns->{$name}
Truth table100%2/2
A
A= $target_fks->{$name}
| |||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | }, | ||||||||||||||||||||||||||||
| 32 | 2 | ▶ | drop => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 2 | my ($name) = @_; | |||||||||||||||||||||||||||
| 34 | $class->new( | ||||||||||||||||||||||||||||
| 35 | action => 'drop', | ||||||||||||||||||||||||||||
| 36 | table_name => $name, | ||||||||||||||||||||||||||||
| 37 | 2 | table_info => $source->{$name}, | |||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | }, | ||||||||||||||||||||||||||||
| 40 | 6 | ); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 4 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=15 SCAR=28.3
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 4 | ▶ | if ($self->action eq 'drop') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 2 | return sprintf 'DROP TABLE `%s`;', $self->table_name; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2 | my @col_defs; | |||||||||||||||||||||||||||
| 52 | my @pk_cols; | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | for my $col (@{ $self->columns }) { | |||||||||||||||||||||||||||
| 55 | 4 | ▶ | push @pk_cols, $col->{column_name} if $col->{is_pk}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 4 | ▶ | my $type = $col->{column_type} || $col->{data_type} || 'text'; | ||||||||||||||||||||||||||
|
Condition40%2/5
$col->{'column_type'} || $col->{'data_type'} || 'text'
$col->{"column_type"} or $col->{"data_type"}
MC/DC0%0/2
$col->{'column_type'} || $col->{'data_type'} || 'text'
$col->{"column_type"}
$col->{"data_type"}
Truth table33%1/3
A or B
A= $col->{"column_type"}B= $col->{"data_type"}
| |||||||||||||||||||||||||||||
| 58 | 4 | my $def = sprintf ' `%s` %s', $col->{column_name}, $type; | |||||||||||||||||||||||||||
| 59 | 4 | ▶ | $def .= ' NOT NULL' if $col->{not_null}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 4 | ▶ | $def .= ' AUTO_INCREMENT' if $col->{is_auto_increment}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 4 | ▶ | if (defined $col->{default_value}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $def .= " DEFAULT '$col->{default_value}'"; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 4 | push @col_defs, $def; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 2 | ▶ | if (@pk_cols) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | push @col_defs, sprintf ' PRIMARY KEY (%s)', | ||||||||||||||||||||||||||||
| 69 | 2 | join(', ', map { "`$_`" } @pk_cols); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 2 | for my $fk (@{ $self->foreign_keys }) { | |||||||||||||||||||||||||||
| 73 | push @col_defs, sprintf ' FOREIGN KEY (%s) REFERENCES `%s`(%s)', | ||||||||||||||||||||||||||||
| 74 | 1 | join(', ', map { "`$_`" } @{ $fk->{from_columns} }), | |||||||||||||||||||||||||||
| 75 | $fk->{to_table}, | ||||||||||||||||||||||||||||
| 76 | 1 | join(', ', map { "`$_`" } @{ $fk->{to_columns} }); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | my $info = $self->table_info // {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->table_info // {}
MC/DC0%0/1
$self->table_info // {}
$self->table_info
Truth table50%1/2
A
A= $self->table_info
| |||||||||||||||||||||||||||||
| 80 | 2 | ▶ | my $engine = $info->{engine} || 'InnoDB'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$info->{'engine'} || 'InnoDB'
MC/DC0%0/1
$info->{'engine'} || 'InnoDB'
$info->{'engine'}
Truth table50%1/2
A
A= $info->{'engine'}
| |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | my $charset = ($info->{table_collation} && $info->{table_collation} =~ /^([^_]+)_/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$info->{"table_collation"} and $info->{"table_collation"} =~ /^([^_]+)_/
MC/DC0%0/2
$info->{"table_collation"} and $info->{"table_collation"} =~ /^([^_]+)_/
$info->{"table_collation"}
$info->{"table_collation"} =~ /^([^_]+)_/
Truth table33%1/3
A and B
A= $info->{"table_collation"}B= $info->{"table_collation"} =~ /^([^_]+)_/
| |||||||||||||||||||||||||||||
| 82 | ? $1 : 'utf8mb4'; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | return sprintf "CREATE TABLE `%s` (\n%s\n) ENGINE=%s DEFAULT CHARSET=%s;", | |||||||||||||||||||||||||||
| 85 | $self->table_name, join(",\n", @col_defs), $engine, $charset; | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 91 | 1 | ▶ | my $prefix = $self->action eq 'create' ? '+' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 1 | return sprintf '%s table: %s', $prefix, $self->table_name; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 4 | 1; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =pod | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head1 NAME | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | DBIO::MySQL::Diff::Table - Diff operations for MySQL/MariaDB tables | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 VERSION | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | version 0.900001 | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Represents a table-level diff operation in MySQL/MariaDB: C<CREATE | ||||||||||||||||||||||||||||
| 114 | TABLE> or C<DROP TABLE>. C<create> ops carry the target columns and FKs | ||||||||||||||||||||||||||||
| 115 | so C<as_sql> can render the full inline definition (parallel to | ||||||||||||||||||||||||||||
| 116 | L<DBIO::SQLite::Diff::Table>). | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 METHODS | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head2 diff | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | my @ops = DBIO::MySQL::Diff::Table->diff( | ||||||||||||||||||||||||||||
| 123 | $source_tables, $target_tables, | ||||||||||||||||||||||||||||
| 124 | $target_columns, $target_fks, | ||||||||||||||||||||||||||||
| 125 | ); | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head2 as_sql | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 summary | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 138 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||
| 139 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 142 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =cut | ||||||||||||||||||||||||||||