From 5466b9471cc85ff5ad9719ed61aaa474ae5b4c23 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 14 Sep 2020 09:01:00 +0200 Subject: fix --- src/backend/taler-merchant-httpd.c | 3 +- src/backenddb/merchantdb_plugin.c | 36 ++++++++++++++---- src/backenddb/plugin_merchantdb_postgres.c | 60 ++++++++++++++++++++---------- src/backenddb/test_merchantdb.c | 26 +------------ src/include/taler_merchantdb_lib.h | 4 +- src/include/taler_merchantdb_plugin.h | 12 ++++++ src/merchant-tools/taler-merchant-dbinit.c | 9 +---- 7 files changed, 90 insertions(+), 60 deletions(-) diff --git a/src/backend/taler-merchant-httpd.c b/src/backend/taler-merchant-httpd.c index d4cc835f..bf8de66d 100644 --- a/src/backend/taler-merchant-httpd.c +++ b/src/backend/taler-merchant-httpd.c @@ -1752,7 +1752,8 @@ run (void *cls, return; } if (NULL == - (TMH_db = TALER_MERCHANTDB_plugin_load (cfg))) + (TMH_db = TALER_MERCHANTDB_plugin_load (cfg, + false))) { GNUNET_SCHEDULER_shutdown (); return; diff --git a/src/backenddb/merchantdb_plugin.c b/src/backenddb/merchantdb_plugin.c index a8e046e5..d548ab0a 100644 --- a/src/backenddb/merchantdb_plugin.c +++ b/src/backenddb/merchantdb_plugin.c @@ -1,6 +1,6 @@ /* This file is part of TALER - Copyright (C) 2015, 2016 GNUnet e.V. and INRIA + Copyright (C) 2015, 2016, 2020 Taler Systems SA TALER is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software @@ -22,6 +22,7 @@ #include "platform.h" #include #include "taler_merchantdb_plugin.h" +#include "taler_merchantdb_lib.h" #include @@ -29,10 +30,12 @@ * Initialize the plugin. * * @param[in,out] cfg configuration to use + * @param reset_db should the DB be reset on load? * @return #GNUNET_OK on success */ struct TALER_MERCHANTDB_Plugin * -TALER_MERCHANTDB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg) +TALER_MERCHANTDB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg, + bool reset_db) { char *plugin_name; char *lib_name; @@ -55,10 +58,29 @@ TALER_MERCHANTDB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg) GNUNET_free (plugin_name); plugin = GNUNET_PLUGIN_load (lib_name, (void *) cfg); - if (NULL != plugin) - plugin->library_name = lib_name; - else + if (NULL == plugin) + { GNUNET_free (lib_name); + return NULL; + } + plugin->library_name = lib_name; + if (reset_db) + { + if (GNUNET_OK != + plugin->connect (plugin->cls, + true)) + { + TALER_MERCHANTDB_plugin_unload (plugin); + return NULL; + } + (void) plugin->drop_tables (plugin->cls); + } + if (GNUNET_OK !=plugin->connect (plugin->cls, + false)) + { + TALER_MERCHANTDB_plugin_unload (plugin); + return NULL; + } return plugin; } @@ -92,7 +114,7 @@ static char *old_dlsearchpath; * Setup libtool paths. */ void __attribute__ ((constructor)) -plugin_init () +plugin_init (void) { int err; const char *opath; @@ -133,7 +155,7 @@ plugin_init () * Shutdown libtool. */ void __attribute__ ((destructor)) -plugin_fini () +plugin_fini (void) { lt_dlsetsearchpath (old_dlsearchpath); if (NULL != old_dlsearchpath) diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c index b965b0e6..ee07245a 100644 --- a/src/backenddb/plugin_merchantdb_postgres.c +++ b/src/backenddb/plugin_merchantdb_postgres.c @@ -6031,17 +6031,17 @@ postgres_insert_pickup_blind_signature ( /** - * Initialize Postgres database subsystem. + * Establish connection to the database. * - * @param cls a configuration instance - * @return NULL on error, otherwise a `struct TALER_MERCHANTDB_Plugin` + * @param cls plugin context + * @param for_drop is this to drop/reset the database (so no point in + * preparing statements)? */ -void * -libtaler_plugin_merchantdb_postgres_init (void *cls) +static int +postgres_connect (void *cls, + bool for_drop) { - const struct GNUNET_CONFIGURATION_Handle *cfg = cls; - struct PostgresClosure *pg; - struct TALER_MERCHANTDB_Plugin *plugin; + struct PostgresClosure *pg = cls; struct GNUNET_PQ_PreparedStatement ps[] = { GNUNET_PQ_make_prepare ("end_transaction", "COMMIT", @@ -8413,6 +8413,32 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) GNUNET_PQ_PREPARED_STATEMENT_END }; + pg->conn = GNUNET_PQ_connect_with_cfg (pg->cfg, + "merchantdb-postgres", + "merchant-", + NULL, + for_drop + ? NULL + : ps); + if (NULL == pg->conn) + return GNUNET_SYSERR; + return GNUNET_OK; +} + + +/** + * Initialize Postgres database subsystem. + * + * @param cls a configuration instance + * @return NULL on error, otherwise a `struct TALER_MERCHANTDB_Plugin` + */ +void * +libtaler_plugin_merchantdb_postgres_init (void *cls) +{ + const struct GNUNET_CONFIGURATION_Handle *cfg = cls; + struct PostgresClosure *pg; + struct TALER_MERCHANTDB_Plugin *plugin; + pg = GNUNET_new (struct PostgresClosure); pg->cfg = cfg; if (GNUNET_OK != @@ -8427,17 +8453,6 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) GNUNET_free (pg); return NULL; } - pg->conn = GNUNET_PQ_connect_with_cfg (cfg, - "merchantdb-postgres", - "merchant-", - NULL, - ps); - if (NULL == pg->conn) - { - GNUNET_free (pg->sql_dir); - GNUNET_free (pg); - return NULL; - } if (GNUNET_OK != TALER_config_get_currency (cfg, &pg->currency)) @@ -8449,6 +8464,7 @@ libtaler_plugin_merchantdb_postgres_init (void *cls) } plugin = GNUNET_new (struct TALER_MERCHANTDB_Plugin); plugin->cls = pg; + plugin->connect = &postgres_connect; plugin->drop_tables = &postgres_drop_tables; plugin->preflight = &postgres_preflight; plugin->start = &postgres_start; @@ -8541,7 +8557,11 @@ libtaler_plugin_merchantdb_postgres_done (void *cls) struct TALER_MERCHANTDB_Plugin *plugin = cls; struct PostgresClosure *pg = plugin->cls; - GNUNET_PQ_disconnect (pg->conn); + if (NULL != pg->conn) + { + GNUNET_PQ_disconnect (pg->conn); + pg->conn = NULL; + } GNUNET_free (pg->sql_dir); GNUNET_free (pg->currency); GNUNET_free (pg); diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c index e0ec18d4..fa7dffa8 100644 --- a/src/backenddb/test_merchantdb.c +++ b/src/backenddb/test_merchantdb.c @@ -6660,18 +6660,8 @@ run (void *cls) /* Data for 'store_payment()' */ /* Drop the tables to cleanup anything that might cause issues */ - if (NULL == (plugin = TALER_MERCHANTDB_plugin_load (cfg))) - { - result = 77; - return; - } - - GNUNET_break (GNUNET_OK == - plugin->drop_tables (plugin->cls)); - TALER_MERCHANTDB_plugin_unload (plugin); - - /* Load the plugin */ - if (NULL == (plugin = TALER_MERCHANTDB_plugin_load (cfg))) + if (NULL == (plugin = TALER_MERCHANTDB_plugin_load (cfg, + true))) { result = 77; return; @@ -6691,18 +6681,6 @@ run (void *cls) return; } - /* Unload the plugin */ - TALER_MERCHANTDB_plugin_unload (plugin); - if (NULL == (plugin = TALER_MERCHANTDB_plugin_load (cfg))) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Plugin unload failed\n"); - result = 77; - return; - } - - GNUNET_break (GNUNET_OK == - plugin->drop_tables (plugin->cls)); TALER_MERCHANTDB_plugin_unload (plugin); plugin = NULL; } diff --git a/src/include/taler_merchantdb_lib.h b/src/include/taler_merchantdb_lib.h index 5ebbf73f..2820e903 100644 --- a/src/include/taler_merchantdb_lib.h +++ b/src/include/taler_merchantdb_lib.h @@ -34,10 +34,12 @@ struct TALER_MERCHANTDB_Plugin; * Connect to postgresql database * * @param[in,out] cfg the configuration handle + * @param reset_db should the DB be reset on load? * @return connection to the database; NULL upon error */ struct TALER_MERCHANTDB_Plugin * -TALER_MERCHANTDB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg); +TALER_MERCHANTDB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg, + bool reset_db); /** diff --git a/src/include/taler_merchantdb_plugin.h b/src/include/taler_merchantdb_plugin.h index 0270dca4..9d2dd234 100644 --- a/src/include/taler_merchantdb_plugin.h +++ b/src/include/taler_merchantdb_plugin.h @@ -668,6 +668,18 @@ struct TALER_MERCHANTDB_Plugin */ char *library_name; + /** + * Connect to the database, possibly only to drop the tables. Called first + * to complete the initialization. + * + * @param cls closure + * @param for_drop are we going to call drop_tables() next (and then + * possibly again connect()? + */ + int + (*connect) (void *cls, + bool for_drop); + /** * Drop merchant tables. Used for testcases. * diff --git a/src/merchant-tools/taler-merchant-dbinit.c b/src/merchant-tools/taler-merchant-dbinit.c index d5f2da6b..938f7d30 100644 --- a/src/merchant-tools/taler-merchant-dbinit.c +++ b/src/merchant-tools/taler-merchant-dbinit.c @@ -54,7 +54,8 @@ run (void *cls, cfg = GNUNET_CONFIGURATION_dup (config); if (NULL == - (plugin = TALER_MERCHANTDB_plugin_load (cfg))) + (plugin = TALER_MERCHANTDB_plugin_load (cfg, + reset_db))) { fprintf (stderr, "Failed to initialize database plugin.\n"); @@ -62,12 +63,6 @@ run (void *cls, GNUNET_CONFIGURATION_destroy (cfg); return; } - if (reset_db) - { - (void) plugin->drop_tables (plugin->cls); - TALER_MERCHANTDB_plugin_unload (plugin); - plugin = TALER_MERCHANTDB_plugin_load (cfg); - } TALER_MERCHANTDB_plugin_unload (plugin); GNUNET_CONFIGURATION_destroy (cfg); } -- cgit v1.2.3