/*
  This file is part of Anastasis
  Copyright (C) 2026 Anastasis SARL

  Anastasis is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  Anastasis 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 Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file stasis/update_auth_iban_in_currency.c
 * @brief Anastasis database: stamp the configured currency onto IBAN
 *        transfers migrated from before amounts carried one
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis-db_pg.h"
#include "anastasis/anastasis-database/update_auth_iban_in_currency.h"
#include "anastasis/anastasis-database/transaction.h"
#include "anastasis/anastasis-database/preflight.h"
#include <taler/taler_pq_lib.h>


/**
 * Fill in the currency of IBAN wire transfers recorded before amounts
 * carried one.
 *
 * The stasis-0004 migration widened the amount columns to include a
 * currency, but SQL cannot know which one: it lives in [anastasis]
 * CURRENCY.  The rows are therefore left with a NULL currency for this
 * function to complete.  Unlike the three payment tables, these rows
 * cannot simply be stamped with a placeholder --- the IBAN plugin
 * compares them against the amount it expects, so a wrong currency would
 * silently invalidate every challenge still in flight across the upgrade.
 *
 * @return transaction status; #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if
 *         there was nothing left to fill in, which is the normal case
 */
enum GNUNET_DB_QueryStatus
ANASTASIS_DB_update_auth_iban_in_currency (void)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_string (pg->currency),
    GNUNET_PQ_query_param_end
  };

  /* Idempotent: the WHERE clause matches nothing once the backfill has
     run, so re-running anastasis-dbinit is free, and a provider that
     changes CURRENCY later does not have its history rewritten. */
  PREPARE ("update_auth_iban_in_currency",
           "UPDATE anastasis_auth_iban_in"
           " SET credit=ROW((credit).val,(credit).frac,$1)::taler_amount_currency"
           " WHERE (credit).curr IS NULL;");
  return GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                             "update_auth_iban_in_currency",
                                             params);
}


/* end of update_auth_iban_in_currency.c */
