gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-anastasis] branch master updated: move SQL table creation/destruc


From: gnunet
Subject: [taler-anastasis] branch master updated: move SQL table creation/destruction into resource files and enable GNUnet-style SQL versioning
Date: Fri, 06 Nov 2020 14:25:41 +0100

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository anastasis.

The following commit(s) were added to refs/heads/master by this push:
     new f7e2529  move SQL table creation/destruction into resource files and 
enable GNUnet-style SQL versioning
f7e2529 is described below

commit f7e2529dd831360d44c771ec4d5cb85b91f6cab3
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Fri Nov 6 14:25:39 2020 +0100

    move SQL table creation/destruction into resource files and enable 
GNUnet-style SQL versioning
---
 src/stasis/Makefile.am                     |  16 +-
 src/stasis/anastasis_db_plugin.c           |   6 +-
 src/stasis/drop0001.sql                    |  37 ++++
 src/stasis/plugin_anastasis_postgres.c     |  87 ++-------
 src/stasis/stasis-0000.sql                 | 293 +++++++++++++++++++++++++++++
 src/stasis/stasis-0001.sql                 |  87 +++++++++
 src/stasis/stasis-postgres.conf            |   6 +
 src/stasis/test_anastasis_db.c             |  13 +-
 src/stasis/test_anastasis_db_postgres.conf |   2 +-
 9 files changed, 464 insertions(+), 83 deletions(-)

diff --git a/src/stasis/Makefile.am b/src/stasis/Makefile.am
index e806c98..39950a6 100644
--- a/src/stasis/Makefile.am
+++ b/src/stasis/Makefile.am
@@ -15,6 +15,18 @@ if USE_COVERAGE
   XLIB = -lgcov
 endif
 
+sqldir = $(prefix)/share/anastasis/sql/
+
+sql_DATA = \
+  stasis-0000.sql \
+  stasis-0001.sql \
+  drop0001.sql
+
+pkgcfgdir = $(prefix)/share/anastasis/config.d/
+
+pkgcfg_DATA = \
+  stasis-postgres.conf
+
 bin_PROGRAMS = \
   anastasis-dbinit
 
@@ -69,8 +81,10 @@ test_anastasis_db_postgres_LDFLAGS = \
   -ltalerpq \
   -luuid
 
