IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 28756


Ignore:
Timestamp:
Jul 28, 2010, 4:20:06 PM (16 years ago)
Author:
rhenders
Message:

Added revision and upgrade stuff

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippToPsps/perl/ippToPsps/IppToPspsDb.pm

    r28698 r28756  
    1111#######################################################################################
    1212#
    13 # Gets the last processed exposure ID (ini file for now - will be Db eventually TODO)
     13#  Override constructor
     14#
     15########################################################################################
     16sub new {
     17    my ($class) = @_;
     18
     19    # Call the constructor of the parent class, Person.
     20    my $self = $class->SUPER::new($_[1], $_[2], $_[3], $_[4], $_[5], $_[6]);
     21
     22    bless $self, $class;
     23
     24    $self->update();
     25    return $self;
     26}
     27
     28#######################################################################################
     29#
     30# Gets the last processed exposure ID
    1431#
    1532########################################################################################
     
    4259    my ($self, $batchId, $expId, $processed, $published, $totalDetections) = @_;
    4360
     61print "HJHJH '$batchId', '$expId', '$processed', '$published', '$totalDetections'\n";
     62
     63if (!$totalDetections) {$totalDetections = -1;}
     64
    4465    my $query = $self->{_db}->prepare(<<SQL);
    4566    UPDATE batches
     
    82103########################################################################################
    83104sub getNewBatchId {
    84     my ($self, $expId, $surveyType) = @_;
     105    my ($self, $expId, $surveyType, $batchType) = @_;
    85106
    86107    my $query = $self->{_db}->prepare(<<SQL);
     
    99120    my $query = $self->{_db}->prepare(<<SQL);
    100121    INSERT INTO batches
    101         (batch_id, exp_id, survey_id)
     122        (batch_id, exp_id, survey_id, batch_type)
    102123        VALUES
    103         ($batchId, $expId, '$surveyType');
     124        ($batchId, $expId, '$surveyType', '$batchType');
    104125
    105126SQL
     
    112133}
    113134
    114 
     135###########################################################################
     136#
     137# Update Db to newest version
     138#
     139###########################################################################
     140sub update {
     141    my ($self) = @_;
     142
     143    my $currentRevision = -1;
     144    my $latestRevision = 4;
     145
     146    while ($currentRevision != $latestRevision) {
     147
     148        $currentRevision = $self->getRevision();
     149        if ($self->{_verbose}) {print "* Current revision = $currentRevision\n";}
     150
     151        if ($currentRevision == 0) {$self->createRevision_1();}
     152        elsif ($currentRevision == 1) {$self->createRevision_2();}
     153        elsif ($currentRevision == 2) {$self->createRevision_3();}
     154        elsif ($currentRevision == 3) {$self->createRevision_4();}
     155    }
     156}
     157
     158#######################################################################################
     159#
     160# Create revision 1 of the database
     161#
     162#######################################################################################
     163sub createRevision_1 {
     164    my ($self) = @_;
     165
     166    print "* Creating revision 1 of '$dbname'\n";
     167
     168    my $query = $self->{_db}->prepare(<<SQL);
     169    CREATE TABLE revision (
     170            revision INT,
     171            created TIMESTAMP DEFAULT NOW(),
     172            primary key (revision)
     173            );
     174SQL
     175        $query->execute;
     176
     177    my $query = $self->{_db}->prepare(<<SQL);
     178    CREATE TABLE batches (
     179            batch_id BIGINT NOT NULL,
     180            exp_id BIGINT NOT NULL,
     181            survey_id VARCHAR(30) DEFAULT "NONE",
     182            processed TINYINT DEFAULT 0,
     183            on_datastore TINYINT DEFAULT 0,
     184            loaded_to_ODM TINYINT DEFAULT 0,
     185            merge_worthy TINYINT DEFAULT 0,
     186            deleted TINYINT DEFAULT 0,
     187            created TIMESTAMP DEFAULT NOW(),
     188            primary key (batch_id, exp_id)
     189            );
     190
     191SQL
     192        $query->execute;
     193
     194    setRevision(1);
     195}
     196
     197#######################################################################################
     198#
     199# Create revision 2 of the database
     200#
     201#######################################################################################
     202sub createRevision_2 {
     203    my ($self) = @_;
     204
     205    print "* Creating revision 2 of '$dbname'\n";
     206
     207    my $query = $self->{_db}->prepare(<<SQL);
     208    ALTER TABLE batches
     209        ADD COLUMN merged TINYINT DEFAULT 0
     210SQL
     211        $query->execute;
     212
     213    setRevision(2);
     214}
     215
     216#######################################################################################
     217#
     218# Create revision 3 of the database
     219#
     220#######################################################################################
     221sub createRevision_3 {
     222    my ($self) = @_;
     223
     224    print "* Creating revision 3 of '$dbname'\n";
     225
     226    my $query = $self->{_db}->prepare(<<SQL);
     227    ALTER TABLE batches
     228        ADD COLUMN total_detections BIGINT DEFAULT 0
     229SQL
     230        $query->execute;
     231
     232    setRevision(3);
     233}
     234
     235#######################################################################################
     236#
     237# Create revision 4 of the database
     238#
     239#######################################################################################
     240sub createRevision_4 {
     241    my ($self) = @_;
     242
     243    print "* Creating revision 4 of '$dbname'\n";
     244
     245    my $query = $self->{_db}->prepare(<<SQL);
     246    ALTER TABLE batches
     247        ADD COLUMN batch_type VARCHAR(30) DEFAULT "UNKNOWN"
     248SQL
     249        $query->execute;
     250
     251    setRevision(4);
     252}
     253
     254#######################################################################################
     255#
     256# Sets current revision of ippToPsps database
     257#
     258#######################################################################################
     259sub setRevision {
     260    my ($self, $revision) = @_;
     261
     262    my $query = $self->{_db}->prepare(<<SQL);
     263    INSERT INTO revision (revision) VALUES ($revision);
     264SQL
     265        $query->execute;
     266}
     267
     268#######################################################################################
     269#
     270# Gets current revision of ippToPsps database
     271#
     272#######################################################################################
     273sub getRevision {
     274    my ($self) = @_;
     275
     276    if (!$self->SUPER::doesTableExist("revision")) {return 0;}
     277
     278    my $query = $self->{_db}->prepare(<<SQL);
     279    SELECT revision
     280        FROM revision
     281        ORDER BY revision DESC LIMIT 1;
     282SQL
     283
     284        $query->execute;
     285    my @row = $query->fetchrow_array();
     286
     287    return $row[0];
     288}
     2891
Note: See TracChangeset for help on using the changeset viewer.