Changeset 304


Ignore:
Timestamp:
May 20, 2006, 11:07:13 AM (14 years ago)
Author:
Dominic Hargreaves
Message:

Check for the schema version before trying to use the database (closes #5)

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

Legend:

Unmodified
Added
Removed
  • wiki-toolkit/trunk/Changes

    r301 r304  
    110.69_04
    22        Feed code being refactored.
     3        Check that the database schema is up-to-date when initializing the
     4        store and abort otherwise.
    35
    460.69_03 13 May 2006
  • wiki-toolkit/trunk/MANIFEST

    r303 r304  
    5757t/022_list_unmoderated.t
    5858t/025_list_node_all_versions.t
     59t/026_schema_current.t
    5960t/040_rename.t
    6061t/050_mysql_store.t
  • wiki-toolkit/trunk/lib/Wiki/Toolkit/Store/Database.pm

    r302 r304  
    1313
    1414$VERSION = '0.27';
     15my $SCHEMA_VER = 9;
    1516
    1617# first, detect if Encode is available - it's not under 5.6. If we _are_
     
    113114          or croak "Can't connect to database $dbname using $dsn: "
    114115                   . DBI->errstr;
     116    }
     117
     118    my ($cur_ver, $db_ver) = $self->schema_current;
     119    if ($db_ver < $cur_ver) {
     120        croak "Database schema version $db_ver is too old (need $cur_ver)";
     121    } elsif ($db_ver > $cur_ver) {
     122        croak "Database schema version $db_ver is too new (need $cur_ver)";
    115123    }
    116124
     
    16481656}
    16491657
     1658=item B<schema_current>
     1659
     1660  my ($code_version, $db_version) = $store->schema_current;
     1661  if ($code_version == $db_version)
     1662      # Do stuff
     1663  } else {
     1664      # Bail
     1665  }
     1666
     1667=cut
     1668
     1669sub schema_current {
     1670    my $self = shift;
     1671    my $dbh = $self->dbh;
     1672    my $sth;
     1673    eval { $sth = $dbh->prepare("SELECT version FROM schema_info") };
     1674    if ($@) {
     1675        return ($SCHEMA_VER, 0);
     1676    }
     1677    eval { $sth->execute };
     1678    if ($@) {
     1679        return ($SCHEMA_VER, 0);
     1680    }
     1681    my $version;
     1682    eval { $version = $sth->fetchrow_array };
     1683    if ($@) {
     1684        return ($SCHEMA_VER, 0);
     1685    } else {
     1686        return ($SCHEMA_VER, $version);
     1687    }
     1688}
     1689
     1690
    16501691=item B<dbh>
    16511692
Note: See TracChangeset for help on using the changeset viewer.