Sledge のセッションを Pg から File に

諸般の事情により、Sledge::Session::Pg から Sledge::Session::File に変更したい。
移行スクリプトを書いた。

#!/usr/bin/perl
use strict;
use DBI;
use MIME::Base64;

my $dbname = shift || die;
my $dir    = shift || die;

my $dbh = DBI->connect("dbi:Pg:dbname=$dbname", "username", "password",
                       { RaiseError => 1, AutoCommit => 0, });
my $sth = $dbh->prepare(
    qq{SELECT id, a_session FROM sessions
       WHERE last_access > ('today'::date - '@ 7 day'::interval)}
);
$sth->execute();
while ( my $r = $sth->fetchrow_arrayref ) {
    my $file = sprintf '%s/Sledge-Session-%s', $dir, $r->[0];
    print "session $r->[0] output to $file\n";
    open my $out, ">", $file or warn $!;
    print $out decode_base64( $r->[1] );
    close $out;
    chmod 0666, $file;
}

$dbh->commit();
$dbh->disconnect();