Change CDBI storage to JSON (#877)

This commit is contained in:
Xavier Guimard 2016-01-11 06:27:14 +00:00
parent f80b22ea46
commit 64099c2ca3
2 changed files with 18 additions and 12 deletions

View File

@ -2,7 +2,7 @@ package Lemonldap::NG::Common::Conf::CDBI;
use strict; use strict;
use utf8; use utf8;
require Storable; use JSON::MaybeXS;
use Lemonldap::NG::Common::Conf::_DBI; use Lemonldap::NG::Common::Conf::_DBI;
our $VERSION = '1.4.0'; our $VERSION = '1.4.0';
@ -14,7 +14,7 @@ sub store {
my $req; my $req;
my $lastCfg = $self->lastCfg; my $lastCfg = $self->lastCfg;
$fields = Storable::nfreeze($fields); $fields = encode_json($fields);
if ( $lastCfg == $cfgNum ) { if ( $lastCfg == $cfgNum ) {
$req = $self->_dbh->prepare( $req = $self->_dbh->prepare(
@ -46,7 +46,13 @@ sub load {
return 0; return 0;
} }
my $r; my $r;
eval { $r = Storable::thaw( $row->[0] ); }; if ( $row->[0] =~ /^\s*\{/s ) {
eval { $r = decode_json( $row->[0] ); };
}
else { # Old format
require Storable;
eval { $r = Storable::thaw( $row->[0] ); };
}
if ($@) { if ($@) {
$Lemonldap::NG::Common::Conf::msg .= $Lemonldap::NG::Common::Conf::msg .=
"Bad stored data in conf database: $@ \n"; "Bad stored data in conf database: $@ \n";

View File

@ -9,7 +9,6 @@ use strict;
use Test::More; use Test::More;
BEGIN { BEGIN {
use_ok('DBI');
use_ok('Lemonldap::NG::Common::Conf'); use_ok('Lemonldap::NG::Common::Conf');
} }
@ -21,21 +20,22 @@ BEGIN {
my $h; my $h;
@ARGV = ("help=groups"); @ARGV = ("help=groups");
unlink 't/lmConf.sql'; unlink 't/lmConf.sql';
my $count = 2; my $count = 1;
eval { require DBD::SQLite }; eval { require DBD::SQLite };
unless ($@) { unless ($@) {
require DBI; use_ok('DBI');
my $dbh = DBI->connect( "dbi:SQLite:dbname=t/lmConf.sql", '', '', {AutoCommit=>1} ); my $dbh = DBI->connect( "dbi:SQLite:dbname=t/lmConf.sql",
'', '', { AutoCommit => 1 } );
$dbh->{sqlite_unicode} = 1; $dbh->{sqlite_unicode} = 1;
ok( ok(
$dbh->do( $dbh->do(
' CREATE TABLE lmConfig ( cfgNum int not null primary key, data longblob)' ' CREATE TABLE lmConfig ( cfgNum int not null primary key, data text)'
), ),
'Database created' 'Test database created'
); );
undef $dbh; undef $dbh;
$count++; $count += 2;
ok( ok(
$h = new Lemonldap::NG::Common::Conf( $h = new Lemonldap::NG::Common::Conf(
@ -66,11 +66,11 @@ unless ($@) {
); );
for ( my $i = 0 ; $i < @test ; $i++ ) { for ( my $i = 0 ; $i < @test ; $i++ ) {
ok( $h->store( $test[$i] ) == $i+1, "Test $i is stored" ) ok( $h->store( $test[$i] ) == $i + 1, "Test $i is stored" )
or print STDERR "$Lemonldap::NG::Common::Conf::msg $!"; or print STDERR "$Lemonldap::NG::Common::Conf::msg $!";
$count++; $count++;
my $cfg; my $cfg;
ok( $cfg = $h->load($i+1), "Test $i can be read" ) ok( $cfg = $h->load( $i + 1 ), "Test $i can be read" )
or print STDERR $Lemonldap::NG::Common::Conf::msg; or print STDERR $Lemonldap::NG::Common::Conf::msg;
ok( $cfg->{test} eq $test[$i]->{test}, "Test $i is restored" ); ok( $cfg->{test} eq $test[$i]->{test}, "Test $i is restored" );
$count += 2; $count += 2;