gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnunet] branch master updated: make GNUNET_PQ_FLAG_CHECK_CURRENT work n


From: gnunet
Subject: [gnunet] branch master updated: make GNUNET_PQ_FLAG_CHECK_CURRENT work nicely
Date: Sun, 10 Nov 2024 16:57:00 +0100

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository gnunet.

The following commit(s) were added to refs/heads/master by this push:
     new c273d8d03 make GNUNET_PQ_FLAG_CHECK_CURRENT work nicely
c273d8d03 is described below

commit c273d8d0333f4ae22b457dc858d6a7c0c0ea9228
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Sun Nov 10 16:56:42 2024 +0100

    make GNUNET_PQ_FLAG_CHECK_CURRENT work nicely
---
 src/include/gnunet_disk_lib.h |   2 +
 src/include/gnunet_pq_lib.h   | 104 ++++++------
 src/lib/pq/pq.h               |  10 ++
 src/lib/pq/pq_connect.c       | 367 ++++++++++++++++++++++++++----------------
 4 files changed, 290 insertions(+), 193 deletions(-)

diff --git a/src/include/gnunet_disk_lib.h b/src/include/gnunet_disk_lib.h
index 7718a44a7..584f90148 100644
--- a/src/include/gnunet_disk_lib.h
+++ b/src/include/gnunet_disk_lib.h
@@ -276,6 +276,7 @@ GNUNET_DISK_handle_invalid (const struct 
GNUNET_DISK_FileHandle *h);
 enum GNUNET_GenericReturnValue
 GNUNET_DISK_file_test (const char *fil);
 
+
 /**
  * Check that fil corresponds to a filename and the file has read permissions.
  *
@@ -287,6 +288,7 @@ GNUNET_DISK_file_test (const char *fil);
 enum GNUNET_GenericReturnValue
 GNUNET_DISK_file_test_read (const char *fil);
 
+
 /**
  * Move a file out of the way (create a backup) by renaming it to "orig.NUM~"
  * where NUM is the smallest number that is not used yet.
diff --git a/src/include/gnunet_pq_lib.h b/src/include/gnunet_pq_lib.h
index ff05431ac..5ece4920a 100644
--- a/src/include/gnunet_pq_lib.h
+++ b/src/include/gnunet_pq_lib.h
@@ -132,13 +132,13 @@ GNUNET_PQ_cleanup_query_params_closures (
  * End of query parameter specification.
  */
 #define GNUNET_PQ_query_param_end \
-  {                               \
-    .conv = NULL,                 \
-    .conv_cls = NULL,             \
-    .data = NULL,                 \
-    .size = 0,                    \
-    .num_params = 0               \
-  }
+        {                               \
+          .conv = NULL,                 \
+          .conv_cls = NULL,             \
+          .data = NULL,                 \
+          .size = 0,                    \
+          .num_params = 0               \
+        }
 
 
 /**
@@ -302,11 +302,11 @@ GNUNET_PQ_query_param_array_bytes (
  * elements
  * @return query parameter to use
  */
-struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_bytes(
-    unsigned int num,
-    const void *elements[static num],
-    const size_t *sizes,
-    struct GNUNET_PQ_Context *db);
+struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_bytes (
+  unsigned int num,
+  const void *elements[static num],
+  const size_t *sizes,
+  struct GNUNET_PQ_Context *db);
 
 /**
  * Generate query parameter for an array of buffers @a elements,
@@ -335,10 +335,10 @@ GNUNET_PQ_query_param_array_bytes_same_size (
  * @return query parameter to use
  */
 #define GNUNET_PQ_query_param_array_auto_from_type(num, elements, db) \
-  GNUNET_PQ_query_param_array_bytes_same_size ((num), \
-                                               (elements), \
-                                               sizeof(*(elements)), \
-                                               (db))
+        GNUNET_PQ_query_param_array_bytes_same_size ((num), \
+                                                     (elements), \
+                                                     sizeof(*(elements)), \
+                                                     (db))
 
 /**
  * Generate query parameter for an array of pointers to buffers @a elements,
@@ -369,10 +369,12 @@ GNUNET_PQ_query_param_array_ptrs_bytes_same_size (
  * @return query parameter to use
  */
 #define GNUNET_PQ_query_param_array_ptrs_auto_from_type(num, elements, db) \
