xref: /freebsd/contrib/kyua/cli/cmd_db_migrate.cpp (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1 // Copyright 2013 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include "cli/cmd_db_migrate.hpp"
30 
31 #include <cstdlib>
32 
33 #include "cli/common.ipp"
34 #include "store/exceptions.hpp"
35 #include "store/layout.hpp"
36 #include "store/migrate.hpp"
37 #include "utils/cmdline/options.hpp"
38 #include "utils/cmdline/ui.hpp"
39 #include "utils/defs.hpp"
40 #include "utils/format/macros.hpp"
41 #include "utils/fs/path.hpp"
42 
43 namespace cmdline = utils::cmdline;
44 namespace config = utils::config;
45 namespace fs = utils::fs;
46 namespace layout = store::layout;
47 
48 using cli::cmd_db_migrate;
49 
50 
51 /// Default constructor for cmd_db_migrate.
52 cmd_db_migrate::cmd_db_migrate(void) : cli_command(
53     "db-migrate", "", 0, 0,
54     "Upgrades the schema of an existing results file to the currently "
55     "implemented version.  A backup of the results file is created, but "
56     "this operation is not reversible")
57 {
58     add_option(results_file_open_option);
59 }
60 
61 
62 /// Entry point for the "db-migrate" subcommand.
63 ///
64 /// \param ui Object to interact with the I/O of the program.
65 /// \param cmdline Representation of the command line to the subcommand.
66 ///
67 /// \return 0 if everything is OK, 1 if the statement is invalid or if there is
68 /// any other problem.
69 int
70 cmd_db_migrate::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
71                     const config::tree& /* user_config */)
72 {
73     try {
74         const fs::path results_file = layout::find_results(
75             results_file_open(cmdline));
76         store::migrate_schema(results_file);
77         return EXIT_SUCCESS;
78     } catch (const store::error& e) {
79         cmdline::print_error(ui, F("Migration failed: %s.") % e.what());
80         return EXIT_FAILURE;
81     }
82 }
83