« blib/lib/DBIO/Test/MooCakeSchema/Result/CD.pm
↑ Summary
blib/lib/DBIO/Test/MooCakeSchema/ResultSet/Artist.pm »
| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||||||
| 2 | # PODNAME: dbioadmin | ||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: CLI for DBIO schema administration | ||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Getopt::Long qw(:config no_ignore_case bundling); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | sub usage { | ||||||||||||||||||||||||||||||||||
|
Subroutine
usage: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||
| 11 | 2 | my ($exit_code) = @_; | |||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | $exit_code = 1 unless defined $exit_code; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | 2 | print <<'USAGE'; | |||||||||||||||||||||||||||||||||||
| 15 | Usage: dbioadmin [OPTIONS] | ||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||
| 17 | Actions (pick one): | ||||||||||||||||||||||||||||||||||||
| 18 | --upgrade Upgrade the database to the current schema | ||||||||||||||||||||||||||||||||||||
| 19 | --deploy Deploy the schema to the database | ||||||||||||||||||||||||||||||||||||
| 20 | --select Select data from a resultset | ||||||||||||||||||||||||||||||||||||
| 21 | --insert Insert data into a resultset | ||||||||||||||||||||||||||||||||||||
| 22 | --update Update rows in a resultset | ||||||||||||||||||||||||||||||||||||
| 23 | --delete Delete rows from a resultset | ||||||||||||||||||||||||||||||||||||
| 24 | --op=ACTION Compatibility alias for action flags | ||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||
| 26 | Arguments: | ||||||||||||||||||||||||||||||||||||
| 27 | --schema-class=CLASS Schema class (required) | ||||||||||||||||||||||||||||||||||||
| 28 | --schema=CLASS Alias for --schema-class | ||||||||||||||||||||||||||||||||||||
| 29 | --connect=JSON connect_info as JSON, e.g. ["dsn","user","pass",{}] | ||||||||||||||||||||||||||||||||||||
| 30 | --connect-info connect_info as key=value pairs (dsn=... user=...) | ||||||||||||||||||||||||||||||||||||
| 31 | --password-env=VAR password from the environment variable VAR | ||||||||||||||||||||||||||||||||||||
| 32 | --password-stdin password from one line of STDIN (no echo on a terminal) | ||||||||||||||||||||||||||||||||||||
| 33 | --config=FILE Config file parsable by Config::Any | ||||||||||||||||||||||||||||||||||||
| 34 | --config-stanza=PATH Stanza path for connect_info (e.g. Model::DB) | ||||||||||||||||||||||||||||||||||||
| 35 | --resultset=CLASS Resultset to operate on for select/insert/update/delete | ||||||||||||||||||||||||||||||||||||
| 36 | --class=CLASS Alias for --resultset | ||||||||||||||||||||||||||||||||||||
| 37 | --sql-dir=DIR SQL directory for deploy | ||||||||||||||||||||||||||||||||||||
| 38 | --mode=MODE upgrade mode: auto|native (default: auto) | ||||||||||||||||||||||||||||||||||||
| 39 | --set=JSON JSON payload for insert/update | ||||||||||||||||||||||||||||||||||||
| 40 | --attrs=JSON JSON attrs hash for search | ||||||||||||||||||||||||||||||||||||
| 41 | --where=JSON JSON where hash for search | ||||||||||||||||||||||||||||||||||||
| 42 | --format=FMT select output format: tsv|csv (default: tsv) | ||||||||||||||||||||||||||||||||||||
| 43 | --all Allow update/delete without --where (the whole resultset) | ||||||||||||||||||||||||||||||||||||
| 44 | --force Skip confirmation prompts for destructive actions | ||||||||||||||||||||||||||||||||||||
| 45 | (for --upgrade: release a diff that drops or alters data) | ||||||||||||||||||||||||||||||||||||
| 46 | --dry-run With --upgrade: print the summary and the SQL, run nothing | ||||||||||||||||||||||||||||||||||||
| 47 | --trace Enable DBIO SQL trace output | ||||||||||||||||||||||||||||||||||||
| 48 | --quiet Reduce output | ||||||||||||||||||||||||||||||||||||
| 49 | -I PATH Prepend PATH to \@INC (repeatable) | ||||||||||||||||||||||||||||||||||||
| 50 | --help Show this help | ||||||||||||||||||||||||||||||||||||
| 51 | USAGE | ||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||
| 53 | 2 | exit $exit_code; | |||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||
| 56 | 3 | my %opts; | |||||||||||||||||||||||||||||||||||
| 57 | my @include_paths; | ||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | GetOptions( | ||||||||||||||||||||||||||||||||||||
| 60 | 'upgrade' => \$opts{upgrade}, | ||||||||||||||||||||||||||||||||||||
| 61 | 'deploy' => \$opts{deploy}, | ||||||||||||||||||||||||||||||||||||
| 62 | 'select' => \$opts{select}, | ||||||||||||||||||||||||||||||||||||
| 63 | 'insert' => \$opts{insert}, | ||||||||||||||||||||||||||||||||||||
| 64 | 'update' => \$opts{update}, | ||||||||||||||||||||||||||||||||||||
| 65 | 'delete' => \$opts{delete}, | ||||||||||||||||||||||||||||||||||||
| 66 | 'op=s' => \$opts{op}, | ||||||||||||||||||||||||||||||||||||
| 67 | 'schema-class|schema=s' => \$opts{schema_class}, | ||||||||||||||||||||||||||||||||||||
| 68 | 'connect=s' => \$opts{connect}, | ||||||||||||||||||||||||||||||||||||
| 69 | 'connect-info=s%' => \$opts{connect_info_hash}, | ||||||||||||||||||||||||||||||||||||
| 70 | 'password-env=s' => \$opts{password_env}, | ||||||||||||||||||||||||||||||||||||
| 71 | 'password-stdin' => \$opts{password_stdin}, | ||||||||||||||||||||||||||||||||||||
| 72 | 'config|config-file=s' => \$opts{config_file}, | ||||||||||||||||||||||||||||||||||||
| 73 | 'config-stanza=s' => \$opts{config_stanza}, | ||||||||||||||||||||||||||||||||||||
| 74 | 'resultset|resultset-class|class=s' => \$opts{resultset}, | ||||||||||||||||||||||||||||||||||||
| 75 | 'sql-dir=s' => \$opts{sql_dir}, | ||||||||||||||||||||||||||||||||||||
| 76 | 'mode=s' => \$opts{mode}, | ||||||||||||||||||||||||||||||||||||
| 77 | 'set=s' => \$opts{set}, | ||||||||||||||||||||||||||||||||||||
| 78 | 'attrs=s' => \$opts{attrs}, | ||||||||||||||||||||||||||||||||||||
| 79 | 'where=s' => \$opts{where}, | ||||||||||||||||||||||||||||||||||||
| 80 | 'format=s' => \$opts{format}, | ||||||||||||||||||||||||||||||||||||
| 81 | 'all' => \$opts{all}, | ||||||||||||||||||||||||||||||||||||
| 82 | 'force' => \$opts{force}, | ||||||||||||||||||||||||||||||||||||
| 83 | 'dry-run' => \$opts{dry_run}, | ||||||||||||||||||||||||||||||||||||
| 84 | 'trace' => \$opts{trace}, | ||||||||||||||||||||||||||||||||||||
| 85 | 'quiet' => \$opts{quiet}, | ||||||||||||||||||||||||||||||||||||
| 86 | 'I=s@' => \@include_paths, | ||||||||||||||||||||||||||||||||||||
| 87 | 'help' => \$opts{help}, | ||||||||||||||||||||||||||||||||||||
| 88 | 3 | ▶ | ) or usage(1); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||
| 90 | 3 | ▶ | usage(0) if $opts{help}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | if (@include_paths) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 93 | 0 | require lib; | |||||||||||||||||||||||||||||||||||
| 94 | 0 | lib->import(@include_paths); | |||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||
| 97 | 1 | require DBIO::Admin; | |||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||
| 99 | 1 | my $action; | |||||||||||||||||||||||||||||||||||
| 100 | 1 | for my $act (qw(upgrade deploy select insert update delete)) { | |||||||||||||||||||||||||||||||||||
| 101 | 6 | ▶ | if ($opts{$act}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 102 | 1 | ▶ | die "Multiple actions specified\n" if $action; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 103 | 1 | $action = $act; | |||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||
| 107 | 1 | ▶ | if ($opts{op}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | die "Multiple actions specified\n" if $action; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 109 | 0 | $action = $opts{op}; | |||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | die "No action specified. Use --help for usage.\n" unless $action; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | die "No --schema-class specified. Use --help for usage.\n" unless $opts{schema_class}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||
| 115 | 1 | my %admin_args = (schema_class => $opts{schema_class}); | |||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | if ($opts{connect}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 118 | 0 | $admin_args{connect_info} = $opts{connect}; | |||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||
| 120 | elsif ($opts{connect_info_hash}) { | ||||||||||||||||||||||||||||||||||||
| 121 | 1 | $admin_args{connect_info} = $opts{connect_info_hash}; | |||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||
| 124 | 1 | for my $key (qw(config_file config_stanza resultset sql_dir | |||||||||||||||||||||||||||||||||||
| 125 | mode set attrs where all force dry_run trace quiet)) { | ||||||||||||||||||||||||||||||||||||
| 126 | 13 | ▶ | $admin_args{$key} = $opts{$key} if defined $opts{$key}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||
| 129 | 1 | my $admin = DBIO::Admin->new(%admin_args); | |||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||
| 131 | # The password is not an argument of this command (ps and the shell history | ||||||||||||||||||||||||||||||||||||
| 132 | # show those): it comes from the environment or STDIN and is set in the | ||||||||||||||||||||||||||||||||||||
| 133 | # connect info, wherever that came from. | ||||||||||||||||||||||||||||||||||||
| 134 | 1 | require DBIO::Util; | |||||||||||||||||||||||||||||||||||
| 135 | my $password = DBIO::Util::password_from_source( | ||||||||||||||||||||||||||||||||||||
| 136 | env => $opts{password_env}, stdin => $opts{password_stdin}, | ||||||||||||||||||||||||||||||||||||
| 137 | 1 | ); | |||||||||||||||||||||||||||||||||||
| 138 | 1 | ▶ | if (defined $password) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 139 | 1 | my $info = $admin->connect_info; | |||||||||||||||||||||||||||||||||||
| 140 | 1 | ▶ | if (ref $info eq 'ARRAY' and ref $info->[0] eq 'HASH') { $info->[0]{password} = $password } | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
ref $info eq "ARRAY" and ref $info->[0] eq "HASH"
MC/DC0%0/2
ref $info eq "ARRAY" and ref $info->[0] eq "HASH"
ref $info eq "ARRAY"
ref $info->[0] eq "HASH"
Truth table33%1/3
A and B
A= ref $info eq "ARRAY"B= ref $info->[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||
| 141 | 0 | elsif (ref $info eq 'ARRAY') { $info->[2] = $password } | |||||||||||||||||||||||||||||||||||
| 142 | 0 | elsif (ref $info eq 'HASH') { $info->{password} = $password } | |||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||
| 144 | 1 | ▶ | print "Performing action $action...\n" if !$opts{quiet}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||
| 146 | 1 | my $res = eval { $admin->$action(); }; | |||||||||||||||||||||||||||||||||||
| 147 | 1 | ▶ | if (my $err = $@) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 148 | 1 | die $err; | |||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if ($action eq 'select') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my $format = lc($opts{format} || 'tsv'); | ||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts{'format'} || 'tsv'
MC/DC0%0/1
$opts{'format'} || 'tsv'
$opts{'format'}
Truth table0%0/2
A
A= $opts{'format'}
| |||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | die "Invalid --format '$format' (expected tsv|csv)\n" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 154 | unless $format =~ /\A(?:tsv|csv)\z/; | ||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | if ($format eq 'tsv') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | for my $row (@{$res||[]}) { | ||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$res || []
MC/DC0%0/1
$res || []
$res
Truth table0%0/2
A
A= $res
| |||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | my @vals = map { defined $_ ? $_ : '' } @$row; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 159 | 0 | print join("\t", @vals), "\n"; | |||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||
| 162 | else { | ||||||||||||||||||||||||||||||||||||
| 163 | 0 | require Text::CSV; | |||||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | my $csv = Text::CSV->new({ binary => 1 }) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 165 | or die "Failed to create Text::CSV object\n"; | ||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | for my $row (@{$res||[]}) { | ||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$res || []
MC/DC0%0/1
$res || []
$res
Truth table0%0/2
A
A= $res
| |||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | $csv->combine(map { defined $_ ? $_ : '' } @$row); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 168 | 0 | print $csv->string, "\n"; | |||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||
| 173 | 0 | exit 0; | |||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||||||||||