/*
  This file is part of TALER
  (C) 2014--2025 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
  Foundation; either version 3, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file src/backenddb/create_tables.c
 * @brief implementation of the create_tables function
 * @author Christian Grothoff
 */
#include <gnunet/gnunet_util_lib.h>
#include "merchantdb_lib.h"
#include "merchant-database/create_tables.h"
#include "helper.h"


enum GNUNET_GenericReturnValue
TALER_MERCHANTDB_create_tables (
  struct TALER_MERCHANTDB_PostgresContext *pg)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_end
  };

  if (GNUNET_SYSERR ==
      GNUNET_PQ_load_versioning (pg->conn))
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  if (GNUNET_OK !=
      GNUNET_PQ_run_sql (pg->conn,
                         "merchant-"))
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  if (GNUNET_OK !=
      GNUNET_PQ_exec_sql (pg->conn,
                          "global_procedures"))
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  if (GNUNET_OK !=
      GNUNET_PQ_exec_sql (pg->conn,
                          "instance_procedures"))
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }

  if (GNUNET_OK !=
      GNUNET_PQ_prepare_anon (
        pg->conn,
        "CALL merchant.sync_all_instance_procedures()"))
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  if (0 >
      GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                          "",
                                          params))
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  return GNUNET_OK;
}
