LCOV - code coverage report
Current view: top level - merchant-tools - taler-merchant-dbinit.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 53.5 % 43 23
Test Date: 2026-09-11 17:45:24 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2014, 2015, 2020 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/merchant-tools/taler-merchant-dbinit.c
      18              :  * @brief Create tables for the merchant database.
      19              :  * @author Florian Dold
      20              :  * @author Marcello Stanisci
      21              :  */
      22              : #include "platform.h"
      23              : #include <taler/taler_util.h>
      24              : #include <gnunet/gnunet_util_lib.h>
      25              : #include "taler/taler_merchant_util.h"
      26              : #include "merchantdb_lib.h"
      27              : #include "merchant-database/create_tables.h"
      28              : #include "merchant-database/drop_tables.h"
      29              : #include "merchant-database/gc.h"
      30              : 
      31              : 
      32              : /**
      33              :  * Return value from main().
      34              :  */
      35              : static int global_ret;
      36              : 
      37              : /**
      38              :  * -r option: do full DB reset
      39              :  */
      40              : static int reset_db;
      41              : 
      42              : /**
      43              :  * -g option: garbage collect DB
      44              :  */
      45              : static int gc_db;
      46              : 
      47              : /**
      48              :  * Main function that will be run.
      49              :  *
      50              :  * @param cls closure
      51              :  * @param args remaining command-line arguments
      52              :  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
      53              :  * @param cfg configuration
      54              :  */
      55              : static void
      56           19 : run (void *cls,
      57              :      char *const *args,
      58              :      const char *cfgfile,
      59              :      const struct GNUNET_CONFIGURATION_Handle *cfg)
      60              : {
      61              :   struct TALER_MERCHANTDB_PostgresContext *pg;
      62              : 
      63              :   /* Reset retains precedence over GC.  Scheduled GC must never migrate. */
      64           19 :   if (reset_db)
      65           18 :     gc_db = 0;
      66           19 :   pg = gc_db
      67            0 :        ? TALER_MERCHANTDB_connect (cfg)
      68           19 :        : TALER_MERCHANTDB_connect_admin (cfg);
      69           19 :   if (NULL == pg)
      70              :   {
      71            0 :     fprintf (stderr,
      72              :              "Failed to initialize database connection.\n");
      73            0 :     global_ret = 1;
      74            0 :     return;
      75              :   }
      76           19 :   if (reset_db)
      77              :   {
      78           18 :     if (GNUNET_OK !=
      79           18 :         TALER_MERCHANTDB_drop_tables (pg))
      80              :     {
      81            0 :       global_ret = 1;
      82            0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      83              :                   "Failed to reset the database\n");
      84            0 :       TALER_MERCHANTDB_disconnect (pg);
      85            0 :       return;
      86              :     }
      87           18 :     gc_db = 0; /* not useful */
      88              :   }
      89           19 :   if (gc_db)
      90              :   {
      91            0 :     if (GNUNET_OK !=
      92            0 :         TALER_MERCHANTDB_gc (pg))
      93              :     {
      94            0 :       global_ret = 1;
      95            0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      96              :                   "Failed to garbage collect database\n");
      97              :     }
      98            0 :     TALER_MERCHANTDB_disconnect (pg);
      99            0 :     return;
     100              :   }
     101           19 :   if (GNUNET_OK !=
     102           19 :       TALER_MERCHANTDB_create_tables (pg))
     103              :   {
     104            0 :     global_ret = 1;
     105            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     106              :                 "Failed to initialize tables\n");
     107            0 :     TALER_MERCHANTDB_disconnect (pg);
     108            0 :     return;
     109              :   }
     110           19 :   TALER_MERCHANTDB_disconnect (pg);
     111              : }
     112              : 
     113              : 
     114              : /**
     115              :  * The main function of the database initialization tool.
     116              :  * Used to initialize the Taler Exchange's database.
     117              :  *
     118              :  * @param argc number of arguments from the command line
     119              :  * @param argv command line arguments
     120              :  * @return 0 ok, 1 on error
     121              :  */
     122              : int
     123           19 : main (int argc,
     124              :       char *const *argv)
     125              : {
     126           19 :   struct GNUNET_GETOPT_CommandLineOption options[] = {
     127           19 :     GNUNET_GETOPT_option_flag ('g',
     128              :                                "gc",
     129              :                                "garbage collect database",
     130              :                                &gc_db),
     131           19 :     GNUNET_GETOPT_option_flag ('r',
     132              :                                "reset",
     133              :                                "reset database (DANGEROUS: all existing data is lost!)",
     134              :                                &reset_db),
     135              : 
     136           19 :     GNUNET_GETOPT_option_version (PACKAGE_VERSION),
     137              :     GNUNET_GETOPT_OPTION_END
     138              :   };
     139              :   enum GNUNET_GenericReturnValue ret;
     140              : 
     141           19 :   ret = GNUNET_PROGRAM_run (
     142              :     TALER_MERCHANT_project_data (),
     143              :     argc, argv,
     144              :     "taler-merchant-dbinit",
     145              :     gettext_noop ("Initialize Taler merchant database"),
     146              :     options,
     147              :     &run, NULL);
     148           19 :   if (GNUNET_SYSERR == ret)
     149            0 :     return 3;
     150           19 :   if (GNUNET_NO == ret)
     151            0 :     return 0;
     152           19 :   return global_ret;
     153              : }
     154              : 
     155              : 
     156              : /* end of taler-merchant-dbinit.c */
        

Generated by: LCOV version 2.0-1