#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - autofixer2/fix_mariadb_show_grants_roles
#                                                  Copyright 2020 cPanel, L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use strict;
use warnings;

use Cpanel::MysqlUtils::Connect ();
use Cpanel::MysqlUtils::Version ();
use Cpanel::MariaDB             ();

__PACKAGE__->update() if !caller;

sub update {

    my $version = eval { Cpanel::MysqlUtils::Version::mysqlversion() };
    return if !$version;
    return if !Cpanel::MariaDB::version_is_mariadb($version);

    my $dbh = eval { Cpanel::MysqlUtils::Connect::get_dbi_handle() };
    return if !$dbh;

    my @grants = $dbh->show_grants( 'root', 'localhost' );

    if ( grep { $_ =~ /^SET DEFAULT ROLE 0/ } @grants ) {

        $dbh->do(
            q{
        ALTER TABLE
            mysql.user
        MODIFY IF EXISTS
            password_last_changed timestamp DEFAULT CURRENT_TIMESTAMP NULL AFTER max_statement_time,
        MODIFY IF EXISTS
            password_lifetime smallint unsigned DEFAULT NULL AFTER password_last_changed,
        MODIFY IF EXISTS
            account_locked enum('N', 'Y') COLLATE utf8_general_ci DEFAULT 'N' NOT NULL after password_lifetime
        }
        );

        $dbh->do(q{FLUSH PRIVILEGES});
    }

    return;
}

1;
