From 7a219e1a617e37b279edeedf4abda619826b2442 Mon Sep 17 00:00:00 2001 From: Yadd Date: Sun, 27 Jun 2021 19:13:37 +0200 Subject: [PATCH] Fix RDBI when configuration is forced (Closes: #2493) --- .../Lemonldap/NG/Common/Conf/Backends/RDBI.pm | 23 ++++------- lemonldap-ng-common/t/03-Common-Conf-RDBI.t | 1 - lemonldap-ng-manager/t/30-DBI-Cli.t | 38 ++++++++++--------- .../t/lemonldap-ng-DBI-conf.ini | 2 +- 4 files changed, 30 insertions(+), 34 deletions(-) diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/RDBI.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/RDBI.pm index b1ee50ea5..d46fca79e 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/RDBI.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Backends/RDBI.pm @@ -16,30 +16,23 @@ sub store { my $req; my $lastCfg = $self->lastCfg; + $req = $self->_dbh->prepare( + "INSERT INTO $self->{dbiTable} (cfgNum,field,value) VALUES (?,?,?)"); if ( $lastCfg == $cfgNum ) { - $req = $self->_dbh->prepare( -"UPDATE $self->{dbiTable} SET field=?, value=? WHERE cfgNum=? AND field=?" - ); - - } - else { - $req = $self->_dbh->prepare( - "INSERT INTO $self->{dbiTable} (cfgNum,field,value) VALUES (?,?,?)" - ); + my $r = + $self->_dbh->prepare("DELETE FROM $self->{dbiTable} where cfgNum=?"); + $r->execute($cfgNum); } unless ($req) { $self->logError; return UNKNOWN_ERROR; } while ( my ( $k, $v ) = each %$fields ) { - my @execValues; - if ( $lastCfg == $cfgNum ) { - @execValues = ( $k, $v, $cfgNum, $k ); - } - else { @execValues = ( $cfgNum, $k, $v ); } + my @execValues = ( $cfgNum, $k, $v ); my $execute; eval { $execute = $req->execute(@execValues); }; + print STDERR $@ if $@; unless ($execute) { $self->logError; $self->_dbh->do("ROLLBACK"); @@ -55,7 +48,7 @@ sub load { my $sth = $self->_dbh->prepare( "SELECT field,value from " . $self->{dbiTable} . " WHERE cfgNum=?" ) - or $self->logError; + or $self->logError; $sth->execute($cfgNum) or $self->logError; my ( $res, @row ); while ( @row = $sth->fetchrow_array ) { diff --git a/lemonldap-ng-common/t/03-Common-Conf-RDBI.t b/lemonldap-ng-common/t/03-Common-Conf-RDBI.t index c7158caf2..3ed0fde1f 100644 --- a/lemonldap-ng-common/t/03-Common-Conf-RDBI.t +++ b/lemonldap-ng-common/t/03-Common-Conf-RDBI.t @@ -44,7 +44,6 @@ SKIP: { ok( $h->_dbh->do( "CREATE TABLE lmConfig ( cfgNum int not null, field varchar(255) NOT NULL DEFAULT '', value longblob, PRIMARY KEY (cfgNum,field))" - ), 'Test database created' ); diff --git a/lemonldap-ng-manager/t/30-DBI-Cli.t b/lemonldap-ng-manager/t/30-DBI-Cli.t index 220ea37b7..ea5e8d374 100644 --- a/lemonldap-ng-manager/t/30-DBI-Cli.t +++ b/lemonldap-ng-manager/t/30-DBI-Cli.t @@ -8,7 +8,7 @@ use Test::More; my $count = 0; my $file = 't/conf.db'; -my $maintests = 1; +my $maintests = 8; my ( $res, $client ); eval { unlink $file }; @@ -25,16 +25,27 @@ SKIP: { } my $dbh = DBI->connect("dbi:SQLite:dbname=$file"); - $dbh->do('CREATE TABLE lmConfig (cfgNum int, data text)') - or die $DBI::errstr; + $dbh->do( +"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; open my $f, '<', 't/conf/lmConf-1.json'; my $content = <$f>; close $f; - my $sth = $dbh->prepare('INSERT INTO lmConfig VALUES(1,?)') - or die $DBI::errstr; - $sth->execute($content) or die $DBI::errstr; + ok( $h->store( from_json($content) ), 'Conf 1 saved' ); } use_ok('Lemonldap::NG::Manager::Cli::Lib'); @@ -44,23 +55,16 @@ SKIP: { ), 'Client object' ); - count(1); use_ok('Lemonldap::NG::Manager::Cli'); - count(1); my @args = (qw(-yes 1 -force 1 set ldapSetPassword 0)); $ENV{LLNG_DEFAULTCONFFILE} = 't/lemonldap-ng-DBI-conf.ini'; Lemonldap::NG::Manager::Cli->run(@args); - my $res = $dbh->selectall_arrayref('SELECT * FROM lmConfig'); - my $conf = from_json( $res->[0]->[1] ); - ok( ( - defined( $conf->{ldapSetPassword} ) - and $conf->{ldapSetPassword} == 0 - ), - 'Key inserted' - ); - count(1); + my $res = $dbh->selectrow_hashref( + "SELECT * FROM lmConfig WHERE field='ldapSetPassword'"); + ok( $res, 'Key inserted' ); + ok( $res and $res->{value} == '0', 'Value is 0' ); } eval { unlink $file }; diff --git a/lemonldap-ng-manager/t/lemonldap-ng-DBI-conf.ini b/lemonldap-ng-manager/t/lemonldap-ng-DBI-conf.ini index 5457aafcf..8357553e2 100644 --- a/lemonldap-ng-manager/t/lemonldap-ng-DBI-conf.ini +++ b/lemonldap-ng-manager/t/lemonldap-ng-DBI-conf.ini @@ -6,7 +6,7 @@ localSessionStorageOptions = [configuration] -type=CDBI +type=RDBI dbiChain=dbi:SQLite:dbname=t/conf.db [portal]