Index: server/trunk/web/bin/sophie_scan
===================================================================
--- server/trunk/web/bin/sophie_scan	(revision 41)
+++ server/trunk/web/bin/sophie_scan	(revision 42)
@@ -109,5 +109,5 @@
                 foreach my $pathkey (@{ $job }) {
                     my @delta = Sophie::Base::RpmsPath->new($pathkey)
-                    ->find_delta;
+                        ->find_delta;
                     while (my @d = splice(@delta, 0, 25)) {
                         my $path = Sophie::Base::RpmsPath->new($pathkey);
Index: server/trunk/web/lib/Sophie.pm
===================================================================
--- server/trunk/web/lib/Sophie.pm	(revision 41)
+++ server/trunk/web/lib/Sophie.pm	(revision 42)
@@ -74,6 +74,9 @@
 __PACKAGE__->config->{session} = {
     expires   => 3600,
-    dbi_dbh   => 'Base',
+    dbi_dbh   => 'Session',
     dbi_table => 'sessions',
+#    dbi_dsn => 'dbi:Pg:' . $config->{dbconnect},
+#    dbi_user => $config->{dbuser},
+#    dbi_pass => $config->{dbpassword},
 };
 
Index: server/trunk/web/lib/Sophie/Base.pm
===================================================================
--- server/trunk/web/lib/Sophie/Base.pm	(revision 41)
+++ server/trunk/web/lib/Sophie/Base.pm	(revision 42)
@@ -10,13 +10,5 @@
 __PACKAGE__->load_namespaces();
 
-sub connection {
-    my ($class) = @_;
-    $class->SUPER::connection(
-        sub { __PACKAGE__->db },
-   )
-}
-
-sub db {
-   my ($self) = @_;
+sub default_config {
    my $config;
    foreach my $file ('sophie.conf', "$Bin/../sophie.conf",
@@ -27,14 +19,25 @@
     }
     $config or die "No config found";
+    return $config;
+}
 
-   DBI->connect_cached(
-       'dbi:Pg:' . $config->{dbconnect},
-       $config->{dbuser},
-       $config->{dbpassword},
-       {
-           AutoCommit => 0,
-           PrintError => 1,
-       }
-   ); 
+sub connection {
+    my ($class, $connect_info) = @_;
+    if (! $connect_info->{dsn}) {
+        my $config = default_config();
+        $connect_info->{dsn} = 'dbi:Pg:' . $config->{dbconnect};
+        $connect_info->{user} = $config->{dbuser};
+        $connect_info->{password} = $config->{dbpassword};
+    }
+    exists($connect_info->{AutoCommit}) or $connect_info->{AutoCommit} = 0;
+    $class->SUPER::connection(
+        $connect_info,
+   )
+}
+
+sub db {
+   my ($self) = @_;
+
+   $self->connect->storage->dbh;
 }
 
Index: server/trunk/web/lib/Sophie/Base/Header.pm
===================================================================
--- server/trunk/web/lib/Sophie/Base/Header.pm	(revision 41)
+++ server/trunk/web/lib/Sophie/Base/Header.pm	(revision 42)
@@ -13,6 +13,8 @@
     my ($class, $pkgid) = @_;
 
-    bless(\$pkgid, $class);
+    bless({ key => $pkgid }, $class);
 }
+
+sub key { $_[0]->{key} }
 
 sub rpm_path {
@@ -26,5 +28,5 @@
         }
     );
-    $listrpm->execute($$self);
+    $listrpm->execute($self->key);
 
     return $listrpm->fetchall_hashref({});
@@ -51,5 +53,5 @@
         select (rpmqueryfiles(header)).* from rpms where pkgid = ?
     });
-    $list_file->execute($$self);
+    $list_file->execute($self->key);
 
     my $files = $list_file->fetchall_hashref([ 'dirname', 'basename' ]);
