summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2021-01-02 20:40:11 +0100
committerChristian Grothoff <christian@grothoff.org>2021-01-02 20:40:11 +0100
commit7334cfc40995c98aa550fedc7b249d9c08100028 (patch)
tree27ee7cc6ed6fe18615fff32f67b3839de84c7805
parentfeb6c3c694f6f083d7b8d09a35f2074d5c3d44f4 (diff)
downloadmerchant-7334cfc40995c98aa550fedc7b249d9c08100028.tar.gz
merchant-7334cfc40995c98aa550fedc7b249d9c08100028.tar.bz2
merchant-7334cfc40995c98aa550fedc7b249d9c08100028.zip
fix DB initialization logic to avoid CREATE table attempts in taler-merchant-httpd
-rw-r--r--src/backend/taler-merchant-httpd.c11
-rw-r--r--src/backenddb/merchantdb_plugin.c20
-rw-r--r--src/backenddb/plugin_merchantdb_postgres.c63
-rw-r--r--src/backenddb/test_merchantdb.c16
-rw-r--r--src/include/taler_merchantdb_lib.h8
-rw-r--r--src/include/taler_merchantdb_plugin.h19
-rw-r--r--src/merchant-tools/taler-merchant-dbinit.c19
7 files changed, 97 insertions, 59 deletions
diff --git a/src/backend/taler-merchant-httpd.c b/src/backend/taler-merchant-httpd.c
index 1dcc51c8..bf1c4db3 100644
--- a/src/backend/taler-merchant-httpd.c
+++ b/src/backend/taler-merchant-httpd.c
@@ -1754,12 +1754,19 @@ run (void *cls,
return;
}
if (NULL ==
- (TMH_db = TALER_MERCHANTDB_plugin_load (cfg,
- false)))
+ (TMH_db = TALER_MERCHANTDB_plugin_load (cfg)))
{
GNUNET_SCHEDULER_shutdown ();
return;
}
+ if (GNUNET_OK !=
+ TMH_db->connect (TMH_db->cls))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Failed to initialze database connection\n");
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
/* load instances */
{
enum GNUNET_DB_QueryStatus qs;
diff --git a/src/backenddb/merchantdb_plugin.c b/src/backenddb/merchantdb_plugin.c
index c08906ad..7dfb44e8 100644
--- a/src/backenddb/merchantdb_plugin.c
+++ b/src/backenddb/merchantdb_plugin.c
@@ -27,8 +27,7 @@
struct TALER_MERCHANTDB_Plugin *
-TALER_MERCHANTDB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg,
- bool reset_db)
+TALER_MERCHANTDB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg)
{
char *plugin_name;
char *lib_name;
@@ -57,23 +56,6 @@ TALER_MERCHANTDB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg,
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;
}
diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c
index 47147c07..5954e3c6 100644
--- a/src/backenddb/plugin_merchantdb_postgres.c
+++ b/src/backenddb/plugin_merchantdb_postgres.c
@@ -105,28 +105,50 @@ struct PostgresClosure
};
-/* ********************* NEW API ************************** */
+/**
+ * Drop all Taler tables. This should only be used by testcases.
+ *
+ * @param cls the `struct PostgresClosure` with the plugin-specific state
+ * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
+ */
+static int
+postgres_drop_tables (void *cls)
+{
+ struct PostgresClosure *pc = cls;
+ struct GNUNET_PQ_Context *conn;
+
+ conn = GNUNET_PQ_connect_with_cfg (pc->cfg,
+ "merchantdb-postgres",
+ "drop",
+ NULL,
+ NULL);
+ if (NULL == conn)
+ return GNUNET_SYSERR;
+ GNUNET_PQ_disconnect (conn);
+ return GNUNET_OK;
+}
/**
- * Drop merchant tables
+ * Initialize tables.
*
- * @param cls closure our `struct Plugin`
+ * @param cls the `struct PostgresClosure` with the plugin-specific state
* @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
*/
static int
-postgres_drop_tables (void *cls)
+postgres_create_tables (void *cls)
{
- struct PostgresClosure *pg = cls;
- char *load_path;
-
- GNUNET_asprintf (&load_path,
- "%s%s",
- pg->sql_dir,
- "drop");
- GNUNET_PQ_run_sql (pg->conn,
- load_path);
- GNUNET_free (load_path);
+ struct PostgresClosure *pc = cls;
+ struct GNUNET_PQ_Context *conn;
+
+ conn = GNUNET_PQ_connect_with_cfg (pc->cfg,
+ "merchantdb-postgres",
+ "merchant-",
+ NULL,
+ NULL);
+ if (NULL == conn)
+ return GNUNET_SYSERR;
+ GNUNET_PQ_disconnect (conn);
return GNUNET_OK;
}
@@ -6015,12 +6037,10 @@ postgres_insert_pickup_blind_signature (
* Establish connection to the database.
*
* @param cls plugin context
- * @param for_drop is this to drop/reset the database (so no point in
- * preparing statements)?
+ * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
*/
static int
-postgres_connect (void *cls,
- bool for_drop)
+postgres_connect (void *cls)
{
struct PostgresClosure *pg = cls;
struct GNUNET_PQ_PreparedStatement ps[] = {
@@ -8396,11 +8416,9 @@ postgres_connect (void *cls,
pg->conn = GNUNET_PQ_connect_with_cfg (pg->cfg,
"merchantdb-postgres",
- "merchant-",
NULL,
- for_drop
- ? NULL
- : ps);
+ NULL,
+ ps);
if (NULL == pg->conn)
return GNUNET_SYSERR;
return GNUNET_OK;
@@ -8446,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->create_tables = &postgres_create_tables;
plugin->drop_tables = &postgres_drop_tables;
plugin->preflight = &postgres_preflight;
plugin->start = &postgres_start;
diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c
index a08139e9..9be7933d 100644
--- a/src/backenddb/test_merchantdb.c
+++ b/src/backenddb/test_merchantdb.c
@@ -6660,12 +6660,24 @@ 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,
- true)))
+ if (NULL == (plugin = TALER_MERCHANTDB_plugin_load (cfg)))
{
result = 77;
return;
}
+ (void) plugin->drop_tables (plugin->cls);
+ if (GNUNET_OK !=
+ plugin->create_tables (plugin->cls))
+ {
+ result = 77;
+ return;
+ }
+ if (GNUNET_OK !=
+ plugin->connect (plugin->cls))
+ {
+ result = 17;
+ return;
+ }
/* Run the preflight */
plugin->preflight (plugin->cls);
diff --git a/src/include/taler_merchantdb_lib.h b/src/include/taler_merchantdb_lib.h
index 11678fa7..e24198ef 100644
--- a/src/include/taler_merchantdb_lib.h
+++ b/src/include/taler_merchantdb_lib.h
@@ -1,6 +1,6 @@
/*
This file is part of TALER
- Copyright (C) 2014, 2015, 2016 INRIA
+ Copyright (C) 2014, 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
@@ -33,13 +33,11 @@ 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?
+ * @param cfg the configuration handle
* @return connection to the database; NULL upon error
*/
struct TALER_MERCHANTDB_Plugin *
-TALER_MERCHANTDB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg,
- bool reset_db);
+TALER_MERCHANTDB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg);
/**
diff --git a/src/include/taler_merchantdb_plugin.h b/src/include/taler_merchantdb_plugin.h
index 67c1c5c1..2d64ce43 100644
--- a/src/include/taler_merchantdb_plugin.h
+++ b/src/include/taler_merchantdb_plugin.h
@@ -669,19 +669,15 @@ struct TALER_MERCHANTDB_Plugin
char *library_name;
/**
- * Connect to the database, possibly only to drop the tables. Called first
- * to complete the initialization.
+ * Connect to the database.
*
* @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);
+ (*connect) (void *cls);
/**
- * Drop merchant tables. Used for testcases.
+ * Drop merchant tables. Used for testcases and to reset the DB.
*
* @param cls closure
* @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
@@ -690,6 +686,15 @@ struct TALER_MERCHANTDB_Plugin
(*drop_tables) (void *cls);
/**
+ * Initialize merchant tables
+ *
+ * @param cls closure
+ * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
+ */
+ int
+ (*create_tables) (void *cls);
+
+ /**
* Do a pre-flight check that we are not in an uncommitted transaction.
* If we are, try to commit the previous transaction and output a warning.
* Does not return anything, as we will continue regardless of the outcome.
diff --git a/src/merchant-tools/taler-merchant-dbinit.c b/src/merchant-tools/taler-merchant-dbinit.c
index 938f7d30..73ec3519 100644
--- a/src/merchant-tools/taler-merchant-dbinit.c
+++ b/src/merchant-tools/taler-merchant-dbinit.c
@@ -54,8 +54,7 @@ run (void *cls,
cfg = GNUNET_CONFIGURATION_dup (config);
if (NULL ==
- (plugin = TALER_MERCHANTDB_plugin_load (cfg,
- reset_db)))
+ (plugin = TALER_MERCHANTDB_plugin_load (cfg)))
{
fprintf (stderr,
"Failed to initialize database plugin.\n");
@@ -63,6 +62,22 @@ run (void *cls,
GNUNET_CONFIGURATION_destroy (cfg);
return;
}
+ if (reset_db)
+ {
+ if (GNUNET_OK !=
+ plugin->drop_tables (plugin->cls))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Failed to reset the database\n");
+ }
+ }
+ if (GNUNET_OK !=
+ plugin->create_tables (plugin->cls))
+ {
+ global_ret = 1;
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Failed to initialize tables\n");
+ }
TALER_MERCHANTDB_plugin_unload (plugin);
GNUNET_CONFIGURATION_destroy (cfg);
}