Changeset 437


Ignore:
Timestamp:
May 11, 2008, 8:24:34 PM (14 years ago)
Author:
Dominic Hargreaves
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.

Location:
wiki-toolkit/trunk/lib/Wiki/Toolkit
Files:
5 edited

Legend:

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

    r431 r437  
    188188# Is an upgrade to the database required?
    189189sub get_database_upgrade_required {
    190     my ($dbh,$VERSION) = @_;
     190    my ($dbh,$new_version) = @_;
    191191
    192192    # Get the schema version
     
    194194
    195195    # Compare it
    196     my $new_ver = $VERSION * 100;
    197     if($schema_version eq $new_ver) {
     196    if($schema_version eq $new_version) {
    198197        # At latest version
    199198        return undef;
     199    } elsif ($schema_version eq 'old' or $schema_version < $new_version) {
     200        return $schema_version."_to_".$new_version;
    200201    } else {
    201         return $schema_version."_to_".$new_ver;
     202        die "Aiee! We seem to be trying to downgrade the database schema from $schema_version to $new_version. Aborting.\n";
    202203    }
    203204}
  • 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
  • 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
  • wiki-toolkit/trunk/lib/Wiki/Toolkit/Setup/SQLite.pm

    r431 r437  
    1313use Carp;
    1414
    15 my %create_sql = (
    16     schema_info => "
     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);
    20 ",
    21 
    22     node => "
     23|, qq|
     24INSERT INTO schema_info VALUES (9)
     25| ],
     26
     27        node => [ qq|
    2328CREATE TABLE node (
    2429  id        integer      NOT NULL PRIMARY KEY AUTOINCREMENT,
     
    2934  moderate  boolean      NOT NULL default '0'
    3035)
    31 ",
    32     content => "
     36| ],
     37        content => [ qq|
    3338CREATE TABLE content (
    3439  node_id   integer      NOT NULL,
     
    4045  PRIMARY KEY (node_id, version)
    4146)
    42 ",
    43     internal_links => "
     47| ],
     48        internal_links => [ qq|
    4449CREATE TABLE internal_links (
    4550  link_from varchar(200) NOT NULL default '',
     
    4752  PRIMARY KEY (link_from, link_to)
    4853)
    49 ",
    50     metadata => "
     54| ],
     55        metadata => [ qq|
    5156CREATE TABLE metadata (
    5257  node_id        integer      NOT NULL,
     
    5560  metadata_value mediumtext   NOT NULL DEFAULT ''
    5661)
    57 "
    58 );
     62| ]
     63    },
     64};
    5965
    6066=head1 NAME
     
    94100again with a fresh database, run C<cleardb> first.
    95101
     102An optional second argument may be passed specifying the schema version
     103to use; this is B<ONLY> intended to be used during unit testing and should
     104not normally be specified.
     105
    96106=cut
    97107
     
    100110    my $dbh = _get_dbh( @args );
    101111    my $disconnect_required = _disconnect_required( @args );
     112    my $wanted_schema = _get_wanted_schema( @args ) || $SCHEMA_VERSION;
    102113
    103114    # Check whether tables exist, set them up if not.
     
    109120    my @cur_data;
    110121    if(scalar keys %tables > 0) {
    111         $upgrade_schema = Wiki::Toolkit::Setup::Database::get_database_upgrade_required($dbh,$VERSION);
     122        $upgrade_schema = Wiki::Toolkit::Setup::Database::get_database_upgrade_required($dbh,$wanted_schema);
    112123    }
    113124    if($upgrade_schema) {
     
    124135
    125136    # Set up tables if not found
    126     foreach my $required ( keys %create_sql ) {
     137    foreach my $required ( keys %{$create_sql->{$SCHEMA_VERSION}} ) {
    127138        if ( $tables{$required} ) {
    128139            print "Table $required already exists... skipping...\n";
    129140        } else {
    130141            print "Creating table $required... done\n";
    131             $dbh->do($create_sql{$required}) or croak $dbh->errstr;
     142            foreach my $sql (@{$create_sql->{$SCHEMA_VERSION}->{$required}} ) {
     143                $dbh->do($sql) or croak $dbh->errstr;
     144            }
    132145        }
    133146    }
    134 
    135     # Schema version
    136     $dbh->do("DELETE FROM schema_info");
    137     $dbh->do("INSERT INTO schema_info VALUES (". ($VERSION*100) .")");
    138147
    139148    # If upgrading, load in the new data
     
    153162    my $sql = "SELECT name FROM sqlite_master
    154163               WHERE type='table' AND name in ("
    155             . join( ",", map { $dbh->quote($_) } keys %create_sql ) . ")";
     164            . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$SCHEMA_VERSION}} ) . ")";
    156165    my $sth = $dbh->prepare($sql) or croak $dbh->errstr;
    157166    $sth->execute;
     
    195204    my $sql = "SELECT name FROM sqlite_master
    196205               WHERE type='table' AND name in ("
    197             . join( ",", map { $dbh->quote($_) } keys %create_sql ) . ")";
     206            . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$SCHEMA_VERSION}} ) . ")";
    198207    foreach my $tableref (@{$dbh->selectall_arrayref($sql)}) {
    199208        $dbh->do("DROP TABLE $tableref->[0]") or croak $dbh->errstr;
     
    216225        if ( $args{dbh} ) {
    217226            return $args{dbh};
    218     } else {
     227        } else {
    219228            return _make_dbh( %args );
    220229        }
     
    223232    # Args passed as list of connection details.
    224233    return _make_dbh( dbname => $_[0] );
     234}
     235
     236sub _get_wanted_schema {
     237    # Database handle passed in.
     238    if ( ref $_[0] and ref $_[0] eq 'DBI::db' ) {
     239        return undef;
     240    }
     241
     242    # Args passed as hashref.
     243    if ( ref $_[0] and ref $_[0] eq 'HASH' ) {
     244        my %args = %{$_[0]};
     245        return $args{wanted_schema};
     246    }
     247
     248    # Args passed as list of connection details.
     249    return $_[1];
    225250}
    226251
     
    236261        if ( $args{dbh} ) {
    237262            return 0;
    238     } else {
     263        } else {
    239264            return 1;
    240265        }
  • wiki-toolkit/trunk/lib/Wiki/Toolkit/TestLib.pm

    r432 r437  
    271271}
    272272
     273=item B<wiki_info>
     274
     275  my @wiki_info = $iterator->wiki_info;
     276
     277  Returns the @wiki_info array detailing configured test backends.
     278  Useful for very low-level testing only.
     279
     280=cut
     281
     282sub wiki_info {
     283    @wiki_info;
     284}
     285
    273286sub _test_dsn {
    274287    my ( $dsn, $dbuser, $dbpass, $dbhost ) = @_;
Note: See TracChangeset for help on using the changeset viewer.