-  GNUNET_PQ_query_param_array_ptrs_bytes_same_size ((num), \
-                                                    (const void **) 
(elements), \
-                                                    sizeof(*(elements[0])), \
-                                                    (db))
+        GNUNET_PQ_query_param_array_ptrs_bytes_same_size ((num), \
+                                                          (const void **) ( \
+                                                            elements), \
+                                                          
sizeof(*(elements[0])) \
+                                                          , \
+                                                          (db))
 
 
 /**
@@ -413,7 +415,7 @@ GNUNET_PQ_query_param_array_ptrs_string (
  * @return query parameter to use
  */
 #define GNUNET_PQ_query_param_auto_from_type(x) \
-  GNUNET_PQ_query_param_fixed_size ((x), sizeof(*(x)))
+        GNUNET_PQ_query_param_fixed_size ((x), sizeof(*(x)))
 
 /**
  * Generate query parameter for an array of absolute time stamps (continuous)
@@ -755,17 +757,17 @@ struct GNUNET_PQ_ResultSpec
  * @return array last entry for the result specification to use
  */
 #define GNUNET_PQ_result_spec_end         \
-  {                                       \
-    .conv = NULL,                         \
-    .cleaner = NULL,                      \
-    .cls = NULL,                          \
-    .dst = NULL,                          \
-    .dst_size = 0,                        \
-    .fname = NULL,                        \
-    .result_size = NULL,                  \
-    .is_nullable = false,                 \
-    .is_null = NULL                       \
-  }
+        {                                       \
+          .conv = NULL,                         \
+          .cleaner = NULL,                      \
+          .cls = NULL,                          \
+          .dst = NULL,                          \
+          .dst_size = 0,                        \
+          .fname = NULL,                        \
+          .result_size = NULL,                  \
+          .is_nullable = false,                 \
+          .is_null = NULL                       \
+        }
 
 
 /**
@@ -818,7 +820,7 @@ GNUNET_PQ_result_spec_fixed_size (const char *name,
  * @return array entry for the result specification to use
  */
 #define GNUNET_PQ_result_spec_auto_from_type(name, dst) \
-  GNUNET_PQ_result_spec_fixed_size (name, (dst), sizeof(*(dst)))
+        GNUNET_PQ_result_spec_fixed_size (name, (dst), sizeof(*(dst)))
 
 
 /**
@@ -1139,12 +1141,12 @@ GNUNET_PQ_result_spec_array_fixed_size (
  * @return array entry for the result specification to use
  */
 #define GNUNET_PQ_result_spec_auto_array_from_type(db, name, num, dst) \
-  GNUNET_PQ_result_spec_array_fixed_size ( \
-    (db), \
-    (name), \
-    sizeof(*(dst)), \
-    (num), \
-    (void *) &(dst))
+        GNUNET_PQ_result_spec_array_fixed_size ( \
+          (db), \
+          (name), \
+          sizeof(*(dst)), \
+          (num), \
+          (void *) &(dst))
 
 
 /**
@@ -1362,9 +1364,9 @@ struct GNUNET_PQ_PreparedStatement
  * Terminator for prepared statement list.
  */
 #define GNUNET_PQ_PREPARED_STATEMENT_END \
-  {                                      \
-    NULL, NULL                           \
-  }
+        {                                      \
+          NULL, NULL                           \
+        }
 
 
 /**
@@ -1434,9 +1436,9 @@ struct GNUNET_PQ_ExecuteStatement
  * Terminator for executable statement list.
  */
 #define GNUNET_PQ_EXECUTE_STATEMENT_END \
-  {                                     \
-    NULL, GNUNET_SYSERR                 \
-  }
+        {                                     \
+          NULL, GNUNET_SYSERR                 \
+        }
 
 
 /**
@@ -1733,17 +1735,19 @@ GNUNET_PQ_get_event_notify_channel (const struct 
GNUNET_DB_EventHeaderP *es);
 
 
 /**
- * Within the @a db context, run all the SQL files
- * from the @a load_path from 0000-9999.sql (as long
- * as the files exist contiguously).
+ * Within the @a db context, run all the SQL files in the load path
+ * where the name starts with the @a load_suffix and ends with
+ * consecutive numbers from "[0000-9999].sql".
  *
  * @param db database context to use
- * @param load_path where to find the XXXX.sql files
+ * @param load_suffix suffix to append to the load path
+ *         to find the XXXX.sql files, usually "subsystem-".
+ *
  * @return #GNUNET_OK on success
  */
 enum GNUNET_GenericReturnValue
 GNUNET_PQ_run_sql (struct GNUNET_PQ_Context *db,
-                   const char *load_path);
+                   const char *load_suffix);
 
 
 /**
diff --git a/src/lib/pq/pq.h b/src/lib/pq/pq.h
index 65b72cedd..8fd030116 100644
--- a/src/lib/pq/pq.h
+++ b/src/lib/pq/pq.h
@@ -99,6 +99,16 @@ struct GNUNET_PQ_Context
    */
   enum GNUNET_PQ_Options flags;
 
