Luc Didry a4149b
package Lufi::Command::sqliteToOtherDB;
Luc Didry a4149b
use Mojo::Base 'Mojolicious::Command';
Luc Didry 8d6f10
use Lufi::DB::BreakingChange;
Luc Didry a4149b
use Lufi::DB::File;
Luc Didry a4149b
use Lufi::DB::Slice;
Luc Didry 8b68d7
use Lufi::DB::Invitation;
Luc Didry a4149b
use Mojo::SQLite;
Luc Didry a4149b
use FindBin qw($Bin);
Luc Didry a4149b
use Term::ProgressBar;
Luc Didry b57ed7
use Lufi::DefaultConfig qw($default_config);
Luc Didry a4149b
Luc Didry a4149b
has description => 'Migrate the records from a SQLite db to the currently configured database';
Luc Didry 8d6f10
has usage       => sub { shift->extract_usage };
Luc Didry a4149b
Luc Didry a4149b
sub run {
Luc Didry a4149b
    my $c = shift;
Luc Didry a4149b
Luc Didry a4149b
    my $cfile = Mojo::File->new($Bin, '..' , 'lufi.conf');
Luc Didry a4149b
    if (defined $ENV{MOJO_CONFIG}) {
Luc Didry a4149b
        $cfile = Mojo::File->new($ENV{MOJO_CONFIG});
Luc Didry a4149b
        unless (-e $cfile->to_abs) {
Luc Didry a4149b
            $cfile = Mojo::File->new($Bin, '..', $ENV{MOJO_CONFIG});
Luc Didry a4149b
        }
Luc Didry a4149b
    }
Luc Didry a4149b
    my $config = $c->app->plugin('Config', {
Luc Didry a4149b
        file    => $cfile,
Luc Didry b57ed7
        default => $default_config
Luc Didry a4149b
    });
Luc Didry a4149b
Luc Didry a4149b
    if ($config->{dbtype} eq 'sqlite') {
Luc Didry a4149b
        say 'Please configure `dbtype` to something else than `sqlite` to use this command.';
Luc Didry a4149b
        print $c->usage;
Luc Didry a4149b
        exit 1;
Luc Didry a4149b
    }
Luc Didry a4149b
Luc Didry 8b68d7
    my $sqlite      = Mojo::SQLite->new('sqlite:'.$config->{db_path});
Luc Didry 8b68d7
    my $files       = $sqlite->db->select('files', undef)->hashes;
Luc Didry 8b68d7
    my $slices      = $sqlite->db->select('slices', undef)->hashes;
Luc Didry 8b68d7
    my $invitations = $sqlite->db->select('invitations', undef)->hashes;
Luc Didry 8d6f10
    my $changes     = $sqlite->db->select('breakingchanges', undef)->hashes;
Luc Didry a4149b
Luc Didry 8d6f10
    my $progress = Term::ProgressBar->new({count => $files->size + $slices->size + $invitations->size + $changes->size});
Luc Didry a4149b
Luc Didry a4149b
    $files->each(sub {
Luc Didry a4149b
        my ($file, $num) = @_;
Luc Didry a4149b
Luc Didry a4149b
        $progress->update();
Luc Didry a4149b
Luc Didry a4149b
        Lufi::DB::File->new(app => $c->app)
Luc Didry a4149b
                      ->short($file->{short})
Luc Didry a4149b
                      ->deleted($file->{deleted})
Luc Didry a4149b
                      ->mediatype($file->{mediatype})
Luc Didry a4149b
                      ->filename($file->{filename})
Luc Didry a4149b
                      ->filesize($file->{filesize})
Luc Didry a4149b
                      ->counter($file->{counter})
Luc Didry a4149b
                      ->delete_at_first_view($file->{delete_at_first_view})
Luc Didry a4149b
                      ->delete_at_day($file->{delete_at_day})
Luc Didry a4149b
                      ->created_at($file->{created_at})
Luc Didry a4149b
                      ->created_by($file->{created_by})
Luc Didry a4149b
                      ->last_access_at($file->{last_access_at})
Luc Didry a4149b
                      ->mod_token($file->{mod_token})
Luc Didry a4149b
                      ->nbslices($file->{nbslices})
Luc Didry a4149b
                      ->complete($file->{complete})
Luc Didry a4149b
                      ->passwd($file->{passwd})
Luc Didry a4149b
                      ->abuse($file->{abuse})
Luc Didry a4149b
                      ->write();
Luc Didry a4149b
    });
Luc Didry a4149b
    $slices->each(sub {
Luc Didry a4149b
        my ($slice, $num) = @_;
Luc Didry a4149b
Luc Didry a4149b
        Lufi::DB::Slice->new(app => $c->app)
Luc Didry a4149b
                      ->short($slice->{short})
Luc Didry a4149b
                      ->j($slice->{j})
Luc Didry a4149b
                      ->write();
Luc Didry a4149b
Luc Didry a4149b
        $progress->update();
Luc Didry a4149b
    });
Luc Didry 8b68d7
    $invitations->each(sub {
Luc Didry 8b68d7
        my ($invitation, $num) = @_;
Luc Didry 8b68d7
Luc Didry 8b68d7
        Lufi::DB::Invitation->new(app => $c->app)
Luc Didry 8b68d7
                            ->token($invitation->{token})
Luc Didry 8b68d7
                            ->ldap_user($invitation->{ldap_user})
Luc Didry 8b68d7
                            ->ldap_user_mail($invitation->{ldap_user_mail})
Luc Didry 8b68d7
                            ->guest_mail($invitation->{guest_mail})
Luc Didry 8b68d7
                            ->created_at($invitation->{created_at})
Luc Didry 8b68d7
                            ->expire_at($invitation->{expire_at})
Luc Didry 8b68d7
                            ->files_sent_at($invitation->{files_sent_at})
Luc Didry 8b68d7
                            ->expend_expire_at($invitation->{expend_expire_at})
Luc Didry 8b68d7
                            ->files($invitation->{files})
Luc Didry 8b68d7
                            ->show_in_list($invitation->{show_in_list})
Luc Didry 8b68d7
                            ->deleted($invitation->{deleted})
Luc Didry 8b68d7
                            ->write();
Luc Didry 8b68d7
        $progress->update();
Luc Didry 8b68d7
    });
Luc Didry 8d6f10
    $changes->each(sub {
Luc Didry 8d6f10
        my ($change, $num) = @_;
Luc Didry 8d6f10
Luc Didry 8d6f10
        Lufi::DB::BreakingChange->new(app => $c->app)
Luc Didry 8d6f10
                                ->change($change->{change})
Luc Didry 8d6f10
                                ->ack($change->{ack})
Luc Didry 8d6f10
                                ->write();
Luc Didry 8d6f10
        $progress->update();
Luc Didry 8d6f10
    });
Luc Didry a4149b
}
Luc Didry a4149b
Luc Didry a4149b
=encoding utf8
Luc Didry a4149b
Luc Didry a4149b
=head1 NAME
Luc Didry a4149b
Luc Didry 8d6f10
Lufi::Command::sqliteToOtherDB Migrate the records from a SQLite db to the currently configured database
Luc Didry a4149b
Luc Didry a4149b
=head1 SYNOPSIS
Luc Didry a4149b
Luc Didry a4149b
  Usage: script/lufi sqliteToOtherDB
Luc Didry a4149b
Luc Didry a4149b
  This command needs you to:
Luc Didry a4149b
    - set `db_path` in your config file (otherwise, it will use the default path, `lufi.db` to migrate data from)
Luc Didry a4149b
    - set `dbtype` to an other database type in your config file
Luc Didry a4149b
    - configure the other database access in your config file
Luc Didry a4149b
Luc Didry a4149b
=cut
Luc Didry a4149b
Luc Didry a4149b
1;