Revision 7a22d3a4
Von Bernd Bleßmann vor mehr als 3 Jahren hinzugefügt
sql/Pg-upgrade2/cvars_remove_dublicate_entries.pl | ||
---|---|---|
1 |
# @tag: cvars_remove_duplicate_entries |
|
2 |
# @description: Doppelte Einträge für gleiche benutzerdefinierte Variablen entfernen (behalte den Neusten). |
|
3 |
# @depends: release_3_4_1 |
|
4 |
|
|
5 |
package SL::DBUpgrade2::cvars_remove_duplicate_entries; |
|
6 |
|
|
7 |
use strict; |
|
8 |
use utf8; |
|
9 |
|
|
10 |
use parent qw(SL::DBUpgrade2::Base); |
|
11 |
|
|
12 |
use SL::DBUtils; |
|
13 |
|
|
14 |
sub run { |
|
15 |
my ($self) = @_; |
|
16 |
|
|
17 |
# get all duplicates |
|
18 |
my $query_all_dups = qq| |
|
19 |
SELECT trans_id, config_id, sub_module FROM custom_variables |
|
20 |
GROUP BY trans_id, config_id, sub_module |
|
21 |
HAVING COUNT(*) > 1 |
|
22 |
|; |
|
23 |
|
|
24 |
my $refs = selectall_hashref_query($::form, $self->dbh, $query_all_dups); |
|
25 |
|
|
26 |
# remove all but the newest one (order by itime descending) |
|
27 |
my $query_delete = qq| |
|
28 |
DELETE FROM custom_variables WHERE id = ?; |
|
29 |
|; |
|
30 |
my $sth_delete = $self->dbh->prepare($query_delete); |
|
31 |
|
|
32 |
my $query_all_but_newest = qq| |
|
33 |
SELECT id FROM custom_variables WHERE trans_id = ? AND config_id = ? AND sub_module = ? ORDER BY itime DESC OFFSET 1 |
|
34 |
|; |
|
35 |
my $sth_all_but_newest = $self->dbh->prepare($query_all_but_newest); |
|
36 |
|
|
37 |
foreach my $ref (@$refs) { |
|
38 |
my @to_delete_ids; |
|
39 |
$sth_all_but_newest->execute($ref->{trans_id}, $ref->{config_id}, $ref->{sub_module}) || $::form->dberror($query_all_but_newest); |
|
40 |
while (my ($row) = $sth_all_but_newest->fetchrow_array()) { |
|
41 |
push(@to_delete_ids, $row); |
|
42 |
} |
|
43 |
($sth_delete->execute($_) || $::form->dberror($query_delete)) for @to_delete_ids; |
|
44 |
} |
|
45 |
|
|
46 |
$sth_all_but_newest->finish; |
|
47 |
$sth_delete->finish; |
|
48 |
|
|
49 |
return 1; |
|
50 |
} |
|
51 |
|
|
52 |
1; |
Auch abrufbar als: Unified diff
Upgrade-Skript um Doubletten bei benutzerderfinierten Variablen zu entfernen.
Im großen commit 52518527bc507767386d21e1870cc2888269ba70
"CSV-Import Artikel: Einige Erweiterungen ..."
kam ein Bug rein, der für Part-CVars doppelte Einträge anlegen konnte.
Dieses Upgrade-Skript löscht die Doubletten - der neuste Eintrag wird behalten.