head	1.2;
access;
symbols;
locks; strict;
comment	@# @;


1.2
date	2001.07.30.18.44.24;	author leonardr;	state dead;
branches;
next	1.1;

1.1
date	2001.06.11.21.33.06;	author kmaples;	state Exp;
branches;
next	;


desc
@@


1.2
log
@Moved stuff from 1.1.0 migration directory to 1.2.0 directory to reflect actual future versioning.
@
text
@#!/usr/bin/perl -w
# 
# -*- Perl -*- script 
#
# $Id: projectDownloadMigrate.pl,v 1.1 2001/06/11 21:33:06 kmaples Exp $

use DBI;

BEGIN
{
    unshift(@@INC, "$ENV{SANDBOX}/scripts");
}
use HelmDB;

my $err = 0;

#  Directory paths:
my $downloads_src_dir = $ENV{SANDBOX} . "/data/helm/fileserv";
my $documents_src_dir = $ENV{SANDBOX} . "/data/helm/fileserv/documents";

my $files_target_dir  = $ENV{SANDBOX} . "/data/helm/fileserv/documents";
my $files_tmp_dir     = $ENV{SANDBOX} . "/data/helm/fileserv/documents_tmp";

my $sql;
my @@rows;

# Connect to the tigris database:
my $dbh = HelmDB::connect(
    $ENV{DATABASE_NAME}, 
    $ENV{DATABASE_ADMIN_USER},
    $ENV{DATABASE_ADMIN_PASSWORD});

# Quit unless the source dirs are present:
unless(-d $downloads_src_dir && $documents_src_dir){
    die "documents or downloads dirs not found: $!\n";  
}

# Create temp dir for files while
unless(mkdir($files_tmp_dir, 0755)){
    die "unable to create temp dir $files_tmp_dir: $!\n";
}

# Relocate all the files in HELM_PROJECTDOCUMENT:
$sql = qq[
    SELECT DOCUMENT_ID, FILE_NAME, LOCATION
    FROM HELM_PROJECTDOCUMENT
    WHERE LOCATION IS NOT NULL
];


print "Getting list of docs in HELM_PROJECTDOCUMENT to move ...\n";
@@rows = HelmDB::runQuery($dbh, $sql);
print "Moving docs found in HELM_PROJECTDOCUMENT ... \n";

foreach(@@rows){
    my ($doc_id, $file_name, $location) = @@{$_};
    unless(-f "$location/$file_name"){
        print "WARNING: HELM_PROJECTDOCUMENT.DOCUMENT_ID = $doc_id\n";
        print "  ($location/$file_name) not found on filesystem\n";
        next;   
    }
    unless($doc_id && mkdir("$files_tmp_dir/$doc_id",0755)){
        print "WARNING: unable to create dir $files_tmp_dir/$doc_id: $!\n"; 
        $err++;
        next;
    }
    unless(system('mv', "$location/$file_name", "$files_tmp_dir/$doc_id/$file_name") == 0){
        print "WARNING: unable to move $file_name to $files_tmp_dir/$doc_id: $!\n";
        $err++;
        next;   
    }
    
    print "  Moved DOCUMENT_ID = $doc_id : $file_name\n";
    
    # We were able to move the file to its new (temporary)
    # location, so update the record - note that this is a little
    # disengenuous, since the file's still in it's temporary home:
    $sql = qq[
        UPDATE HELM_PROJECTDOCUMENT 
        SET LOCATION      = '$files_target_dir/$doc_id'
        WHERE DOCUMENT_ID = $doc_id
    ];
    
    unless(HelmDB::runSQL($dbh, $sql)){
        print "WARNING: unable to update HELM_PROJECTDOCUMENT.DOCUMENT_ID = $doc_id\n";
        $err++;
    }
}

# Create new records and relocate files in HELM_PROJECTDOWNLOAD table:
$sql = qq[
    SELECT DOWNLOAD_ID, PROJECT_ID, FILE_NAME, LOCATION, PROPOSED_BY, 
        APPROVED_BY, IS_PUBLIC, DESCRIPTION, UPLOADED_DATE, SIZE, MD5
    FROM HELM_PROJECTDOWNLOAD
];

print "Getting docs in HELM_PROJECTDOWNLOAD to merge into HELM_PROJECTDOCUMENT ...\n";
@@rows = HelmDB::runQuery($dbh, $sql);
print "Moving and merging docs from HELM_PROJECTDOWNLOAD\n";

