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