From 138ba53c92dc10c1b9b9bb31448b76f9bbac0ecd Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Thu, 7 Feb 2019 16:20:39 +0100 Subject: DB/remove_archive: Properly handle cases where the DB is empty after removal Empty here means that the DB does not contain any backup archive information because all information that is already in the db has to be removed. The old code tries to copy existing data into the new table, but since there are no archives to copy data for, the sql query fails. Also, it would copy all rows and only populate the path column, but if there are no archives that have timestamps, that's actually useless work. This patch ensures that the table is kept empty if there are no archives. Signed-off-by: Florian Pritz --- lib/App/BorgRestore/DB.pm | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'lib/App/BorgRestore') diff --git a/lib/App/BorgRestore/DB.pm b/lib/App/BorgRestore/DB.pm index 3c9e0fb..fd8506f 100644 --- a/lib/App/BorgRestore/DB.pm +++ b/lib/App/BorgRestore/DB.pm @@ -105,21 +105,27 @@ method remove_archive($archive) { } my @columns_to_copy = map {'`'._prefix_archive_id($_).'`'} @keep_archives; + my @timestamp_columns_to_copy = @columns_to_copy; @columns_to_copy = ('`path`', @columns_to_copy); - $self->{dbh}->do('insert into `files_new` select '.join(',', @columns_to_copy).' from files'); + + if (@timestamp_columns_to_copy > 0) { + $self->{dbh}->do('insert into `files_new` select '.join(',', @columns_to_copy).' from files'); + } $self->{dbh}->do('drop table `files`'); $self->{dbh}->do('alter table `files_new` rename to `files`'); - my $sql = 'delete from `files` where '; - $sql .= join(' is null and ', grep {$_ ne '`path`' } @columns_to_copy); - $sql .= " is null"; + if (@timestamp_columns_to_copy > 0) { + my $sql = 'delete from `files` where '; + $sql .= join(' is null and ', @timestamp_columns_to_copy); + $sql .= " is null"; - my $st = $self->{dbh}->prepare($sql); - my $rows = $st->execute(); + my $st = $self->{dbh}->prepare($sql); + $st->execute(); + } - $st = $self->{dbh}->prepare('delete from `archives` where `archive_name` = ?;'); + my $st = $self->{dbh}->prepare('delete from `archives` where `archive_name` = ?;'); $st->execute($archive); } -- cgit v1.2.3-24-g4f1b