1 // Copyright (C) 2015-2019 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef TEST_MYSQL_SCHEMA_H
8 #define TEST_MYSQL_SCHEMA_H
9 
10 #include <config.h>
11 #include <database/testutils/schema.h>
12 #include <string>
13 
14 namespace isc {
15 namespace db {
16 namespace test {
17 
18 extern const char* MYSQL_VALID_TYPE;
19 
20 /// Return valid connection string
21 ///
22 /// @return valid MySQL connection string.
23 std::string validMySQLConnectionString();
24 
25 /// @brief Clear the unit test database
26 ///
27 /// In order to reduce test execution time, this function
28 /// defaults to first attempting to delete transient data
29 /// from the database by calling @c wipeMySQLData.  If that
30 /// function fails it will then attempt to destroy the database
31 /// schema by running the SQL script:
32 ///
33 ///  <TEST_ADMIN_SCRIPTS_DIR>/mysql/dhcpdb_drop.mysql
34 ///
35 /// The default behavior of wiping the data only may be overridden
36 /// in one of two ways:
37 ///
38 /// -# Setting the force parameter to true
39 /// -# Defining the environment variable:
40 ///    KEA_TEST_DB_WIPE_DATA_ONLY="false"
41 ///
42 /// @param show_err flag which governs whether or not stderr is suppressed.
43 /// @param force if true, the function will skip deleting the data and
44 /// destroy the schema.
45 void destroyMySQLSchema(bool show_err = false, bool force = false);
46 
47 /// @brief Create the unit test MySQL Schema
48 ///
49 /// Ensures the unit test database is a empty and version-correct.
50 /// Unless, the force parameter is true, it will first attempt
51 /// to wipe the data from the database by calling @c wipeMySQLData.
52 /// If this call succeeds the function returns, otherwise it will
53 /// will call @c destroyMySQLSchema to forcibly remove the
54 /// existing schema and then submits the SQL script:
55 ///
56 ///  <TEST_ADMIN_SCRIPTS_DIR>/mysql/dhcpdb_create.mysql
57 ///
58 /// to the unit test MySQL database.
59 ///
60 /// The default behavior of wiping the data only may be overridden
61 /// in one of two ways:
62 ///
63 /// -# Setting the force parameter to true
64 /// -# Defining the environment variable:
65 ///    KEA_TEST_DB_WIPE_DATA_ONLY="false"
66 ///
67 /// @param show_err flag which governs whether or not stderr is suppressed.
68 /// @param force flag when true, the function will recreate the database
69 /// schema.
70 void createMySQLSchema(bool show_err = false, bool force = false);
71 
72 
73 /// @brief Attempts to wipe data from the MySQL unit test database
74 ///
75 /// Runs the shell script
76 ///
77 ///  <TEST_ADMIN_SCRIPTS_DIR>/mysql/wipe_data.sh
78 ///
79 /// This will fail if there is no schema, if the existing schema
80 /// version is incorrect (i.e. does not match MYSQL_SCHEMA_VERSION_MAJOR
81 /// and MYSQL_SCHEMA_VERSION_MINOR), or a SQL error occurs.  Otherwise,
82 /// the script is should delete all transient data, leaving intact
83 /// reference tables.
84 ///
85 /// @param show_err flag which governs whether or not stderr is suppressed.
86 bool wipeMySQLData(bool show_err = false);
87 
88 /// @brief Run a MySQL SQL script against the MySQL unit test database
89 ///
90 /// Submits the given SQL script to MySQL via mysql CLI. The output of
91 /// stderr is suppressed unless the parameter, show_err is true.  The is done
92 /// to suppress warnings that might otherwise make test output needlessly
93 /// noisy.  An exception is thrown if the script fails to execute.
94 ///
95 /// @param path - path (if not blank) of the script to execute
96 /// @param script_name - file name of the path to execute
97 /// @param show_err flag which governs whether or not stderr is suppressed.
98 /// @throw Unexpected when the script returns an error.
99 void runMySQLScript(const std::string& path, const std::string& script_name,
100                     bool show_err);
101 
102 };
103 };
104 };
105 
106 #endif
107