From d0911e115e89c18810c27c7dc1271c45051d6ac6 Mon Sep 17 00:00:00 2001 From: hjp Date: Sun, 13 Apr 2008 10:37:20 +0000 Subject: [PATCH] script to split versions table. --- scripts/convert_db_to_v2 | 119 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 scripts/convert_db_to_v2 diff --git a/scripts/convert_db_to_v2 b/scripts/convert_db_to_v2 new file mode 100644 index 0000000..efca034 --- /dev/null +++ b/scripts/convert_db_to_v2 @@ -0,0 +1,119 @@ +#!/usr/bin/perl + +=head1 NAME + +convert_db_to_v2 - convert simba metadata database to v2 layout + +=head1 DESCRIPTION + +This script converts the simba metadata database from v1 (up to r62) +to v2 (somewhere after r63) layout, by splitting the versions table into +two new tables versions2 and instances. + +=cut + + +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}; + +$dbh->{mysql_use_result} = 1; # fetch row by row, not all at once + +$dbh->do(q{ + create table versions2 ( + `id` int(11) NOT NULL auto_increment, + `file_type` char(1) default NULL, + `file_size` bigint(20) default NULL, + `file_mtime` int(11) default NULL, + `file_owner` varchar(255) default NULL, + `file_group` varchar(255) default NULL, + `file_acl` varchar(255) default NULL, + `file_unix_bits` set('setuid','setgid','sticky') default NULL, + `file_rdev` int(11) default NULL, + `checksum` varchar(255) default NULL, + `file_linktarget` text, + PRIMARY KEY (`id`), + KEY `versions2_content_idx` (`checksum`, file_mtime) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8; + }); + +$dbh->do(q{ + create table instances ( + `id` int(11) NOT NULL auto_increment, + `file` int(11) default NULL, + `file_id` varchar(255) default NULL, + `date` int(11) default NULL, + `online` tinyint(1) default NULL, + `session` int(11) default NULL, + version int not null, + PRIMARY KEY (`id`), + KEY `instances_session_file_idx` (`session`,`file`), + KEY `instances_file_session_idx` (file, `session`), + KEY instances_version_idx (version) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8; + }); + +my $sth_select = $dbh->prepare("select * from versions"); + +my @version2_fields = qw( + file_type + file_size + file_mtime + file_owner + file_group + file_acl + file_unix_bits + file_rdev + checksum + file_linktarget + ); + +my $sth_ins_versions2 + = $dbh->prepare( + 'insert into versions2( ' . + join (",", @version2_fields) . + ") values (" . + join(",", ("?") x @version2_fields) . + ")" + ); + +my $sth_ins_instances + = $dbh->prepare( + q{ + insert into instances( + id, + file, + file_id, + date, + online, + session, + version + ) + values(?, ?, ?, ?, ?, ?) + } + ); + +$sth_select->execute(); +my %versions2; +while(my $r = $sth_select->fetchrow_hashref) { + my $key = join($;, map((defined $_ ? $_ : ''), + @{$r}{@version2_fields} + ) + ); + my $version2_id; + if ($versions2{$key}) { + $version2_id = $versions2{$key}; + } else { + $sth_ins_versions2->execute(@{$r}{@version2_fields}); + $version2_id = $sth_ins_versions2->{mysql_insertid}; + $versions2{$key} = $version2_id; + } + $sth_ins_instances->execute(@{$r}{qw(id file file_id date online session)}, $version2_id); +}