@@ -92,5 +94,5 @@
                     $add_content->execute(
                         $enc && ref $enc ? encode('utf8', $enc->decode($content)) : $content,
-                        $$self,
+                        $self->key,
                         $entry->{count}) or do {
                         $self->db->pg_rollback_to('FILECONTENT');
Index: server/trunk/web/lib/Sophie/Base/RpmsPath.pm
===================================================================
--- server/trunk/web/lib/Sophie/Base/RpmsPath.pm	(revision 41)
+++ server/trunk/web/lib/Sophie/Base/RpmsPath.pm	(revision 42)
@@ -15,6 +15,8 @@
     my ($class, $pathkey) = @_;
 
-    bless(\$pathkey, $class);
-}
+    bless({ key => $pathkey }, $class);
+}
+
+sub key { $_[0]->{key} }
 
 sub path {
@@ -24,5 +26,5 @@
         q{select path from d_path where d_path_key = ?}
     );
-    $sth->execute($$self);
+    $sth->execute($self->key);
     my $res = $sth->fetchrow_hashref;
     $sth->finish;
@@ -36,5 +38,5 @@
         q{select * from rpmfiles where d_path = ?}
     );
-    $sth->execute($$self);
+    $sth->execute($self->key);
     $sth->fetchall_hashref([ 'filename' ]);
 }
@@ -101,5 +103,5 @@
     $self->db->prepare_cached(q{
         update d_path set updated = now() where d_path_key = ?
-        })->execute($$self);
+        })->execute($self->key);
     $self->db->commit;
 }
@@ -114,5 +116,5 @@
     );
     for (1 .. 3) {
-        if ($remove->execute($$self, $rpm)) { 
+        if ($remove->execute($self->key, $rpm)) { 
             warn "deleting $rpm";
             $self->db->commit;
@@ -135,5 +137,5 @@
                 }
             );
-            if ($register->execute($$self, $rpm, $pkgid)) {
+            if ($register->execute($self->key, $rpm, $pkgid)) {
                 warn "adding $rpm";
                 $self->db->commit;
Index: server/trunk/web/lib/Sophie/Controller/Admin.pm
===================================================================
--- server/trunk/web/lib/Sophie/Controller/Admin.pm	(revision 41)
+++ server/trunk/web/lib/Sophie/Controller/Admin.pm	(revision 42)
@@ -2,4 +2,5 @@
 use Moose;
 use namespace::autoclean;
+use YAML qw/freeze thaw/;
 
 BEGIN {extends 'Catalyst::Controller'; }
@@ -89,5 +90,5 @@
     }
 
-    @{ $c->stash->{xmlrpc} } =
+    $c->stash->{xmlrpc}  = [
     $c->model('Base')->resultset('Distribution')
         ->search($distribution ? (name => $distribution) : ())
@@ -97,5 +98,5 @@
         ->search_related('MediasPaths')
         ->search_related('Paths')->get_column('path')
-        ->all;
+        ->all ];
 }
 
@@ -156,5 +157,5 @@
         ->search_related('Release', version => $version)
         ->search_related('Arch', arch => $arch)
-        ->search_related('Medias', label => $label)->next or return;
+        ->search_related('Medias', label => $label)->find or return;
 
     my $rspath = $c->model('Base')->resultset('Paths')
@@ -162,9 +163,8 @@
             return;
     };
-    my $new = $c->model('Base')->resultset('MediasPaths')->new({
-            Medias => $med,
-            Paths =>  $rspath,
-        });
-    $new->delete;
+    my $new = $c->model('Base')->resultset('MediasPaths')->search({
+            d_media => $med->d_media_key,
+            d_path =>  $rspath->d_path_key,
+        })->next->delete;
 
     $c->model('Base')->storage->dbh->commit;
@@ -177,4 +177,82 @@
 }
 