+  /**
+   * Did we prepare the gnunet_pq_check_patch statement?
+   */
+  bool prepared_check_patch;
+
+  /**
+   * Did we prepare the gnunet_pq_get_oid_by_name statement?
+   */
+  bool prepared_get_oid_by_name;
+
   /**
    * Mapping between array types and Oid's, pre-filled at reconnect.
    * More entries are captured in via GNUNET_PQ_get_oid_by_name.
diff --git a/src/lib/pq/pq_connect.c b/src/lib/pq/pq_connect.c
index 4ad88ef53..4bd090e75 100644
--- a/src/lib/pq/pq_connect.c
+++ b/src/lib/pq/pq_connect.c
@@ -28,6 +28,180 @@
 #include <pthread.h>
 
 
+/**
+ * Close connection to @a db and mark it as uninitialized.
+ *
+ * @param[in,out] db connection to close
+ */
+static void
+reset_connection (struct GNUNET_PQ_Context *db)
+{
+  if (NULL == db->conn)
+    return;
+  PQfinish (db->conn);
+  db->conn = NULL;
+  db->prepared_check_patch = false;
+  db->prepared_get_oid_by_name = false;
+}
+
+
+/**
+ * Prepare the "gnunet_pq_check_patch" statement.
+ *
+ * @param[in,out] db database to prepare statement for
+ * @return #GNUNET_OK on success,
+ *         #GNUNET_SYSERR on failure
+ */
+static enum GNUNET_GenericReturnValue
+prepare_check_patch (struct GNUNET_PQ_Context *db)
+{
+  PGresult *res;
+
+  if (db->prepared_check_patch)
+    return GNUNET_OK;
+  res = PQprepare (db->conn,
+                   "gnunet_pq_check_patch",
+                   "SELECT"
+                   " applied_by"
+                   " FROM _v.patches"
+                   " WHERE patch_name = $1"
+                   " LIMIT 1",
+                   1,
+                   NULL);
+  if (PGRES_COMMAND_OK !=
+      PQresultStatus (res))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Failed to run SQL logic to setup database versioning logic: 
%s/%s\n",
+                PQresultErrorMessage (res),
+                PQerrorMessage (db->conn));
+    PQclear (res);
+    reset_connection (db);
+    return GNUNET_SYSERR;
+  }
+  PQclear (res);
+  db->prepared_check_patch = true;
+  return GNUNET_OK;
+}
+
+
+/**
+ * Prepare the "gnunet_pq_get_oid_by_name" statement.
+ *
+ * @param[in,out] db database to prepare statement for
+ * @return #GNUNET_OK on success,
+ *         #GNUNET_SYSERR on failure
+ */
+static enum GNUNET_GenericReturnValue
+prepare_get_oid_by_name (struct GNUNET_PQ_Context *db)
+{
+  PGresult *res;
+
+  if (db->prepared_get_oid_by_name)
+    return GNUNET_OK;
+  res = PQprepare (db->conn,
+                   "gnunet_pq_get_oid_by_name",
+                   "SELECT"
+                   " typname, oid"
+                   " FROM pg_type"
+                   " WHERE typname = $1"
+                   " LIMIT 1",
+                   1,
+                   NULL);
+  if (PGRES_COMMAND_OK != PQresultStatus (res))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Failed to run SQL statement prepare OID lookups: %s/%s\n",
+                PQresultErrorMessage (res),
+                PQerrorMessage (db->conn));
+    PQclear (res);
+    reset_connection (db);
+    return GNUNET_SYSERR;
+  }
+  PQclear (res);
+  db->prepared_get_oid_by_name = true;
+  return GNUNET_OK;
+}
+
+
+/**
+ * Check if the patch with @a patch_number from the given
+ * @a load_path was already applied on the @a db.
+ *
+ * @param[in] db database to check
+ * @param load_path file system path to database setup files
+ * @param patch_number number of the patch to check
+ * @return #GNUNET_OK if patch is applied
+ *         #GNUNET_NO if patch is not applied
+ *         #GNUNET_SYSERR on internal error (DB failure)
+ */
+static enum GNUNET_GenericReturnValue
+check_patch_applied (struct GNUNET_PQ_Context *db,
+                     const char *load_path,
+                     unsigned int patch_number)
+{
+  const char *load_path_suffix;
+  size_t slen = strlen (load_path) + 10;
+  char patch_name[slen];
+
+  load_path_suffix = strrchr (load_path,
+                              '/');
+  if (NULL == load_path_suffix)
+    load_path_suffix = load_path;
+  else
+    load_path_suffix++; /* skip '/' */
+  GNUNET_snprintf (patch_name,
+                   sizeof (patch_name),
+                   "%s%04u",
+                   load_path_suffix,
+                   patch_number);
+  {
+    struct GNUNET_PQ_QueryParam params[] = {
+      GNUNET_PQ_query_param_string (patch_name),
+      GNUNET_PQ_query_param_end
+    };
+    char *applied_by;
+    struct GNUNET_PQ_ResultSpec rs[] = {
+      GNUNET_PQ_result_spec_string ("applied_by",
+                                    &applied_by),
+      GNUNET_PQ_result_spec_end
+    };
+    enum GNUNET_DB_QueryStatus qs;
+
+    if (GNUNET_OK !=
+        prepare_check_patch (db))
+    {
+      GNUNET_break (0);
+      return GNUNET_SYSERR;
+    }
+    qs = GNUNET_PQ_eval_prepared_singleton_select (db,
+                                                   "gnunet_pq_check_patch",
+                                                   params,
+                                                   rs);
+    switch (qs)
+    {
+    case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Database version %s already applied by %s\n",
+                  patch_name,
+                  applied_by);
+      GNUNET_PQ_cleanup_result (rs);
+      return GNUNET_OK;
+    case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
+      return GNUNET_NO;
+    case GNUNET_DB_STATUS_SOFT_ERROR:
+      GNUNET_break (0);
+      return GNUNET_SYSERR;
+    case GNUNET_DB_STATUS_HARD_ERROR:
+      GNUNET_break (0);
+      return GNUNET_SYSERR;
+    }
+    GNUNET_assert (0);
+    return GNUNET_SYSERR;
+  }
+}
+
+
 /**
  * Function called by libpq whenever it wants to log something.
  * We already log whenever we care, so this function does nothing
@@ -155,7 +329,7 @@ GNUNET_PQ_exec_sql (struct GNUNET_PQ_Context *db,
                    db->load_path,
                    buf);
   if (GNUNET_YES !=
-      GNUNET_DISK_file_test (fn))
+      GNUNET_DISK_file_test_read (fn))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                 "SQL resource `%s' does not exist\n",
@@ -163,6 +337,8 @@ GNUNET_PQ_exec_sql (struct GNUNET_PQ_Context *db,
     GNUNET_free (fn);
     return GNUNET_NO;
   }
+  if (0 != (GNUNET_PQ_FLAG_CHECK_CURRENT & db->flags))
+    return GNUNET_SYSERR;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Applying SQL file `%s' on database %s\n",
               fn,
@@ -222,89 +398,48 @@ GNUNET_PQ_exec_sql (struct GNUNET_PQ_Context *db,
 
 enum GNUNET_GenericReturnValue
 GNUNET_PQ_run_sql (struct GNUNET_PQ_Context *db,
-                   const char *load_path)
+                   const char *load_suffix)
 {
-  const char *load_path_suffix;
-  size_t slen = strlen (load_path) + 10;
+  size_t slen = strlen (load_suffix) + 10;
+  char patch_name[slen];
 
-  load_path_suffix = strrchr (load_path, '/');
-  if (NULL == load_path_suffix)
-    load_path_suffix = load_path;
-  else
-    load_path_suffix++; /* skip '/' */
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Loading SQL resources from `%s'\n",
-              load_path);
+              load_suffix);
   for (unsigned int i = 1; i<10000; i++)
   {
-    char patch_name[slen];
-    enum GNUNET_DB_QueryStatus qs;
+    enum GNUNET_GenericReturnValue ret;
 
-    /* Check with DB versioning schema if this patch was already applied,
-       if so, skip it. */
-    GNUNET_snprintf (patch_name,
-                     sizeof (patch_name),
-                     "%s%04u",
-                     load_path_suffix,
-                     i);
+    ret = check_patch_applied (db,
+                               load_suffix,
+                               i);
+    if (GNUNET_SYSERR == ret)
     {
-      char *applied_by;
-      struct GNUNET_PQ_QueryParam params[] = {
-        GNUNET_PQ_query_param_string (patch_name),
-        GNUNET_PQ_query_param_end
-      };
-      struct GNUNET_PQ_ResultSpec rs[] = {
-        GNUNET_PQ_result_spec_string ("applied_by",
-                                      &applied_by),
-        GNUNET_PQ_result_spec_end
-      };
-
-      qs = GNUNET_PQ_eval_prepared_singleton_select (db,
-                                                     "gnunet_pq_check_patch",
-                                                     params,
-                                                     rs);
-      if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
-      {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "Database version %s already applied by %s, skipping\n",
-                    patch_name,
-                    applied_by);
-        GNUNET_PQ_cleanup_result (rs);
-      }
-      if (GNUNET_DB_STATUS_HARD_ERROR == qs)
-      {
-        GNUNET_break (0);
-        return GNUNET_SYSERR;
-      }
+      GNUNET_break (0);
+      return GNUNET_SYSERR;
     }
-    if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
+    if (GNUNET_OK  == ret)
       continue; /* patch already applied, skip it */
 
-    if (0 != (GNUNET_PQ_FLAG_CHECK_CURRENT & db->flags))
+    GNUNET_snprintf (patch_name,
+                     sizeof (patch_name),
+                     "%s%04u",
+                     load_suffix,
+                     i);
+    ret = GNUNET_PQ_exec_sql (db,
+                              patch_name);
+    if (GNUNET_NO == ret)
+      break;
+    if ( (GNUNET_SYSERR == ret) &&
+         (0 != (GNUNET_PQ_FLAG_CHECK_CURRENT & db->flags)) )
     {
       /* We are only checking, found unapplied patch, bad! */
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                   "Database outdated, patch %s missing. Aborting!\n",
                   patch_name);
-      return GNUNET_SYSERR;
-    }
-    else
-    {
-      /* patch not yet applied, run it! */
-      enum GNUNET_GenericReturnValue ret;
-
-      GNUNET_snprintf (patch_name,
-                       sizeof (patch_name),
-                       "%s%04u",
-                       load_path,
-                       i);
-      ret = GNUNET_PQ_exec_sql (db,
-                                patch_name);
-      if (GNUNET_NO == ret)
-        break;
-      if (GNUNET_SYSERR == ret)
-        return GNUNET_SYSERR;
     }
+    if (GNUNET_SYSERR == ret)
+      return GNUNET_SYSERR;
   }
   return GNUNET_OK;
 }
@@ -389,10 +524,10 @@ GNUNET_PQ_get_oid_by_name (
  * array-datatypes
  *
  * @param db The database context
- * @return GNUNET_OK on success, GNUNET_SYSERR if any of the types couldn't be 
found
+ * @return #GNUNET_OK on success,
+ *         #GNUNET_SYSERR if any of the types couldn't be found
  */
-static
-enum GNUNET_GenericReturnValue
+static enum GNUNET_GenericReturnValue
 load_initial_oids (struct GNUNET_PQ_Context *db)
 {
   static const char *typnames[] = {
@@ -428,8 +563,7 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
 {
   GNUNET_PQ_event_reconnect_ (db,
                               -1);
-  if (NULL != db->conn)
-    PQfinish (db->conn);
+  reset_connection (db);
   db->conn = PQconnectdb (db->config_str);
   if ( (NULL == db->conn) ||
        (CONNECTION_OK != PQstatus (db->conn)) )
@@ -441,11 +575,7 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
                      (NULL != db->conn)
                      ? PQerrorMessage (db->conn)
                      : "PQconnectdb returned NULL");
-    if (NULL != db->conn)
-    {
-      PQfinish (db->conn);
-      db->conn = NULL;
-    }
+    reset_connection (db);
     return;
   }
   PQsetNoticeReceiver (db->conn,
@@ -472,8 +602,7 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                   "Failed to run statement to check versioning schema. 
Bad!\n");
       PQclear (res);
-      PQfinish (db->conn);
-      db->conn = NULL;
+      reset_connection (db);
       return;
     }
     if (0 == PQntuples (res))
@@ -485,8 +614,7 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
       {
         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                     "Versioning schema does not exist yet. Not attempting 
drop!\n");
-        PQfinish (db->conn);
-        db->conn = NULL;
+        reset_connection (db);
         return;
       }
       ret = GNUNET_PQ_exec_sql (db,
@@ -495,16 +623,14 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
       {
         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                     "Failed to find SQL file to load database versioning 
logic\n");
-        PQfinish (db->conn);
-        db->conn = NULL;
+        reset_connection (db);
         return;
       }
       if (GNUNET_SYSERR == ret)
       {
         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                     "Failed to run SQL logic to setup database versioning 
logic\n");
-        PQfinish (db->conn);
-        db->conn = NULL;
+        reset_connection (db);
         return;
       }
     }
@@ -515,31 +641,9 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
   }
 
   /* Prepare statement for OID lookup by name */
-  {
-    PGresult *res;
-
-    res = PQprepare (db->conn,
-                     "gnunet_pq_get_oid_by_name",
-                     "SELECT"
-                     " typname, oid"
-                     " FROM pg_type"
-                     " WHERE typname = $1"
-                     " LIMIT 1",
-                     1,
-                     NULL);
-    if (PGRES_COMMAND_OK != PQresultStatus (res))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  "Failed to run SQL statement prepare OID lookups: %s/%s\n",
-                  PQresultErrorMessage (res),
-                  PQerrorMessage (db->conn));
-      PQclear (res);
-      PQfinish (db->conn);
-      db->conn = NULL;
-      return;
-    }
-    PQclear (res);
-  }
+  if (GNUNET_OK !=
+      prepare_get_oid_by_name (db))
+    return;
 
   /* Reset the OID-cache and retrieve the OIDs for the supported Array types */
   db->oids.num = 0;
@@ -547,47 +651,26 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Failed to retrieve OID information for array types!\n");
-    PQfinish (db->conn);
-    db->conn = NULL;
+    reset_connection (db);
     return;
   }
 
   if (NULL != db->auto_suffix)
   {
-    PGresult *res;
-
     GNUNET_assert (NULL != db->load_path);
-    res = PQprepare (db->conn,
-                     "gnunet_pq_check_patch",
-                     "SELECT"
-                     " applied_by"
-                     " FROM _v.patches"
-                     " WHERE patch_name = $1"
-                     " LIMIT 1",
-                     1,
-                     NULL);
-    if (PGRES_COMMAND_OK != PQresultStatus (res))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  "Failed to run SQL logic to setup database versioning logic: 
%s/%s\n",
-                  PQresultErrorMessage (res),
-                  PQerrorMessage (db->conn));
-      PQclear (res);
-      PQfinish (db->conn);
-      db->conn = NULL;
+    if (GNUNET_OK !=
+        prepare_check_patch (db))
       return;
-    }
-    PQclear (res);
 
     if (GNUNET_SYSERR ==
         GNUNET_PQ_run_sql (db,
                            db->auto_suffix))
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                  "Failed to load SQL statements from `%s*'\n",
-                  db->auto_suffix);
-      PQfinish (db->conn);
-      db->conn = NULL;
+      if (0 == (GNUNET_PQ_FLAG_CHECK_CURRENT & db->flags))
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                    "Failed to load SQL statements from `%s*'\n",
+                    db->auto_suffix);
+      reset_connection (db);
       return;
     }
   }
@@ -597,8 +680,7 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
         GNUNET_PQ_exec_statements (db,
                                    db->es)) )
   {
-    PQfinish (db->conn);
-    db->conn = NULL;
+    reset_connection (db);
     return;
   }
   if ( (NULL != db->ps) &&
@@ -606,8 +688,7 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
         GNUNET_PQ_prepare_statements (db,
                                       db->ps)) )
   {
-    PQfinish (db->conn);
-    db->conn = NULL;
+    reset_connection (db);
     return;
   }
   GNUNET_PQ_event_reconnect_ (db,

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]