Fix RDBI when configuration is forced (Closes: #2493)

This commit is contained in:
Yadd 2021-06-27 19:13:37 +02:00
parent 499e811ecf
commit 7a219e1a61
4 changed files with 30 additions and 34 deletions

View File

@ -16,30 +16,23 @@ sub store {
my $req; my $req;
my $lastCfg = $self->lastCfg; my $lastCfg = $self->lastCfg;
$req = $self->_dbh->prepare(
"INSERT INTO $self->{dbiTable} (cfgNum,field,value) VALUES (?,?,?)");
if ( $lastCfg == $cfgNum ) { if ( $lastCfg == $cfgNum ) {
$req = $self->_dbh->prepare( my $r =
"UPDATE $self->{dbiTable} SET field=?, value=? WHERE cfgNum=? AND field=?" $self->_dbh->prepare("DELETE FROM $self->{dbiTable} where cfgNum=?");
); $r->execute($cfgNum);
}
else {
$req = $self->_dbh->prepare(
"INSERT INTO $self->{dbiTable} (cfgNum,field,value) VALUES (?,?,?)"
);
} }
unless ($req) { unless ($req) {
$self->logError; $self->logError;
return UNKNOWN_ERROR; return UNKNOWN_ERROR;
} }
while ( my ( $k, $v ) = each %$fields ) { while ( my ( $k, $v ) = each %$fields ) {
my @execValues; my @execValues = ( $cfgNum, $k, $v );
if ( $lastCfg == $cfgNum ) {
@execValues = ( $k, $v, $cfgNum, $k );
}
else { @execValues = ( $cfgNum, $k, $v ); }
my $execute; my $execute;
eval { $execute = $req->execute(@execValues); }; eval { $execute = $req->execute(@execValues); };
print STDERR $@ if $@;
unless ($execute) { unless ($execute) {
$self->logError; $self->logError;
$self->_dbh->do("ROLLBACK"); $self->_dbh->do("ROLLBACK");
@ -55,7 +48,7 @@ sub load {
my $sth = my $sth =
$self->_dbh->prepare( $self->_dbh->prepare(
"SELECT field,value from " . $self->{dbiTable} . " WHERE cfgNum=?" ) "SELECT field,value from " . $self->{dbiTable} . " WHERE cfgNum=?" )
or $self->logError; or $self->logError;
$sth->execute($cfgNum) or $self->logError; $sth->execute($cfgNum) or $self->logError;
my ( $res, @row ); my ( $res, @row );
while ( @row = $sth->fetchrow_array ) { while ( @row = $sth->fetchrow_array ) {

View File

@ -44,7 +44,6 @@ SKIP: {
ok( ok(
$h->_dbh->do( $h->_dbh->do(
"CREATE TABLE lmConfig ( cfgNum int not null, field varchar(255) NOT NULL DEFAULT '', value longblob, PRIMARY KEY (cfgNum,field))" "CREATE TABLE lmConfig ( cfgNum int not null, field varchar(255) NOT NULL DEFAULT '', value longblob, PRIMARY KEY (cfgNum,field))"
), ),
'Test database created' 'Test database created'
); );

View File

@ -8,7 +8,7 @@ use Test::More;
my $count = 0; my $count = 0;
my $file = 't/conf.db'; my $file = 't/conf.db';
my $maintests = 1; my $maintests = 8;
my ( $res, $client ); my ( $res, $client );
eval { unlink $file }; eval { unlink $file };
@ -25,16 +25,27 @@ SKIP: {
} }
my $dbh = DBI->connect("dbi:SQLite:dbname=$file"); my $dbh = DBI->connect("dbi:SQLite:dbname=$file");
$dbh->do('CREATE TABLE lmConfig (cfgNum int, data text)') $dbh->do(
or die $DBI::errstr; "CREATE TABLE lmConfig ( cfgNum int not null, field varchar(255) NOT NULL DEFAULT '', value longblob, PRIMARY KEY (cfgNum,field))"
) or die $DBI::errstr;
use_ok('Lemonldap::NG::Common::Conf');
my $h;
ok(
$h = new Lemonldap::NG::Common::Conf( {
type => 'RDBI',
dbiChain => "DBI:SQLite:dbname=$file",
dbiUser => '',
dbiPassword => '',
}
),
'RDBI object'
);
{ {
local $/ = undef; local $/ = undef;
open my $f, '<', 't/conf/lmConf-1.json'; open my $f, '<', 't/conf/lmConf-1.json';
my $content = <$f>; my $content = <$f>;
close $f; close $f;
my $sth = $dbh->prepare('INSERT INTO lmConfig VALUES(1,?)') ok( $h->store( from_json($content) ), 'Conf 1 saved' );
or die $DBI::errstr;
$sth->execute($content) or die $DBI::errstr;
} }
use_ok('Lemonldap::NG::Manager::Cli::Lib'); use_ok('Lemonldap::NG::Manager::Cli::Lib');
@ -44,23 +55,16 @@ SKIP: {
), ),
'Client object' 'Client object'
); );
count(1);
use_ok('Lemonldap::NG::Manager::Cli'); use_ok('Lemonldap::NG::Manager::Cli');
count(1);
my @args = (qw(-yes 1 -force 1 set ldapSetPassword 0)); my @args = (qw(-yes 1 -force 1 set ldapSetPassword 0));
$ENV{LLNG_DEFAULTCONFFILE} = 't/lemonldap-ng-DBI-conf.ini'; $ENV{LLNG_DEFAULTCONFFILE} = 't/lemonldap-ng-DBI-conf.ini';
Lemonldap::NG::Manager::Cli->run(@args); Lemonldap::NG::Manager::Cli->run(@args);
my $res = $dbh->selectall_arrayref('SELECT * FROM lmConfig'); my $res = $dbh->selectrow_hashref(
my $conf = from_json( $res->[0]->[1] ); "SELECT * FROM lmConfig WHERE field='ldapSetPassword'");
ok( ( ok( $res, 'Key inserted' );
defined( $conf->{ldapSetPassword} ) ok( $res and $res->{value} == '0', 'Value is 0' );
and $conf->{ldapSetPassword} == 0
),
'Key inserted'
);
count(1);
} }
eval { unlink $file }; eval { unlink $file };

View File

@ -6,7 +6,7 @@ localSessionStorageOptions =
[configuration] [configuration]
type=CDBI type=RDBI
dbiChain=dbi:SQLite:dbname=t/conf.db dbiChain=dbi:SQLite:dbname=t/conf.db
[portal] [portal]