foreach(@@rows){
    my ($download_id, $project_id, $file_name, $location, $proposed_by,
        $approved_by, $is_public, $description, $uploaded_date, $size, $md5) = @@{$_};

    unless(-f "$location/$file_name"){
        print "WARNING: HELM_PROJECTDOWNLOAD.DOWNLOAD_ID = $download_id\n";
        print "  ($location/$file_name) not found on filesystem\n";
        next;   
    }

    # Perform insert first, get the id, and move the file:
    $sql = qq[
        INSERT INTO HELM_PROJECTDOCUMENT
        SET
            PROJECT_ID    = $project_id,
            NAME          = '$file_name',
            DESCRIPTION   = '$description',
            FILE_NAME     = '$file_name',
            IS_PUBLIC     = '$is_public',
            MD5           = '$md5',
            SIZE          = '$size',
            CREATED_DATE  = '$uploaded_date',
            LOCATION      = '',
            URL           = '',
            TEXT          = '',
            PROPOSED_BY   = '$proposed_by',
            APPROVED_BY   = '$approved_by',
            STATUS        = 'Active',
            MODIFIED_DATE = '$uploaded_date',
            MODIFIED_BY   = '$proposed_by'
    ];

    unless(HelmDB::runSQL($dbh, $sql)){
        print "WARNING: unable to insert record from HELM_PROJECTDOWNLOAD.DOWNLOAD_ID=$download_id\n";
        print "  into HELM_PROJECTDOCUMENT\n";
        $err++;
        next;
    }
    
    # Get the new id:
    $sql = qq[
        SELECT LAST_INSERT_ID()  
    ];
    
    my @@tmp = HelmDB::runQuery($dbh, $sql);
    my $doc_id = $tmp[0]->[0];

    unless($doc_id && mkdir("$files_tmp_dir/$doc_id",0755)){
        print "WARNING: unable to create dir $files_tmp_dir/$doc_id: $!\n"; 
        $err++;
        next;
    }
    unless(system('mv', "$location/$file_name", "$files_tmp_dir/$doc_id/$file_name") == 0){
        print "WARNING: unable to move $file_name to $files_tmp_dir/$doc_id: $!\n";
        $err++;
        next;   
    }

    print "  Moved DOWNLOAD_ID = $download_id to DOCUMENT_ID = $doc_id : $file_name\n";

    # We were able to move the file to its new (temporary)
    # location:
    $sql = qq[
        UPDATE HELM_PROJECTDOCUMENT 
        SET LOCATION      = '$files_target_dir/$doc_id'
        WHERE DOCUMENT_ID = $doc_id
    ];
    
    unless(HelmDB::runSQL($dbh, $sql)){
        print "WARNING: unable to update HELM_PROJECTDOCUMENT.DOCUMENT_ID = $doc_id\n";
        $err++;
    }
}

print "Finished processing data in tables\n";

if($err){
    print "ERROR: filesystem or database errors occurred which prevent\n";
    print "  migration from completing automatically.  Please investigate\n";
    print "  and resolve warnings above, remove old $downloads_src_dir/[0-9]*,\n";
    print "  and $documents_src_dir, mv $files_tmp_dir $files_target_dir,\n";
    print "  and drop HELM_PROJECTDOWNLOAD from database.\n";
} else {
    print "No errors found; cleaning up filesystem ...\n";
    if(system("rm -rf $downloads_src_dir/[0-9]*") == 0 &&
        system("rm -rf $documents_src_dir") == 0){
        print "old filesystem removed, moving new filesystem ...\n";

        if(system('mv', $files_tmp_dir, $files_target_dir) == 0){
            # success all around, drop the old table:
            HelmDB::runSQL($dbh, 'DROP TABLE HELM_PROJECTDOWNLOAD');     
        } else {
            print "ERROR: unable to move $files_tmp_dir, $files_target_dir: $!\n";
            print "  Resolve permissions problems manually,\n";
            print "  mv $files_tmp_dir $files_target_dir, \n";
            print "  and drop HELM_PROJECTDOWNLOAD from database.\n";
        }       
    } else {
        print "ERROR: Errors occurred cleaning the filesystem; please remove\n";
        print "  $downloads_src_dir/[0-9]*, $documents_src_dir\n";
        print "  mv $files_tmp_dir $files_target_dir, \n";
        print "  and drop HELM_PROJECTDOWNLOAD from database.\n";
    }       
}

# Disconnect from db
$dbh->disconnect();

exit;
@


1.1
log
@perl script to migrate ProjectDownload date to the HELM_PROJECTDOCUMENTS table
@
text
@d5 1
a5 1
# $Id: $
@

