Link Here
|
54 |
=head2 new |
54 |
=head2 new |
55 |
|
55 |
|
56 |
$mgr = Koha::Database::Commenter->new({ |
56 |
$mgr = Koha::Database::Commenter->new({ |
57 |
dbh => $dbh, database => $d, fh => $fh, schema_file => $s |
57 |
dbh => $dbh, database => $d, schema_file => $s |
58 |
}); |
58 |
}); |
59 |
|
59 |
|
60 |
Object constructor. |
60 |
Object constructor. |
61 |
Param dbh is mandatory. Params database, fh and schema_file are |
61 |
Param dbh is mandatory. Params database and schema_file are |
62 |
optional. |
62 |
optional. |
63 |
Param database can be used to move away from current database of |
63 |
Param database can be used to move away from current database of |
64 |
db handle. |
64 |
db handle. |
65 |
Param fh can be used to redirect output. |
|
|
66 |
Param schema_file is needed for resetting to schema. Falls back to |
65 |
Param schema_file is needed for resetting to schema. Falls back to |
67 |
the constant for Koha structure file. |
66 |
the constant for Koha structure file. |
68 |
|
67 |
|
69 |
=cut |
68 |
=cut |
70 |
|
69 |
|
71 |
sub new { |
70 |
sub new { |
72 |
my ( $class, $params ) = @_; # params: database, dbh, fh, schema_file |
71 |
my ( $class, $params ) = @_; # params: database, dbh, schema_file |
73 |
my $self = bless $params // {}, $class; |
72 |
my $self = bless $params // {}, $class; |
74 |
|
73 |
|
75 |
Koha::Exceptions::MissingParameter->throw( parameter => 'dbh' ) unless $self->{dbh}; |
74 |
Koha::Exceptions::MissingParameter->throw( parameter => 'dbh' ) unless $self->{dbh}; |
Link Here
|
77 |
unless ref($self->{dbh}) eq DBI_HANDLE_CLASS; |
76 |
unless ref($self->{dbh}) eq DBI_HANDLE_CLASS; |
78 |
|
77 |
|
79 |
$self->{database} //= ( $self->{dbh}->selectrow_array('SELECT DATABASE()') )[0]; |
78 |
$self->{database} //= ( $self->{dbh}->selectrow_array('SELECT DATABASE()') )[0]; |
80 |
$self->{fh} //= *STDOUT; |
|
|
81 |
$self->{schema_file} //= KOHA_STRUCTURE; |
79 |
$self->{schema_file} //= KOHA_STRUCTURE; |
82 |
$self->{schema_info} = {}; |
80 |
$self->{schema_info} = {}; |
83 |
|
81 |
|
Link Here
|
86 |
|
84 |
|
87 |
=head2 clear |
85 |
=head2 clear |
88 |
|
86 |
|
89 |
$object->clear({ dry_run => 0, table => $table, verbose => 0 }); |
87 |
$object->clear({ dry_run => 0, table => $table }, $messages ); |
90 |
|
88 |
|
91 |
Clears all current column comments in storage. |
89 |
Clears all current column comments in storage. |
92 |
If table is passed, only that table is changed. |
90 |
If table is passed, only that table is changed. |
93 |
Dry run only prints sql statements. |
91 |
Dry run only returns sql statements in $messages (arrayref). |
94 |
|
92 |
|
95 |
=cut |
93 |
=cut |
96 |
|
94 |
|
97 |
sub clear { |
95 |
sub clear { |
98 |
my ( $self, $params ) = @_; # dry_run, table, verbose |
96 |
my ( $self, $params, $messages ) = @_; # dry_run, table |
99 |
my $cols = $self->_fetch_stored_comments($params); |
97 |
my $cols = $self->_fetch_stored_comments($params); |
100 |
foreach my $col ( @$cols ) { |
98 |
foreach my $col ( @$cols ) { |
101 |
next if !$col->{column_comment}; |
99 |
next if !$col->{column_comment}; |
102 |
next if $params->{table} && $col->{table_name} ne $params->{table}; |
100 |
next if $params->{table} && $col->{table_name} ne $params->{table}; |
103 |
$self->_change_column( $col->{table_name}, $col->{column_name}, undef, $params ); # undef clears |
101 |
$self->_change_column( $col->{table_name}, $col->{column_name}, undef, $params, $messages ); # undef clears |
104 |
} |
102 |
} |
105 |
} |
103 |
} |
106 |
|
104 |
|
107 |
=head2 reset_to_schema |
105 |
=head2 reset_to_schema |
108 |
|
106 |
|
109 |
$object->reset_to_schema({ dry_run => 0, table => $table, verbose => 0 }); |
107 |
$object->reset_to_schema({ dry_run => 0, table => $table }, $messages ); |
110 |
|
108 |
|
111 |
Resets column comments in storage to schema definition. |
109 |
Resets column comments in storage to schema definition. |
112 |
Other column comments are cleared. |
110 |
Other column comments are cleared. |
113 |
When you pass table, only that table is changed. |
111 |
When you pass table, only that table is changed. |
114 |
Dry run only prints sql statements. |
112 |
Dry run only returns sql statements in $messages (arrayref). |
115 |
|
113 |
|
116 |
=cut |
114 |
=cut |
117 |
|
115 |
|
118 |
sub reset_to_schema { |
116 |
sub reset_to_schema { |
119 |
my ( $self, $params ) = @_; # dry_run, table, verbose |
117 |
my ( $self, $params, $messages ) = @_; # dry_run, table |
120 |
$self->clear($params); |
118 |
$self->clear( $params, $messages ); |
121 |
my $schema_comments = $self->_fetch_schema_comments; |
119 |
my $schema_comments = $self->_fetch_schema_comments; |
122 |
foreach my $table ( sort keys %$schema_comments ) { |
120 |
foreach my $table ( sort keys %$schema_comments ) { |
123 |
next if $params->{table} && $table ne $params->{table}; |
121 |
next if $params->{table} && $table ne $params->{table}; |
124 |
foreach my $col ( sort keys %{$schema_comments->{$table}} ) { |
122 |
foreach my $col ( sort keys %{$schema_comments->{$table}} ) { |
125 |
$self->_change_column( $table, $col, $schema_comments->{$table}->{$col}, $params ); |
123 |
$self->_change_column( $table, $col, $schema_comments->{$table}->{$col}, $params, $messages ); |
126 |
} |
124 |
} |
127 |
} |
125 |
} |
128 |
} |
126 |
} |
129 |
|
127 |
|
130 |
=head2 renumber |
128 |
=head2 renumber |
131 |
|
129 |
|
132 |
$object->renumber({ dry_run => 0, table => $table, verbose => 0 }); |
130 |
$object->renumber({ dry_run => 0, table => $table }, $messages ); |
133 |
|
131 |
|
134 |
This is primarily meant for testing purposes (verifying results across |
132 |
This is primarily meant for testing purposes (verifying results across |
135 |
whole database). |
133 |
whole database). |
136 |
It adds comments like Comment_1, Comment_2 etc. |
134 |
It adds comments like Comment_1, Comment_2 etc. |
137 |
When you pass table, only that table is changed. Otherwise all tables |
135 |
When you pass table, only that table is changed. Otherwise all tables |
138 |
are affected; note that the column counter does not reset by table. |
136 |
are affected; note that the column counter does not reset by table. |
139 |
Dry run only prints sql statements. |
137 |
Dry run only returns sql statements in $messages (arrayref). |
140 |
|
138 |
|
141 |
=cut |
139 |
=cut |
142 |
|
140 |
|
143 |
sub renumber { |
141 |
sub renumber { |
144 |
my ( $self, $params ) = @_; # dry_run, table, verbose |
142 |
my ( $self, $params, $messages ) = @_; # dry_run, table |
145 |
my $cols = $self->_fetch_stored_comments($params); |
143 |
my $cols = $self->_fetch_stored_comments($params); |
146 |
my $i = 0; |
144 |
my $i = 0; |
147 |
foreach my $col ( @$cols ) { |
145 |
foreach my $col ( @$cols ) { |
148 |
next if $params->{table} && $col->{table_name} ne $params->{table}; |
146 |
next if $params->{table} && $col->{table_name} ne $params->{table}; |
149 |
$i++; |
147 |
$i++; |
150 |
$self->_change_column( $col->{table_name}, $col->{column_name}, "Column_$i", $params ); |
148 |
$self->_change_column( $col->{table_name}, $col->{column_name}, "Column_$i", $params, $messages ); |
151 |
} |
149 |
} |
152 |
} |
150 |
} |
153 |
|
151 |
|
Link Here
|
203 |
sub _change_column { |
201 |
sub _change_column { |
204 |
# NOTE: We do not want to use DBIx schema here, but we use stored structure, |
202 |
# NOTE: We do not want to use DBIx schema here, but we use stored structure, |
205 |
# since we only want to change comment not actual table structure. |
203 |
# since we only want to change comment not actual table structure. |
206 |
my ( $self, $table_name, $column_name, $comment, $params ) = @_; # params: dry_run, verbose |
204 |
my ( $self, $table_name, $column_name, $comment, $params, $messages ) = @_; # params: dry_run |
207 |
$params //= {}; |
205 |
$params //= {}; |
208 |
|
206 |
|
209 |
my $dbh = $self->{dbh}; |
207 |
my $dbh = $self->{dbh}; |
Link Here
|
239 |
} |
237 |
} |
240 |
$rv =~ s/\s+$//; # remove trailing spaces |
238 |
$rv =~ s/\s+$//; # remove trailing spaces |
241 |
|
239 |
|
242 |
# Print |
240 |
# Dry run |
243 |
if( $params->{dry_run} ) { |
241 |
if( $params->{dry_run} ) { |
244 |
print { $self->{fh} } "$rv;\n"; |
242 |
push @$messages, "$rv;" if $messages; |
245 |
return; |
243 |
return; |
246 |
} |
244 |
} |
|
|
245 |
|
247 |
# Deploy |
246 |
# Deploy |
248 |
eval { $dbh->do($rv) }; |
247 |
eval { $dbh->do($rv) }; |
249 |
if( $@ ) { |
248 |
if( $@ ) { |
250 |
warn "Failure for $table_name:$column_name"; |
249 |
warn "Failure for $table_name:$column_name"; |
251 |
print { $self->{fh} } "-- FAILED: $rv;\n"; |
250 |
push @$messages, "-- FAILED: $rv;" if $messages; |
252 |
} elsif( $params->{verbose} ) { |
251 |
} else { |
253 |
print { $self->{fh} } "$rv;\n"; |
252 |
push @$messages, "$rv;" if $messages; |
254 |
} |
253 |
} |
255 |
} |
254 |
} |
256 |
|
255 |
|