App-BorgRestore
view release on metacpan or search on metacpan
lib/App/BorgRestore/DB.pm view on Meta::CPAN
method _migrate() {
my $version = $self->_get_db_version();
$log->debugf("Current database schema version: %s", $version);
my $schema = {
1 => sub {
$self->{dbh}->do('create table if not exists `files` (`path` text, primary key (`path`)) without rowid;');
$self->{dbh}->do('create table if not exists `archives` (`archive_name` text unique);');
},
2 => sub {
$self->{dbh}->do('alter table `archives` rename to `archives_old`');
$self->{dbh}->do('create table `archives` (`id` integer primary key autoincrement, `archive_name` text unique);');
$self->{dbh}->do('insert into `archives` select null, * from `archives_old`');
$self->{dbh}->do('drop table `archives_old`');
my $st = $self->{dbh}->prepare("select `archive_name` from `archives`;");
$st->execute();
while (my $result = $st->fetchrow_hashref) {
my $archive = $result->{archive_name};
# We trust all values here since they have already been
# sucessfully put into the DB previously. Thus they must be
# safe to deal with.
$archive = untaint($archive, qr(.*));
my $archive_id = $self->get_archive_id($archive);
$self->{dbh}->do("alter table `files` rename column `timestamp-$archive` to `$archive_id`");
}
},
3 => sub {
# Drop all cached files due to a bug in
# lib/App/BorgRestore/PathTimeTable/DB.pm that caused certain files
# to be skipped rather than being added to the `files` table.
$self->{dbh}->do('delete from `archives`');
$self->{dbh}->do('delete from `files`');
},
4 => sub {
lib/App/BorgRestore/DB.pm view on Meta::CPAN
$self->{dbh}->do('alter table `files_new` add column `'.$archive_id.'` integer;');
}
if (@archive_ids > 0) {
my @columns_to_copy = map {'`'.$_.'`'} @archive_ids;
@columns_to_copy = ('`path`', @columns_to_copy);
$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 $ran_migrations = 0;
for my $target_version (sort { $a <=> $b } keys %$schema) {
if ($version < $target_version) {
$log->debugf("Migrating to schema version %s", $target_version);
$self->{dbh}->begin_work();
$schema->{$target_version}->();
$self->_set_db_version($target_version);
lib/App/BorgRestore/DB.pm view on Meta::CPAN
my @columns_to_copy = map {'`'.$self->get_archive_id($_).'`'} @keep_archives;
my @timestamp_columns_to_copy = @columns_to_copy;
@columns_to_copy = ('`path`', @columns_to_copy);
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`');
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);
$st->execute();
}
( run in 0.226 second using v1.01-cache-2.11-cpan-b888b73be4d )