Show
Ignore:
Timestamp:
05/11/08 17:04:41 (4 years ago)
Author:
dom
Message:

fix much tab/whitespace damage; no functional changes.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • wiki-toolkit/trunk/lib/Wiki/Toolkit/Setup/Database.pm

    r424 r431  
    1616# Fetch from the old style database, ready for an upgrade to db version 8 
    1717sub fetch_upgrade_old_to_8 { 
    18         # Compatible with old_to_9 
    19         fetch_upgrade_old_to_9(@_); 
     18    # Compatible with old_to_9 
     19    fetch_upgrade_old_to_9(@_); 
    2020} 
    2121 
    2222# Fetch from the old style database, ready for an upgrade to db version 9 
    2323sub fetch_upgrade_old_to_9 { 
    24         my $dbh = shift; 
    25         my %nodes; 
    26         my %metadatas; 
    27         my %contents; 
    28         my @internal_links; 
    29         my %ids; 
    30  
    31         print "Grabbing and upgrading old data... "; 
    32  
    33         # Grab all the nodes, and give them an ID 
    34         my $sth = $dbh->prepare("SELECT name,version,text,modified FROM node"); 
    35         $sth->execute; 
    36         my $id = 0; 
    37         while( my($name,$version,$text,$modified) = $sth->fetchrow_array) { 
    38                 my %node; 
    39                 $id++; 
    40                 $node{'name'} = $name; 
    41                 $node{'version'} = $version; 
    42                 $node{'text'} = $text; 
    43                 $node{'modified'} = $modified; 
    44                 $node{'id'} = $id; 
    45                 $node{'moderate'} = 0; 
    46                 $nodes{$name} = \%node; 
    47                 $ids{$name} = $id; 
    48         } 
    49         print " read $id nodes...  "; 
    50  
    51         # Grab all the content, and upgrade to ID from name 
    52         $sth = $dbh->prepare("SELECT name,version,text,modified,comment FROM content"); 
    53         $sth->execute; 
    54         while ( my($name,$version,$text,$modified,$comment) = $sth->fetchrow_array) { 
    55                 my $id = $ids{$name}; 
    56                 if($id) { 
    57                         my %content; 
    58                         $content{'node_id'} = $id; 
    59                         $content{'version'} = $version; 
    60                         $content{'text'} = $text; 
    61                         $content{'modified'} = $modified; 
    62                         $content{'comment'} = $comment; 
    63                         $content{'moderated'} = 1; 
    64                         $contents{$id."-".$version} = \%content; 
    65                 } else { 
    66                         warn("There was no node entry for content with name '$name', unable to migrate it!"); 
    67                 } 
    68         } 
    69         print " read ".(scalar keys %contents)." contents...  "; 
    70  
    71         # Grab all the metadata, and upgrade to ID from node 
    72         $sth = $dbh->prepare("SELECT node,version,metadata_type,metadata_value FROM metadata"); 
    73         $sth->execute; 
    74         my $i = 0; 
    75         while( my($node,$version,$metadata_type,$metadata_value) = $sth->fetchrow_array) { 
    76                 my $id = $ids{$node}; 
    77                 if($id) { 
    78                         my %metadata; 
    79                         $metadata{'node_id'} = $id; 
    80                         $metadata{'version'} = $version; 
    81                         $metadata{'metadata_type'} = $metadata_type; 
    82                         $metadata{'metadata_value'} = $metadata_value; 
    83                         $metadatas{$id."-".($i++)} = \%metadata; 
    84                 } else { 
    85                         warn("There was no node entry for metadata with name (node) '$node', unable to migrate it!"); 
    86                 } 
    87         } 
    88  
    89         # Grab all the internal links 
    90         $sth = $dbh->prepare("SELECT link_from,link_to FROM internal_links"); 
    91         $sth->execute; 
    92         while( my($link_from,$link_to) = $sth->fetchrow_array) { 
    93                 my %il; 
    94                 $il{'link_from'} = $link_from; 
    95                 $il{'link_to'} = $link_to; 
    96                 push @internal_links, \%il; 
    97         } 
    98  
    99         print "done\n"; 
    100  
    101         # Return it all 
    102         return (\%nodes,\%contents,\%metadatas,\@internal_links,\%ids); 
     24    my $dbh = shift; 
     25    my %nodes; 
     26    my %metadatas; 
     27    my %contents; 
     28    my @internal_links; 
     29    my %ids; 
     30 
     31    print "Grabbing and upgrading old data... "; 
     32 
     33    # Grab all the nodes, and give them an ID 
     34    my $sth = $dbh->prepare("SELECT name,version,text,modified FROM node"); 
     35    $sth->execute; 
     36    my $id = 0; 
     37    while( my($name,$version,$text,$modified) = $sth->fetchrow_array) { 
     38        my %node; 
     39        $id++; 
     40        $node{'name'} = $name; 
     41        $node{'version'} = $version; 
     42        $node{'text'} = $text; 
     43        $node{'modified'} = $modified; 
     44        $node{'id'} = $id; 
     45        $node{'moderate'} = 0; 
     46        $nodes{$name} = \%node; 
     47        $ids{$name} = $id; 
     48    } 
     49    print " read $id nodes...  "; 
     50 
     51    # Grab all the content, and upgrade to ID from name 
     52    $sth = $dbh->prepare("SELECT name,version,text,modified,comment FROM content"); 
     53    $sth->execute; 
     54    while ( my($name,$version,$text,$modified,$comment) = $sth->fetchrow_array) { 
     55        my $id = $ids{$name}; 
     56        if($id) { 
     57            my %content; 
     58            $content{'node_id'} = $id; 
     59            $content{'version'} = $version; 
     60            $content{'text'} = $text; 
     61            $content{'modified'} = $modified; 
     62            $content{'comment'} = $comment; 
     63            $content{'moderated'} = 1; 
     64            $contents{$id."-".$version} = \%content; 
     65        } else { 
     66            warn("There was no node entry for content with name '$name', unable to migrate it!"); 
     67        } 
     68    } 
     69    print " read ".(scalar keys %contents)." contents...  "; 
     70 
     71    # Grab all the metadata, and upgrade to ID from node 
     72    $sth = $dbh->prepare("SELECT node,version,metadata_type,metadata_value FROM metadata"); 
     73    $sth->execute; 
     74    my $i = 0; 
     75    while( my($node,$version,$metadata_type,$metadata_value) = $sth->fetchrow_array) { 
     76        my $id = $ids{$node}; 
     77        if($id) { 
     78            my %metadata; 
     79            $metadata{'node_id'} = $id; 
     80            $metadata{'version'} = $version; 
     81            $metadata{'metadata_type'} = $metadata_type; 
     82            $metadata{'metadata_value'} = $metadata_value; 
     83            $metadatas{$id."-".($i++)} = \%metadata; 
     84        } else { 
     85            warn("There was no node entry for metadata with name (node) '$node', unable to migrate it!"); 
     86        } 
     87    } 
     88 
     89    # Grab all the internal links 
     90    $sth = $dbh->prepare("SELECT link_from,link_to FROM internal_links"); 
     91    $sth->execute; 
     92    while( my($link_from,$link_to) = $sth->fetchrow_array) { 
     93        my %il; 
     94        $il{'link_from'} = $link_from; 
     95        $il{'link_to'} = $link_to; 
     96        push @internal_links, \%il; 
     97    } 
     98 
     99    print "done\n"; 
     100 
     101    # Return it all 
     102    return (\%nodes,\%contents,\%metadatas,\@internal_links,\%ids); 
    103103} 
    104104 
    105105# Fetch from schema version 8, and upgrade to version 9 
    106106sub fetch_upgrade_8_to_9 { 
    107         my $dbh = shift; 
    108         my %nodes; 
    109         my %metadatas; 
    110         my %contents; 
    111         my @internal_links; 
    112  
    113         print "Grabbing and upgrading old data... "; 
    114  
    115         # Grab all the nodes 
    116         my $sth = $dbh->prepare("SELECT id,name,version,text,modified FROM node"); 
    117         $sth->execute; 
    118         while( my($id,$name,$version,$text,$modified) = $sth->fetchrow_array) { 
    119                 my %node; 
    120                 $node{'name'} = $name; 
    121                 $node{'version'} = $version; 
    122                 $node{'text'} = $text; 
    123                 $node{'modified'} = $modified; 
    124                 $node{'id'} = $id; 
    125                 $node{'moderate'} = 0; 
    126                 $nodes{$name} = \%node; 
    127         } 
    128  
    129         # Grab all the content 
    130         $sth = $dbh->prepare("SELECT node_id,version,text,modified,comment FROM content"); 
    131         $sth->execute; 
    132         while ( my($node_id,$version,$text,$modified,$comment) = $sth->fetchrow_array) { 
    133                 my %content; 
    134                 $content{'node_id'} = $node_id; 
    135                 $content{'version'} = $version; 
    136                 $content{'text'} = $text; 
    137                 $content{'modified'} = $modified; 
    138                 $content{'comment'} = $comment; 
    139                 $content{'moderated'} = 1; 
    140                 $contents{$node_id."-".$version} = \%content; 
    141         } 
    142  
    143         # Grab all the metadata 
    144         $sth = $dbh->prepare("SELECT node_id,version,metadata_type,metadata_value FROM metadata"); 
    145         $sth->execute; 
    146         my $i = 0; 
    147         while( my($node_id,$version,$metadata_type,$metadata_value) = $sth->fetchrow_array) { 
    148                 my %metadata; 
    149                 $metadata{'node_id'} = $node_id; 
    150                 $metadata{'version'} = $version; 
    151                 $metadata{'metadata_type'} = $metadata_type; 
    152                 $metadata{'metadata_value'} = $metadata_value; 
    153                 $metadatas{$node_id."-".($i++)} = \%metadata; 
    154         } 
    155  
    156         # Grab all the internal links 
    157         $sth = $dbh->prepare("SELECT link_from,link_to FROM internal_links"); 
    158         $sth->execute; 
    159         while( my($link_from,$link_to) = $sth->fetchrow_array) { 
    160                 my %il; 
    161                 $il{'link_from'} = $link_from; 
    162                 $il{'link_to'} = $link_to; 
    163                 push @internal_links, \%il; 
    164         } 
    165  
    166         print "done\n"; 
    167  
    168         # Return it all 
    169         return (\%nodes,\%contents,\%metadatas,\@internal_links); 
     107    my $dbh = shift; 
     108    my %nodes; 
     109    my %metadatas; 
     110    my %contents; 
     111    my @internal_links; 
     112 
     113    print "Grabbing and upgrading old data... "; 
     114 
     115    # Grab all the nodes 
     116    my $sth = $dbh->prepare("SELECT id,name,version,text,modified FROM node"); 
     117    $sth->execute; 
     118    while( my($id,$name,$version,$text,$modified) = $sth->fetchrow_array) { 
     119        my %node; 
     120        $node{'name'} = $name; 
     121        $node{'version'} = $version; 
     122        $node{'text'} = $text; 
     123        $node{'modified'} = $modified; 
     124        $node{'id'} = $id; 
     125        $node{'moderate'} = 0; 
     126        $nodes{$name} = \%node; 
     127    } 
     128 
     129    # Grab all the content 
     130    $sth = $dbh->prepare("SELECT node_id,version,text,modified,comment FROM content"); 
     131    $sth->execute; 
     132    while ( my($node_id,$version,$text,$modified,$comment) = $sth->fetchrow_array) { 
     133        my %content; 
     134        $content{'node_id'} = $node_id; 
     135        $content{'version'} = $version; 
     136        $content{'text'} = $text; 
     137        $content{'modified'} = $modified; 
     138        $content{'comment'} = $comment; 
     139        $content{'moderated'} = 1; 
     140        $contents{$node_id."-".$version} = \%content; 
     141    } 
     142 
     143    # Grab all the metadata 
     144    $sth = $dbh->prepare("SELECT node_id,version,metadata_type,metadata_value FROM metadata"); 
     145    $sth->execute; 
     146    my $i = 0; 
     147    while( my($node_id,$version,$metadata_type,$metadata_value) = $sth->fetchrow_array) { 
     148        my %metadata; 
     149        $metadata{'node_id'} = $node_id; 
     150        $metadata{'version'} = $version; 
     151        $metadata{'metadata_type'} = $metadata_type; 
     152        $metadata{'metadata_value'} = $metadata_value; 
     153        $metadatas{$node_id."-".($i++)} = \%metadata; 
     154    } 
     155 
     156    # Grab all the internal links 
     157    $sth = $dbh->prepare("SELECT link_from,link_to FROM internal_links"); 
     158    $sth->execute; 
     159    while( my($link_from,$link_to) = $sth->fetchrow_array) { 
     160        my %il; 
     161        $il{'link_from'} = $link_from; 
     162        $il{'link_to'} = $link_to; 
     163        push @internal_links, \%il; 
     164    } 
     165 
     166    print "done\n"; 
     167 
     168    # Return it all 
     169    return (\%nodes,\%contents,\%metadatas,\@internal_links); 
    170170} 
    171171 
    172172# Get the version of the database schema 
    173173sub get_database_version { 
    174         my $dbh = shift; 
    175         my $sql = "SELECT version FROM schema_info"; 
    176         my $sth; 
    177         eval{ $sth = $dbh->prepare($sql) }; 
    178         if($@) { return "old"; } 
    179         eval{ $sth->execute }; 
    180         if($@) { return "old"; } 
    181  
    182         my ($cur_schema) = $sth->fetchrow_array; 
    183         unless($cur_schema) { return "old"; } 
    184  
    185         return $cur_schema; 
     174    my $dbh = shift; 
     175    my $sql = "SELECT version FROM schema_info"; 
     176    my $sth; 
     177    eval{ $sth = $dbh->prepare($sql) }; 
     178    if($@) { return "old"; } 
     179    eval{ $sth->execute }; 
     180    if($@) { return "old"; } 
     181 
     182    my ($cur_schema) = $sth->fetchrow_array; 
     183    unless($cur_schema) { return "old"; } 
     184 
     185    return $cur_schema; 
    186186} 
    187187 
    188188# Is an upgrade to the database required? 
    189189sub get_database_upgrade_required { 
    190         my ($dbh,$VERSION) = @_; 
    191  
    192         # Get the schema version 
    193         my $schema_version = get_database_version($dbh); 
    194  
    195         # Compare it 
    196         my $new_ver = $VERSION * 100; 
    197         if($schema_version eq $new_ver) { 
    198                 # At latest version 
    199                 return undef; 
    200         } else { 
    201                 return $schema_version."_to_".$new_ver; 
    202         } 
     190    my ($dbh,$VERSION) = @_; 
     191 
     192    # Get the schema version 
     193    my $schema_version = get_database_version($dbh); 
     194 
     195    # Compare it 
     196    my $new_ver = $VERSION * 100; 
     197    if($schema_version eq $new_ver) { 
     198        # At latest version 
     199        return undef; 
     200    } else { 
     201        return $schema_version."_to_".$new_ver; 
     202    } 
    203203} 
    204204 
    205205# Put the latest data into the latest database structure 
    206206sub bulk_data_insert { 
    207         my ($dbh, $nodesref, $contentsref, $metadataref, $internallinksref) = @_; 
    208  
    209         print "Bulk inserting upgraded data... "; 
    210  
    211         # Add nodes 
    212         my $sth = $dbh->prepare("INSERT INTO node (id,name,version,text,modified,moderate) VALUES (?,?,?,?,?,?)"); 
    213         foreach my $name (keys %$nodesref) { 
    214                 my %node = %{$nodesref->{$name}}; 
    215                 $sth->execute($node{'id'}, 
     207    my ($dbh, $nodesref, $contentsref, $metadataref, $internallinksref) = @_; 
     208 
     209    print "Bulk inserting upgraded data... "; 
     210 
     211    # Add nodes 
     212    my $sth = $dbh->prepare("INSERT INTO node (id,name,version,text,modified,moderate) VALUES (?,?,?,?,?,?)"); 
     213    foreach my $name (keys %$nodesref) { 
     214        my %node = %{$nodesref->{$name}}; 
     215        $sth->execute($node{'id'}, 
    216216                      $node{'name'}, 
    217217                      $node{'version'}, 
     
    219219                      $node{'modified'}, 
    220220                      $node{'moderate'}); 
    221         } 
    222         print "added ".(scalar keys %$nodesref)." nodes...  "; 
    223  
    224         # Add content 
    225         $sth = $dbh->prepare("INSERT INTO content (node_id,version,text,modified,comment,moderated) VALUES (?,?,?,?,?,?)"); 
    226         foreach my $key (keys %$contentsref) { 
    227                 my %content = %{$contentsref->{$key}}; 
    228                 $sth->execute($content{'node_id'}, 
     221    } 
     222    print "added ".(scalar keys %$nodesref)." nodes...  "; 
     223 
     224    # Add content 
     225    $sth = $dbh->prepare("INSERT INTO content (node_id,version,text,modified,comment,moderated) VALUES (?,?,?,?,?,?)"); 
     226    foreach my $key (keys %$contentsref) { 
     227        my %content = %{$contentsref->{$key}}; 
     228        $sth->execute($content{'node_id'}, 
    229229                      $content{'version'}, 
    230230                      $content{'text'}, 
     
    232232                      $content{'comment'}, 
    233233                      $content{'moderated'}); 
    234         } 
     234    } 
    235235 
    236236    # Add metadata