diff options
Diffstat (limited to 'src/syncdb')
-rw-r--r-- | src/syncdb/Makefile.am | 22 | ||||
-rw-r--r-- | src/syncdb/drop.sql (renamed from src/syncdb/drop0001.sql) | 21 | ||||
-rw-r--r-- | src/syncdb/plugin_syncdb_postgres.c | 140 | ||||
-rw-r--r-- | src/syncdb/sync-0001.sql | 20 | ||||
-rw-r--r-- | src/syncdb/sync-dbinit.c | 2 | ||||
-rw-r--r-- | src/syncdb/sync_db_plugin.c | 13 | ||||
-rw-r--r-- | src/syncdb/sync_db_postgres.conf | 4 | ||||
-rw-r--r-- | src/syncdb/test_sync_db.c | 7 | ||||
-rw-r--r-- | src/syncdb/versioning.sql (renamed from src/syncdb/sync-0000.sql) | 3 |
9 files changed, 121 insertions, 111 deletions
diff --git a/src/syncdb/Makefile.am b/src/syncdb/Makefile.am index d71e24e..09b577c 100644 --- a/src/syncdb/Makefile.am +++ b/src/syncdb/Makefile.am @@ -1,6 +1,11 @@ # This Makefile.am is in the public domain AM_CPPFLAGS = -I$(top_srcdir)/src/include +pkgcfgdir = $(prefix)/share/sync/config.d/ + +pkgcfg_DATA = \ + sync_db_postgres.conf + plugindir = $(libdir)/sync if HAVE_POSTGRESQL @@ -18,9 +23,9 @@ endif sqldir = $(prefix)/share/sync/sql/ sql_DATA = \ - sync-0000.sql \ + versioning.sql \ sync-0001.sql \ - drop0001.sql + drop.sql bin_PROGRAMS = \ sync-dbinit @@ -53,14 +58,14 @@ libsyncdb_la_LDFLAGS = \ libsync_plugin_db_postgres_la_SOURCES = \ plugin_syncdb_postgres.c -libsync_plugin_db_postgres_la_LIBADD = \ - $(LTLIBINTL) libsync_plugin_db_postgres_la_LDFLAGS = \ - $(SYNC_PLUGIN_LDFLAGS) \ - -lgnunetpq \ - -lpq \ + $(SYNC_PLUGIN_LDFLAGS) +libsync_plugin_db_postgres_la_LIBADD = \ + $(LTLIBINTL) \ -ltalerpq \ + -lgnunetpq \ -lgnunetutil \ + -lpq \ $(XLIB) check_PROGRAMS = \ @@ -76,8 +81,11 @@ test_sync_db_postgres_LDFLAGS = \ -ltalerutil \ $(XLIB) +AM_TESTS_ENVIRONMENT=export SYNC_PREFIX=$${SYNC_PREFIX:-@libdir@};export PATH=$${SYNC_PREFIX:-@prefix@}/bin:$$PATH; TESTS = \ test_sync_db-postgres EXTRA_DIST = \ + $(pkgcfg_DATA) \ + $(sql_DATA) \ test_sync_db_postgres.conf diff --git a/src/syncdb/drop0001.sql b/src/syncdb/drop.sql index 9253517..aeaa102 100644 --- a/src/syncdb/drop0001.sql +++ b/src/syncdb/drop.sql @@ -1,6 +1,6 @@ -- -- This file is part of TALER --- Copyright (C) 2021 Taler Systems SA +-- Copyright (C) 2021, 2022 Taler Systems SA -- -- TALER is free software; you can redistribute it and/or modify it under the -- terms of the GNU General Public License as published by the Free Software @@ -17,18 +17,15 @@ -- Everything in one big transaction BEGIN; --- This script DROPs all of the tables we create. --- --- Unlike the other SQL files, it SHOULD be updated to reflect the --- latest requirements for dropping tables. - --- Drops for 0001.sql -DROP TABLE IF EXISTS payments CASCADE; -DROP TABLE IF EXISTS backups CASCADE; -DROP TABLE IF EXISTS accounts CASCADE; +WITH xpatches AS ( + SELECT patch_name + FROM _v.patches + WHERE starts_with(patch_name,'sync-') +) + SELECT _v.unregister_patch(xpatches.patch_name) + FROM xpatches; --- Unregister patch (0001.sql) -SELECT _v.unregister_patch('sync-0001'); +DROP SCHEMA sync CASCADE; -- And we're out of here... COMMIT; diff --git a/src/syncdb/plugin_syncdb_postgres.c b/src/syncdb/plugin_syncdb_postgres.c index 8e294cb..d250c82 100644 --- a/src/syncdb/plugin_syncdb_postgres.c +++ b/src/syncdb/plugin_syncdb_postgres.c @@ -1,6 +1,6 @@ /* This file is part of TALER - (C) 2014--2021 Taler Systems SA + (C) 2014--2022 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 @@ -14,7 +14,7 @@ TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ /** - * @file sync/plugin_syncdb_postgres.c + * @file syncdb/plugin_syncdb_postgres.c * @brief database helper functions for postgres used by sync * @author Christian Grothoff */ @@ -78,16 +78,25 @@ postgres_drop_tables (void *cls) { struct PostgresClosure *pg = cls; struct GNUNET_PQ_Context *conn; + enum GNUNET_GenericReturnValue ret; + if (NULL != pg->conn) + { + GNUNET_PQ_disconnect (pg->conn); + pg->conn = NULL; + pg->init = false; + } conn = GNUNET_PQ_connect_with_cfg (pg->cfg, "syncdb-postgres", - "drop", + NULL, NULL, NULL); if (NULL == conn) return GNUNET_SYSERR; + ret = GNUNET_PQ_exec_sql (conn, + "drop"); GNUNET_PQ_disconnect (conn); - return GNUNET_OK; + return ret; } @@ -107,19 +116,16 @@ prepare_statements (void *cls) "(account_pub" ",expiration_date" ") VALUES " - "($1,$2);", - 2), + "($1,$2);"), GNUNET_PQ_make_prepare ("payment_insert", "INSERT INTO payments " "(account_pub" ",order_id" ",token" ",timestamp" - ",amount_val" - ",amount_frac" + ",amount" ") VALUES " - "($1,$2,$3,$4,$5,$6);", - 6), + "($1,$2,$3,$4,$5);"), GNUNET_PQ_make_prepare ("payment_done", "UPDATE payments " "SET" @@ -129,57 +135,48 @@ prepare_statements (void *cls) " AND" " account_pub=$2" " AND" - " paid=FALSE;", - 2), + " paid=FALSE;"), GNUNET_PQ_make_prepare ("account_update", "UPDATE accounts " "SET" " expiration_date=$1 " "WHERE" - " account_pub=$2;", - 2), + " account_pub=$2;"), GNUNET_PQ_make_prepare ("account_select", "SELECT" " expiration_date " "FROM" " accounts " "WHERE" - " account_pub=$1;", - 1), + " account_pub=$1;"), GNUNET_PQ_make_prepare ("payments_select", "SELECT" " account_pub" ",order_id" - ",amount_val" - ",amount_frac" + ",amount" " FROM payments" - " WHERE paid=FALSE;", - 0), + " WHERE paid=FALSE;"), GNUNET_PQ_make_prepare ("payments_select_by_account", "SELECT" " timestamp" ",order_id" ",token" - ",amount_val" - ",amount_frac" + ",amount" " FROM payments" " WHERE" " paid=FALSE" " AND" - " account_pub=$1;", - 1), + " account_pub=$1;"), GNUNET_PQ_make_prepare ("gc_accounts", "DELETE FROM accounts " "WHERE" - " expiration_date < $1;", - 1), + " expiration_date < $1;"), GNUNET_PQ_make_prepare ("gc_pending_payments", "DELETE FROM payments " "WHERE" " paid=FALSE" " AND" - " timestamp < $1;", - 1), + " timestamp < $1;"), GNUNET_PQ_make_prepare ("backup_insert", "INSERT INTO backups " "(account_pub" @@ -188,8 +185,7 @@ prepare_statements (void *cls) ",backup_hash" ",data" ") VALUES " - "($1,$2,$3,$4,$5);", - 5), + "($1,$2,$3,$4,$5);"), GNUNET_PQ_make_prepare ("backup_update", "UPDATE backups " " SET" @@ -200,16 +196,14 @@ prepare_statements (void *cls) " WHERE" " account_pub=$5" " AND" - " backup_hash=$6;", - 6), + " backup_hash=$6;"), GNUNET_PQ_make_prepare ("backup_select_hash", "SELECT " " backup_hash " "FROM" " backups " "WHERE" - " account_pub=$1;", - 1), + " account_pub=$1;"), GNUNET_PQ_make_prepare ("backup_select", "SELECT " " account_sig" @@ -219,11 +213,9 @@ prepare_statements (void *cls) "FROM" " backups " "WHERE" - " account_pub=$1;", - 1), + " account_pub=$1;"), GNUNET_PQ_make_prepare ("do_commit", - "COMMIT", - 0), + "COMMIT"), GNUNET_PQ_PREPARED_STATEMENT_END }; enum GNUNET_GenericReturnValue ret; @@ -264,10 +256,14 @@ internal_setup (struct PostgresClosure *pg, "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE;"), GNUNET_PQ_make_try_execute ("SET enable_sort=OFF;"), GNUNET_PQ_make_try_execute ("SET enable_seqscan=OFF;"), + GNUNET_PQ_make_execute ("SET search_path TO sync;"), GNUNET_PQ_EXECUTE_STATEMENT_END }; #else - struct GNUNET_PQ_ExecuteStatement *es = NULL; + struct GNUNET_PQ_ExecuteStatement es[] = { + GNUNET_PQ_make_execute ("SET search_path TO sync;"), + GNUNET_PQ_EXECUTE_STATEMENT_END + }; #endif struct GNUNET_PQ_Context *db_conn; @@ -314,7 +310,11 @@ postgres_preflight (void *cls) if (GNUNET_OK != internal_setup (pg, false)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Failed to ensure DB is initialized\n"); return GNUNET_SYSERR; + } } if (NULL == pg->transaction_name) return GNUNET_OK; /* all good */ @@ -452,11 +452,11 @@ postgres_gc (void *cls, { struct PostgresClosure *pg = cls; struct GNUNET_PQ_QueryParam params[] = { - TALER_PQ_query_param_absolute_time (&expire_backups), + GNUNET_PQ_query_param_absolute_time (&expire_backups), GNUNET_PQ_query_param_end }; struct GNUNET_PQ_QueryParam params2[] = { - TALER_PQ_query_param_absolute_time (&expire_pending_payments), + GNUNET_PQ_query_param_absolute_time (&expire_pending_payments), GNUNET_PQ_query_param_end }; enum GNUNET_DB_QueryStatus qs; @@ -496,13 +496,14 @@ postgres_store_payment (void *cls, struct PostgresClosure *pg = cls; enum GNUNET_DB_QueryStatus qs; struct TALER_ClaimTokenP tok; - struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); + struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get (); struct GNUNET_PQ_QueryParam params[] = { GNUNET_PQ_query_param_auto_from_type (account_pub), GNUNET_PQ_query_param_string (order_id), GNUNET_PQ_query_param_auto_from_type (&tok), - GNUNET_PQ_query_param_absolute_time (&now), - TALER_PQ_query_param_amount (amount), + GNUNET_PQ_query_param_timestamp (&now), + TALER_PQ_query_param_amount (pg->conn, + amount), GNUNET_PQ_query_param_end }; @@ -570,7 +571,7 @@ struct PaymentIteratorContext * * @param cls closure of type `struct PaymentIteratorContext *` * @param result the postgres result - * @param num_result the number of results in @a result + * @param num_results the number of results in @a result */ static void payment_by_account_cb (void *cls, @@ -581,13 +582,13 @@ payment_by_account_cb (void *cls, for (unsigned int i = 0; i < num_results; i++) { - struct GNUNET_TIME_Absolute timestamp; + struct GNUNET_TIME_Timestamp timestamp; char *order_id; struct TALER_Amount amount; struct TALER_ClaimTokenP token; struct GNUNET_PQ_ResultSpec rs[] = { - GNUNET_PQ_result_spec_absolute_time ("timestamp", - ×tamp), + GNUNET_PQ_result_spec_timestamp ("timestamp", + ×tamp), GNUNET_PQ_result_spec_string ("order_id", &order_id), GNUNET_PQ_result_spec_auto_from_type ("token", @@ -709,7 +710,6 @@ postgres_store_backup (void *cls, GNUNET_break (0); return SYNC_DB_SOFT_ERROR; case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: - GNUNET_break (0); return SYNC_DB_NO_RESULTS; case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: return SYNC_DB_ONE_RESULT; @@ -723,7 +723,7 @@ postgres_store_backup (void *cls, /* First, check if account exists */ { - struct GNUNET_TIME_Absolute ed; + struct GNUNET_TIME_Timestamp ed; struct GNUNET_PQ_QueryParam params[] = { GNUNET_PQ_query_param_auto_from_type (account_pub), GNUNET_PQ_query_param_end @@ -864,7 +864,7 @@ postgres_update_backup (void *cls, /* First, check if account exists */ { - struct GNUNET_TIME_Absolute ed; + struct GNUNET_TIME_Timestamp ed; struct GNUNET_PQ_QueryParam params[] = { GNUNET_PQ_query_param_auto_from_type (account_pub), GNUNET_PQ_query_param_end @@ -953,7 +953,7 @@ postgres_update_backup (void *cls, * * @param cls closure * @param account_pub account to store @a backup under - * @param backup_hash[OUT] set to hash of @a backup + * @param[out] backup_hash set to hash of @a backup * @return transaction status */ static enum SYNC_DB_QueryStatus @@ -1000,7 +1000,7 @@ postgres_lookup_account (void *cls, /* check if account exists */ { - struct GNUNET_TIME_Absolute expiration; + struct GNUNET_TIME_Timestamp expiration; struct GNUNET_PQ_ResultSpec rs[] = { GNUNET_PQ_result_spec_auto_from_type ("expiration_date", &expiration), @@ -1037,11 +1037,11 @@ postgres_lookup_account (void *cls, * * @param cls closure * @param account_pub account to store @a backup under - * @param account_sig[OUT] set to signature affirming storage request - * @param prev_hash[OUT] set to hash of previous @a backup, all zeros if none - * @param backup_hash[OUT] set to hash of @a backup - * @param backup_size[OUT] set to number of bytes in @a backup - * @param backup[OUT] set to raw data to backup, caller MUST FREE + * @param[out] account_sig set to signature affirming storage request + * @param[out] prev_hash set to hash of previous @a backup, all zeros if none + * @param[out] backup_hash set to hash of @a backup + * @param[out] backup_size set to number of bytes in @a backup + * @param[out] backup set to raw data to backup, caller MUST FREE */ static enum SYNC_DB_QueryStatus postgres_lookup_backup (void *cls, @@ -1111,7 +1111,7 @@ postgres_increment_lifetime (void *cls, struct GNUNET_TIME_Relative lifetime) { struct PostgresClosure *pg = cls; - struct GNUNET_TIME_Absolute expiration; + struct GNUNET_TIME_Timestamp expiration; enum GNUNET_DB_QueryStatus qs; check_connection (pg); @@ -1157,8 +1157,8 @@ postgres_increment_lifetime (void *cls, GNUNET_PQ_query_param_end }; struct GNUNET_PQ_ResultSpec rs[] = { - TALER_PQ_result_spec_absolute_time ("expiration_date", - &expiration), + GNUNET_PQ_result_spec_timestamp ("expiration_date", + &expiration), GNUNET_PQ_result_spec_end }; @@ -1180,11 +1180,11 @@ postgres_increment_lifetime (void *cls, { struct GNUNET_PQ_QueryParam params[] = { GNUNET_PQ_query_param_auto_from_type (account_pub), - GNUNET_PQ_query_param_absolute_time (&expiration), + GNUNET_PQ_query_param_timestamp (&expiration), GNUNET_PQ_query_param_end }; - expiration = GNUNET_TIME_relative_to_absolute (lifetime); + expiration = GNUNET_TIME_relative_to_timestamp (lifetime); qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, "account_insert", params); @@ -1193,13 +1193,14 @@ postgres_increment_lifetime (void *cls, case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: { struct GNUNET_PQ_QueryParam params[] = { - GNUNET_PQ_query_param_absolute_time (&expiration), + GNUNET_PQ_query_param_timestamp (&expiration), GNUNET_PQ_query_param_auto_from_type (account_pub), GNUNET_PQ_query_param_end }; - expiration = GNUNET_TIME_absolute_add (expiration, - lifetime); + expiration = GNUNET_TIME_absolute_to_timestamp ( + GNUNET_TIME_absolute_add (expiration.abs_time, + lifetime)); qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, "account_update", params); @@ -1258,11 +1259,15 @@ postgres_create_tables (void *cls) { struct PostgresClosure *pc = cls; struct GNUNET_PQ_Context *conn; + struct GNUNET_PQ_ExecuteStatement es[] = { + GNUNET_PQ_make_execute ("SET search_path TO sync;"), + GNUNET_PQ_EXECUTE_STATEMENT_END + }; conn = GNUNET_PQ_connect_with_cfg (pc->cfg, "syncdb-postgres", "sync-", - NULL, + es, NULL); if (NULL == conn) return GNUNET_SYSERR; @@ -1351,6 +1356,7 @@ libsync_plugin_db_postgres_done (void *cls) struct PostgresClosure *pg = plugin->cls; GNUNET_PQ_disconnect (pg->conn); + GNUNET_free (pg->currency); GNUNET_free (pg->sql_dir); GNUNET_free (pg); GNUNET_free (plugin); diff --git a/src/syncdb/sync-0001.sql b/src/syncdb/sync-0001.sql index a463d72..c487923 100644 --- a/src/syncdb/sync-0001.sql +++ b/src/syncdb/sync-0001.sql @@ -1,6 +1,6 @@ -- -- This file is part of TALER --- Copyright (C) 2021 Taler Systems SA +-- Copyright (C) 2021, 2023 Taler Systems SA -- -- TALER is free software; you can redistribute it and/or modify it under the -- terms of the GNU General Public License as published by the Free Software @@ -20,6 +20,19 @@ BEGIN; -- Check patch versioning is in place. SELECT _v.register_patch('sync-0001', NULL, NULL); +CREATE SCHEMA sync; +COMMENT ON SCHEMA sync IS 'sync data'; + +SET search_path TO sync; + +CREATE TYPE taler_amount + AS + (val INT8 + ,frac INT4 + ); +COMMENT ON TYPE taler_amount + IS 'Stores an amount, fraction is in units of 1/100000000 of the base value'; + CREATE TABLE IF NOT EXISTS accounts (account_pub BYTEA PRIMARY KEY CHECK (length(account_pub)=32) ,expiration_date INT8 NOT NULL); @@ -30,11 +43,10 @@ CREATE INDEX IF NOT EXISTS accounts_expire ON CREATE TABLE IF NOT EXISTS payments (account_pub BYTEA CHECK (length(account_pub)=32) - ,order_id VARCHAR PRIMARY KEY + ,order_id TEXT PRIMARY KEY ,token BYTEA CHECK (length(token)=16) ,timestamp INT8 NOT NULL - ,amount_val INT8 NOT NULL - ,amount_frac INT4 NOT NULL + ,amount taler_amount NOT NULL ,paid BOOLEAN NOT NULL DEFAULT FALSE); CREATE INDEX IF NOT EXISTS payments_timestamp ON diff --git a/src/syncdb/sync-dbinit.c b/src/syncdb/sync-dbinit.c index be7b2ae..d1c9e39 100644 --- a/src/syncdb/sync-dbinit.c +++ b/src/syncdb/sync-dbinit.c @@ -14,7 +14,7 @@ TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ /** - * @file util/sync-dbinit.c + * @file syncdb/sync-dbinit.c * @brief Create tables for the sync database. * @author Christian Grothoff */ diff --git a/src/syncdb/sync_db_plugin.c b/src/syncdb/sync_db_plugin.c index 2c3bd48..6739e4d 100644 --- a/src/syncdb/sync_db_plugin.c +++ b/src/syncdb/sync_db_plugin.c @@ -24,12 +24,6 @@ #include <ltdl.h> -/** - * Initialize the plugin. - * - * @param cfg configuration to use - * @return NULL on failure - */ struct SYNC_DatabasePlugin * SYNC_DB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg) { @@ -62,11 +56,6 @@ SYNC_DB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg) } -/** - * Shutdown the plugin. - * - * @param plugin the plugin to unload - */ void SYNC_DB_plugin_unload (struct SYNC_DatabasePlugin *plugin) { @@ -102,7 +91,7 @@ plugin_init () if (err > 0) { fprintf (stderr, - _ ("Initialization of plugin mechanism failed: %s!\n"), + "Initialization of plugin mechanism failed: %s!\n", lt_dlerror ()); return; } diff --git a/src/syncdb/sync_db_postgres.conf b/src/syncdb/sync_db_postgres.conf index db41bd0..ddf7d06 100644 --- a/src/syncdb/sync_db_postgres.conf +++ b/src/syncdb/sync_db_postgres.conf @@ -1,7 +1,3 @@ -[anastasis] -#The DB plugin to use -DB = postgres - [syncdb-postgres] #The connection string the plugin has to use for connecting to the database CONFIG = postgres:///sync diff --git a/src/syncdb/test_sync_db.c b/src/syncdb/test_sync_db.c index 8bea684..d01941b 100644 --- a/src/syncdb/test_sync_db.c +++ b/src/syncdb/test_sync_db.c @@ -60,7 +60,7 @@ static struct SYNC_DatabasePlugin *plugin; */ static void payment_it (void *cls, - struct GNUNET_TIME_Absolute timestamp, + struct GNUNET_TIME_Timestamp timestamp, const char *order_id, const struct TALER_ClaimTokenP *token, const struct TALER_Amount *amount) @@ -235,7 +235,6 @@ run (void *cls) 4, "DATA")); ts = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS); - (void) GNUNET_TIME_round_abs (&ts); FAILIF (0 > plugin->gc (plugin->cls, ts, @@ -275,7 +274,9 @@ main (int argc, GNUNET_break (0); return EXIT_FAILURE; } - GNUNET_log_setup (argv[0], "DEBUG", NULL); + GNUNET_log_setup (argv[0], + "DEBUG", + NULL); (void) TALER_project_data_default (); GNUNET_OS_init (SYNC_project_data_default ()); plugin_name++; diff --git a/src/syncdb/sync-0000.sql b/src/syncdb/versioning.sql index 116f409..444cf95 100644 --- a/src/syncdb/sync-0000.sql +++ b/src/syncdb/versioning.sql @@ -146,12 +146,13 @@ BEGIN; + -- This file adds versioning support to database it will be loaded to. -- It requires that PL/pgSQL is already loaded - will raise exception otherwise. -- All versioning "stuff" (tables, functions) is in "_v" schema. -- All functions are defined as 'RETURNS SETOF INT4' to be able to make them to RETURN literally nothing (0 rows). --- >> RETURNS VOID<< IS similar, but it still outputs "empty line" in psql when calling. +-- >> RETURNS VOID<< IS similar, but it still outputs "empty line" in psql when calling CREATE SCHEMA IF NOT EXISTS _v; COMMENT ON SCHEMA _v IS 'Schema for versioning data and functionality.'; |