Changeset 2040

Show
Ignore:
Timestamp:
04/22/08 23:02:23 (7 months ago)
Author:
mpaschal
Message:

Improve process of converting metadata from serialized blobs to auxiliary tables
BugzID: 79409

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/release-35/lib/MT/Upgrade.pm

    r1955 r2040  
    898898sub core_upgrade_meta { 
    899899    my $self = shift; 
    900     # we could possibly determine the list of types to process 
    901     # programmatically, but this will do... 
    902     $self->add_step('core_upgrade_meta_for_table', type => 'entry'); 
    903     $self->add_step('core_upgrade_meta_for_table', type => 'author'); 
    904     $self->add_step('core_upgrade_meta_for_table', type => 'blog'); 
    905     $self->add_step('core_upgrade_meta_for_table', type => 'template'); 
    906     $self->add_step('core_upgrade_meta_for_table', type => 'asset'); 
    907     $self->add_step('core_upgrade_meta_for_table', type => 'category', 
    908         plugindata => 1); 
     900    my $types = MT->registry('object_types'); 
     901    my %added_step; 
     902    TYPE: while (my ($type, $reg_class) = each %$types) { 
     903        next TYPE if $type eq 'plugin' && ref $reg_class;  # plugin reference 
     904 
     905        my $class = MT->model($type); 
     906        next TYPE if !$class->has_meta();  # nothing to upgrade 
     907 
     908        next TYPE if $added_step{$class->datasource};  # already got that one 
     909 
     910        my $class_type = $class->properties->{class_type}; 
     911        if ($class_type && $class_type ne $class->datasource) { 
     912            if (my $super_class = MT->model($class->datasource)) { 
     913                $class = $super_class 
     914                    if $super_class->datasource eq $class->datasource; 
     915            } 
     916            # If there's no appropriate superclass, go to update with the class 
     917            # we have, not the class we want. 
     918        } 
     919 
     920        my %step_param = ( type => $type ); 
     921        $step_param{plugindata} = 1 if $type eq 'category'; 
     922        $step_param{meta_column} = $class->properties->{meta_column} 
     923            if $class->properties->{meta_column}; 
     924        $self->add_step('core_upgrade_meta_for_table', %step_param); 
     925        $added_step{$class->datasource} = 1; 
     926    } 
    909927    return 0; 
     928} 
     929 
     930sub _save_meta { 
     931    my $self = shift; 
     932    my ($obj, $type, $value) = @_; 
     933 
     934    my $meta_obj = $obj->meta_pkg->new; 
     935 
     936    my @class_keys = @{ $obj->primary_key_tuple }; 
     937    my @meta_keys  = @{ $meta_obj->primary_key_tuple }; 
     938    for my $i (0..$#class_keys) { 
     939        my $class_field = $class_keys[$i]; 
     940        my $meta_field  = $meta_keys[$i]; 
     941        $meta_obj->$meta_field($obj->$class_field()); 
     942    } 
     943 
     944    # Set the type without checking if it's defined, unlike real meta(). 
     945    $meta_obj->type($type); 
     946 
     947    # Does this meta type have a data type defined? 
     948    my $datatype; 
     949    if (my $field = MT::Meta->metadata_by_name(ref $obj || $obj, $type)) { 
     950        if (my $type_id = $field->{type_id}) { 
     951            $datatype = $MT::Meta::Types{$type_id}; 
     952        } 
     953    } 
     954    $datatype ||= 'vblob'; 
     955 
     956    $meta_obj->$datatype($value); 
     957 
     958    my $meta_col_def = $meta_obj->column_def($datatype); 
     959    my $meta_is_blob = $meta_col_def ? $meta_col_def->{type} eq 'blob' : 0; 
     960    MT::Meta::Proxy::serialize_blob(undef, $meta_obj) if $meta_is_blob; 
     961    $meta_obj->save(); 
     962    MT::Meta::Proxy::unserialize_blob($meta_obj) if $meta_is_blob; 
    910963} 
    911964 
     
    943996    my $count = int($param{count} || 0); 
    944997 
    945     my $pid = $param{step} . "_type"
     998    my $pid = join q{:}, $param{step} . "_type", $class
    946999 
    9471000    my $msg = MT->translate("Upgrading metadata storage for [_1]", $class->class_label_plural); 
     
    9671020    $stmt->add_where( $meta_col => { not_null => 1 } ); 
    9681021    $stmt->limit( 101 ); 
    969     $stmt->offset( $offset ) if $offset; 
    9701022 
    9711023    my $sql = join ' ', 'SELECT', $meta_col, ',', $id_col, 'FROM', 
     
    9741026        $stmt->as_limit; 
    9751027 
    976     my $sth = $dbh->prepare($sql) 
    977         or return 0; # ignore this operation if _meta column doesn't exist 
     1028    my $sth = $dbh->prepare($sql); 
     1029    return 0 if !$sth; # ignore this operation if _meta column doesn't exist 
    9781030    $sth->execute 
    9791031        or return $self->error($dbh->errstr || $DBI::errstr); 
     
    10131065 
    10141066                                $changed++; 
    1015                                 $obj->meta('field.' . $cfname, $cfvalue); 
     1067                                $self->_save_meta($obj, 'field.' . $cfname, $cfvalue); 
    10161068                            } 
    10171069                        } else { 
    10181070                            $changed++; 
    1019                             $obj->meta($metaname, $metavalue); 
     1071                            $self->_save_meta($obj, $metaname, $metavalue); 
    10201072                        } 
    10211073                    } 
    10221074                    if ($changed) { 
    1023                         $obj->save if $changed; 
    10241075                        push @ids, $obj->id; 
    10251076                    } 
     
    10281079        } 
    10291080        last if $rows == 100; 
     1081    } 
     1082    if ($rows == 100 && $sth->fetchrow_arrayref) { 
     1083        $rows++; 
    10301084    } 
    10311085    $sth->finish; 
     
    10451099        $sql = $dbd->ddl_class->drop_column_sql($class, $param{meta_column} || 'meta'); 
    10461100        $dbh->do($sql); 
     1101        $self->progress($msg . ' (100%)', $pid); 
    10471102        $offset = 0;  # done! 
    10481103    }