Changeset 2243


Ignore:
Timestamp:
05/21/19 17:30:15 (5 years ago)
Author:
nanardon
Message:

Improve schema upgrade procedure

Location:
trunk/LATMOS-Accounts
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/Changes

    r2119 r2243  
     16.0.1 
     2    - Schema upgrade is now done automatically by la-sync-manager (update 
     3      module), or manually using la-sql-upgrade 
     4      Using a not up to date database will fail. 
     5 
    165.2.23 
    27    - add physicalDeliveryOfficeName attribute to nethost 
  • trunk/LATMOS-Accounts/README

    r1 r2243  
    2424DEPENDENCIES 
    2525 
    26 This module requires these other modules and libraries: 
    27  
    28   blah blah blah 
    29  
    3026COPYRIGHT AND LICENCE 
    3127 
  • trunk/LATMOS-Accounts/bin/la-sql-upgrade

    r2085 r2243  
    4545$| = 1; # autoflush 
    4646 
     47if ($nocommit) { 
     48    $ENV{LA_NO_COMMIT} = 1; 
     49} 
     50 
    4751my $LA = LATMOS::Accounts->new($config, noacl => 1); 
    48 my $labase = $LA->base($base); 
     52my $labase = $LA->_load_base($base); 
    4953$labase->unexported(1); 
    50  
    51 require LATMOS::Accounts::Bases::Sql::upgrade; 
    5254 
    5355$labase->SchemaUpgrade() or die "An error occur"; 
    5456 
    55 if ($nocommit) { 
    56     $labase->rollback; 
    57 } else { 
    58     $labase->commit; 
    59 } 
    6057print "Process terminated successfully\n"; 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts.pm

    r2202 r2243  
    134134    my ($self, $section) = @_; 
    135135    # this method perform a cache 
    136     $self->_load_base($section || $self->default_base_name); 
     136    my $base = $self->_load_base($section || $self->default_base_name); 
     137 
     138    $base->load or do { 
     139        la_log(LA_ERR, "%s didn't load", $base->label); 
     140        return; 
     141    }; 
     142    $base; 
    137143} 
    138144 
     
    166172        return; 
    167173    }; 
    168     $base->load or do { 
    169         la_log(LA_ERR, "%s didn't load", $base->label); 
    170         return; 
    171     }; 
    172     $base; 
    173174} 
    174175 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm

    r2236 r2243  
    1818our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; 
    1919 
    20 sub SCHEMA_VERSION { 31 }; 
     20sub SCHEMA_VERSION { 32 }; 
    2121 
    2222=head1 NAME 
     
    9595        $self->log(LA_DEBUG, 'New connection to DB'); 
    9696 
    97         { 
    98             my $sv = $self->get_global_value('schema_version') || 1; 
    99             if ($sv < SCHEMA_VERSION) { 
    100                 require LATMOS::Accounts::Bases::Sql::upgrade; 
    101                 if ($self->SchemaUpgrade()) { 
    102                     $self->commit; 
    103                 } else { 
    104                     $self->rollback; 
    105                     return; 
    106                 } 
    107             } 
    108         } 
    109  
    110         $self->{_db}->commit; 
    11197        return $self->{_db}; 
    11298    } 
    11399} 
    114100 
     101sub IsSchemaUpToDate { 
     102    my ($self) = @_; 
     103 
     104    my $sv = $self->get_global_value('schema_version') || 1; 
     105 
     106    return $sv == SCHEMA_VERSION; 
     107} 
     108 
     109sub SchemaUpgrade { 
     110    my ($self) = @_; 
     111 
     112    if (!$self->IsSchemaUpToDate) { 
     113        require LATMOS::Accounts::Bases::Sql::upgrade; 
     114        if ($self->_SchemaUpgrade()) { 
     115            $self->commit; 
     116        } else { 
     117            $self->rollback; 
     118            return; 
     119        } 
     120    } else { 
     121        return 1; 
     122    } 
     123} 
     124 
    115125=head2 load 
    116126 
     
    123133    if (!$self->db) { return 0 }; 
    124134 
    125     my $sv = $self->get_global_value('schema_version') || 1; 
    126     if ($sv < SCHEMA_VERSION) { 
    127         $self->log(LA_CRIT, 
    128             "Schema version %d found, %d is need, please update db using " . 
    129             "`la-sql-upgrade' tool for `%s' base", 
    130             $sv, 
    131             SCHEMA_VERSION, 
    132             $self->label, 
    133         ); 
    134         # return; 
    135     } 
     135    if (!$self->IsSchemaUpToDate) { 
     136        my $sv = $self->get_global_value('schema_version') || 1; 
     137        $self->log(LA_ERR, "Schema need update, please run `la-sql-upgrade -b %s'", $self->label);  
     138        return; 
     139    } 
     140 
    136141    if (!$self->_CreateInternalObjects) { 
    137142        $self->rollback; 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/upgrade.pm

    r2236 r2243  
    11961196            ], 
    11971197        }, 
     1198        { 
     1199            ver => 32, 
     1200            recompute => 1, 
     1201        } 
    11981202    ); 
    11991203 
     
    12451249=cut 
    12461250 
    1247 sub SchemaUpgrade { 
     1251sub _SchemaUpgrade { 
    12481252    my ($self) = @_; 
    12491253 
     1254    $self->log(LA_INFO, "Waiting to lock database to upgrade the schema..."); 
     1255 
     1256    $self->db->do(q{SELECT 1 FROM settings WHERE varname = 'schema_version' FOR UPDATE}); 
     1257 
     1258    $self->log(LA_INFO, "Database locked upgrading schema !"); 
     1259  
    12501260    return $self->temp_switch_unexported( 
    12511261        sub { 
     
    12681278    ); 
    12691279 
     1280    if ($self->{_base}->get_global_value('NeedRecomputeObject')) { 
     1281        foreach my $otype (qw(user)) { 
     1282            foreach my $name ($self->listRealObjects($otype)) { 
     1283                my $object = $self->get_object($otype, $name); 
     1284                $object->PostSetAttribute(); 
     1285            } 
     1286        } 
     1287        $self->{_base}->del_global_value('NeedRecomputeObject'); 
     1288    } 
     1289 
     1290    $self->commit; 
     1291 
    12701292    return 1; 
    12711293} 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Task/Update.pm

    r2220 r2243  
    1818=cut 
    1919 
     20sub order { -1 } 
    2021 
    2122sub init { 
    2223    my ($self) = @_; 
    2324    my $LA = LATMOS::Accounts->new($self->{config}); 
    24     my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->base; 
    25     $labase && $labase->load or die "Cannot load base"; 
     25    my $labase && $labase->_load_base($self->{base}) or die "Cannot load base"; 
    2626 
    2727    $self->{_la} = $LA; 
     
    3434    my ($self) = @_; 
    3535 
    36     if ($self->{_base}->get_global_value('NeedRecomputeObject')) { 
    37         $self->{_base}->log(LA_INFO, "NeedRecomputeObject detected, recomputing objects"); 
    38     } else { 
    39         return 1; 
    40     } 
    41  
    42     foreach my $otype (qw(user)) { 
    43         foreach my $name ($self->{_base}->listRealObjects($otype)) { 
    44             my $object = $self->{_base}->get_object($otype, $name); 
    45             $object->PostSetAttribute(); 
    46             $self->{_base}->commit; 
    47         } 
    48     } 
    49  
    50     $self->{_base}->del_global_value('NeedRecomputeObject'); 
    51     $self->{_base}->commit; 
     36    $self->SchemaUpgrade(); 
    5237 
    5338    1; 
Note: See TracChangeset for help on using the changeset viewer.