#!/usr/bin/perl

use strict;
use warnings;
use LATMOS::Accounts;
use Getopt::Long;
use Pod::Usage;

my $DATADIR = '@DATADIR@';

=head1 NAME

    la-sql-upgrade - Update SQL base schema

=head1 SYNOPSIS

    la-sql-upgrade [options] [name]

=cut

GetOptions(
    'c|config=s'     => \my $config,
    'b|base=s'       => \my $base,
    'no-commit'      => \my $nocommit,
    'v|verbose'      => \my $verbose,
    'h|help'         => sub { pod2usage(1); },
) or pod2usage();

=head1 OPTIONS

=over 4

=item -c|--config configdir

Use this configuration directory instead of the default one.

=item -b|--base basename

Query this specific base instead of the default one.

=back

=cut

$| = 1; # autoflush

my $LA = LATMOS::Accounts->new($config, noacl => 1);
my $labase = $LA->base($base);
$labase->unexported(1);

require LATMOS::Accounts::Bases::Sql::upgrade;

$labase->SchemaUpgrade() or die "An error occur";

if ($nocommit) {
    $labase->rollback;
} else {
    $labase->commit;
}
print "Process terminated successfully\n";
