add: User profile data table is now a flat table.

This commit is contained in:
Doug Bell 2007-05-28 21:35:34 +00:00
parent 8590ef89d5
commit 07a40788bb
41 changed files with 493 additions and 164 deletions

View file

@ -21,6 +21,8 @@ my $quiet; # this line required
my $session = start(); # this line required
# upgrade functions go here
fixProfileDataWithoutFields($session);
buildNewUserProfileTable($session);
finish($session); # this line required
@ -32,6 +34,125 @@ finish($session); # this line required
# # and here's our code
#}
#----------------------------------------------------------------------------
sub fixProfileDataWithoutFields {
my $session = shift;
my $db = $session->db;
use WebGUI::ProfileField;
print "\tFixing profile data without entries in userProfileField table..." unless $quiet;
for my $fieldName (qw{ firstDayOfWeek language timeZone uiLevel }) {
next if WebGUI::ProfileField->new($session, $fieldName);
$db->write(
q{INSERT INTO userProfileField (fieldName, label, visible, fieldType, protected, editable)
VALUES (?,?,0,"ReadOnly",1,0)},
[$fieldName, $fieldName]
);
}
print "OK!\n" unless $quiet;
}
#----------------------------------------------------------------------------
sub buildNewUserProfileTable {
my $session = shift;
my $db = $session->db;
print "\tBuilding new user profile table. This may take a while...\n" unless $quiet;
use WebGUI::ProfileField;
use List::Util qw( first );
print "\t\tCreating structure..." unless $quiet;
# Create a new temporary table
$db->write(q{
CREATE TABLE tmp_userProfileData (
userId VARCHAR(22) BINARY NOT NULL,
PRIMARY KEY (userId)
)
});
# Loop through the current fields and add them to the new table
my @profileFields;
my $sth = $db->read(q{SELECT fieldName, fieldType FROM userProfileField});
while (my %fieldData = $sth->hash) {
push @profileFields, $fieldData{fieldName};
my $fieldType = 'WebGUI::Form::'.ucfirst $fieldData{fieldType};
my $fieldName = $db->dbh->quote_identifier($fieldData{fieldName});
eval "use $fieldType;";
my $dataType = $fieldType->new($session)->get("dbDataType");
$db->write(
"ALTER TABLE tmp_userProfileData ADD COLUMN ($fieldName $dataType)"
);
}
print " OK!\n" unless $quiet;
# Find fields that were not in the userProfileField database.
print "\t\tLooking for profile fields not defined in User Profiling... \n" unless $quiet;
my @dataFields = $db->buildArray("SELECT fieldName FROM userProfileData GROUP BY fieldName");
for my $dataField (@dataFields) {
if (!first { $_ eq $dataField } @profileFields) {
print "\t\t\tCreating invisible, read-only profile field '$dataField'\n" unless $quiet;
my $fieldType = 'WebGUI::Form::ReadOnly';
my $fieldName = $db->dbh->quote_identifier($dataField);
eval "use $fieldType;";
my $dataType = $fieldType->new($session)->get("dbDataType");
$db->write(
"ALTER TABLE tmp_userProfileData ADD COLUMN ($fieldName $dataType)"
);
# Create the profile field
WebGUI::ProfileField->create($session, $dataField, {
label => $dataField,
fieldType => "ReadOnly",
visible => 0,
protected => 1,
});
}
}
print "\t\t... Done!\n";
print "\t\tMigrating data to temporary table... " unless $quiet;
# Loop over the old table and put them in the new table
$sth = $db->read(q{SELECT userId FROM users});
while (my $user = $sth->hashRef) {
# Get all of this user's profile data
my %profile
= $db->buildHash(
"SELECT fieldName, fieldData FROM userProfileData WHERE userId=?",
[$user->{userId}]
);
# Write to the temp table
my $sql
= q{INSERT INTO tmp_userProfileData }
. q{(userId,} . join(",", map { $db->dbh->quote_identifier($_) } keys %profile) . q{)}
. q{VALUES (?,} . join(",",("?")x values %profile) . q{)}
;
$db->write($sql, [$user->{userId},values %profile]);
}
$sth->finish;
print "OK!\n" unless $quiet;
# Delete the old table
print "\t\tExchanging old data with new... ";
$db->write("drop table userProfileData");
# Rename the new table
$db->write("rename table tmp_userProfileData to userProfileData");
print "OK!\n" unless $quiet;
print "\t\t... Done!\n" unless $quiet;
}
# ---- DO NOT EDIT BELOW THIS LINE ----