Index: /trunk/LATMOS-Accounts/Changes
===================================================================
--- /trunk/LATMOS-Accounts/Changes	(revision 2242)
+++ /trunk/LATMOS-Accounts/Changes	(revision 2243)
@@ -1,2 +1,7 @@
+6.0.1
+    - Schema upgrade is now done automatically by la-sync-manager (update
+      module), or manually using la-sql-upgrade
+      Using a not up to date database will fail.
+
 5.2.23
     - add physicalDeliveryOfficeName attribute to nethost
Index: /trunk/LATMOS-Accounts/README
===================================================================
--- /trunk/LATMOS-Accounts/README	(revision 2242)
+++ /trunk/LATMOS-Accounts/README	(revision 2243)
@@ -24,8 +24,4 @@
 DEPENDENCIES
 
-This module requires these other modules and libraries:
-
-  blah blah blah
-
 COPYRIGHT AND LICENCE
 
Index: /trunk/LATMOS-Accounts/bin/la-sql-upgrade
===================================================================
--- /trunk/LATMOS-Accounts/bin/la-sql-upgrade	(revision 2242)
+++ /trunk/LATMOS-Accounts/bin/la-sql-upgrade	(revision 2243)
@@ -45,16 +45,13 @@
 $| = 1; # autoflush
 
+if ($nocommit) {
+    $ENV{LA_NO_COMMIT} = 1;
+}
+
 my $LA = LATMOS::Accounts->new($config, noacl => 1);
-my $labase = $LA->base($base);
+my $labase = $LA->_load_base($base);
 $labase->unexported(1);
-
-require LATMOS::Accounts::Bases::Sql::upgrade;
 
 $labase->SchemaUpgrade() or die "An error occur";
 
-if ($nocommit) {
-    $labase->rollback;
-} else {
-    $labase->commit;
-}
 print "Process terminated successfully\n";
Index: /trunk/LATMOS-Accounts/lib/LATMOS/Accounts.pm
===================================================================
--- /trunk/LATMOS-Accounts/lib/LATMOS/Accounts.pm	(revision 2242)
+++ /trunk/LATMOS-Accounts/lib/LATMOS/Accounts.pm	(revision 2243)
@@ -134,5 +134,11 @@
     my ($self, $section) = @_;
     # this method perform a cache
-    $self->_load_base($section || $self->default_base_name);
+    my $base = $self->_load_base($section || $self->default_base_name);
+
+    $base->load or do {
+        la_log(LA_ERR, "%s didn't load", $base->label);
+        return;
+    };
+    $base;
 }
 
@@ -166,9 +172,4 @@
         return;
     };
-    $base->load or do {
-        la_log(LA_ERR, "%s didn't load", $base->label);
-        return;
-    };
-    $base;
 }
 
Index: /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm
===================================================================
--- /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm	(revision 2242)
+++ /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm	(revision 2243)
@@ -18,5 +18,5 @@
 our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
 
-sub SCHEMA_VERSION { 31 };
+sub SCHEMA_VERSION { 32 };
 
 =head1 NAME
@@ -95,22 +95,32 @@
         $self->log(LA_DEBUG, 'New connection to DB');
 
-        {
-            my $sv = $self->get_global_value('schema_version') || 1;
-            if ($sv < SCHEMA_VERSION) {
-                require LATMOS::Accounts::Bases::Sql::upgrade;
-                if ($self->SchemaUpgrade()) {
-                    $self->commit;
-                } else {
-                    $self->rollback;
-                    return;
-                }
-            }
-        }
-
-        $self->{_db}->commit;
         return $self->{_db};
     }
 }
 
+sub IsSchemaUpToDate {
+    my ($self) = @_;
+
+    my $sv = $self->get_global_value('schema_version') || 1;
+
+    return $sv == SCHEMA_VERSION;
+}
+
+sub SchemaUpgrade {
+    my ($self) = @_;
+
+    if (!$self->IsSchemaUpToDate) {
+        require LATMOS::Accounts::Bases::Sql::upgrade;
+        if ($self->_SchemaUpgrade()) {
+            $self->commit;
+        } else {
+            $self->rollback;
+            return;
+        }
+    } else {
+        return 1;
+    }
+}
+
 =head2 load
 
@@ -123,15 +133,10 @@
     if (!$self->db) { return 0 };
 
-    my $sv = $self->get_global_value('schema_version') || 1;
-    if ($sv < SCHEMA_VERSION) {
-        $self->log(LA_CRIT,
-            "Schema version %d found, %d is need, please update db using " .
-            "`la-sql-upgrade' tool for `%s' base",
-            $sv,
-            SCHEMA_VERSION,
-            $self->label,
-        );
-        # return;
-    }
+    if (!$self->IsSchemaUpToDate) {
+        my $sv = $self->get_global_value('schema_version') || 1;
+        $self->log(LA_ERR, "Schema need update, please run `la-sql-upgrade -b %s'", $self->label); 
+        return;
+    }
+
     if (!$self->_CreateInternalObjects) {
         $self->rollback;
Index: /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/upgrade.pm
===================================================================
--- /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/upgrade.pm	(revision 2242)
+++ /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/upgrade.pm	(revision 2243)
@@ -1196,4 +1196,8 @@
             ],
         },
+        {
+            ver => 32,
+            recompute => 1,
+        }
     );
 
@@ -1245,7 +1249,13 @@
 =cut
 
-sub SchemaUpgrade {
+sub _SchemaUpgrade {
     my ($self) = @_;
 
+    $self->log(LA_INFO, "Waiting to lock database to upgrade the schema...");
+
+    $self->db->do(q{SELECT 1 FROM settings WHERE varname = 'schema_version' FOR UPDATE});
+
+    $self->log(LA_INFO, "Database locked upgrading schema !");
+ 
     return $self->temp_switch_unexported(
         sub {
@@ -1268,4 +1278,16 @@
     );
 
+    if ($self->{_base}->get_global_value('NeedRecomputeObject')) {
+        foreach my $otype (qw(user)) {
+            foreach my $name ($self->listRealObjects($otype)) {
+                my $object = $self->get_object($otype, $name);
+                $object->PostSetAttribute();
+            }
+        }
+        $self->{_base}->del_global_value('NeedRecomputeObject');
+    }
+
+    $self->commit;
+
     return 1;
 }
Index: /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Task/Update.pm
===================================================================
--- /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Task/Update.pm	(revision 2242)
+++ /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Task/Update.pm	(revision 2243)
@@ -18,10 +18,10 @@
 =cut
 
+sub order { -1 }
 
 sub init {
     my ($self) = @_;
     my $LA = LATMOS::Accounts->new($self->{config});
-    my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->base;
-    $labase && $labase->load or die "Cannot load base";
+    my $labase && $labase->_load_base($self->{base}) or die "Cannot load base";
 
     $self->{_la} = $LA;
@@ -34,20 +34,5 @@
     my ($self) = @_;
 
-    if ($self->{_base}->get_global_value('NeedRecomputeObject')) {
-        $self->{_base}->log(LA_INFO, "NeedRecomputeObject detected, recomputing objects");
-    } else {
-        return 1;
-    }
-
-    foreach my $otype (qw(user)) {
-        foreach my $name ($self->{_base}->listRealObjects($otype)) {
-            my $object = $self->{_base}->get_object($otype, $name);
-            $object->PostSetAttribute();
-            $self->{_base}->commit;
-        }
-    }
-
-    $self->{_base}->del_global_value('NeedRecomputeObject');
-    $self->{_base}->commit;
+    $self->SchemaUpgrade();
 
     1;
