Changeset 440


Ignore:
Timestamp:
May 12, 2008, 1:21:15 AM (14 years ago)
Author:
Dominic Hargreaves
Message:

Add more pieces to support schema upgrade testing (including support for
setting up version 8 schemas) and add actual test script

Location:
wiki-toolkit/trunk
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • wiki-toolkit/trunk/Changes

    r439 r440  
    110.76
    22        Really add missing prereq of DBI!
     3        Add testing of database schema upgrades
    34
    450.75    11 May 2008
  • wiki-toolkit/trunk/MANIFEST

    r435 r440  
    8181t/303_feed_atom_node_all_versions.t
    8282t/304_feed_atom_gen_node_dist_feed.t
     83t/400_upgrade.t
    8384t/lib/Wiki/Toolkit/Plugin/Bar.pm
    8485t/lib/Wiki/Toolkit/Plugin/Foo.pm
  • wiki-toolkit/trunk/lib/Wiki/Toolkit/Setup/Database.pm

    r438 r440  
    33use strict;
    44
    5 use vars qw( $VERSION @SUPPORTED_SCHEMAS );
     5use vars qw( $VERSION @SUPPORTED_SCHEMAS);
    66
    77$VERSION = 0.08;
    8 @SUPPORTED_SCHEMAS = qw(9);
     8@SUPPORTED_SCHEMAS = qw(8 9);
    99
    1010=head1 NAME
  • wiki-toolkit/trunk/lib/Wiki/Toolkit/Setup/MySQL.pm

    r438 r440  
    1616
    1717my $create_sql = {
     18    8 => {
     19        schema_info => [ qq|
     20CREATE TABLE schema_info (
     21  version   int(10)      NOT NULL default 0
     22)
     23|, qq|
     24INSERT INTO schema_info VALUES (8)
     25| ],
     26
     27        node => [ qq|
     28CREATE TABLE node (
     29  id        integer      NOT NULL AUTO_INCREMENT,
     30  name      varchar(200) NOT NULL DEFAULT '',
     31  version   int(10)      NOT NULL default 0,
     32  text      mediumtext   NOT NULL default '',
     33  modified  datetime     default NULL,
     34  PRIMARY KEY (id)
     35)
     36| ],
     37
     38        content => [ qq|
     39CREATE TABLE content (
     40  node_id   integer      NOT NULL,
     41  version   int(10)      NOT NULL default 0,
     42  text      mediumtext   NOT NULL default '',
     43  modified  datetime     default NULL,
     44  comment   mediumtext   NOT NULL default '',
     45  PRIMARY KEY (node_id, version)
     46)
     47| ],
     48        internal_links => [ qq|
     49CREATE TABLE internal_links (
     50  link_from varchar(200) NOT NULL default '',
     51  link_to   varchar(200) NOT NULL default '',
     52  PRIMARY KEY (link_from, link_to)
     53)
     54| ],
     55        metadata => [ qq|
     56CREATE TABLE metadata (
     57  node_id        integer      NOT NULL,
     58  version        int(10)      NOT NULL default 0,
     59  metadata_type  varchar(200) NOT NULL DEFAULT '',
     60  metadata_value mediumtext   NOT NULL DEFAULT ''
     61)
     62|, qq|
     63CREATE INDEX metadata_index ON metadata(node_id, version, metadata_type, metadata_value(10))
     64| ]
     65    },
    1866    9 => {
    1967        schema_info => [ qq|
     
    119167    my $wanted_schema = _get_wanted_schema( @args ) || $SCHEMA_VERSION;
    120168
     169    die "No schema information for requested schema version $wanted_schema\n"
     170        unless $create_sql->{$wanted_schema};
     171
    121172    # Check whether tables exist
    122     my %tables = fetch_tables_listing($dbh);
     173    my %tables = fetch_tables_listing($dbh, $wanted_schema);
    123174
    124175    # Do we need to upgrade the schema of existing tables?
     
    127178    my @cur_data;
    128179    if(scalar keys %tables > 0) {
    129         $upgrade_schema = Wiki::Toolkit::Setup::Database::get_database_upgrade_required($dbh,$SCHEMA_VERSION);
     180        $upgrade_schema = Wiki::Toolkit::Setup::Database::get_database_upgrade_required($dbh,$wanted_schema);
    130181    }
    131182    if($upgrade_schema) {
     
    146197
    147198        # Grab new list of tables
    148         %tables = fetch_tables_listing($dbh);
     199        %tables = fetch_tables_listing($dbh, $wanted_schema);
    149200    }
    150201
    151202    # Set up tables if not found
    152     foreach my $required ( keys %{$create_sql->{$SCHEMA_VERSION}} ) {
     203    foreach my $required ( keys %{$create_sql->{$wanted_schema}} ) {
    153204        if ( $tables{$required} ) {
    154205            print "Table $required already exists... skipping...\n";
    155206        } else {
    156207            print "Creating table $required... done\n";
    157             foreach my $sql ( @{$create_sql->{$SCHEMA_VERSION}->{$required}} ) {
     208            foreach my $sql ( @{$create_sql->{$wanted_schema}->{$required}} ) {
    158209                $dbh->do($sql) or croak $dbh->errstr;
    159210            }
     
    173224sub fetch_tables_listing {
    174225    my $dbh = shift;
     226    my $wanted_schema = shift;
    175227
    176228    # Check what tables exist
     
    179231    my %tables;
    180232    while ( my $table = $sth->fetchrow_array ) {
    181         exists $create_sql->{$SCHEMA_VERSION}->{$table} and $tables{$table} = 1;
     233        exists $create_sql->{$wanted_schema}->{$table} and $tables{$table} = 1;
    182234    }
    183235    return %tables;
     
    264316        return $args{wanted_schema};
    265317    }
    266 
    267     # Args passed as list of connection details.
    268     return $_[1];
    269318}
    270319
  • wiki-toolkit/trunk/lib/Wiki/Toolkit/Setup/Pg.pm

    r438 r440  
    1616
    1717my $create_sql = {
     18    8 => {
     19        schema_info => [ qq|
     20CREATE TABLE schema_info (
     21  version   integer      NOT NULL default 0
     22)
     23|, qq|
     24INSERT INTO schema_info VALUES (8)
     25| ],
     26
     27        node => [ qq|
     28CREATE SEQUENCE node_seq
     29|, qq|
     30CREATE TABLE node (
     31  id        integer      NOT NULL DEFAULT NEXTVAL('node_seq'),
     32  name      varchar(200) NOT NULL DEFAULT '',
     33  version   integer      NOT NULL default 0,
     34  text      text         NOT NULL default '',
     35  modified  timestamp without time zone    default NULL,
     36  CONSTRAINT pk_id PRIMARY KEY (id)
     37)
     38|, qq|
     39CREATE UNIQUE INDEX node_name ON node (name)
     40| ],
     41
     42        content => [ qq|
     43CREATE TABLE content (
     44  node_id   integer      NOT NULL,
     45  version   integer      NOT NULL default 0,
     46  text      text         NOT NULL default '',
     47  modified  timestamp without time zone    default NULL,
     48  comment   text         NOT NULL default '',
     49  CONSTRAINT pk_node_id PRIMARY KEY (node_id,version),
     50  CONSTRAINT fk_node_id FOREIGN KEY (node_id) REFERENCES node (id)
     51)
     52| ],
     53
     54        internal_links => [ qq|
     55CREATE TABLE internal_links (
     56  link_from varchar(200) NOT NULL default '',
     57  link_to   varchar(200) NOT NULL default ''
     58)
     59|, qq|
     60CREATE UNIQUE INDEX internal_links_pkey ON internal_links (link_from, link_to)
     61| ],
     62
     63        metadata => [ qq|
     64CREATE TABLE metadata (
     65  node_id        integer      NOT NULL,
     66  version        integer      NOT NULL default 0,
     67  metadata_type  varchar(200) NOT NULL DEFAULT '',
     68  metadata_value text         NOT NULL DEFAULT '',
     69  CONSTRAINT fk_node_id FOREIGN KEY (node_id) REFERENCES node (id)
     70)
     71|, qq|
     72CREATE INDEX metadata_index ON metadata (node_id, version, metadata_type, metadata_value)
     73| ]
     74
     75    },
    1876    9 => {
    1977        schema_info => [ qq|
     
    113171ALTER TABLE metadata ADD CONSTRAINT fk_node_id FOREIGN KEY (node_id) REFERENCES node (id);
    114172CREATE INDEX metadata_index ON metadata (node_id, version, metadata_type, metadata_value)
     173|,
     174
     175qq|
     176CREATE TABLE schema_info (version integer NOT NULL default 0);
     177INSERT INTO schema_info VALUES (8)
    115178|
    116179],
     
    126189ALTER TABLE content ALTER COLUMN moderated SET DEFAULT '1';
    127190ALTER TABLE content ALTER COLUMN moderated SET NOT NULL;
     191UPDATE schema_info SET version = 9;
    128192|
    129193],
     
    185249    my $wanted_schema = _get_wanted_schema( @args ) || $SCHEMA_VERSION;
    186250
     251    die "No schema information for requested schema version $wanted_schema\n"
     252        unless $create_sql->{$wanted_schema};
     253
    187254    # Check whether tables exist
    188255    my $sql = "SELECT tablename FROM pg_tables
    189256               WHERE tablename in ("
    190             . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$SCHEMA_VERSION}} ) . ")";
     257            . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$wanted_schema}} ) . ")";
    191258    my $sth = $dbh->prepare($sql) or croak $dbh->errstr;
    192259    $sth->execute;
    193260    my %tables;
    194261    while ( my $table = $sth->fetchrow_array ) {
    195         exists $create_sql->{$SCHEMA_VERSION}->{$table} and $tables{$table} = 1;
     262        exists $create_sql->{$wanted_schema}->{$table} and $tables{$table} = 1;
    196263    }
    197264
     
    206273
    207274    # Set up tables if not found
    208     foreach my $required ( reverse sort keys %{$create_sql->{$SCHEMA_VERSION}} ) {
     275    foreach my $required ( reverse sort keys %{$create_sql->{$wanted_schema}} ) {
    209276        if ( $tables{$required} ) {
    210277            print "Table $required already exists... skipping...\n";
    211278        } else {
    212279            print "Creating table $required... done\n";
    213             foreach my $sql ( @{ $create_sql->{$SCHEMA_VERSION}->{$required} } ) {
     280            foreach my $sql ( @{ $create_sql->{$wanted_schema}->{$required} } ) {
    214281                $dbh->do($sql) or croak $dbh->errstr;
    215282            }
     
    329396        return $args{wanted_schema};
    330397    }
    331 
    332     # Args passed as list of connection details.
    333     return $_[1];
    334398}
    335399
  • wiki-toolkit/trunk/lib/Wiki/Toolkit/Setup/SQLite.pm

    r438 r440  
    1616
    1717my $create_sql = {
     18    8 => {
     19        schema_info => [ qq|
     20CREATE TABLE schema_info (
     21  version   integer      NOT NULL default 0
     22);
     23|, qq|
     24INSERT INTO schema_info VALUES (8)
     25| ],
     26        node => [ qq|
     27CREATE TABLE node (
     28  id        integer      NOT NULL PRIMARY KEY AUTOINCREMENT,
     29  name      varchar(200) NOT NULL DEFAULT '',
     30  version   integer      NOT NULL default 0,
     31  text      mediumtext   NOT NULL default '',
     32  modified  datetime     default NULL
     33)
     34| ],
     35        content => [ qq|
     36CREATE TABLE content (
     37  node_id   integer      NOT NULL,
     38  version   integer      NOT NULL default 0,
     39  text      mediumtext   NOT NULL default '',
     40  modified  datetime     default NULL,
     41  comment   mediumtext   NOT NULL default '',
     42  PRIMARY KEY (node_id, version)
     43)
     44| ],
     45        internal_links => [ qq|
     46CREATE TABLE internal_links (
     47  link_from varchar(200) NOT NULL default '',
     48  link_to   varchar(200) NOT NULL default '',
     49  PRIMARY KEY (link_from, link_to)
     50)
     51| ],
     52        metadata => [ qq|
     53CREATE TABLE metadata (
     54  node_id        integer      NOT NULL,
     55  version        integer      NOT NULL default 0,
     56  metadata_type  varchar(200) NOT NULL DEFAULT '',
     57  metadata_value mediumtext   NOT NULL DEFAULT ''
     58)
     59| ]
     60    },
    1861    9 => {
    1962        schema_info => [ qq|
     
    112155    my $wanted_schema = _get_wanted_schema( @args ) || $SCHEMA_VERSION;
    113156
     157    die "No schema information for requested schema version $wanted_schema\n"
     158            unless $create_sql->{$wanted_schema};
     159
    114160    # Check whether tables exist, set them up if not.
    115     my %tables = fetch_tables_listing($dbh);
     161    my %tables = fetch_tables_listing($dbh, $wanted_schema);
    116162
    117163    # Do we need to upgrade the schema?
     
    131177
    132178        # Grab new list of tables
    133         %tables = fetch_tables_listing($dbh);
     179        %tables = fetch_tables_listing($dbh, $wanted_schema);
    134180    }
    135181
    136182    # Set up tables if not found
    137     foreach my $required ( keys %{$create_sql->{$SCHEMA_VERSION}} ) {
     183    foreach my $required ( keys %{$create_sql->{$wanted_schema}} ) {
    138184        if ( $tables{$required} ) {
    139185            print "Table $required already exists... skipping...\n";
    140186        } else {
    141187            print "Creating table $required... done\n";
    142             foreach my $sql (@{$create_sql->{$SCHEMA_VERSION}->{$required}} ) {
     188            foreach my $sql (@{$create_sql->{$wanted_schema}->{$required}} ) {
    143189                $dbh->do($sql) or croak $dbh->errstr;
    144190            }
     
    158204sub fetch_tables_listing {
    159205    my $dbh = shift;
     206    my $wanted_schema = shift;
    160207
    161208    # Check whether tables exist, set them up if not.
    162209    my $sql = "SELECT name FROM sqlite_master
    163210               WHERE type='table' AND name in ("
    164             . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$SCHEMA_VERSION}} ) . ")";
     211            . join( ",", map { $dbh->quote($_) } keys %{$create_sql->{$wanted_schema}} ) . ")";
    165212    my $sth = $dbh->prepare($sql) or croak $dbh->errstr;
    166213    $sth->execute;
     
    245292        return $args{wanted_schema};
    246293    }
    247 
    248     # Args passed as list of connection details.
    249     return $_[1];
    250294}
    251295
  • wiki-toolkit/trunk/lib/Wiki/Toolkit/TestLib.pm

    r438 r440  
    7070                                                 dbhost => $config{dbhost},
    7171                                               },
     72                                     dsn    => $dsn
    7273                                   };
    7374    }
Note: See TracChangeset for help on using the changeset viewer.