diff --git a/scripts/remove_session b/scripts/remove_session new file mode 100755 index 0000000..ccc90d7 --- /dev/null +++ b/scripts/remove_session @@ -0,0 +1,54 @@ +#!/usr/bin/perl + +# This script removes all data associated with the given sessions. +# For each session it first removes all instances of that session and +# then cleans up any orphans. + +use warnings; +use strict; + +use Simba::CA; + + +my $ca = Simba::CA->new({ + dbi_file => $ENV{SIMBA_DB_CONN} || "$ENV{HOME}/.dbi/simba", + }); + +my $dbh = $ca->{dbh}; + +for my $session (@ARGV[0]) { + print "deleting session $session\n"; + my $n_instances = $dbh->do("delete from instances where session=?", {}, $session); + print "\t$n_instances instances deleted\n"; + + # remove orphaned records: + $sessions + = $dbh->selectcol_arrayref( + q{select s.id from instances i right outer join sessions s on i.session=s.id where i.id is null} + ); + + for my $session (@$sessions) { + $dbh->do(q{delete from sessions where id=?}, {}, $session); + print "\tsession $session deleted\n"; + } + + my $files + = $dbh->selectcol_arrayref( + q{select f.id from instances i right outer join files f on i.file=f.id where i.id is null} + ); + + for my $file (@$files) { + $dbh->do(q{delete from files where id=?}, {}, $file); + print "\tfile $file deleted\n"; + } + + my $versions + = $dbh->selectcol_arrayref( + q{select v.id from instances i right outer join versions2 v on i.version=v.id where i.id is null} + ); + + for my $version (@$versions) { + $dbh->do(q{delete from versions2 where id=?}, {}, $version); + print "\tversion $version deleted\n"; + } +}