taler-merchant-dbinit.c (3946B)
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 config configuration 54 */ 55 static void 56 run (void *cls, 57 char *const *args, 58 const char *cfgfile, 59 const struct GNUNET_CONFIGURATION_Handle *config) 60 { 61 struct TALER_MERCHANTDB_PostgresContext *pg; 62 struct GNUNET_CONFIGURATION_Handle *cfg; 63 64 cfg = GNUNET_CONFIGURATION_dup (config); 65 if (NULL == 66 (pg = TALER_MERCHANTDB_connect (cfg))) 67 { 68 fprintf (stderr, 69 "Failed to initialize database plugin.\n"); 70 global_ret = 1; 71 GNUNET_CONFIGURATION_destroy (cfg); 72 return; 73 } 74 if (reset_db) 75 { 76 if (GNUNET_OK != 77 TALER_MERCHANTDB_drop_tables (cfg)) 78 { 79 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 80 "Failed to reset the database\n"); 81 } 82 } 83 if (gc_db) 84 { 85 if (GNUNET_OK != 86 TALER_MERCHANTDB_gc (pg)) 87 { 88 global_ret = 1; 89 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 90 "Failed to garbage collect database\n"); 91 } 92 } 93 else 94 { 95 if (GNUNET_OK != 96 TALER_MERCHANTDB_create_tables (cfg)) 97 { 98 global_ret = 1; 99 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 100 "Failed to initialize tables\n"); 101 } 102 } 103 TALER_MERCHANTDB_disconnect (pg); 104 GNUNET_CONFIGURATION_destroy (cfg); 105 } 106 107 108 /** 109 * The main function of the database initialization tool. 110 * Used to initialize the Taler Exchange's database. 111 * 112 * @param argc number of arguments from the command line 113 * @param argv command line arguments 114 * @return 0 ok, 1 on error 115 */ 116 int 117 main (int argc, 118 char *const *argv) 119 { 120 struct GNUNET_GETOPT_CommandLineOption options[] = { 121 GNUNET_GETOPT_option_flag ('g', 122 "gc", 123 "garbage collect database", 124 &gc_db), 125 GNUNET_GETOPT_option_flag ('r', 126 "reset", 127 "reset database (DANGEROUS: all existing data is lost!)", 128 &reset_db), 129 130 GNUNET_GETOPT_option_version (PACKAGE_VERSION "-" VCS_VERSION), 131 GNUNET_GETOPT_OPTION_END 132 }; 133 enum GNUNET_GenericReturnValue ret; 134 135 ret = GNUNET_PROGRAM_run ( 136 TALER_MERCHANT_project_data (), 137 argc, argv, 138 "taler-merchant-dbinit", 139 gettext_noop ("Initialize Taler merchant database"), 140 options, 141 &run, NULL); 142 if (GNUNET_SYSERR == ret) 143 return 3; 144 if (GNUNET_NO == ret) 145 return 0; 146 return global_ret; 147 } 148 149 150 /* end of taler-merchant-dbinit.c */