Skip to content

Commit

Permalink
initial import and export
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Axel 'fREW' Schmidt committed May 4, 2012
1 parent 1001fe9 commit 2eea09c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/DU/App/Command/maint/export.pm
@@ -0,0 +1,41 @@
package DU::App::Command::maint::export;

use 5.14.1;
use warnings;

use DU::App -command;
use DU::Util 'drink_as_data';

sub abstract { '' }

sub usage_desc { '' }

sub execute {
my ($self, $opt, $args) = @_;

my $s = $self->app->app->schema;

require Archive::Tar;
require JSON;
use IO::Compress::Xz 'xz';

my $tar = Archive::Tar->new;

$tar->add_data('export_version.txt', 1);
$tar->add_data('drinks.json',
JSON::encode_json([
map drink_as_data($_), $s->resultset('Drink')->all
])
);

my $name = $args->[0] || 'export';
$tar->write(".$name.tar");

xz ".$name.tar", "$name.tar.xz";
unlink ".$name.tar";

say 'done';
}

1;

46 changes: 46 additions & 0 deletions lib/DU/App/Command/maint/import.pm
@@ -0,0 +1,46 @@
package DU::App::Command::maint::import;

use 5.14.1;
use warnings;

use DU::App -command;
use DU::Util 'drink_as_data';

sub abstract { '' }

sub usage_desc { '' }

sub execute {
my ($self, $opt, $args) = @_;

my $s = $self->app->app->schema;

require Archive::Tar;
require JSON;
use IO::Uncompress::UnXz 'unxz';

my $name = $args->[0] || 'export';
unxz "$name.tar.xz", ".$name.tar";

my $tar = Archive::Tar->new;

$tar->read(".$name.tar", { extract => 1 });

my $version = $tar->get_content('export_version.txt');

my $drink_rs = $s->resultset('Drink');
my @drinks = @{JSON::decode_json($tar->get_content('drinks.json'))};

for my $drink_data (@drinks) {
if ($drink_rs->find_by_name($drink_data->{name})) {
die "$drink_data->{name} is already in your database"
} else {
$s->create_drink($drink_data)
}
}

say 'done';
}

1;

24 changes: 24 additions & 0 deletions t/app.t
Expand Up @@ -173,17 +173,41 @@ subtest 'maint' => sub {
on_connect_do => 'PRAGMA foreign_keys = ON',
},
});

subtest 'install' => sub {
my $result = test_app($app1 => [qw(maint install),]);
stdout_is($result, [ 'done' ]);
is($app1->schema->resultset('Drink')->count, 3, 'drinks correctly seeded');
};

subtest 'install --no-seeding' => sub {
my $result = test_app($app2 => [qw(maint install --no-seeding),]);
stdout_is($result, [ 'done' ]);
is($app2->schema->resultset('Drink')->count, 0, 'drinks correctly unseeded');

$app2->schema->resultset('Unit')->populate([
[qw(name gills)],
[ounce => 1 / 4 ] ,
[tablespoon => 1 / 4 / 2 ] ,
[teaspoon => 1 / 4 / 2 / 3 ] ,
[dash => undef ] ,
]);

};

subtest 'export' => sub {
ok(!-f 'test-export.tar.xz', 'no export yet');
my $result = test_app($app1 => [qw(maint export test-export),]);
stdout_is($result, [ 'done' ]);
ok(-f 'test-export.tar.xz', 'export created');
};
subtest 'import' => sub {
my $result = test_app($app2 => [qw(maint import test-export),]);
stdout_is($result, [ 'done' ]);
unlink 'test-export.tar.xz';
};
};

done_testing;

sub stdout_is {
Expand Down

0 comments on commit 2eea09c

Please sign in to comment.