summaryrefslogtreecommitdiffstats
path: root/src/share/database/scripts/pgsql/wipe_data.sh.in
blob: 088b802e249d55690837b00a62f08bca687e2331 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/sh

# Copyright (C) 2019-2022 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# This script is primarily used for MySQL unit tests, which need to
# ensure an empty, but schema correct database for each test.  It
# deletes ALL transient data from an existing Kea MySQL schema,
# including leases, reservations, etc... Use at your own peril.
# Reference tables will be left in-tact.

# shellcheck disable=SC1091
# SC1091: Not following: ... was not specified as input (see shellcheck -x).

# Exit with error if commands exit with non-zero and if undefined variables are
# used.
set -eu

# shellcheck disable=SC2034
# SC2034: ... appears unused. Verify use (or export if used externally).
prefix="@prefix@"

# Include utilities. Use installed version if available and
# use build version if it isn't.
if [ -e @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh ]; then
    . "@datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh"
else
    . "@abs_top_builddir@/src/bin/admin/admin-utils.sh"
fi

# First argument must be the expected schema version <major>.<minor>
# Check if it's passed at all.
if [ "$#" -lt "1" ]; then
    printf "Required at least one parameter: schema version number, e.g. 7.0\n"
    exit 1
fi
exp_version="$1"
shift

# Remaining arguments are used as pgsql command line arguments

# If the existing schema doesn't match, the fail
VERSION=$(pgsql_version "$@")
if [ "$VERSION" = "" ]; then
    printf "Cannot wipe data, schema version could not be detected.\n"
    exit 1
fi

if [ "$VERSION" != "$exp_version" ]; then
    printf 'Cannot wipe data, wrong schema version. '
    printf 'Expected version %s, found %s.\n' "${exp_version}" "${VERSION}"
    exit 1
fi

# Delete transient data from tables.  We're using delete instead
# of truncate because it is much faster since our unit tests
# create very little data.
# Note we disable revision auditing to avoid issues with delete
# triggers.
psql "$@" >/dev/null <<EOF
SELECT set_config('kea.disable_audit', 'true', false);
START TRANSACTION;
DELETE FROM hosts CASCADE;
DELETE FROM dhcp4_options;
DELETE FROM ipv6_reservations;
DELETE FROM dhcp6_options;
DELETE FROM lease4;
DELETE FROM lease4_stat;
DELETE FROM lease6;
DELETE FROM lease6_stat;
DELETE FROM logs;
DELETE FROM lease4_stat_by_client_class;
DELETE FROM lease6_stat_by_client_class;

-- Config Backend tables
DELETE FROM dhcp4_audit;
DELETE FROM dhcp4_audit_revision;
DELETE FROM dhcp4_global_parameter;
DELETE FROM dhcp4_global_parameter_server;
DELETE FROM dhcp4_option_def;
DELETE FROM dhcp4_option_def_server;
DELETE FROM dhcp4_options;
DELETE FROM dhcp4_options_server;
DELETE FROM dhcp4_pool;
DELETE FROM dhcp4_shared_network;
DELETE FROM dhcp4_subnet;
DELETE FROM dhcp4_subnet_server;
DELETE FROM dhcp4_shared_network_server;
DELETE FROM dhcp4_client_class_order;
DELETE FROM dhcp4_client_class_dependency;
DELETE FROM dhcp4_client_class_server;
DELETE FROM dhcp4_client_class;
DELETE FROM dhcp4_server WHERE tag != 'all'; -- preserve the special tag

DELETE FROM dhcp6_audit;
DELETE FROM dhcp6_audit_revision;
DELETE FROM dhcp6_global_parameter;
DELETE FROM dhcp6_global_parameter_server;
DELETE FROM dhcp6_option_def;
DELETE FROM dhcp6_option_def_server;
DELETE FROM dhcp6_options;
DELETE FROM dhcp6_options_server;
DELETE FROM dhcp6_pool;
DELETE FROM dhcp6_pd_pool;
DELETE FROM dhcp6_shared_network;
DELETE FROM dhcp6_subnet;
DELETE FROM dhcp6_client_class_order;
DELETE FROM dhcp6_client_class_dependency;
DELETE FROM dhcp6_client_class_server;
DELETE FROM dhcp6_client_class;
DELETE FROM dhcp6_server WHERE tag != 'all'; -- preserve the special tag

COMMIT;
EOF