From 977ddd7bb97f9cec780407f27ccb2dbf8d1cf722 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 24 Jul 2022 11:32:52 +0200 Subject: -adapt to latest libgnunetpq --- src/exchangedb/Makefile.am | 35 ++-- src/exchangedb/benchmark-0000.sql | 293 ---------------------------- src/exchangedb/drop-exchange-part.sql | 108 ++++++++++ src/exchangedb/drop-shard-part.sql | 27 +++ src/exchangedb/drop0001-exchange-part.sql | 108 ---------- src/exchangedb/drop0001-shard-part.sql | 27 --- src/exchangedb/exchange-0000.sql | 293 ---------------------------- src/exchangedb/plugin_exchangedb_postgres.c | 44 ++--- src/exchangedb/versioning.sql | 293 ++++++++++++++++++++++++++++ 9 files changed, 462 insertions(+), 766 deletions(-) delete mode 100644 src/exchangedb/benchmark-0000.sql create mode 100644 src/exchangedb/drop-exchange-part.sql create mode 100644 src/exchangedb/drop-shard-part.sql delete mode 100644 src/exchangedb/drop0001-exchange-part.sql delete mode 100644 src/exchangedb/drop0001-shard-part.sql delete mode 100644 src/exchangedb/exchange-0000.sql create mode 100644 src/exchangedb/versioning.sql (limited to 'src/exchangedb') diff --git a/src/exchangedb/Makefile.am b/src/exchangedb/Makefile.am index afa2a3ac6..1be810148 100644 --- a/src/exchangedb/Makefile.am +++ b/src/exchangedb/Makefile.am @@ -18,34 +18,30 @@ sqlinputs = \ common-0001.sql \ drop-common.sql \ exchange-0001-part.sql \ - drop0001-exchange-part.sql \ + drop-exchange-part.sql \ shard-0001-part.sql \ - drop0001-shard-part.sql + drop-shard-part.sql sql_DATA = \ - benchmark-0000.sql \ benchmark-0001.sql \ - exchange-0000.sql \ + versioning.sql \ exchange-0001.sql \ - drop0001.sql \ - shard-0000.sql \ + drop.sql \ shard-0001.sql \ - shard-drop0001.sql + shard-drop.sql BUILT_SOURCES = \ - shard-0000.sql \ shard-0001.sql \ exchange-0001.sql \ - drop0001.sql \ - shard-drop0001.sql + drop.sql \ + shard-drop.sql CLEANFILES = \ - shard-0000.sql \ shard-0001.sql \ exchange-0001.sql \ - drop0001.sql \ - shard-drop0001.sql + drop.sql \ + shard-drop.sql exchange-0001.sql: common-0001.sql exchange-0001-part.sql chmod +w $@ || true @@ -57,19 +53,14 @@ shard-0001.sql: common-0001.sql shard-0001-part.sql cat common-0001.sql shard-0001-part.sql >$@ chmod ugo-w $@ -shard-0000.sql: exchange-0000.sql +drop.sql: drop-common.sql drop-exchange-part.sql chmod +w $@ || true - cp exchange-0000.sql $@ + cat drop-common.sql drop-exchange-part.sql >$@ chmod ugo-w $@ -drop0001.sql: drop-common.sql drop0001-exchange-part.sql +shard-drop.sql: drop-common.sql drop-shard-part.sql chmod +w $@ || true - cat drop-common.sql drop0001-exchange-part.sql >$@ - chmod ugo-w $@ - -shard-drop0001.sql: drop-common.sql drop0001-shard-part.sql - chmod +w $@ || true - cat drop-common.sql drop0001-shard-part.sql >$@ + cat drop-common.sql drop-shard-part.sql >$@ chmod ugo-w $@ EXTRA_DIST = \ diff --git a/src/exchangedb/benchmark-0000.sql b/src/exchangedb/benchmark-0000.sql deleted file mode 100644 index 116f409b7..000000000 --- a/src/exchangedb/benchmark-0000.sql +++ /dev/null @@ -1,293 +0,0 @@ --- LICENSE AND COPYRIGHT --- --- Copyright (C) 2010 Hubert depesz Lubaczewski --- --- This program is distributed under the (Revised) BSD License: --- L --- --- 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/exchangedb/drop-exchange-part.sql b/src/exchangedb/drop-exchange-part.sql new file mode 100644 index 000000000..4cf6a72fc --- /dev/null +++ b/src/exchangedb/drop-exchange-part.sql @@ -0,0 +1,108 @@ +-- +-- This file is part of TALER +-- Copyright (C) 2014--2021 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 +-- 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 +-- + +BEGIN; + +-- Unregister patch (exchange-0001.sql) +SELECT _v.unregister_patch('exchange-0001'); + + +-- Drops for exchange-0001-part.sql +DROP TRIGGER IF EXISTS reserves_out_on_insert ON reserves_out; +DROP TRIGGER IF EXISTS reserves_out_on_delete ON reserves_out; +DROP TRIGGER IF EXISTS deposits_on_insert ON deposits; +DROP TRIGGER IF EXISTS deposits_on_delete ON deposits; +DROP TRIGGER IF EXISTS recoup_on_insert ON recoup; +DROP TRIGGER IF EXISTS recoup_on_delete ON recoup; + +DROP TABLE IF EXISTS revolving_work_shards CASCADE; +DROP TABLE IF EXISTS extensions CASCADE; +DROP TABLE IF EXISTS auditors CASCADE; +DROP TABLE IF EXISTS auditor_denom_sigs CASCADE; +DROP TABLE IF EXISTS exchange_sign_keys CASCADE; +DROP TABLE IF EXISTS wire_accounts CASCADE; +DROP TABLE IF EXISTS signkey_revocations CASCADE; +DROP TABLE IF EXISTS work_shards CASCADE; +DROP TABLE IF EXISTS prewire CASCADE; +DROP TABLE IF EXISTS recoup CASCADE; +DROP TABLE IF EXISTS recoup_refresh CASCADE; +DROP TABLE IF EXISTS aggregation_transient CASCADE; +DROP TABLE IF EXISTS aggregation_tracking CASCADE; +DROP TABLE IF EXISTS wire_out CASCADE; +DROP TABLE IF EXISTS wire_targets CASCADE; +DROP TABLE IF EXISTS wire_fee CASCADE; +DROP TABLE IF EXISTS deposits CASCADE; +DROP TABLE IF EXISTS deposits_by_ready CASCADE; +DROP TABLE IF EXISTS deposits_for_matching CASCADE; +DROP TABLE IF EXISTS extension_details CASCADE; +DROP TABLE IF EXISTS refunds CASCADE; +DROP TABLE IF EXISTS refresh_commitments CASCADE; +DROP TABLE IF EXISTS refresh_revealed_coins CASCADE; +DROP TABLE IF EXISTS refresh_transfer_keys CASCADE; +DROP TABLE IF EXISTS known_coins CASCADE; +DROP TABLE IF EXISTS reserves_close CASCADE; +DROP TABLE IF EXISTS reserves_out CASCADE; +DROP TABLE IF EXISTS reserves_out_by_reserve CASCADE; +DROP TABLE IF EXISTS reserves_in CASCADE; +DROP TABLE IF EXISTS reserves CASCADE; +DROP TABLE IF EXISTS denomination_revocations CASCADE; +DROP TABLE IF EXISTS denominations CASCADE; +DROP TABLE IF EXISTS cs_nonce_locks CASCADE; +DROP TABLE IF EXISTS global_fee CASCADE; +DROP TABLE IF EXISTS recoup_by_reserve CASCADE; + + +DROP TABLE IF EXISTS partners CASCADE; +DROP TABLE IF EXISTS account_merges CASCADE; +DROP TABLE IF EXISTS purse_merges CASCADE; +DROP TABLE IF EXISTS purse_deposits CASCADE; +DROP TABLE IF EXISTS contracts CASCADE; +DROP TABLE IF EXISTS history_requests CASCADE; +DROP TABLE IF EXISTS close_requests CASCADE; +DROP TABLE IF EXISTS purse_requests CASCADE; +DROP TABLE IF EXISTS purse_refunds CASCADE; +DROP TABLE IF EXISTS wads_out CASCADE; +DROP TABLE IF EXISTS wad_out_entries CASCADE; +DROP TABLE IF EXISTS wads_in CASCADE; +DROP TABLE IF EXISTS wad_in_entries CASCADE; +DROP TABLE IF EXISTS partner_accounts CASCADE; +DROP TABLE IF EXISTS purse_actions CASCADE; + +DROP FUNCTION IF EXISTS exchange_do_withdraw; +DROP FUNCTION IF EXISTS exchange_do_withdraw_limit_check; +DROP FUNCTION IF EXISTS exchange_do_recoup_by_reserve; +DROP FUNCTION IF EXISTS recoup_insert_trigger; +DROP FUNCTION IF EXISTS recoup_delete_trigger; +DROP FUNCTION IF EXISTS deposits_insert_trigger; +DROP FUNCTION IF EXISTS deposits_update_trigger; +DROP FUNCTION IF EXISTS deposits_delete_trigger; +DROP FUNCTION IF EXISTS reserves_out_by_reserve_insert_trigger; +DROP FUNCTION IF EXISTS reserves_out_by_reserve_delete_trigger; +DROP FUNCTION IF EXISTS exchange_do_deposit; +DROP FUNCTION IF EXISTS exchange_do_melt; +DROP FUNCTION IF EXISTS exchange_do_refund; +DROP FUNCTION IF EXISTS exchange_do_recoup_to_coin; +DROP FUNCTION IF EXISTS exchange_do_recoup_to_reserve; +DROP FUNCTION IF EXISTS exchange_do_purse_deposit; +DROP FUNCTION IF EXISTS exchange_do_purse_merge; +DROP FUNCTION IF EXISTS exchange_do_account_merge; +DROP FUNCTION IF EXISTS exchange_do_history_request; +DROP FUNCTION IF EXISTS exchange_do_close_request; +DROP FUNCTION IF EXISTS exchange_do_reserve_purse; + +-- And we're out of here... + +COMMIT; diff --git a/src/exchangedb/drop-shard-part.sql b/src/exchangedb/drop-shard-part.sql new file mode 100644 index 000000000..9cf3eeb3a --- /dev/null +++ b/src/exchangedb/drop-shard-part.sql @@ -0,0 +1,27 @@ +-- +-- This file is part of TALER +-- Copyright (C) 2014--2021 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 +-- 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 +-- + +BEGIN; + +-- Unregister patch (shard-0001.sql) +SELECT _v.unregister_patch('shard-0001'); + +-- Drops for shard-0001-part.sql + +DROP FUNCTION IF EXISTS drop_shard; +DROP FUNCTION IF EXISTS setup_shard; + +COMMIT; diff --git a/src/exchangedb/drop0001-exchange-part.sql b/src/exchangedb/drop0001-exchange-part.sql deleted file mode 100644 index 4cf6a72fc..000000000 --- a/src/exchangedb/drop0001-exchange-part.sql +++ /dev/null @@ -1,108 +0,0 @@ --- --- This file is part of TALER --- Copyright (C) 2014--2021 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 --- 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 --- - -BEGIN; - --- Unregister patch (exchange-0001.sql) -SELECT _v.unregister_patch('exchange-0001'); - - --- Drops for exchange-0001-part.sql -DROP TRIGGER IF EXISTS reserves_out_on_insert ON reserves_out; -DROP TRIGGER IF EXISTS reserves_out_on_delete ON reserves_out; -DROP TRIGGER IF EXISTS deposits_on_insert ON deposits; -DROP TRIGGER IF EXISTS deposits_on_delete ON deposits; -DROP TRIGGER IF EXISTS recoup_on_insert ON recoup; -DROP TRIGGER IF EXISTS recoup_on_delete ON recoup; - -DROP TABLE IF EXISTS revolving_work_shards CASCADE; -DROP TABLE IF EXISTS extensions CASCADE; -DROP TABLE IF EXISTS auditors CASCADE; -DROP TABLE IF EXISTS auditor_denom_sigs CASCADE; -DROP TABLE IF EXISTS exchange_sign_keys CASCADE; -DROP TABLE IF EXISTS wire_accounts CASCADE; -DROP TABLE IF EXISTS signkey_revocations CASCADE; -DROP TABLE IF EXISTS work_shards CASCADE; -DROP TABLE IF EXISTS prewire CASCADE; -DROP TABLE IF EXISTS recoup CASCADE; -DROP TABLE IF EXISTS recoup_refresh CASCADE; -DROP TABLE IF EXISTS aggregation_transient CASCADE; -DROP TABLE IF EXISTS aggregation_tracking CASCADE; -DROP TABLE IF EXISTS wire_out CASCADE; -DROP TABLE IF EXISTS wire_targets CASCADE; -DROP TABLE IF EXISTS wire_fee CASCADE; -DROP TABLE IF EXISTS deposits CASCADE; -DROP TABLE IF EXISTS deposits_by_ready CASCADE; -DROP TABLE IF EXISTS deposits_for_matching CASCADE; -DROP TABLE IF EXISTS extension_details CASCADE; -DROP TABLE IF EXISTS refunds CASCADE; -DROP TABLE IF EXISTS refresh_commitments CASCADE; -DROP TABLE IF EXISTS refresh_revealed_coins CASCADE; -DROP TABLE IF EXISTS refresh_transfer_keys CASCADE; -DROP TABLE IF EXISTS known_coins CASCADE; -DROP TABLE IF EXISTS reserves_close CASCADE; -DROP TABLE IF EXISTS reserves_out CASCADE; -DROP TABLE IF EXISTS reserves_out_by_reserve CASCADE; -DROP TABLE IF EXISTS reserves_in CASCADE; -DROP TABLE IF EXISTS reserves CASCADE; -DROP TABLE IF EXISTS denomination_revocations CASCADE; -DROP TABLE IF EXISTS denominations CASCADE; -DROP TABLE IF EXISTS cs_nonce_locks CASCADE; -DROP TABLE IF EXISTS global_fee CASCADE; -DROP TABLE IF EXISTS recoup_by_reserve CASCADE; - - -DROP TABLE IF EXISTS partners CASCADE; -DROP TABLE IF EXISTS account_merges CASCADE; -DROP TABLE IF EXISTS purse_merges CASCADE; -DROP TABLE IF EXISTS purse_deposits CASCADE; -DROP TABLE IF EXISTS contracts CASCADE; -DROP TABLE IF EXISTS history_requests CASCADE; -DROP TABLE IF EXISTS close_requests CASCADE; -DROP TABLE IF EXISTS purse_requests CASCADE; -DROP TABLE IF EXISTS purse_refunds CASCADE; -DROP TABLE IF EXISTS wads_out CASCADE; -DROP TABLE IF EXISTS wad_out_entries CASCADE; -DROP TABLE IF EXISTS wads_in CASCADE; -DROP TABLE IF EXISTS wad_in_entries CASCADE; -DROP TABLE IF EXISTS partner_accounts CASCADE; -DROP TABLE IF EXISTS purse_actions CASCADE; - -DROP FUNCTION IF EXISTS exchange_do_withdraw; -DROP FUNCTION IF EXISTS exchange_do_withdraw_limit_check; -DROP FUNCTION IF EXISTS exchange_do_recoup_by_reserve; -DROP FUNCTION IF EXISTS recoup_insert_trigger; -DROP FUNCTION IF EXISTS recoup_delete_trigger; -DROP FUNCTION IF EXISTS deposits_insert_trigger; -DROP FUNCTION IF EXISTS deposits_update_trigger; -DROP FUNCTION IF EXISTS deposits_delete_trigger; -DROP FUNCTION IF EXISTS reserves_out_by_reserve_insert_trigger; -DROP FUNCTION IF EXISTS reserves_out_by_reserve_delete_trigger; -DROP FUNCTION IF EXISTS exchange_do_deposit; -DROP FUNCTION IF EXISTS exchange_do_melt; -DROP FUNCTION IF EXISTS exchange_do_refund; -DROP FUNCTION IF EXISTS exchange_do_recoup_to_coin; -DROP FUNCTION IF EXISTS exchange_do_recoup_to_reserve; -DROP FUNCTION IF EXISTS exchange_do_purse_deposit; -DROP FUNCTION IF EXISTS exchange_do_purse_merge; -DROP FUNCTION IF EXISTS exchange_do_account_merge; -DROP FUNCTION IF EXISTS exchange_do_history_request; -DROP FUNCTION IF EXISTS exchange_do_close_request; -DROP FUNCTION IF EXISTS exchange_do_reserve_purse; - --- And we're out of here... - -COMMIT; diff --git a/src/exchangedb/drop0001-shard-part.sql b/src/exchangedb/drop0001-shard-part.sql deleted file mode 100644 index 9cf3eeb3a..000000000 --- a/src/exchangedb/drop0001-shard-part.sql +++ /dev/null @@ -1,27 +0,0 @@ --- --- This file is part of TALER --- Copyright (C) 2014--2021 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 --- 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 --- - -BEGIN; - --- Unregister patch (shard-0001.sql) -SELECT _v.unregister_patch('shard-0001'); - --- Drops for shard-0001-part.sql - -DROP FUNCTION IF EXISTS drop_shard; -DROP FUNCTION IF EXISTS setup_shard; - -COMMIT; diff --git a/src/exchangedb/exchange-0000.sql b/src/exchangedb/exchange-0000.sql deleted file mode 100644 index 116f409b7..000000000 --- a/src/exchangedb/exchange-0000.sql +++ /dev/null @@ -1,293 +0,0 @@ --- LICENSE AND COPYRIGHT --- --- Copyright (C) 2010 Hubert depesz Lubaczewski --- --- This program is distributed under the (Revised) BSD License: --- L --- --- 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/exchangedb/plugin_exchangedb_postgres.c b/src/exchangedb/plugin_exchangedb_postgres.c index 277e3bc4f..abcd221bd 100644 --- a/src/exchangedb/plugin_exchangedb_postgres.c +++ b/src/exchangedb/plugin_exchangedb_postgres.c @@ -155,22 +155,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, "exchangedb-postgres", - "drop", + NULL, NULL, NULL); if (NULL == conn) return GNUNET_SYSERR; + ret = GNUNET_PQ_exec_sql (conn, + "drop"); GNUNET_PQ_disconnect (conn); - if (NULL != pg->conn) - { - GNUNET_PQ_disconnect (pg->conn); - pg->conn = NULL; - pg->init = false; - } - return GNUNET_OK; + return ret; } @@ -200,6 +203,13 @@ postgres_drop_shard_tables (void *cls, 1), GNUNET_PQ_PREPARED_STATEMENT_END }; + + if (NULL != pg->conn) + { + GNUNET_PQ_disconnect (pg->conn); + pg->conn = NULL; + pg->init = false; + } conn = GNUNET_PQ_connect_with_cfg (pg->cfg, "exchangedb-postgres", NULL, @@ -211,22 +221,10 @@ postgres_drop_shard_tables (void *cls, "drop_shard_tables", params)) ret = GNUNET_SYSERR; + if (GNUNET_OK == ret) + ret = GNUNET_PQ_exec_sql (conn, + "shard-drop"); GNUNET_PQ_disconnect (conn); - - conn = GNUNET_PQ_connect_with_cfg (pg->cfg, - "exchangedb-postgres", - "shard-drop", - NULL, - NULL); - if (NULL == conn) - return GNUNET_SYSERR; - GNUNET_PQ_disconnect (conn); - if (NULL != pg->conn) - { - GNUNET_PQ_disconnect (pg->conn); - pg->conn = NULL; - pg->init = false; - } return ret; } diff --git a/src/exchangedb/versioning.sql b/src/exchangedb/versioning.sql new file mode 100644 index 000000000..116f409b7 --- /dev/null +++ b/src/exchangedb/versioning.sql @@ -0,0 +1,293 @@ +-- LICENSE AND COPYRIGHT +-- +-- Copyright (C) 2010 Hubert depesz Lubaczewski +-- +-- This program is distributed under the (Revised) BSD License: +-- L +-- +-- 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; -- cgit v1.2.3