preflight.c (2840B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2026 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file src/auditordb/preflight.c 18 * @brief Implementation of the preflight function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "auditor-database/preflight.h" 23 #include "pg_helper.h" 24 25 26 /** 27 * Connect to the db if the connection does not exist yet. 28 * 29 * @param[in,out] pg the plugin-specific state 30 * @return #GNUNET_OK on success 31 */ 32 static enum GNUNET_GenericReturnValue 33 setup_connection (struct TALER_AUDITORDB_PostgresContext *pg) 34 { 35 struct GNUNET_PQ_ExecuteStatement es[] = { 36 GNUNET_PQ_make_try_execute ("SET search_path TO auditor;"), 37 GNUNET_PQ_EXECUTE_STATEMENT_END 38 }; 39 struct GNUNET_PQ_Context *db_conn; 40 41 if (NULL != pg->conn) 42 { 43 GNUNET_PQ_reconnect_if_down (pg->conn); 44 return GNUNET_OK; 45 } 46 db_conn = GNUNET_PQ_connect_with_cfg2 (pg->cfg, 47 "auditordb-postgres", 48 "auditor-", 49 es, 50 NULL, /* prepared statements */ 51 GNUNET_PQ_FLAG_CHECK_CURRENT); 52 if (NULL == db_conn) 53 return GNUNET_SYSERR; 54 pg->conn = db_conn; 55 pg->prep_gen++; 56 return GNUNET_OK; 57 } 58 59 60 enum GNUNET_GenericReturnValue 61 TALER_AUDITORDB_preflight (struct TALER_AUDITORDB_PostgresContext *pg) 62 { 63 struct GNUNET_PQ_ExecuteStatement es[] = { 64 GNUNET_PQ_make_execute ("ROLLBACK"), 65 GNUNET_PQ_EXECUTE_STATEMENT_END 66 }; 67 68 if ( (NULL == pg->conn) && 69 (GNUNET_OK != 70 setup_connection (pg)) ) 71 return GNUNET_SYSERR; 72 if (NULL == pg->transaction_name) 73 return GNUNET_OK; /* all good */ 74 if (GNUNET_OK == 75 GNUNET_PQ_exec_statements (pg->conn, 76 es)) 77 { 78 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 79 "BUG: Preflight check rolled back transaction `%s'!\n", 80 pg->transaction_name); 81 } 82 else 83 { 84 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 85 "BUG: Preflight check failed to rollback transaction `%s'!\n", 86 pg->transaction_name); 87 } 88 pg->transaction_name = NULL; 89 return GNUNET_NO; 90 }