+AM_TESTS_ENVIRONMENT=export 
ANASTASIS_PREFIX=$${ANASTASIS_PREFIX:-@libdir@};export 
PATH=$${ANASTASIS_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset 
XDG_CONFIG_HOME;
 TESTS = \
   test_anastasis_db-postgres
 
 EXTRA_DIST = \
-  test_anastasis_db_postgres.conf
+  test_anastasis_db_postgres.conf \
+  $(sql_DATA)
diff --git a/src/stasis/anastasis_db_plugin.c b/src/stasis/anastasis_db_plugin.c
index 2da2037..e45d3c1 100644
--- a/src/stasis/anastasis_db_plugin.c
+++ b/src/stasis/anastasis_db_plugin.c
@@ -36,7 +36,6 @@ ANASTASIS_DB_plugin_load (const struct 
GNUNET_CONFIGURATION_Handle *cfg)
 {
   char *plugin_name;
   char *lib_name;
-  struct GNUNET_CONFIGURATION_Handle *cfg_dup;
   struct ANASTASIS_DatabasePlugin *plugin;
 
   if (GNUNET_SYSERR ==
@@ -54,13 +53,12 @@ ANASTASIS_DB_plugin_load (const struct 
GNUNET_CONFIGURATION_Handle *cfg)
                           "libanastasis_plugin_db_%s",
                           plugin_name);
   GNUNET_free (plugin_name);
-  cfg_dup = GNUNET_CONFIGURATION_dup (cfg);
-  plugin = GNUNET_PLUGIN_load (lib_name, cfg_dup);
+  plugin = GNUNET_PLUGIN_load (lib_name,
+                               (void *) cfg);
   if (NULL != plugin)
     plugin->library_name = lib_name;
   else
     lib_name = NULL;
-  GNUNET_CONFIGURATION_destroy (cfg_dup);
   return plugin;
 }
 
diff --git a/src/stasis/drop0001.sql b/src/stasis/drop0001.sql
new file mode 100644
index 0000000..afc457d
--- /dev/null
+++ b/src/stasis/drop0001.sql
@@ -0,0 +1,37 @@
+--
+-- This file is part of ANASTASIS
+-- Copyright (C) 2014--2020 Anastasis Systems SA
+--
+-- ANASTASIS 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
+-- 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 General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License along with
+-- ANASTASIS; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+--
+
+-- 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 anastasis_truth CASCADE;
+DROP TABLE IF EXISTS anastasis_user CASCADE;
+DROP TABLE IF EXISTS anastasis_recdoc_payment;
+DROP TABLE IF EXISTS anastasis_recoverydocument;
+DROP TABLE IF EXISTS anastasis_challengecode;
+DROP TABLE IF EXISTS anastasis_challenge_payment;
+
+-- Unregister patch (0001.sql)
+SELECT _v.unregister_patch('stasis-0001');
+
+-- And we're out of here...
+COMMIT;
diff --git a/src/stasis/plugin_anastasis_postgres.c 
b/src/stasis/plugin_anastasis_postgres.c
index dc9093c..f9f4eed 100644
--- a/src/stasis/plugin_anastasis_postgres.c
+++ b/src/stasis/plugin_anastasis_postgres.c
@@ -74,22 +74,17 @@ static int
 postgres_drop_tables (void *cls)
 {
   struct PostgresClosure *pg = cls;
-  struct GNUNET_PQ_ExecuteStatement es[] = {
-    GNUNET_PQ_make_try_execute (
-      "DROP TABLE IF EXISTS anastasis_truth CASCADE;"),
-    GNUNET_PQ_make_try_execute ("DROP TABLE IF EXISTS anastasis_user 
CASCADE;"),
-    GNUNET_PQ_make_try_execute (
-      "DROP TABLE IF EXISTS anastasis_recdoc_payment;"),
-    GNUNET_PQ_make_try_execute (
-      "DROP TABLE IF EXISTS anastasis_recoverydocument;"),
-    GNUNET_PQ_make_try_execute (
-      "DROP TABLE IF EXISTS anastasis_challengecode;"),
-    GNUNET_PQ_make_try_execute (
-      "DROP TABLE IF EXISTS anastasis_challenge_payment;"),
-    GNUNET_PQ_EXECUTE_STATEMENT_END
-  };
-  return GNUNET_PQ_exec_statements (pg->conn,
-                                    es);
+  struct GNUNET_PQ_Context *conn;
+
+  conn = GNUNET_PQ_connect_with_cfg (pg->cfg,
+                                     "stasis-postgres",
+                                     "drop",
+                                     NULL,
+                                     NULL);
+  if (NULL == conn)
+    return GNUNET_SYSERR;
+  GNUNET_PQ_disconnect (conn);
+  return GNUNET_OK;
 }
 
 
@@ -1954,62 +1949,6 @@ libanastasis_plugin_db_postgres_init (void *cls)
   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
   struct PostgresClosure *pg;
   struct ANASTASIS_DatabasePlugin *plugin;
-  struct GNUNET_PQ_ExecuteStatement es[] = {
-    /* Orders created by the frontend, not signed or given a nonce yet.
-       The contract terms will change (nonce will be added) when moved to the
-       contract terms table */
-    GNUNET_PQ_make_execute ("CREATE TABLE IF NOT EXISTS anastasis_truth"
-                            "(truth_public_key BYTEA PRIMARY KEY NOT NULL,"
-                            " key_share_data BYTEA NOT NULL,"
-                            " method VARCHAR,"
-                            " encrypted_truth BYTEA NOT NULL,"
-                            " truth_mime VARCHAR,"
-                            " expiration TIMESTAMP NOT NULL"
-                            ");"),
-    GNUNET_PQ_make_execute ("CREATE TABLE IF NOT EXISTS anastasis_user"
-                            "( user_id BYTEA PRIMARY KEY 
CHECK(LENGTH(user_id)=32),"
-                            "  expiration_date TIMESTAMP NOT NULL"
-                            ");"),
-    GNUNET_PQ_make_execute (
-      "CREATE TABLE IF NOT EXISTS anastasis_recdoc_payment"
-      "(payment_id BIGSERIAL PRIMARY KEY,"
-      " user_id BYTEA NOT NULL REFERENCES anastasis_user(user_id),"
-      " post_counter INT4 NOT NULL DEFAULT 0 CHECK(post_counter >= 0),"
-      " amount_val INT8 NOT NULL,"                       /* amount we were 
paid */
-      " amount_frac INT4 NOT NULL,"
-      " payment_identifier BYTEA NOT NULL 
CHECK(LENGTH(payment_identifier)=32),"
-      " timestamp TIMESTAMP NOT NULL DEFAULT NOW(),"
-      " paid BOOLEAN NOT NULL DEFAULT FALSE"
-      ");"),
-    GNUNET_PQ_make_execute (
-      "CREATE TABLE IF NOT EXISTS anastasis_challenge_payment"
-      "(payment_id BIGSERIAL PRIMARY KEY,"
-      " truth_public_key BYTEA NOT NULL,"
-      " amount_val INT8 NOT NULL,"                       /* amount we were 
paid */
-      " amount_frac INT4 NOT NULL,"
-      " payment_identifier BYTEA NOT NULL 
CHECK(LENGTH(payment_identifier)=32),"
-      " timestamp TIMESTAMP NOT NULL DEFAULT NOW(),"
-      " paid BOOLEAN NOT NULL DEFAULT FALSE"
-      ");"),
-    GNUNET_PQ_make_execute (
-      "CREATE TABLE IF NOT EXISTS anastasis_recoverydocument"
-      "( user_id BYTEA NOT NULL REFERENCES anastasis_user(user_id),"
-      " version INT4 NOT NULL,"
-      " account_sig BYTEA NOT NULL CHECK(LENGTH(account_sig)=64),"
-      " recovery_data_hash BYTEA NOT NULL 
CHECK(length(recovery_data_hash)=64),"
-      " recovery_data BYTEA NOT NULL,"
-      " PRIMARY KEY (user_id, version)"
-      ");"),
-    GNUNET_PQ_make_execute (
-      "CREATE TABLE IF NOT EXISTS anastasis_challengecode"
-      "( truth_public_key BYTEA NOT NULL,"
-      " code INT8 NOT NULL,"
-      " creation_date TIMESTAMP NOT NULL DEFAULT NOW(),"
-      " expiration_date TIMESTAMP NOT NULL,"
-      " retry_counter INT4 NOT NULL"
-      ");"),
-    GNUNET_PQ_EXECUTE_STATEMENT_END
-  };
   struct GNUNET_PQ_PreparedStatement ps[] = {
     GNUNET_PQ_make_prepare ("user_insert",
                             "INSERT INTO anastasis_user "
@@ -2277,9 +2216,9 @@ libanastasis_plugin_db_postgres_init (void *cls)
   pg = GNUNET_new (struct PostgresClosure);
   pg->cfg = cfg;
   pg->conn = GNUNET_PQ_connect_with_cfg (cfg,
-                                         "anastasisdb-postgres",
+                                         "stasis-postgres",
+                                         "stasis-",
                                          NULL,
-                                         es,
                                          ps);
   if (NULL == pg->conn)
   {
diff --git a/src/stasis/stasis-0000.sql b/src/stasis/stasis-0000.sql
new file mode 100644
index 0000000..116f409
--- /dev/null
+++ b/src/stasis/stasis-0000.sql
@@ -0,0 +1,293 @@
+-- LICENSE AND COPYRIGHT
+--
+-- Copyright (C) 2010 Hubert depesz Lubaczewski
+--
+-- This program is distributed under the (Revised) BSD License:
+-- L<http://www.opensource.org/licenses/bsd-license.php>
+--
+-- Redistribution and use in source and binary forms, with or without
+-- modification, are permitted provided that the following conditions
+-- are met:
+--
+-- * Redistributions of source code must retain the above copyright
+-- notice, this list of conditions and the following disclaimer.
+--
+-- * Redistributions in binary form must reproduce the above copyright
+--   notice, this list of conditions and the following disclaimer in the
+--   documentation and/or other materials provided with the distribution.
+--
+-- * Neither the name of Hubert depesz Lubaczewski's Organization
+--   nor the names of its contributors may be used to endorse or
+--   promote products derived from this software without specific
+--   prior written permission.
+--
+-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE
+-- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+-- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+-- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY,
+-- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
USE
+-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+--
+-- Code origin: 
https://gitlab.com/depesz/Versioning/blob/master/install.versioning.sql
+--
+--
+-- # NAME
+--
+-- **Versioning** - simplistic take on tracking and applying changes to 
databases.
+--
+-- # DESCRIPTION
+--
+-- This project strives to provide simple way to manage changes to
+-- database.
+--
+-- Instead of making changes on development server, then finding
+-- differences between production and development, deciding which ones
+-- should be installed on production, and finding a way to install them -
+-- you start with writing diffs themselves!
+--
+-- # INSTALLATION
+--
+-- To install versioning simply run install.versioning.sql in your database
+-- (all of them: production, stage, test, devel, ...).
+--
+-- # USAGE
+--
+-- In your files with patches to database, put whole logic in single
+-- transaction, and use \_v.\* functions - usually \_v.register_patch() at
+-- least to make sure everything is OK.
+--
+-- For example. Let's assume you have patch files:
+--
+-- ## 0001.sql:
+--
+-- ```
+-- create table users (id serial primary key, username text);
+-- ```
+--
+-- ## 0002.sql:
+--
+-- ```
+-- insert into users (username) values ('depesz');
+-- ```
+-- To change it to use versioning you would change the files, to this
+-- state:
+--
+-- 0000.sql:
+--
+-- ```
+-- BEGIN;
+-- select _v.register_patch('000-base', NULL, NULL);
+-- create table users (id serial primary key, username text);
+-- COMMIT;
+-- ```
+--
+-- ## 0002.sql:
+--
+-- ```
+-- BEGIN;
+-- select _v.register_patch('001-users', ARRAY['000-base'], NULL);
+-- insert into users (username) values ('depesz');
+-- COMMIT;
+-- ```
+--
+-- This will make sure that patch 001-users can only be applied after
+-- 000-base.
+--
+-- # AVAILABLE FUNCTIONS
+--
+-- ## \_v.register_patch( TEXT )
+--
+-- Registers named patch, or dies if it is already registered.
+--
+-- Returns integer which is id of patch in \_v.patches table - only if it
+-- succeeded.
+--
+-- ## \_v.register_patch( TEXT, TEXT[] )
+--
+-- Same as \_v.register_patch( TEXT ), but checks is all given patches (given 
as
+-- array in second argument) are already registered.
+--
+-- ## \_v.register_patch( TEXT, TEXT[], TEXT[] )
+--
+-- Same as \_v.register_patch( TEXT, TEXT[] ), but also checks if there are no 
conflicts with preexisting patches.
+--
+-- Third argument is array of names of patches that conflict with current one. 
So
+-- if any of them is installed - register_patch will error out.
+--
+-- ## \_v.unregister_patch( TEXT )
+--
+-- Removes information about given patch from the versioning data.
+--
+-- It doesn't remove objects that were created by this patch - just removes
+-- metainformation.
+--
+-- ## \_v.assert_user_is_superuser()
+--
+-- Make sure that current patch is being loaded by superuser.
+--
+-- If it's not - it will raise exception, and break transaction.
+--
+-- ## \_v.assert_user_is_not_superuser()
+--
+-- Make sure that current patch is not being loaded by superuser.
+--
+-- If it is - it will raise exception, and break transaction.
+--
+-- ## \_v.assert_user_is_one_of(TEXT, TEXT, ... )
+--
+-- Make sure that current patch is being loaded by one of listed users.
+--
+-- If ```current_user``` is not listed as one of arguments - function will 
raise
+-- exception and break the transaction.
+
+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.
+CREATE SCHEMA IF NOT EXISTS _v;
+COMMENT ON SCHEMA _v IS 'Schema for versioning data and functionality.';
+
+CREATE TABLE IF NOT EXISTS _v.patches (
+    patch_name  TEXT        PRIMARY KEY,
+    applied_tsz TIMESTAMPTZ NOT NULL DEFAULT now(),
+    applied_by  TEXT        NOT NULL,
+    requires    TEXT[],
+    conflicts   TEXT[]
+);
+COMMENT ON TABLE _v.patches              IS 'Contains information about what 
patches are currently applied on database.';
+COMMENT ON COLUMN _v.patches.patch_name  IS 'Name of patch, has to be unique 
for every patch.';
+COMMENT ON COLUMN _v.patches.applied_tsz IS 'When the patch was applied.';
+COMMENT ON COLUMN _v.patches.applied_by  IS 'Who applied this patch 
(PostgreSQL username)';
+COMMENT ON COLUMN _v.patches.requires    IS 'List of patches that are required 
for given patch.';
+COMMENT ON COLUMN _v.patches.conflicts   IS 'List of patches that conflict 
with given patch.';
+
+CREATE OR REPLACE FUNCTION _v.register_patch( IN in_patch_name TEXT, IN 
in_requirements TEXT[], in_conflicts TEXT[], OUT versioning INT4 ) RETURNS 
setof INT4 AS $$
+DECLARE
+    t_text   TEXT;
+    t_text_a TEXT[];
+    i INT4;
+BEGIN
+    -- Thanks to this we know only one patch will be applied at a time
+    LOCK TABLE _v.patches IN EXCLUSIVE MODE;
+
+    SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = 
in_patch_name;
+    IF FOUND THEN
+        RAISE EXCEPTION 'Patch % is already applied!', in_patch_name;
+    END IF;
+
+    t_text_a := ARRAY( SELECT patch_name FROM _v.patches WHERE patch_name = 
any( in_conflicts ) );
+    IF array_upper( t_text_a, 1 ) IS NOT NULL THEN
+        RAISE EXCEPTION 'Versioning patches conflict. Conflicting patche(s) 
installed: %.', array_to_string( t_text_a, ', ' );
+    END IF;
+
+    IF array_upper( in_requirements, 1 ) IS NOT NULL THEN
+        t_text_a := '{}';
+        FOR i IN array_lower( in_requirements, 1 ) .. array_upper( 
in_requirements, 1 ) LOOP
+            SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = 
in_requirements[i];
+            IF NOT FOUND THEN
+                t_text_a := t_text_a || in_requirements[i];
+            END IF;
+        END LOOP;
+        IF array_upper( t_text_a, 1 ) IS NOT NULL THEN
+            RAISE EXCEPTION 'Missing prerequisite(s): %.', array_to_string( 
t_text_a, ', ' );
+        END IF;
+    END IF;
+
+    INSERT INTO _v.patches (patch_name, applied_tsz, applied_by, requires, 
conflicts ) VALUES ( in_patch_name, now(), current_user, coalesce( 
in_requirements, '{}' ), coalesce( in_conflicts, '{}' ) );
+    RETURN;
+END;
+$$ language plpgsql;
+COMMENT ON FUNCTION _v.register_patch( TEXT, TEXT[], TEXT[] ) IS 'Function to 
register patches in database. Raises exception if there are conflicts, 
prerequisites are not installed or the migration has already been installed.';
+
+CREATE OR REPLACE FUNCTION _v.register_patch( TEXT, TEXT[] ) RETURNS setof 
INT4 AS $$
+    SELECT _v.register_patch( $1, $2, NULL );
+$$ language sql;
+COMMENT ON FUNCTION _v.register_patch( TEXT, TEXT[] ) IS 'Wrapper to allow 
registration of patches without conflicts.';
+CREATE OR REPLACE FUNCTION _v.register_patch( TEXT ) RETURNS setof INT4 AS $$
+    SELECT _v.register_patch( $1, NULL, NULL );
+$$ language sql;
+COMMENT ON FUNCTION _v.register_patch( TEXT ) IS 'Wrapper to allow 
registration of patches without requirements and conflicts.';
+
+CREATE OR REPLACE FUNCTION _v.unregister_patch( IN in_patch_name TEXT, OUT 
versioning INT4 ) RETURNS setof INT4 AS $$
+DECLARE
+    i        INT4;
+    t_text_a TEXT[];
+BEGIN
+    -- Thanks to this we know only one patch will be applied at a time
+    LOCK TABLE _v.patches IN EXCLUSIVE MODE;
+
+    t_text_a := ARRAY( SELECT patch_name FROM _v.patches WHERE in_patch_name = 
ANY( requires ) );
+    IF array_upper( t_text_a, 1 ) IS NOT NULL THEN
+        RAISE EXCEPTION 'Cannot uninstall %, as it is required by: %.', 
in_patch_name, array_to_string( t_text_a, ', ' );
+    END IF;
+
+    DELETE FROM _v.patches WHERE patch_name = in_patch_name;
+    GET DIAGNOSTICS i = ROW_COUNT;
+    IF i < 1 THEN
+        RAISE EXCEPTION 'Patch % is not installed, so it can''t be 
uninstalled!', in_patch_name;
+    END IF;
+
+    RETURN;
+END;
+$$ language plpgsql;
+COMMENT ON FUNCTION _v.unregister_patch( TEXT ) IS 'Function to unregister 
patches in database. Dies if the patch is not registered, or if unregistering 
it would break dependencies.';
+
+CREATE OR REPLACE FUNCTION _v.assert_patch_is_applied( IN in_patch_name TEXT ) 
RETURNS TEXT as $$
+DECLARE
+    t_text TEXT;
+BEGIN
+    SELECT patch_name INTO t_text FROM _v.patches WHERE patch_name = 
in_patch_name;
+    IF NOT FOUND THEN
+        RAISE EXCEPTION 'Patch % is not applied!', in_patch_name;
+    END IF;
+    RETURN format('Patch %s is applied.', in_patch_name);
+END;
+$$ language plpgsql;
+COMMENT ON FUNCTION _v.assert_patch_is_applied( TEXT ) IS 'Function that can 
be used to make sure that patch has been applied.';
+
+CREATE OR REPLACE FUNCTION _v.assert_user_is_superuser() RETURNS TEXT as $$
+DECLARE
+    v_super bool;
+BEGIN
+    SELECT usesuper INTO v_super FROM pg_user WHERE usename = current_user;
+    IF v_super THEN
+        RETURN 'assert_user_is_superuser: OK';
+    END IF;
+    RAISE EXCEPTION 'Current user is not superuser - cannot continue.';
+END;
+$$ language plpgsql;
+COMMENT ON FUNCTION _v.assert_user_is_superuser() IS 'Function that can be 
used to make sure that patch is being applied using superuser account.';
+
+CREATE OR REPLACE FUNCTION _v.assert_user_is_not_superuser() RETURNS TEXT as $$
+DECLARE
+    v_super bool;
+BEGIN
+    SELECT usesuper INTO v_super FROM pg_user WHERE usename = current_user;
+    IF v_super THEN
+        RAISE EXCEPTION 'Current user is superuser - cannot continue.';
+    END IF;
+    RETURN 'assert_user_is_not_superuser: OK';
+END;
+$$ language plpgsql;
+COMMENT ON FUNCTION _v.assert_user_is_not_superuser() IS 'Function that can be 
used to make sure that patch is being applied using normal (not superuser) 
account.';
+
+CREATE OR REPLACE FUNCTION _v.assert_user_is_one_of(VARIADIC 
p_acceptable_users TEXT[] ) RETURNS TEXT as $$
+DECLARE
+BEGIN
+    IF current_user = any( p_acceptable_users ) THEN
+        RETURN 'assert_user_is_one_of: OK';
+    END IF;
+    RAISE EXCEPTION 'User is not one of: % - cannot continue.', 
p_acceptable_users;
+END;
+$$ language plpgsql;
+COMMENT ON FUNCTION _v.assert_user_is_one_of(TEXT[]) IS 'Function that can be 
used to make sure that patch is being applied by one of defined users.';
+
+COMMIT;
diff --git a/src/stasis/stasis-0001.sql b/src/stasis/stasis-0001.sql
new file mode 100644
index 0000000..0e655a6
--- /dev/null
+++ b/src/stasis/stasis-0001.sql
@@ -0,0 +1,87 @@
+--
+-- This file is part of Anastasis
+-- Copyright (C) 2020 Anastasis SARL SA
+--
+-- ANASTASIS 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
+-- 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 General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License along with
+-- ANASTASIS; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+--
+
+-- Everything in one big transaction
+BEGIN;
+
+-- TODO:
+-- * add comments for every table and column
+-- * avoid 'timestamp' as column name
+
+
+-- Check patch versioning is in place.
+SELECT _v.register_patch('stasis-0001', NULL, NULL);
+
+
+CREATE TABLE IF NOT EXISTS anastasis_truth
+  (truth_public_key BYTEA PRIMARY KEY NOT NULL,
+   key_share_data BYTEA NOT NULL,
+   method VARCHAR,
+   encrypted_truth BYTEA NOT NULL,
+   truth_mime VARCHAR,
+   expiration TIMESTAMP NOT NULL);
+COMMENT ON TABLE anastasis_truth
+  IS 'Truth data is needed to authenticate clients during recovery';
+
+CREATE TABLE IF NOT EXISTS anastasis_user
+  (user_id BYTEA PRIMARY KEY CHECK(LENGTH(user_id)=32),
+   expiration_date TIMESTAMP NOT NULL);
+
+-- FIXME: rename column, do NOT use SQL keyword 'timestamp' for column name!
+CREATE TABLE IF NOT EXISTS anastasis_recdoc_payment
+  (payment_id BIGSERIAL PRIMARY KEY,
+   user_id BYTEA NOT NULL REFERENCES anastasis_user(user_id),
+   post_counter INT4 NOT NULL DEFAULT 0 CHECK(post_counter >= 0),
+   amount_val INT8 NOT NULL,
+   amount_frac INT4 NOT NULL,
+   payment_identifier BYTEA NOT NULL CHECK(LENGTH(payment_identifier)=32),
+   timestamp TIMESTAMP NOT NULL DEFAULT NOW(),
+   paid BOOLEAN NOT NULL DEFAULT FALSE);
+COMMENT ON COLUMN anastasis_recdoc_payment.amount_val
+  IS 'Amount we were paid';
+
+-- FIXME: rename column, do NOT use SQL keyword 'timestamp' for column name!
+CREATE TABLE IF NOT EXISTS anastasis_challenge_payment
+  (payment_id BIGSERIAL PRIMARY KEY,
+   truth_public_key BYTEA NOT NULL,
+   amount_val INT8 NOT NULL,
+   amount_frac INT4 NOT NULL,
+   payment_identifier BYTEA NOT NULL CHECK(LENGTH(payment_identifier)=32),
+   timestamp TIMESTAMP NOT NULL DEFAULT NOW(),
+   paid BOOLEAN NOT NULL DEFAULT FALSE
+  );
+COMMENT ON COLUMN anastasis_challenge_payment.amount_val
+  IS 'Amount we were paid';
+
+
+CREATE TABLE IF NOT EXISTS anastasis_recoverydocument
+  (user_id BYTEA NOT NULL REFERENCES anastasis_user(user_id),
+   version INT4 NOT NULL,
+   account_sig BYTEA NOT NULL CHECK(LENGTH(account_sig)=64),
+   recovery_data_hash BYTEA NOT NULL CHECK(length(recovery_data_hash)=64),
+   recovery_data BYTEA NOT NULL,
+   PRIMARY KEY (user_id, version));
+
+CREATE TABLE IF NOT EXISTS anastasis_challengecode
+  (truth_public_key BYTEA NOT NULL,
+   code INT8 NOT NULL,
+   creation_date TIMESTAMP NOT NULL DEFAULT NOW(),
+   expiration_date TIMESTAMP NOT NULL,
+   retry_counter INT4 NOT NULL);
+
+
+-- Complete transaction
+COMMIT;
diff --git a/src/stasis/stasis-postgres.conf b/src/stasis/stasis-postgres.conf
new file mode 100644
index 0000000..0d9b209
--- /dev/null
+++ b/src/stasis/stasis-postgres.conf
@@ -0,0 +1,6 @@
+[stasis-postgres]
+CONFIG = "postgres:///anastasis"
+
+# Where are the SQL files to setup our tables?
+# Important: this MUST end with a "/"!
+SQL_DIR = $DATADIR/sql/
diff --git a/src/stasis/test_anastasis_db.c b/src/stasis/test_anastasis_db.c
index 57252c1..b0c3a33 100644
--- a/src/stasis/test_anastasis_db.c
+++ b/src/stasis/test_anastasis_db.c
@@ -26,6 +26,7 @@
 #include "anastasis_database_plugin.h"
 #include "anastasis_database_lib.h"
 #include "anastasis_error_codes.h"
+#include "anastasis_util_lib.h"
 #include <gnunet/gnunet_signatures.h>
 
 
@@ -303,6 +304,11 @@ main (int argc,
     GNUNET_break (0);
     return -1;
   }
+  /* FIRST get the libtalerutil initialization out
+      of the way. Then throw that one away, and force
+      the SYNC defaults to be used! */
+  (void) TALER_project_data_default ();
+  GNUNET_OS_init (ANASTASIS_project_data_default ());
   GNUNET_log_setup (argv[0], "DEBUG", NULL);
   plugin_name++;
   (void) GNUNET_asprintf (&testname,
@@ -313,15 +319,16 @@ main (int argc,
                           testname);
   cfg = GNUNET_CONFIGURATION_create ();
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_parse (cfg,
-                                  config_filename))
+      GNUNET_CONFIGURATION_load (cfg,
+                                 config_filename))
   {
     GNUNET_break (0);
     GNUNET_free (config_filename);
     GNUNET_free (testname);
     return 2;
   }
-  GNUNET_SCHEDULER_run (&run, cfg);
+  GNUNET_SCHEDULER_run (&run,
+                        cfg);
   GNUNET_CONFIGURATION_destroy (cfg);
   GNUNET_free (config_filename);
   GNUNET_free (testname);
diff --git a/src/stasis/test_anastasis_db_postgres.conf 
b/src/stasis/test_anastasis_db_postgres.conf
index b00e4e8..8971a62 100644
--- a/src/stasis/test_anastasis_db_postgres.conf
+++ b/src/stasis/test_anastasis_db_postgres.conf
@@ -5,6 +5,6 @@ DB = postgres
 [taler]
 CURRENCY = EUR
 
-[anastasisdb-postgres]
+[stasis-postgres]
 #The connection string the plugin has to use for connecting to the database
 CONFIG = postgres:///anastasischeck

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]