#!/usr/bin/perl

use strict;
use warnings;
use open ':std', ':encoding(UTF-8)';

use JSON::XS;
use FileHandle;
use Text::CSV::Easy(qw/csv_build csv_parse/);

use lib '/app/tools/common/lib';
use Common::Logger;
use Common::DB::Schema;

my $host = shift @ARGV;
my $dbx = Common::DB::Schema->getdbx($host);
my $upd = $dbx->getdbh->prepare(
    qq{
	UPDATE period SET 
		date_created=date_modified, 
		date_modified=date_modified 
	WHERE 
		date_created='0000-00-00 00:00:00'
}
);

foreach my $db ( @{ $dbx->dblist->rps } ) {
    $dbx->switch_schema($db);
    my $rows = $dbx->resultset('RPS::Period')->search( { 'date_created' => '0000-00-00 00:00:00' } );
    if ( defined $rows and $rows->count ) {
        logMessage( 'warn', $rows->count . " updated rows in $db.period table" );
        $upd->execute;
    } else {
        logMessage( 'info', "0 date_created rows in $db.period table" );
    }
}
