-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Christian Parpart <[email protected]>
- Loading branch information
1 parent
a6b6d23
commit a1b24d0
Showing
6 changed files
with
313 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "utils.hpp" | ||
|
||
#include <Lightweight/SqlColumnTypeDefinitions.hpp> | ||
#include <Lightweight/SqlMigration.hpp> | ||
#include <Lightweight/SqlQuery.hpp> | ||
#include <Lightweight/SqlSchema.hpp> | ||
|
||
#include <cstdlib> | ||
#include <iostream> | ||
|
||
void showHelp() | ||
{ | ||
// DRAFT idea on how the CLI utility could look like | ||
// clang-format off | ||
std::cout << "Usage: dbtool <COMMAND ...>\n" | ||
"\n" | ||
"dbtool is a tool for managing database migrations in C++ projects.\n" | ||
"\n" | ||
" dbtool help - prints this help message\n" | ||
" dbtool version - prints the version of dbtool\n" | ||
"\n" | ||
" Migration tasks\n" | ||
"\n" | ||
" dbtool migrate - applies pending migrations to the database\n" | ||
" dbtool list-pending - lists pending migrations\n" | ||
" dbtool list-applied - lists applied migrations\n" | ||
" dbtool create <NAME> - creates a new migration with the given name\n" | ||
" dbtool apply <NAME> - applies the migration with the given name\n" | ||
" dbtool rollback <NAME> - rolls back the migration with the given name\n" | ||
"\n" | ||
" Options:\n" | ||
"\n" | ||
" -m, --module <MODULE_NAME> - specifies the module name (DLL or .so file) to load migrations from\n" | ||
" Migration libraries are loaded automatically relative to the executables path\n" | ||
"\n" | ||
" Databas eadministration tasks\n" | ||
"\n" | ||
" dbtool dump-schema <DSN> - Dumps the schema of a given database to stdout\n" | ||
" dbtool backup <DSN> to <FILE> - Creates a backup of a given database\n" | ||
" dbtool restore <FILE> to <DSN> - Restores a database from a backup file\n" | ||
"\n" | ||
"Examples:\n" | ||
" dbtool create add_users_table\n" | ||
" dbtool apply add_users_table\n" | ||
" dbtool rollback add_users_table\n" | ||
" dbtool migrate\n" | ||
"\n"; | ||
// clang-format on | ||
} | ||
|
||
SqlMigrationQueryBuilder BuildStructureFromSchema(std::string_view databaseName, | ||
std::string_view schemaName, | ||
SqlQueryFormatter const& dialect) | ||
{ | ||
auto builder = SqlMigrationQueryBuilder { dialect }; | ||
SqlSchema::TableList tables = SqlSchema::ReadAllTables(databaseName, schemaName); | ||
for (SqlSchema::Table const& table: tables) | ||
{ | ||
auto tableBuilder = builder.CreateTable(table.name); | ||
for (SqlSchema::Column const& column: table.columns) | ||
{ | ||
auto columnDeclaration = SqlColumnDeclaration { | ||
.name = column.name, | ||
.type = column.type, | ||
.required = !column.isNullable, | ||
.unique = column.isUnique, | ||
.index = false, // TODO | ||
}; | ||
tableBuilder.Column(std::move(columnDeclaration)); | ||
} | ||
} | ||
return builder; | ||
} | ||
|
||
using namespace std::string_literals; | ||
using namespace std::string_view_literals; | ||
|
||
struct DumpSchemaConfiguration | ||
{ | ||
std::string connectionString; | ||
std::string database = "testdb"; | ||
std::optional<std::string> schema; | ||
bool help = false; | ||
}; | ||
|
||
int dumpSchema(int argc, char const* argv[]) | ||
{ | ||
auto config = DumpSchemaConfiguration {}; | ||
if (!parseCommandLineArguments(&config, argc, argv)) | ||
return EXIT_FAILURE; | ||
|
||
if (config.help) | ||
{ | ||
showHelp(); | ||
return EXIT_SUCCESS; | ||
} | ||
|
||
SqlConnection::SetDefaultConnectionString(SqlConnectionString { config.connectionString }); | ||
|
||
auto const* dialect = &SqlQueryFormatter::SqlServer(); // TODO: configurable | ||
|
||
auto createStructureStmts = BuildStructureFromSchema(config.database, config.schema.value_or(""), *dialect); | ||
auto const planSql = createStructureStmts.GetPlan().ToSql(); | ||
for (auto const& sql: planSql) | ||
std::cout << sql << '\n'; | ||
|
||
return EXIT_SUCCESS; | ||
} | ||
|
||
int main(int argc, char const* argv[]) | ||
{ | ||
if (argc <= 1 || (argv[1] == "help"sv || argv[1] == "--help"sv || argv[1] == "-h"sv)) | ||
{ | ||
showHelp(); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
if (argv[1] == "dump-schema"sv) | ||
return dumpSchema(argc - 2, argv + 2); | ||
|
||
std::cerr << "Unknown command: " << argv[1] << '\n'; | ||
return EXIT_FAILURE; | ||
} |
Oops, something went wrong.