script/couch_schema_tool.pl untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 136 CC 4 SCAR 19.1
CC
4
Cov
0%
useage
12.0
db_args
6.0
BEGIN
2.0
CRAP 6.8
1 #!/usr/bin/env perl
2 use DB::CouchDB::Schema;
3 use Getopt::Long;
4 use Pod::Usage;
5  
6 #TODO(jwall): Write POD and convert useage to do pod2useage
7 my ($dump,$load,$create,
8 $backup, $restore,
9 $database,$host,$port,$dsn,
10 $file,$help,
11 );
12  
13 my $opts = GetOptions ("dump" => \$dump,
14 "load" => \$load,
15 "create" => \$create,
16 "file=s" => \$file,
17 "help" => \$help,
18 "db=s" => \$database,
19 "host=s" => \$host,
20 "port=i" => \$port,
21 "backup" => \$backup,
22 "restore" => \$restore,
23 );
24  
25 sub useage {
26 my $message = shift;
27 my $status = shift;
28 $status = 2 if (! defined $status );
29 $message = "did not understand options!!$/" if (! defined $message );
30 pod2usage( -msg => $message,
31 -exitval => $status );
32 }
33  
34 sub db_args {
35 my %dbargs = (db => $database,
36 host => $host);
37 $dbargs{port} = $port
38 if $port;
39 return %dbargs;
40 }
41  
42 if ( $help ) {
43 useage("", 0);
44 }
45  
46 if ($create) {
47 if ( $host && $database ) {
48 my %dbargs = db_args();
49 my $db = DB::CouchDB::Schema->new(%dbargs);
50 eval {
51 $db->create_new_db(db => $database);
52 };
53 if ($@) {
54 useage("failed to create database: $database with error".$@);
55 }
56
57 } else {
58 useage();
59 }
60 }
61  
62 if ( $host && $database ) {
63 my %dbargs = db_args();
64 my $db = DB::CouchDB::Schema->new(%dbargs);
65
66 if ($dump && $file) {
67 open my $fh, '>:encoding(UTF-8)', $file or die $!;
68 my $script = $db->dump(1);
69 print $fh $script;
70 close $fh;
71 exit 0;
72 } elsif ($load && $file) {
73 open my $fh, '>:encoding(UTF-8)', $file or die $!;
74 local $/;
75 $script = <$fh>;
76 print "loading schema: ", $/, $script;
77 $db->wipe();
78 $db->load_schema_from_script($script);
79 $db->push();
80 close $fh;
81 exit 0;
82 } elsif ($backup && $file) {
83 # no the backup and restore code
84 open my $fh, '>:encoding(UTF-8)', $file or die $!;
85 my $script = $db->dump_whole_db();
86 print $fh $script;
87 close $fh;
88 exit 0;
89 } elsif ($restore && $file) {
90 # no the backup and restore code
91 open my $fh, '>:encoding(UTF-8)', $file or die $!;
92 local $/;
93 $script = <$fh>;
94 print "loading data: ", $/, $script;
95 $db->wipe();
96 $db->push_from_script($script);
97 close $fh;
98 exit 0;
99 } else {
100 useage("Did not understand options!! did you specify one of:", $/,
101 "--dump, --load, --backup, or --restore with a --file?", $/);
102 }
103 } else {
104 useage("Must have a db and a hostname");
105 }
106  
107 __END__