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


1.1
date	2001.07.30.18.44.24;	author leonardr;	state Exp;
branches;
next	;


desc
@@


1.1
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 which will get rid of the special "All projects
# in domain" project group and do a couple other minor
# nested-project-related migrations.
#
# $Id: publicProjectMigrate.pl,v 1.1 2001/05/11 19:38:56 leonardr Exp $

#All projects in the "All public projects in domain" project group
#have IS_PUBLIC == "Y"; all others have IS_PUBLIC == "N";

use DBI;
use strict;

my $dbh = dbConnection($ENV{DATABASE_NAME}, $ENV{DATABASE_ADMIN_USER},
		       $ENV{DATABASE_ADMIN_PASSWORD});

print "Migrating to nestable projects.\n";
my $hostID = getProjectID($dbh, "**all projects on host");
my $domainID = getProjectID($dbh, "*all projects in domain");
my $publicDomainID = getProjectID($dbh, "*all public projects in domain");

print " Removing all non-domain projects from host project.\n";
runStatement($dbh, "delete from HELM_PROJECT_PROJECTGROUP where PROJECT_GROUP_ID=$hostID and PROJECT_ID!=$domainID");

print " Changing roles in public/domain to roles in domain.\n";
my $getRoleMappingsQuery  = 
  "select * from HELM_USER_ROLE_PROJECT where PROJECT_ID=$publicDomainID";
$getRoleMappingsQuery =~ s/(.*)\;\s*$/$1/s;
my $sth = $dbh->prepare($getRoleMappingsQuery);
my $rv = $sth->execute();

my $rolesChanged = 0;
my $rolesDeleted = 0;
while (my $hashref=$sth->fetchrow_hashref())
{
  my $userID = $hashref->{USER_ID};
  my $roleID = $hashref->{ROLE_ID};
  my $approvedBy = $hashref->{APPROVED_BY};

  if (urpMappingExists($dbh, $userID, $roleID, $domainID))
  {
    runStatement($dbh, "delete from HELM_USER_ROLE_PROJECT 
                        where USER_ID=$userID and ROLE_ID=$roleID 
                        and PROJECT_ID=$publicDomainID");
    $rolesDeleted++;
  }
  else
  {
    runStatement($dbh, "update HELM_USER_ROLE_PROJECT set PROJECT_ID=$domainID
                        where USER_ID=$userID and ROLE_ID=$roleID 
                        and PROJECT_ID=$publicDomainID");
    $rolesChanged++;
  }
}
print "  $rolesChanged role(s) moved from public/domain to domain.\n";
print "  $rolesDeleted role(s) in both public/domain and domain; deleted from public/domain.\n";

print " Setting all projects as private.\n";
runStatement($dbh, "update HELM_PROJECT set IS_PUBLIC = 'N'");

print " Setting projects in public/domain as public.\n";  
my $projectsMadePublic = 0;
my $getProjectIDsQuery  = 
  "select PROJECT_ID from HELM_PROJECT_PROJECTGROUP 
   where PROJECT_GROUP_ID=$publicDomainID";
$getProjectIDsQuery =~ s/(.*)\;\s*$/$1/s;
$sth = $dbh->prepare($getProjectIDsQuery);
$rv = $sth->execute();
 
while (my $hashref=$sth->fetchrow_hashref())
{
  $projectsMadePublic++;
  my $projectID = $hashref->{PROJECT_ID};
  my $publicQuery = "update HELM_PROJECT set IS_PUBLIC = 'Y' 
                   where PROJECT_ID = $projectID";
  runStatement($dbh, $publicQuery);
}
print "  $projectsMadePublic project(s) made public.\n";

print " Getting rid of public/domain project group mappings.\n";
my $deleteQuery = "delete from HELM_PROJECT_PROJECTGROUP where PROJECT_GROUP_ID=$publicDomainID";

runStatement($dbh, $deleteQuery);

print " Getting rid of public/domain project group.\n";
$deleteQuery = "delete from HELM_PROJECT where PROJECT_ID=$publicDomainID";
runStatement($dbh, $deleteQuery);

$dbh->disconnect();

print " Done.\n";

####################################################################

# Creates and returns a handle to the database (i.e. connection reference).
#
# ex. my $conn = dbConnection('tigris', 'user', 'passwd');
sub dbConnection($$$)
{
    my ($dbName, $dbUser, $dbPasswd) = @@_;
    return DBI->connect
        ("DBI:mysql:$dbName:$ENV{DATABASE_HOST}:$ENV{DATABASE_PORT}",
         $dbUser, $dbPasswd,
        { RaiseError => 1, AutoCommit => 1 });
}

# Returns the ID of a project with the given name;
#
#ex. my $projectID=getProjectID($dbh, 'projectname')
sub getProjectID($$)
{
  my ($dbh, $name) = @@_;
  my $projectQuery = "select PROJECT_ID from HELM_PROJECT where 
                      NAME='$name'";
  my $resultSet = runSelect($dbh, $projectQuery);
  return $resultSet->{PROJECT_ID};
}

# Returns true iff a USER_ROLE_PROJECT mapping exists for the given arguments.
#
#ex. if (urpMappingExists($dbh, 3,4,5))
sub urpMappingExists($$$$)
{
  my ($dbh, $user, $role, $project) = @@_;
  my $projectQuery = "select * from HELM_USER_ROLE_PROJECT where 
                      USER_ID='$user' and ROLE_ID='$role' 
                      and PROJECT_ID='$project'";
  $projectQuery =~ s/(.*)\;\s*$/$1/s;
  my $sth = $dbh->prepare($projectQuery);
  my $rv = $sth->execute();
  return ($sth->rows > 0) 
}

# Runs a SQL statement.
#
# ex. my $rv = runStatement($dbh, 'delete from bar where baz');
sub runStatement($$)
{
    my ($dbh, $sql) = @@_;
    #print "sql=$sql\n";  # debug
    $sql =~ s/(.*)\;\s*$/$1/s;
    my $sth = $dbh->prepare($sql);
    my $rv = $sth->execute();
    $sth->finish();
    return $rv;
}

# Runs a SQL SELECT statement.
#
# ex. my $rv = runSelect($dbh, 'select foo from bar where baz');
sub runSelect($$)
{
  my ($dbh, $sql) = @@_;
  #print "sql=$sql\n";  # debug
  $sql =~ s/(.*)\;\s*$/$1/s;
  my $sth = $dbh->prepare($sql);

  my $rv = $sth->execute();
  my $hashref=$sth->fetchrow_hashref();
  $sth->finish();
  return $hashref;
}
@
