simba/scripts/convert_mysql_to_pgsql

119 lines
3.2 KiB
Plaintext
Raw Normal View History

#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long;
use DBI;
my $mysql_conn;
my $pgsql_conn;
GetOptions(
'mysql:s' => \$mysql_conn,
'pgsql:s' => \$pgsql_conn,
);
die unless ($mysql_conn && $pgsql_conn);
my $dbh_my = DBI->connect(read_conn($mysql_conn), {RaiseError => 1});
my $dbh_pg = DBI->connect(read_conn($pgsql_conn), {AutoCommit => 0, RaiseError => 1});
print STDERR "deleting sessions\n";
$dbh_pg->do("delete from sessions");
print STDERR "deleting files\n";
$dbh_pg->do("delete from files");
print STDERR "deleting filesets\n";
$dbh_pg->do("delete from filesets");
print STDERR "copying sessions\n";
my $sth_my = $dbh_my->prepare("select id, start_date, end_date, prefix from sessions");
my $sth_pg = $dbh_pg->prepare("insert into sessions(id, start_date, end_date, prefix) values(?, ?, ?, ?)");
$sth_my->execute();
while (my @r = $sth_my->fetchrow_array) {
$sth_pg->execute(@r);
}
print STDERR "copying filesets\n";
$sth_my = $dbh_my->prepare("select id, host, dir, options from filesets");
$sth_pg = $dbh_pg->prepare("insert into filesets(id, host, dir, options) values(?, ?, ?, ?)");
$sth_my->execute();
while (my @r = $sth_my->fetchrow_array) {
$sth_pg->execute(@r);
}
my $t0 = time;
my $c = 0;
print STDERR "copying files\n";
$sth_my = $dbh_my->prepare("select id, path, fileset from files");
$sth_pg = $dbh_pg->prepare("insert into files(id, path, fileset) values(?, ?, ?)");
$sth_my->execute();
while (my @r = $sth_my->fetchrow_array) {
$sth_pg->execute(@r);
if (++$c % 1000 == 0) {
my $dt = time - $t0;
printf "\t%6d %6.1f %6.1f\n", $c, $dt, $c / $dt;
}
}
$t0 = time;
$c = 0;
print STDERR "copying versions2\n";
$sth_my = $dbh_my->prepare("select * from versions2");
$sth_pg
= $dbh_pg->prepare(
"insert into versions2(id,
file_type, file_size, file_mtime, file_owner, file_group, file_acl, file_unix_bits, file_rdev,
checksum, file_linktarget)
values(?,
?, ?, ?, ?, ?, ?, ?, ?,
?, ?)");
$sth_my->execute();
my %ub_names_to_bits = (
setuid => '100',
setgid => '010',
sticky => '001',
);
while (my $r = $sth_my->fetchrow_hashref) {
my $file_unix_bits = '000';
for (split(/,/, $r->{file_unix_bits})) {
$file_unix_bits |= $ub_names_to_bits{$_};
}
$r->{file_unix_bits} = $file_unix_bits;
$sth_pg->execute(@{$r}{qw(id file_type file_size file_mtime
file_owner file_group file_acl file_unix_bits file_rdev
checksum file_linktarget)}
);
if (++$c % 1000 == 0) {
my $dt = time - $t0;
printf "\t%6d %6.1f %6.1f\n", $c, $dt, $c / $dt;
}
}
$t0 = time;
$c = 0;
print STDERR "copying instances\n";
$sth_my = $dbh_my->prepare("select id, file, file_id, date, online, session, version from instances");
$sth_pg = $dbh_pg->prepare("insert into instances(id, file, file_id, date, online, session, version) values(?, ?, ?, ?, ?, ?, ?)");
$sth_my->execute();
while (my @r = $sth_my->fetchrow_array) {
$sth_pg->execute(@r);
if (++$c % 1000 == 0) {
my $dt = time - $t0;
printf "\t%6d %6.1f %6.1f\n", $c, $dt, $c / $dt;
}
}
$dbh_pg->commit();
$dbh_pg->disconnect();
$dbh_my->disconnect();
sub read_conn {
my ($filename) = @_;
open (my $fh, '<', $filename) or die "cannot open $filename: $!";
my $line = <$fh>;
chomp($line);
return split(/\s+/, $line);
}