4 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
5 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
7 use Net::Google::Spreadsheets;
8 use Net::Google::DataAPI::Auth::OAuth2;
9 use Net::OAuth2::AccessToken;
13 use lib "$FindBin::Bin/";
14 my $mig_bin = "$FindBin::Bin/";
23 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
25 Mig::die_if_no_env_migschema();
26 die_if_gsheet_tracked_table_does_not_exist();
27 die_if_gsheet_tracked_column_does_not_exist();
41 foreach my $arg (@ARGV) {
42 if ($arg eq '--push') {
43 $next_arg_is_push = 1;
46 if ($next_arg_is_push) {
48 $next_arg_is_push = 0;
51 if ($arg eq '--pull') {
52 $next_arg_is_pull = 1;
55 if ($next_arg_is_pull) {
57 $next_arg_is_pull = 0;
60 if ($arg eq '--export') {
66 abort('must specify --push (db->worksheets) or --pull (worksheets->db)') unless (defined $cmd_push or defined $cmd_pull);
67 if (defined $cmd_push and defined $cmd_pull) { abort('you can not specify both a --push and --pull on the same command'); }
69 my $dbh = Mig::db_connect();
70 my $spreadsheet = connect_gsheet();
71 abort('could not connect to google sheet') unless (defined $spreadsheet);
73 $sql = 'SELECT tab_name FROM gsheet_tracked_table;';
74 $sth = $dbh->prepare($sql);
75 my $ra = $sth->execute();
76 while (my @row = $sth->fetchrow_array) {
77 push @tracked_ws_names, $row[0];
80 if (defined $cmd_pull) {
82 if ($cmd_pull eq 'all') {
83 print "all worksheets.\n";
84 @ws = $spreadsheet->worksheets;
85 foreach my $wsn (@ws) { push @worksheet_names, $wsn->title; }
87 print "only worksheet $cmd_pull.\n";
88 if (!defined $cmd_pull) { abort('command incomplete'); }
89 push @worksheet_names, $cmd_pull;
91 my @m = array_match(\@worksheet_names,\@tracked_ws_names);
93 my $pull_ws = $spreadsheet->worksheet( {title => $w} );
94 my $push_tb = get_table_name($w,$dbh);
95 my @rows = $pull_ws->rows;
97 map { $content[$_->row - 1][$_->col - 1] = $_->content } $pull_ws->cells;
98 my @tab_headers = shift @content;
99 my $tab_headers_length = $#{ $tab_headers[0] };
101 for my $i ( 0 .. $tab_headers_length ) {
102 push @pg_headers, $tab_headers[0][$i];
105 #todo: check for clean headers at some point ...
106 truncate_table($push_tb,$dbh);
107 print "Inserting from $w to $push_tb.\n";
108 for my $j (@content) {
109 insert_row($MIGSCHEMA,$push_tb,$dbh,\@pg_headers,$j);
111 timestamp($push_tb,$dbh,'pull');
112 if ($cmd_export == 1) { export_table($dbh,$push_tb); }
116 if (defined $cmd_push) {
119 if ($cmd_push eq 'all') {
120 print "all worksheets.\n";
121 $sql = 'SELECT tab_name FROM gsheet_tracked_table;';
122 $sth = $dbh->prepare($sql);
123 $ra = $sth->execute();
124 while (my @row = $sth->fetchrow_array) {
125 push @tab_names, $row[0];
128 print "only worksheet $cmd_push.\n";
129 if (!defined $cmd_push) { abort('command incomplete'); }
130 push @tab_names, $cmd_push;
132 foreach my $push_ws_name (@tab_names) {
133 my $pull_tb = get_table_name($push_ws_name,$dbh);
134 my @table_headers = get_pg_column_headers($pull_tb,$MIGSCHEMA);
135 print "worksheetname: $push_ws_name\n";
136 my $push_ws = $spreadsheet->worksheet( {title => $push_ws_name} );
137 if (!defined $push_ws) { next; }
140 foreach my $rth (@table_headers) { $rows[0][$i] = $rth; $i++; }
141 $sql = "SELECT * FROM $pull_tb;";
142 $sth = $dbh->prepare($sql);
144 my $grabhash = $sth->fetchall_arrayref({});
145 erase_sheet($push_ws,$push_ws_name);
147 #get from postgres the headers to use in the sheet from tracked columns
148 $sql = 'SELECT column_name FROM gsheet_tracked_column WHERE table_id = (SELECT id FROM gsheet_tracked_table WHERE table_name = \'' . $pull_tb . '\')';
149 $sth = $dbh->prepare($sql);
151 my $sheet_headers = $sth->fetchall_arrayref();
152 my $sheet_headers_length = @$sheet_headers;
153 #now I need to do new rows using those headers
155 foreach my $row ( @{$grabhash} ) {
157 for my $column ( sort keys %{ $row } ) {
158 #print Dumper(@$sheet_headers);
159 #print "column: $column\n";
160 my $clean_column = $column;
161 $clean_column =~ s/_//g;
162 if ( $column ~~ @$sheet_headers ) {
163 $record->{$clean_column} = $row->{$column};
166 push @content, $record;
168 print "Writing to $push_ws_name\n";
169 foreach my $fillsheet (@content) {
170 my $new_row = $push_ws->add_row (
174 timestamp($pull_tb,$dbh,'push');
175 if ($cmd_export == 1) { export_table($dbh,$pull_tb); }
183 my $dt = DateTime->now;
186 my $efile = $MIGGITDIR . $table . '_' . $date . '_' . $hms . '.tsv';
188 my $record_count = 0;
189 $dbh->do("COPY $table TO STDOUT CSV DELIMITER E'\t' HEADER;");
190 1 while $dbh->pg_getcopydata(\$data[$record_count++]) >= 0;
191 open (my $eout, '>', $efile) or abort("Could NOT open $efile.");
192 foreach my $d (@data) {
195 print "$efile written.\n";
200 sub die_if_gsheet_tracked_table_does_not_exist {
201 if (!check_for_gsheet_tracked_table()) {
202 die "Table gsheet_tracked_table does not exist. Bailing...\n";
212 foreach my $av (@a) {
213 foreach my $bv (@b) {
214 if ($av eq $bv) { push @r, $bv; }
220 sub get_pg_column_headers {
221 my $table_name = shift;
222 my $schema_name = shift;
224 my $dbh = Mig::db_connect();
225 $sql = 'SELECT column_name FROM information_schema.columns WHERE table_schema = ' . $dbh->quote( $schema_name ) . ' AND table_name = ' . $dbh->quote( $table_name ) . ';';
226 $sth = $dbh->prepare($sql);
227 $ra = $sth->execute();
228 while (my @row = $sth->fetchrow_array) {
229 push @headers, $row[0];
238 print "Erasing $ws_name.\n";
239 my @rows = $ws->rows;
250 sub check_for_gsheet_tracked_table {
251 my $dbh = Mig::db_connect();
252 my $sth = $dbh->prepare("
255 FROM information_schema.tables
256 WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
257 AND table_name = 'gsheet_tracked_table'
260 my $rv = $sth->execute()
261 || die "Error checking for table (tracked_gsheet_table): $!";
262 my @cols = $sth->fetchrow_array;
264 Mig::db_disconnect($dbh);
268 sub die_if_gsheet_tracked_column_does_not_exist {
269 if (!check_for_gsheet_tracked_column()) {
270 die "Table $MIGSCHEMA.gsheet_tracked_column does not exist. Bailing...\n";
275 my $worksheet = shift;
278 my $sql = 'SELECT table_name FROM gsheet_tracked_table WHERE tab_name = \'' . $worksheet . '\';';
279 my $sth = $dbh->prepare($sql);
280 my $xs = $sth->execute();
282 while (my @row = $sth->fetchrow_array) {
283 $table_name = $row[0];
289 #sub get_worksheet_name {
293 # my $sql = 'SELECT tab_name FROM gsheet_tracked_table WHERE table_name = \'' . $table . '\';';
295 # my $sth = $dbh->prepare($sql);
296 # my $xs = $sth->execute();
297 # my $worksheet_name;
298 # while (my @row = $sth->fetchrow_array) {
299 # $worksheet_name = $row[0];
302 # return $worksheet_name;
306 sub check_for_gsheet_tracked_column {
307 my $dbh = Mig::db_connect();
308 my $sth = $dbh->prepare("
311 FROM information_schema.tables
312 WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
313 AND table_name = 'gsheet_tracked_column'
316 my $rv = $sth->execute()
317 || die "Error checking for table (gsheet_tracked_column): $!";
318 my @cols = $sth->fetchrow_array;
320 Mig::db_disconnect($dbh);
325 my ($schema, $table, $dbh, $headers_ref, $row_ref) = @_;
326 my @headers = @{ $headers_ref };
327 my @row_data = @{ $row_ref };
329 my $header_string = '(' . join(",", @headers) . ')';
330 map {s/\'/\'\'/g; } @row_data;
331 my $row_string = '(' . join(",", map {qq/'$_'/} @row_data) . ')';
332 #print "INSERT INTO $schema.$table $header_string VALUES $row_string\n";
334 INSERT INTO $schema.$table $header_string VALUES $row_string ;
339 my ($table, $dbh, $action) = @_;
342 if ($action eq 'pull') { $column = 'last_pulled' }
343 else { $column = 'last_pushed' };
346 UPDATE gsheet_tracked_table SET $column = NOW() WHERE table_name = '$table';
357 TRUNCATE TABLE $table;;
359 print "Table $table truncated.\n";
364 print STDERR "$0: $msg", "\n";
369 if (!defined $ENV{'CLIENTID'}) {
370 exec '/bin/bash', '--init-file', '~/.mig/oauth.env';
371 print "Open Authentication settings were not loaded, please re-run.\n";
373 my $session_filename = $ENV{SESSIONFILE};
374 my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
375 client_id => $ENV{CLIENTID},
376 client_secret => $ENV{CLIENTSECRET},
377 scope => ['http://spreadsheets.google.com/feeds/'],
378 redirect_uri => 'https://developers.google.com/oauthplayground',
380 my $session = retrieve($session_filename);
381 my $restored_token = Net::OAuth2::AccessToken->session_thaw(
384 profile => $oauth2->oauth2_webserver,
386 $oauth2->access_token($restored_token);
387 my $service = Net::Google::Spreadsheets->new(auth => $oauth2);
389 my $spreadsheet = $service->spreadsheet(