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/";
18 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
19 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
26 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
28 Mig::die_if_no_env_migschema();
29 die_if_gsheet_tracked_table_does_not_exist();
30 die_if_gsheet_tracked_column_does_not_exist();
44 foreach my $arg (@ARGV) {
45 if ($arg eq '--push') {
46 $next_arg_is_push = 1;
49 if ($next_arg_is_push) {
51 $next_arg_is_push = 0;
54 if ($arg eq '--pull') {
55 $next_arg_is_pull = 1;
58 if ($next_arg_is_pull) {
60 $next_arg_is_pull = 0;
63 if ($arg eq '--export') {
69 abort('must specify --push (db->worksheets) or --pull (worksheets->db)') unless (defined $cmd_push or defined $cmd_pull);
70 if (defined $cmd_push and defined $cmd_pull) { abort('you can not specify both a --push and --pull on the same command'); }
72 my $dbh = Mig::db_connect();
73 my $spreadsheet = connect_gsheet();
74 abort('could not connect to google sheet') unless (defined $spreadsheet);
76 $sql = 'SELECT tab_name FROM ' . $MIGSCHEMA . '.gsheet_tracked_table;';
77 $sth = $dbh->prepare($sql);
78 my $ra = $sth->execute();
79 while (my @row = $sth->fetchrow_array) {
80 push @tracked_ws_names, $row[0];
83 if (defined $cmd_pull) {
85 if ($cmd_pull eq 'all') {
86 print "all worksheets.\n";
87 @ws = $spreadsheet->worksheets;
88 foreach my $wsn (@ws) { push @worksheet_names, $wsn->title; }
90 print "only worksheet $cmd_pull.\n";
91 if (!defined $cmd_pull) { abort('command incomplete'); }
92 push @worksheet_names, $cmd_pull;
94 my @m = array_match(\@worksheet_names,\@tracked_ws_names);
96 my $pull_ws = $spreadsheet->worksheet( {title => $w} );
97 my $push_tb = get_table_name($MIGSCHEMA,$w,$dbh);
98 my @rows = $pull_ws->rows;
100 map { $content[$_->row - 1][$_->col - 1] = $_->content } $pull_ws->cells;
101 my @tab_headers = shift @content;
102 my $tab_headers_length = $#{ $tab_headers[0] };
104 for my $i ( 0 .. $tab_headers_length ) {
105 push @pg_headers, $tab_headers[0][$i];
108 #todo: check for clean headers at some point ...
109 truncate_table($MIGSCHEMA,$push_tb,$dbh);
110 print "Inserting from $w to $push_tb.\n";
111 for my $j (@content) {
112 insert_row($MIGSCHEMA,$push_tb,$dbh,\@pg_headers,$j);
114 timestamp($MIGSCHEMA,$push_tb,$dbh,'pull');
118 if (defined $cmd_push) {
120 if ($cmd_push eq 'all') {
121 print "all tables.\n";
122 $sql = 'SELECT table_name FROM ' . $MIGSCHEMA . '.gsheet_tracked_table';
123 $sth = $dbh->prepare($sql);
124 $ra = $sth->execute();
125 while (my @row = $sth->fetchrow_array) {
126 push @table_names, $row[0];
129 print "only table $cmd_push.\n";
130 if (!defined $cmd_push) { abort('command incomplete'); }
131 push @table_names, $cmd_push;
133 foreach my $t (@table_names) {
134 my $pull_tb = $MIGSCHEMA . "." . $t;;
135 my @table_headers = get_pg_column_headers($t,$MIGSCHEMA);
136 my $push_ws_name = get_worksheet_name($MIGSCHEMA,$t,$dbh);
137 my $push_ws = $spreadsheet->worksheet( {title => $push_ws_name} );
138 if (!defined $push_ws) { next; }
141 foreach my $rth (@table_headers) { $rows[0][$i] = $rth; $i++; }
142 $sql = "SELECT * FROM $pull_tb;";
143 $sth = $dbh->prepare($sql);
145 my $grabhash = $sth->fetchall_arrayref({});
146 erase_sheet($push_ws,$push_ws_name);
148 #get from postgres the headers to use in the sheet from tracked columns
149 $sql = 'SELECT column_name FROM ' . $MIGSCHEMA . '.gsheet_tracked_column WHERE table_id = (SELECT id FROM ' . $MIGSCHEMA . '.gsheet_tracked_table WHERE table_name = \'' . $t . '\')';
150 $sth = $dbh->prepare($sql);
152 my $sheet_headers = $sth->fetchall_arrayref();
153 my $sheet_headers_length = @$sheet_headers;
154 #now I need to do new rows using those headers
156 foreach my $row ( @{$grabhash} ) {
158 for my $column ( sort keys %{ $row } ) {
159 print Dumper(@$sheet_headers);
160 print "column: $column\n";
161 my $clean_column = $column;
162 $clean_column =~ s/_//g;
163 print "clean column: $clean_column\n";
164 if ( $column ~~ @$sheet_headers ) {
165 $record->{$clean_column} = $row->{$column};
168 push @content, $record;
171 foreach my $fillsheet (@content) {
172 my $new_row = $push_ws->add_row (
176 timestamp($MIGSCHEMA,$pull_tb,$dbh,'push');
180 sub die_if_gsheet_tracked_table_does_not_exist {
181 if (!check_for_gsheet_tracked_table()) {
182 die "Table $MIGSCHEMA.gsheet_tracked_table does not exist. Bailing...\n";
192 foreach my $av (@a) {
193 foreach my $bv (@b) {
194 if ($av eq $bv) { push @r, $bv; }
200 sub get_pg_column_headers {
201 my $table_name = shift;
202 my $schema_name = shift;
204 my $dbh = Mig::db_connect();
205 $sql = 'SELECT column_name FROM information_schema.columns WHERE table_schema = ' . $dbh->quote( $schema_name ) . ' AND table_name = ' . $dbh->quote( $table_name ) . ';';
206 $sth = $dbh->prepare($sql);
207 $ra = $sth->execute();
208 while (my @row = $sth->fetchrow_array) {
209 push @headers, $row[0];
218 print "Erasing $ws_name.\n";
219 my @rows = $ws->rows;
222 $j = int(($j / 2))-1;
223 if ($j < 2) { $j = 2; }
225 #bodge until I figure out why google sheets is only deleting even numbered rows
227 foreach my $row (@rows) {
228 if ($i != 1) { $row->delete; }
236 sub check_for_gsheet_tracked_table {
237 my $dbh = Mig::db_connect();
238 my $sth = $dbh->prepare("
241 FROM information_schema.tables
242 WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
243 AND table_name = 'gsheet_tracked_table'
246 my $rv = $sth->execute()
247 || die "Error checking for table (tracked_gsheet_table): $!";
248 my @cols = $sth->fetchrow_array;
250 Mig::db_disconnect($dbh);
254 sub die_if_gsheet_tracked_column_does_not_exist {
255 if (!check_for_gsheet_tracked_column()) {
256 die "Table $MIGSCHEMA.gsheet_tracked_column does not exist. Bailing...\n";
262 my $worksheet = shift;
265 my $sql = 'SELECT table_name FROM ' . $migs . '.gsheet_tracked_table WHERE tab_name = \'' . $worksheet . '\';';
266 my $sth = $dbh->prepare($sql);
267 my $xs = $sth->execute();
269 while (my @row = $sth->fetchrow_array) {
270 $table_name = $row[0];
276 sub get_worksheet_name {
281 my $sql = 'SELECT tab_name FROM ' . $migs . '.gsheet_tracked_table WHERE table_name = \'' . $table . '\';';
282 my $sth = $dbh->prepare($sql);
283 my $xs = $sth->execute();
285 while (my @row = $sth->fetchrow_array) {
286 $worksheet_name = $row[0];
289 return $worksheet_name;
293 sub check_for_gsheet_tracked_column {
294 my $dbh = Mig::db_connect();
295 my $sth = $dbh->prepare("
298 FROM information_schema.tables
299 WHERE table_schema = " . $dbh->quote( $MIGSCHEMA ) . "
300 AND table_name = 'gsheet_tracked_column'
303 my $rv = $sth->execute()
304 || die "Error checking for table (gsheet_tracked_column): $!";
305 my @cols = $sth->fetchrow_array;
307 Mig::db_disconnect($dbh);
312 my ($schema, $table, $dbh, $headers_ref, $row_ref) = @_;
313 my @headers = @{ $headers_ref };
314 my @row_data = @{ $row_ref };
316 my $header_string = '(' . join(",", @headers) . ')';
317 map {s/\'/\'\'/g; } @row_data;
318 my $row_string = '(' . join(",", map {qq/'$_'/} @row_data) . ')';
319 print "INSERT INTO $schema.$table $header_string VALUES $row_string\n";
321 INSERT INTO $schema.$table $header_string VALUES $row_string ;
324 print "Row pushed to $table in schema $schema.\n";
328 my ($schema, $table, $dbh, $action) = @_;
331 if ($action eq 'pull') { $column = 'last_pulled' }
332 else { $column = 'last_pushed' };
335 UPDATE $schema.gsheet_tracked_table SET $column = NOW() WHERE table_name = '$table';
347 TRUNCATE TABLE $schema.$table;;
349 print "Table $schema.$table truncated.\n";
354 print STDERR "$0: $msg", "\n";
359 if (!defined $ENV{'CLIENTID'}) {
360 exec '/bin/bash', '--init-file', '~/.mig/oauth.env';
361 print "Open Authentication settings were not loaded, please re-run.\n";
363 my $session_filename = $ENV{SESSIONFILE};
364 my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
365 client_id => $ENV{CLIENTID},
366 client_secret => $ENV{CLIENTSECRET},
367 scope => ['http://spreadsheets.google.com/feeds/'],
368 redirect_uri => 'https://developers.google.com/oauthplayground',
370 my $session = retrieve($session_filename);
371 my $restored_token = Net::OAuth2::AccessToken->session_thaw(
374 profile => $oauth2->oauth2_webserver,
376 $oauth2->access_token($restored_token);
377 my $service = Net::Google::Spreadsheets->new(auth => $oauth2);
379 my $spreadsheet = $service->spreadsheet(