MySQL/DDL.pm

filter: Statementstmt 15.3% 17 / 111 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 46 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 71.4% 5 / 7 Podpod 100% 1 / 1 totaltotal 9.3% 23 / 246 CC 52 SCAR 73.9
CC
52
Cov
8%
install_ddl
2070.0
_mysql_column_type
72.0
BEGIN
1.0
CRAP 1616.1
↑ Summary MySQL/Introspect/Columns.pm »
1 package DBIO::MySQL::DDL;
2 # ABSTRACT: Generate MySQL/MariaDB DDL from DBIO Result classes
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DBIO::Schema::Type ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use DBIO::MySQL::Adapter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 my $ADAPTER = DBIO::MySQL::Adapter->new;
12  
13  
14  
15 0 ▶sub install_ddl {
Subroutine install_ddl: not called CC=45 SCAR=76.4
Pod documented
16 0 my ($class, $schema) = @_;
17  
18 0 my @stmts;
19 my @view_stmts;
20 0 my %seen_table;
21 0 my %seen_view;
22  
23 0 for my $source_name (sort $schema->sources) {
24 0 my $source = $schema->source($source_name);
25 0 my $result_class = $source->result_class;
26 0 my $table_name = $source->name;
27  
28 # Views: emit CREATE VIEW after all tables; skip virtual views.
29 0 ▶ if ($source->isa('DBIO::ResultSource::View')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->isa("DBIO::ResultSource::View")) 0 0 0
30 0 ▶ next if $source->can('is_virtual') && $source->is_virtual;
Branch: 1 branch
BranchTrueFalseTotal
if $source->can("is_virtual") and $source->is_virtual 0 0 0
Condition0%0/3
$source->can("is_virtual") and $source->is_virtual
!ll&&!rl&&r
000
MC/DC0%0/2
$source->can("is_virtual") and $source->is_virtual
$source->can("is_virtual") $source->is_virtual
Truth table0%0/3
A and B
A= $source->can("is_virtual")B= $source->is_virtual
ABresult
0X0
100
111
31 0 ▶ my $vname = ref $table_name eq 'SCALAR' ? $$table_name : $table_name;
Branch: 1 branch
BranchTrueFalseTotal
ref $table_name eq 'SCALAR' ? : 0 0 0
32 0 ▶ next if ref $vname || $seen_view{$vname}++;
Branch: 1 branch
BranchTrueFalseTotal
if ref $vname or $seen_view{$vname}++ 0 0 0
Condition0%0/3
ref $vname or $seen_view{$vname}++
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $vname or $seen_view{$vname}++
ref $vname $seen_view{$vname}++
Truth table0%0/3
A or B
A= ref $vnameB= $seen_view{$vname}++
ABresult
1X1
011
000
33 0 ▶ my $def = $source->can('view_definition') ? $source->view_definition : undef;
Branch: 1 branch
BranchTrueFalseTotal
$source->can('view_definition') ? : 0 0 0
34 0 ▶ next unless defined $def && length $def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def and length $def 0 0 0
Condition0%0/3
defined $def and length $def
!ll&&!rl&&r
000
MC/DC0%0/2
defined $def and length $def
defined $def length $def
Truth table0%0/3
A and B
A= defined $defB= length $def
ABresult
0X0
100
111
35 0 push @view_stmts, sprintf 'CREATE VIEW %s AS %s;', _quote_ident($vname), $def;
36 0 next;
37 }
38  
39 # A scalar-ref name is either a literal table name (\'cd', emitted
40 # unquoted) or an inline subquery source; deref the former, skip the latter.
41 0 ▶ if (ref $table_name eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $table_name eq "SCALAR") 0 0 0
42 0 $table_name = $$table_name;
43 0 ▶ next if $table_name =~ /\s|\(/;
Branch: 1 branch
BranchTrueFalseTotal
if $table_name =~ /\s|\(/ 0 0 0
44 }
45 0 ▶ next if ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name 0 0 0
46  
47 # Several result sources can map to one physical table; emit it once.
48 0 ▶ next if $seen_table{$table_name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_table{$table_name}++ 0 0 0
49  
50 0 my @col_defs;
51 0 my @pk_cols = $source->primary_columns;
52  
53 0 for my $col_name ($source->columns) {
54 0 my $info = $source->column_info($col_name);
55 0 my $type = _mysql_column_type($info);
56  
57 0 my $def = sprintf ' %s %s', _quote_ident($col_name), $type;
58 0 ▶ $def .= ' NOT NULL' if defined $info->{is_nullable} && !$info->{is_nullable};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"is_nullable"} and not $info->{"is_nullable"} 0 0 0
Condition0%0/3
defined $info->{"is_nullable"} and not $info->{"is_nullable"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $info->{"is_nullable"} and not $info->{"is_nullable"}
defined $info->{"is_nullable"} not $info->{"is_nullable"}
Truth table0%0/3
A and not(B)
A= defined $info->{"is_nullable"}B= not $info->{"is_nullable"}
ABresult
0X0
100
111
59 0 ▶ $def .= ' AUTO_INCREMENT' if $info->{is_auto_increment};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_auto_increment"} 0 0 0
60  
61 0 ▶ if (defined $info->{default_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{"default_value"}) 0 0 0
62 0 my $dv = $info->{default_value};
63 0 ▶ if (ref $dv eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dv eq 'SCALAR') { } 0 0 0
64 0 $def .= " DEFAULT $$dv";
65 } else {
66 0 $def .= " DEFAULT '$dv'";
67 }
68 }
69  
70 0 push @col_defs, $def;
71 }
72  
73 0 ▶ if (@pk_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols) 0 0 0
74 push @col_defs, sprintf ' PRIMARY KEY (%s)',
75 0 join(', ', map { _quote_ident($_) } @pk_cols);
76 }
77  
78 # Unique constraints
79 0 ▶ if ($source->can('unique_constraints')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->can("unique_constraints")) 0 0 0
80 0 my %uniques = $source->unique_constraints;
81 0 for my $uname (sort keys %uniques) {
82 0 ▶ next if $uname eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uname eq "primary" 0 0 0
83 0 my $cols = $uniques{$uname};
84 push @col_defs, sprintf ' UNIQUE KEY %s (%s)',
85 _quote_ident($uname),
86 0 join(', ', map { _quote_ident($_) } @$cols);
87 }
88 }
89  
90 # Foreign keys derived from belongs_to relationships
91 0 for my $rel ($source->relationships) {
92 0 my $info = $source->relationship_info($rel);
93 0 ▶ next unless $info && $info->{attrs} && $info->{attrs}{is_foreign_key_constraint};
Branch: 1 branch
BranchTrueFalseTotal
unless $info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"} 0 0 0
Condition0%0/6
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
!ll&&!rl&&r
000
$info and $info->{"attrs"}
!ll&&!rl&&r
000
MC/DC0%0/3
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info $info->{"attrs"} $info->{"attrs"}{"is_foreign_key_constraint"}
Truth table0%0/4
A and B and C
A= $infoB= $info->{"attrs"}C= $info->{"attrs"}{"is_foreign_key_constraint"}
ABCresult
0XX0
10X0
1100
1111
94  
95 0 my $foreign = $info->{class};
96 0 my $foreign_source = eval { $schema->source($foreign) }
97 0 ▶ // eval { $schema->source($foreign =~ s/.*:://r) };
Condition0%0/3
eval { // eval {
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $schema->source($foreign) } } // eval { do { $schema->source($foreign =~ s/.*:://r) } }
eval { do { $schema->source($foreign) } } eval { do { $schema->source($foreign =~ s/.*:://r) } }
Truth table0%0/3
A // B
A= eval { do { $schema->source($foreign) } }B= eval { do { $schema->source($foreign =~ s/.*:://r) } }
ABresult
1X1
011
000
98 0 ▶ next unless $foreign_source;
Branch: 1 branch
BranchTrueFalseTotal
unless $foreign_source 0 0 0
99  
100 0 my $cond = $info->{cond};
101 0 ▶ next unless ref $cond eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cond eq "HASH" 0 0 0
102  
103 0 my (@from, @to);
104 0 for my $foreign_col (sort keys %$cond) {
105 0 my $fcol = $foreign_col;
106 0 $fcol =~ s/^foreign\.//;
107 0 my $self_col = $cond->{$foreign_col};
108 0 $self_col =~ s/^self\.//;
109 0 push @to, $fcol;
110 0 push @from, $self_col;
111 }
112  
113 # A literal table name (\'cd') arrives as a scalar ref; deref it so
114 # the FK references the table, not "SCALAR(0x...)".
115 0 my $foreign_name = $foreign_source->name;
116 0 ▶ $foreign_name = $$foreign_name if ref $foreign_name eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $foreign_name eq "SCALAR" 0 0 0
117  
118 push @col_defs, sprintf ' FOREIGN KEY (%s) REFERENCES %s(%s)',
119 0 join(', ', map { _quote_ident($_) } @from),
120 _quote_ident($foreign_name),
121 0 join(', ', map { _quote_ident($_) } @to);
122 }
123  
124 0 ▶ my $engine = ($result_class->can('mysql_engine') && $result_class->mysql_engine) || 'InnoDB';
Condition0%0/5
$result_class->can('mysql_engine') && $result_class->mysql_engine || 'InnoDB'
l!l
00
$result_class->can("mysql_engine") and $result_class->mysql_engine
!ll&&!rl&&r
000
MC/DC0%0/2
$result_class->can('mysql_engine') && $result_class->mysql_engine || 'InnoDB'
$result_class->can("mysql_engine") $result_class->mysql_engine
Truth table0%0/3
A and B
A= $result_class->can("mysql_engine")B= $result_class->mysql_engine
ABresult
111
0X0
100
125 0 ▶ my $charset = ($result_class->can('mysql_charset') && $result_class->mysql_charset) || 'utf8mb4';
Condition0%0/5
$result_class->can('mysql_charset') && $result_class->mysql_charset || 'utf8mb4'
l!l
00
$result_class->can("mysql_charset") and $result_class->mysql_charset
!ll&&!rl&&r
000
MC/DC0%0/2
$result_class->can('mysql_charset') && $result_class->mysql_charset || 'utf8mb4'
$result_class->can("mysql_charset") $result_class->mysql_charset
Truth table0%0/3
A and B
A= $result_class->can("mysql_charset")B= $result_class->mysql_charset
ABresult
111
0X0
100
126 0 ▶ my $collate = ($result_class->can('mysql_collate') && $result_class->mysql_collate) || 'utf8mb4_unicode_ci';
Condition0%0/5
$result_class->can('mysql_collate') && $result_class->mysql_collate || 'utf8mb4_unicode_ci'
l!l
00
$result_class->can("mysql_collate") and $result_class->mysql_collate
!ll&&!rl&&r
000
MC/DC0%0/2
$result_class->can('mysql_collate') && $result_class->mysql_collate || 'utf8mb4_unicode_ci'
$result_class->can("mysql_collate") $result_class->mysql_collate
Truth table0%0/3
A and B
A= $result_class->can("mysql_collate")B= $result_class->mysql_collate
ABresult
111
0X0
100
127  
128 0 push @stmts, sprintf "CREATE TABLE %s (\n%s\n) ENGINE=%s DEFAULT CHARSET=%s COLLATE=%s;",
129 _quote_ident($table_name),
130 join(",\n", @col_defs),
131 $engine, $charset, $collate;
132  
133 # MySQL-specific indexes via mysql_indexes (parallel to pg_indexes)
134 0 ▶ if ($result_class->can('mysql_indexes')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("mysql_indexes")) 0 0 0
135 0 my $indexes = $result_class->mysql_indexes;
136 0 for my $idx_name (sort keys %$indexes) {
137 0 my $idx = $indexes->{$idx_name};
138 0 ▶ my $unique = $idx->{unique} ? 'UNIQUE ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$idx->{'unique'} ? : 0 0 0
139 0 ▶ my $type = $idx->{using} ? " USING $idx->{using}" : '';
Branch: 1 branch
BranchTrueFalseTotal
$idx->{'using'} ? : 0 0 0
140 my $cols = join ', ',
141 0 ▶ map { _quote_ident($_) } @{ $idx->{columns} // [] };
Condition0%0/2
$idx->{"columns"} // []
l!l
00
MC/DC0%0/1
$idx->{"columns"} // []
$idx->{"columns"}
Truth table0%0/2
A
A= $idx->{"columns"}
Aresult
11
00
142 0 push @stmts, sprintf 'CREATE %sINDEX %s ON %s (%s)%s;',
143 $unique, _quote_ident($idx_name), _quote_ident($table_name), $cols, $type;
144 }
145 }
146 }
147  
148 0 return join "
149  
150 ", @stmts, @view_stmts;
151 }
152  
153 0 ▶sub _mysql_column_type {
Subroutine _mysql_column_type: not called CC=8 SCAR=42.8
154 0 my ($info) = @_;
155 0 ▶ my $type = $info->{data_type} // 'text';
Condition0%0/2
$info->{"data_type"} // "text"
l!l
00
MC/DC0%0/1
$info->{"data_type"} // "text"
$info->{"data_type"}
Truth table0%0/2
A
A= $info->{"data_type"}
Aresult
11
00
156  
157 # Pre-parameterized types pass through
158 0 ▶ return $type if $type =~ /\(.+\)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\(.+\)$/ 0 0 0
159  
160 # Portable base types: delegate to the adapter (single source of truth)
161 0 ▶ if (DBIO::Schema::Type::is_base_type(lc $type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (DBIO::Schema::Type::is_base_type(lc $type)) 0 0 0
162 0 my $canon = eval { DBIO::Schema::Type::canonical_column('_', $info) };
163 0 ▶ return $ADAPTER->to_native($canon) if $canon;
Branch: 1 branch
BranchTrueFalseTotal
if $canon 0 0 0
164 }
165  
166 # Use size for varchar/varbinary/binary (non-base alias path)
167 0 ▶ if ($info->{size} && $type =~ /^(varchar|varbinary|binary)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"size"} and $type =~ /^(varchar|varbinary|binary)$/i) 0 0 0
Condition0%0/3
$info->{"size"} and $type =~ /^(varchar|varbinary|binary)$/i
!ll&&!rl&&r
000
MC/DC0%0/2
$info->{"size"} and $type =~ /^(varchar|varbinary|binary)$/i
$info->{"size"} $type =~ /^(varchar|varbinary|binary)$/i
Truth table0%0/3
A and B
A= $info->{"size"}B= $type =~ /^(varchar|varbinary|binary)$/i
ABresult
0X0
100
111
168 0 return "$type($info->{size})";
169 }
170  
171 # Legacy / dialect alias names — only aliases, not the 8 base-type names
172 0 my %type_map = (
173 int => 'int',
174 bigint => 'bigint',
175 smallint => 'smallint',
176 tinyint => 'tinyint',
177 serial => 'int',
178 bigserial => 'bigint',
179  
180 varchar => 'varchar(255)',
181 string => 'text',
182  
183 float => 'float',
184 real => 'double',
185 'double precision' => 'double',
186 decimal => 'decimal(10,2)',
187  
188 bytea => 'blob',
189  
190 date => 'date',
191 datetime => 'datetime',
192 time => 'time',
193 json => 'json',
194 );
195  
196 0 ▶ return $type_map{ lc $type } // $type;
Condition0%0/3
$type_map{lc $type} // $type
l!l&&r!l&&!r
000
MC/DC0%0/2
$type_map{lc $type} // $type
$type_map{lc $type} $type
Truth table0%0/3
A // B
A= $type_map{lc $type}B= $type
ABresult
1X1
011
000
197 }
198  
199 1 1;
200  
201 __END__
202  
203 =pod
204  
205 =encoding UTF-8
206  
207 =head1 NAME
208  
209 DBIO::MySQL::DDL - Generate MySQL/MariaDB DDL from DBIO Result classes
210  
211 =head1 VERSION
212  
213 version 0.900001
214  
215 =head1 DESCRIPTION
216  
217 C<DBIO::MySQL::DDL> generates a MySQL DDL script from a L<DBIO::Schema>
218 class hierarchy. It is the desired-state side of the
219 test-deploy-and-compare strategy used by L<DBIO::MySQL::Deploy>.
220  
221 my $ddl = DBIO::MySQL::DDL->install_ddl($schema);
222  
223 The generated DDL is plain SQL, one C<CREATE TABLE> per source. Tables
224 default to C<ENGINE=InnoDB> and C<CHARSET=utf8mb4 COLLATE
225 utf8mb4_unicode_ci>. Result classes can override per-table via
226 C<mysql_engine>, C<mysql_charset>, C<mysql_collate> attributes.
227  
228 =head1 METHODS
229  
230 =head2 install_ddl
231  
232 my $ddl = DBIO::MySQL::DDL->install_ddl($schema);
233  
234 Returns the full installation DDL as a single string.
235  
236 =head1 AUTHOR
237  
238 DBIO & DBIx::Class Authors
239  
240 =head1 COPYRIGHT AND LICENSE
241  
242 Copyright (C) 2026 DBIO Authors
243 Portions Copyright (C) 2005-2025 DBIx::Class Authors
244 Based on DBIx::Class, heavily modified.
245  
246 This is free software; you can redistribute it and/or modify it under
247 the same terms as the Perl 5 programming language system itself.
248  
249 =cut
↑ Summary MySQL/Introspect/Columns.pm »