+sub dump_distrib : XMLRPC {
+    my ($self, $c, $distribution, $version, $arch) = @_;
+    
+    if (!ref $distribution) {
+        $distribution = {
+            distribution => $distribution,
+            release => $version,
+            arch => $arch,
+        };
+    }
+
+    my $ref = {
+        distrib => $distribution,
+    };
+
+    $ref->{media} = $c->forward('/distrib/struct', [ $distribution ]);
+
+    foreach (@{ $ref->{media} || []}) {
+        warn $_->{label};
+        $ref->{path}{$_->{label}} = $c->forward('list_path', [ $distribution,
+                $_->{label} ]);
+    }
+
+    $c->stash->{xmlrpc} = freeze($ref);
+}
+
+sub clean_distrib : XMLRPC {
+    my ($self, $c, $distribution, $version, $arch) = @_;
+    
+    if (!ref $distribution) {
+        $distribution = {
+            distribution => $distribution,
+            release => $version,
+            arch => $arch,
+        };
+    }
+
+    my $rsdist = $c->model('Base')->resultset('Distribution')
+        ->search(name => $distribution->{distribution})
+        ->search_related('Release', version => $distribution->{release})
+        ->search_related('Arch', arch => $distribution->{arch})
+        ->search_related('Medias');
+
+    my $new = $c->model('Base')->resultset('MediasPaths')->search({
+            d_media => { IN => $rsdist->get_column('d_media_key')->as_query },
+        })->delete;
+
+    # $c->model('Base')->storage->dbh->rollback;
+    
+}
+
+sub load_distrib : XMLRPC {
+    my ( $self, $c, $dump ) = @_;
+
+    my $ref = thaw($dump);
+
+    warn keys %{$ref->{path}};
+
+    $c->forward('clean_distrib', [ $ref->{distrib} ]);
+
+    $c->forward('create', [ 
+            $ref->{distrib}{distribution},
+            $ref->{distrib}{release},
+            $ref->{distrib}{arch},
+        ]);
+
+    foreach my $media (@{ $ref->{media} || []}) {
+        $c->forward('add_media', [ $ref->{distrib}, $media ]);
+    }
+    foreach my $media (keys %{ $ref->{path} || [] }) {
+        foreach my $path (@{ $ref->{path}{$media} || [] }) {
+            $c->forward('media_path', [ $ref->{distrib}, $media, $path ]);
+        }
+    }
+
+    #$c->model('Base')->storage->dbh->rollback;
+}
+
 =head1 AUTHOR
 
Index: server/trunk/web/lib/Sophie/Controller/Distrib.pm
===================================================================
--- server/trunk/web/lib/Sophie/Controller/Distrib.pm	(revision 41)
+++ server/trunk/web/lib/Sophie/Controller/Distrib.pm	(revision 42)
@@ -74,5 +74,4 @@
             label => $_->label,
             group_label => $_->group_label,
-            key => $_->d_media_key,
         } 
     } $rs->all ];
Index: server/trunk/web/lib/Sophie/Controller/Root.pm
===================================================================
--- server/trunk/web/lib/Sophie/Controller/Root.pm	(revision 41)
+++ server/trunk/web/lib/Sophie/Controller/Root.pm	(revision 42)
@@ -88,5 +88,5 @@
     }
     $c->stash->{data} = $c->stash->{xmlrpc};
-    $c->model('Base')->storage->dbh->commit;
+    $c->model('Base')->storage->dbh->rollback;
 }
 
Index: server/trunk/web/lib/Sophie/Model/Base.pm
===================================================================
--- server/trunk/web/lib/Sophie/Model/Base.pm	(revision 41)
+++ server/trunk/web/lib/Sophie/Model/Base.pm	(revision 42)
@@ -7,5 +7,7 @@
     schema_class => 'Sophie::Base',
     connect_info => {
-        dsn => 'toto',
+        dsn => '',
+        private_cache_key => __PACKAGE__,
+        AutoCommit => 0,
     },    
 );
Index: server/trunk/web/lib/Sophie/Model/Session.pm
===================================================================
--- server/trunk/web/lib/Sophie/Model/Session.pm	(revision 42)
+++ server/trunk/web/lib/Sophie/Model/Session.pm	(revision 42)
@@ -0,0 +1,42 @@
+package Sophie::Model::Session;
+
+use strict;
+use base 'Catalyst::Model::DBIC::Schema';
+
+__PACKAGE__->config(
+    schema_class => 'Sophie::Base',
+    connect_info => {
+        dsn => '',
+        AutoCommit => 1,
+        private_cache_key => __PACKAGE__,
+    }, 
+);
+
+=head1 NAME
+
+Sophie::Model::Sophie - Catalyst DBIC Schema Model
+
+=head1 SYNOPSIS
+
+See L<Sophie>
+
+=head1 DESCRIPTION
+
+L<Catalyst::Model::DBIC::Schema> Model using schema L<Sophie::Base>
+
+=head1 GENERATED BY
+
+Catalyst::Helper::Model::DBIC::Schema - 0.43
+
+=head1 AUTHOR
+
+Olivier Thauvin
+
+=head1 LICENSE
+
+This library is free software, you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+1;
