X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=blobdiff_plain;f=mig-bin%2Fmig-gsheet;h=5975bd666e1d53a3ecde4e122dc918b4764eb560;hp=dbf15998b1602deb7468e6ab9f3bef8571fc6687;hb=56e3292334866e6b1eecb3b619d650e24ca67dba;hpb=b735c4228a8f6315d4da35e10d3826cf4044bd04 diff --git a/mig-bin/mig-gsheet b/mig-bin/mig-gsheet index dbf1599..5975bd6 100755 --- a/mig-bin/mig-gsheet +++ b/mig-bin/mig-gsheet @@ -14,10 +14,6 @@ use lib "$FindBin::Bin/"; my $mig_bin = "$FindBin::Bin/"; use Mig; use strict; -use Env qw( - HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA - MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR - ); use Switch; use Cwd 'abs_path'; use Pod::Usage; @@ -41,6 +37,8 @@ my $sql; my $sth; my @ws; my @tracked_ws_names; +my $authfile = $ENV{HOME} . '/.mig/oauth.env'; +my $next_arg_is_authfile; foreach my $arg (@ARGV) { if ($arg eq '--push') { @@ -61,6 +59,15 @@ foreach my $arg (@ARGV) { $next_arg_is_pull = 0; next; } + if ($arg eq '--authfile') { + $next_arg_is_authfile = 1; + next; + } + if ($next_arg_is_authfile) { + $authfile = $arg; + $next_arg_is_authfile = 0; + next; + } if ($arg eq '--export') { $cmd_export = 1; next; @@ -70,8 +77,20 @@ foreach my $arg (@ARGV) { abort('must specify --push (db->worksheets) or --pull (worksheets->db)') unless (defined $cmd_push or defined $cmd_pull); if (defined $cmd_push and defined $cmd_pull) { abort('you can not specify both a --push and --pull on the same command'); } +my $clientid; +my $clientsecret; +my $sessionfile; + +open (my $fh, '<', $authfile) or abort("Could not open $authfile"); +while (my $var = <$fh>) { + chomp $var; + my ($var1, $var2) = split /=/,$var; + if ($var1 eq 'CLIENTID') { $clientid = $var2; } + if ($var1 eq 'CLIENTSECRET') { $clientsecret = $var2; } + if ($var1 eq 'SESSIONFILE') { $sessionfile = $var2; } +} my $dbh = Mig::db_connect(); -my $spreadsheet = connect_gsheet(); +my $spreadsheet = connect_gsheet($clientid,$clientsecret,$sessionfile); abort('could not connect to google sheet') unless (defined $spreadsheet); $sql = 'SELECT tab_name FROM gsheet_tracked_table;'; @@ -95,7 +114,7 @@ if (defined $cmd_pull) { my @m = array_match(\@worksheet_names,\@tracked_ws_names); foreach my $w (@m) { my $pull_ws = $spreadsheet->worksheet( {title => $w} ); - my $push_tb = get_table_name($MIGSCHEMA,$w,$dbh); + my $push_tb = get_table_name($w,$dbh); my @rows = $pull_ws->rows; my @content; map { $content[$_->row - 1][$_->col - 1] = $_->content } $pull_ws->cells; @@ -107,35 +126,36 @@ if (defined $cmd_pull) { } shift @content; #todo: check for clean headers at some point ... - truncate_table($MIGSCHEMA,$push_tb,$dbh); + truncate_table($push_tb,$dbh); print "Inserting from $w to $push_tb.\n"; for my $j (@content) { insert_row($MIGSCHEMA,$push_tb,$dbh,\@pg_headers,$j); } - timestamp($MIGSCHEMA,$push_tb,$dbh,'pull'); + timestamp($push_tb,$dbh,'pull'); if ($cmd_export == 1) { export_table($dbh,$push_tb); } } } if (defined $cmd_push) { print "Pushing "; + my @tab_names; if ($cmd_push eq 'all') { - print "all tables.\n"; - $sql = 'SELECT table_name FROM ' . $MIGSCHEMA . '.gsheet_tracked_table'; + print "all worksheets.\n"; + $sql = 'SELECT tab_name FROM gsheet_tracked_table;'; $sth = $dbh->prepare($sql); $ra = $sth->execute(); while (my @row = $sth->fetchrow_array) { - push @table_names, $row[0]; + push @tab_names, $row[0]; } } else { - print "only table $cmd_push.\n"; + print "only worksheet $cmd_push.\n"; if (!defined $cmd_push) { abort('command incomplete'); } - push @table_names, $cmd_push; + push @tab_names, $cmd_push; } - foreach my $t (@table_names) { - my $pull_tb = $MIGSCHEMA . "." . $t;; - my @table_headers = get_pg_column_headers($t,$MIGSCHEMA); - my $push_ws_name = get_worksheet_name($MIGSCHEMA,$t,$dbh); + foreach my $push_ws_name (@tab_names) { + my $pull_tb = get_table_name($push_ws_name,$dbh); + my @table_headers = get_pg_column_headers($pull_tb,$MIGSCHEMA); + print "worksheetname: $push_ws_name\n"; my $push_ws = $spreadsheet->worksheet( {title => $push_ws_name} ); if (!defined $push_ws) { next; } my @rows; @@ -148,7 +168,7 @@ if (defined $cmd_push) { erase_sheet($push_ws,$push_ws_name); #get from postgres the headers to use in the sheet from tracked columns - $sql = 'SELECT column_name FROM ' . $MIGSCHEMA . '.gsheet_tracked_column WHERE table_id = (SELECT id FROM ' . $MIGSCHEMA . '.gsheet_tracked_table WHERE table_name = \'' . $t . '\')'; + $sql = 'SELECT column_name FROM gsheet_tracked_column WHERE table_id = (SELECT id FROM gsheet_tracked_table WHERE table_name = \'' . $pull_tb . '\')'; $sth = $dbh->prepare($sql); $sth->execute(); my $sheet_headers = $sth->fetchall_arrayref(); @@ -168,13 +188,13 @@ if (defined $cmd_push) { } push @content, $record; } - + print "Writing to $push_ws_name\n"; foreach my $fillsheet (@content) { my $new_row = $push_ws->add_row ( $fillsheet ); } - timestamp($MIGSCHEMA,$pull_tb,$dbh,'push'); + timestamp($pull_tb,$dbh,'push'); if ($cmd_export == 1) { export_table($dbh,$pull_tb); } } } @@ -189,7 +209,7 @@ sub export_table { my $efile = $MIGGITDIR . $table . '_' . $date . '_' . $hms . '.tsv'; my @data; my $record_count = 0; - $dbh->do("COPY $table TO STDOUT CSV HEADER;"); + $dbh->do("COPY $table TO STDOUT CSV DELIMITER E'\t' HEADER;"); 1 while $dbh->pg_getcopydata(\$data[$record_count++]) >= 0; open (my $eout, '>', $efile) or abort("Could NOT open $efile."); foreach my $d (@data) { @@ -197,13 +217,12 @@ sub export_table { } print "$efile written.\n"; close $eout; - + return; } - sub die_if_gsheet_tracked_table_does_not_exist { if (!check_for_gsheet_tracked_table()) { - die "Table $MIGSCHEMA.gsheet_tracked_table does not exist. Bailing...\n"; + die "Table gsheet_tracked_table does not exist. Bailing...\n"; } } @@ -241,19 +260,13 @@ sub erase_sheet { print "Erasing $ws_name.\n"; my @rows = $ws->rows; - my $i; - my $j = @rows; - $j = int(($j / 2))-1; - if ($j < 2) { $j = 2; } - while ($j > 0) { - #bodge until I figure out why google sheets is only deleting even numbered rows - $i = 1; - foreach my $row (@rows) { - if ($i != 1) { $row->delete; } - $i++; - }; - $j--; - }; + splice @rows, 0, 1; + my $i = @rows; + while ($i > 0) { + my $row = pop @rows; + $row->delete; + $i--; + } return; } @@ -282,11 +295,10 @@ sub die_if_gsheet_tracked_column_does_not_exist { } sub get_table_name { - my $migs = shift; my $worksheet = shift; my $dbh = shift; - my $sql = 'SELECT table_name FROM ' . $migs . '.gsheet_tracked_table WHERE tab_name = \'' . $worksheet . '\';'; + my $sql = 'SELECT table_name FROM gsheet_tracked_table WHERE tab_name = \'' . $worksheet . '\';'; my $sth = $dbh->prepare($sql); my $xs = $sth->execute(); my $table_name; @@ -297,21 +309,21 @@ sub get_table_name { return $table_name; } -sub get_worksheet_name { - my $migs = shift; - my $table = shift; - my $dbh = shift; - - my $sql = 'SELECT tab_name FROM ' . $migs . '.gsheet_tracked_table WHERE table_name = \'' . $table . '\';'; - my $sth = $dbh->prepare($sql); - my $xs = $sth->execute(); - my $worksheet_name; - while (my @row = $sth->fetchrow_array) { - $worksheet_name = $row[0]; - } - - return $worksheet_name; -} +#sub get_worksheet_name { +# my $table = shift; +# my $dbh = shift; +# +# my $sql = 'SELECT tab_name FROM gsheet_tracked_table WHERE table_name = \'' . $table . '\';'; +# print "$sql \n"; +# my $sth = $dbh->prepare($sql); +# my $xs = $sth->execute(); +# my $worksheet_name; +# while (my @row = $sth->fetchrow_array) { +# $worksheet_name = $row[0]; +# } +# +# return $worksheet_name; +#} sub check_for_gsheet_tracked_column { @@ -347,28 +359,27 @@ sub insert_row { } sub timestamp { - my ($schema, $table, $dbh, $action) = @_; + my ($table, $dbh, $action) = @_; my $column; if ($action eq 'pull') { $column = 'last_pulled' } else { $column = 'last_pushed' }; $dbh->do(qq/ - UPDATE $schema.gsheet_tracked_table SET $column = NOW() WHERE table_name = '$table'; + UPDATE gsheet_tracked_table SET $column = NOW() WHERE table_name = '$table'; /); } sub truncate_table { - my $schema = shift; my $table = shift; my $dbh = shift; $dbh->do(qq/ - TRUNCATE TABLE $schema.$table;; + TRUNCATE TABLE $table;; /); - print "Table $schema.$table truncated.\n"; + print "Table $table truncated.\n"; } sub abort { @@ -378,18 +389,17 @@ sub abort { } sub connect_gsheet { - if (!defined $ENV{'CLIENTID'}) { - exec '/bin/bash', '--init-file', '~/.mig/oauth.env'; - print "Open Authentication settings were not loaded, please re-run.\n"; - } - my $session_filename = $ENV{SESSIONFILE}; + + my ($clientid,$clientsecret,$sessionfile) = @_; + my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new( - client_id => $ENV{CLIENTID}, - client_secret => $ENV{CLIENTSECRET}, + client_id => $clientid, + client_secret => $clientsecret, scope => ['http://spreadsheets.google.com/feeds/'], redirect_uri => 'https://developers.google.com/oauthplayground', ); - my $session = retrieve($session_filename); + if ($sessionfile =~ m/~/) {$sessionfile =~ s/~/$ENV{HOME}/; } + my $session = retrieve($sessionfile); my $restored_token = Net::OAuth2::AccessToken->session_thaw( $session, auto_refresh => 1,