Show
Ignore:
Timestamp:
05/11/08 20:24:34 (4 years ago)
Author:
dom
Message:

Refactor database setup code to support setting up old schema versions and
add wiki_info method to TestLib?, both to facilitiate automated schema upgrade
testing.

Files:
1 modified

Legend:

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

    r432 r437  
    1313use Carp; 
    1414 
    15 my %create_sql = ( 
    16     schema_info => [ qq| 
     15my $SCHEMA_VERSION = $VERSION*100; 
     16 
     17my $create_sql = { 
     18    9 => { 
     19        schema_info => [ qq| 
    1720CREATE TABLE schema_info ( 
    1821  version   int(10)      NOT NULL default 0 
    1922) 
    2023|, qq| 
    21 INSERT INTO schema_info VALUES (|.($VERSION*100).qq|) 
     24INSERT INTO schema_info VALUES (9) 
    2225| ], 
    2326 
    24     node => [ qq| 
     27        node => [ qq| 
    2528CREATE TABLE node ( 
    2629  id        integer      NOT NULL AUTO_INCREMENT, 
     
    3437| ], 
    3538 
    36     content => [ qq| 
     39        content => [ qq| 
    3740CREATE TABLE content ( 
    3841  node_id   integer      NOT NULL, 
     
    4548) 
    4649| ], 
    47     internal_links => [ qq| 
     50        internal_links => [ qq| 
    4851CREATE TABLE internal_links ( 
    4952  link_from varchar(200) NOT NULL default '', 
     
    5255) 
    5356| ], 
    54     metadata => [ qq| 
     57        metadata => [ qq| 
    5558CREATE TABLE metadata ( 
    5659  node_id        integer      NOT NULL, 
     
    6265CREATE INDEX metadata_index ON metadata(node_id, version, metadata_type, metadata_value(10)) 
    6366| ] 
    64 ); 
     67    }, 
     68}; 
    6569 
    6670=head1 NAME 
     
    113117    my $dbh = _get_dbh( @args ); 
    114118    my $disconnect_required = _disconnect_required( @args ); 
     119    my $wanted_schema = _get_wanted_schema( @args ) || $SCHEMA_VERSION; 
    115120 
    116121    # Check whether tables exist 
     
    122127    my @cur_data; 
    123128    if(scalar keys %tables > 0) { 
    124         $upgrade_schema = Wiki::Toolkit::Setup::Database::get_database_upgrade_required($dbh,$VERSION); 
     129        $upgrade_schema = Wiki::Toolkit::Setup::Database::get_database_upgrade_required($dbh,$SCHEMA_VERSION); 
    125130    } 
    126131    if($upgrade_schema) { 
     
    145150 
    146151    # Set up tables if not found 
    147     foreach my $required ( keys %create_sql ) { 
     152    foreach my $required ( keys %{$create_sql->{$SCHEMA_VERSION}} ) { 
    148153        if ( $tables{$required} ) { 
    149154            print "Table $required already exists... skipping...\n"; 
    150155        } else { 
    151156            print "Creating table $required... done\n"; 
    152             foreach my $sql ( @{ $create_sql{$required} } ) { 
     157            foreach my $sql ( @{$create_sql->{$SCHEMA_VERSION}->{$required}} ) { 
    153158                $dbh->do($sql) or croak $dbh->errstr; 
    154159            } 
     
    174179    my %tables; 
    175180    while ( my $table = $sth->fetchrow_array ) { 
    176         exists $create_sql{$table} and $tables{$table} = 1; 
     181        exists $create_sql->{$SCHEMA_VERSION}->{$table} and $tables{$table} = 1; 
    177182    } 
    178183    return %tables; 
     
    215220 
    216221    print "Dropping tables... "; 
    217     $dbh->do("DROP TABLE IF EXISTS " . join( ",", keys %create_sql ) ) 
     222    $dbh->do("DROP TABLE IF EXISTS " . join( ",", keys %{$create_sql->{$SCHEMA_VERSION}} ) ) 
    218223      or croak $dbh->errstr; 
    219224    print "done\n"; 
     
    246251                      dbhost => $_[3], 
    247252                    ); 
     253} 
     254 
     255sub _get_wanted_schema { 
     256    # Database handle passed in. 
     257    if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) { 
     258        return undef; 
     259    } 
     260 
     261    # Args passed as hashref. 
     262    if ( ref $_[0] and ref $_[0] eq 'HASH' ) { 
     263        my %args = %{$_[0]}; 
     264        return $args{wanted_schema}; 
     265    } 
     266 
     267    # Args passed as list of connection details. 
     268    return $_[1]; 
    248269} 
    249270