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/Pg.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   integer      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 SEQUENCE node_seq 
    2629|, qq| 
     
    3841| ], 
    3942 
    40     content => [ qq| 
     43        content => [ qq| 
    4144CREATE TABLE content ( 
    4245  node_id   integer      NOT NULL, 
     
    5154| ], 
    5255 
    53     internal_links => [ qq| 
     56        internal_links => [ qq| 
    5457CREATE TABLE internal_links ( 
    5558  link_from varchar(200) NOT NULL default '', 
     
    6063| ], 
    6164 
    62     metadata => [ qq| 
     65        metadata => [ qq| 
    6366CREATE TABLE metadata ( 
    6467  node_id        integer      NOT NULL, 
     
    7174CREATE INDEX metadata_index ON metadata (node_id, version, metadata_type, metadata_value) 
    7275| ] 
    73  
    74 ); 
     76    }, 
     77}; 
    7578 
    7679my %upgrades = ( 
     
    180183    my $dbh = _get_dbh( @args ); 
    181184    my $disconnect_required = _disconnect_required( @args ); 
     185    my $wanted_schema = _get_wanted_schema( @args ) || $SCHEMA_VERSION; 
    182186 
    183187    # Check whether tables exist 
    184188    my $sql = "SELECT tablename FROM pg_tables 
    185189               WHERE tablename in (" 
    186             . join( ",", map { $dbh->quote($_) } keys %create_sql ) . ")"; 
     190            . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$SCHEMA_VERSION}} ) . ")"; 
    187191    my $sth = $dbh->prepare($sql) or croak $dbh->errstr; 
    188192    $sth->execute; 
    189193    my %tables; 
    190194    while ( my $table = $sth->fetchrow_array ) { 
    191         exists $create_sql{$table} and $tables{$table} = 1; 
     195        exists $create_sql->{$SCHEMA_VERSION}->{$table} and $tables{$table} = 1; 
    192196    } 
    193197 
     
    196200    my $upgrade_schema; 
    197201    if(scalar keys %tables > 0) { 
    198         $upgrade_schema = Wiki::Toolkit::Setup::Database::get_database_upgrade_required($dbh,$VERSION); 
     202        $upgrade_schema = Wiki::Toolkit::Setup::Database::get_database_upgrade_required($dbh,$wanted_schema); 
    199203    } else { 
    200204        print "Skipping schema upgrade check - no tables found\n"; 
     
    202206 
    203207    # Set up tables if not found 
    204     foreach my $required ( reverse sort keys %create_sql ) { 
     208    foreach my $required ( reverse sort keys %{$create_sql->{$SCHEMA_VERSION}} ) { 
    205209        if ( $tables{$required} ) { 
    206210            print "Table $required already exists... skipping...\n"; 
    207211        } else { 
    208212            print "Creating table $required... done\n"; 
    209             foreach my $sql ( @{ $create_sql{$required} } ) { 
     213            foreach my $sql ( @{ $create_sql->{$SCHEMA_VERSION}->{$required} } ) { 
    210214                $dbh->do($sql) or croak $dbh->errstr; 
    211215            } 
     
    272276    my $sql = "SELECT tablename FROM pg_tables 
    273277               WHERE tablename in (" 
    274             . join( ",", map { $dbh->quote($_) } keys %create_sql ) . ")"; 
     278            . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$SCHEMA_VERSION}} ) . ")"; 
    275279    foreach my $tableref (@{$dbh->selectall_arrayref($sql)}) { 
    276280        $dbh->do("DROP TABLE $tableref->[0] CASCADE") or croak $dbh->errstr; 
     
    312316                      dbhost => $_[3], 
    313317                    ); 
     318} 
     319 
     320sub _get_wanted_schema { 
     321    # Database handle passed in. 
     322    if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) { 
     323        return undef; 
     324    } 
     325 
     326    # Args passed as hashref. 
     327    if ( ref $_[0] and ref $_[0] eq 'HASH' ) { 
     328        my %args = %{$_[0]}; 
     329        return $args{wanted_schema}; 
     330    } 
     331 
     332    # Args passed as list of connection details. 
     333    return $_[1]; 
    314334} 
    315335