Files
TinyORM/tests/testdata_tom/main.cpp
silverqx b13728db08 refactored PostgreSQL search_path configuration
Changed from the 'schema' configuration option to the 'search_path',
the 'schema' config. option was dropped and currently throws exception
with a suggestion to use the 'search_path' instead.

The new 'search_path' config. option can be the QString or QStringList
and also supports to set an empty search_path using '' or empty QString
or QStringList.
This search_path config. option is correctly parsed and quoted.
Setting the search_path is initiated from the PostgresConnector
right after the database connection is established.

The PostgresSchemaBuilder is actively using the search_path during
practically all method calls (dropAllTables(), getColumnListing(),
hasTable(), ...). If the search_path is not defined then it queries
the database for the real search_path because the search_path can be
changed anytime and the search_path configuration option value can be
incorrect (out of sync).
It also works correctly with the empty search_path, in this case a fully
qualified table names are required. If the table name is not fully
qualified then the SearchPathEmptyError exception is thrown.
And it also works correctly with the "$user" PostgreSQL's variable, this
variable is resolved to the config["username"] during obtaining
the search_path from the database.

In the pretend mode it behaves little different, if pretending then
the 'search_path' is obtained from the connection configuration and
if the 'search_path' is not defined in the config. the 'public' will
be used as the default.

 - added many new unit and functional tests which are testing all the
   above described scenarios
   - added new functional test case tst_PostgreSQL_SchemaBuilder_f
   - added new functional test case tst_SchemaBuilder
   - added new tests for dropAllTable(), dropAllViews(), hasTable(),
     getColumnListing(), createDatabase(), dropDatabaseIfExists()
     for ALL supported databases
 - added new search_path constant
 - renamed env. variable to DB_PGSQL_SEARCHPATH (from DB_PGSQL_SCHEMA)
 - added new search_path and dont_drop configuration validations to
   check correct type (QString or QStringList)
 - added new 'schema' config. option validation
 - added new exception SearchPathEmptyError
 - started the TinyUtils::Databases refactor
2023-02-11 09:39:28 +01:00

142 lines
6.7 KiB
C++

#include <orm/db.hpp>
#include <orm/utils/configuration.hpp>
#include <tom/application.hpp>
#include "migrations/2022_05_11_170000_create_users_table.hpp"
#include "migrations/2022_05_11_170100_create_roles_table.hpp"
#include "migrations/2022_05_11_170200_create_role_user_table.hpp"
#include "migrations/2022_05_11_170300_create_user_phones_table.hpp"
#include "migrations/2022_05_11_170400_create_settings_table.hpp"
#include "migrations/2022_05_11_170500_create_torrents_table.hpp"
#include "migrations/2022_05_11_170600_create_torrent_peers_table.hpp"
#include "migrations/2022_05_11_170700_create_torrent_previewable_files_table.hpp"
#include "migrations/2022_05_11_170800_create_torrent_previewable_file_properties_table.hpp"
#include "migrations/2022_05_11_170900_create_file_property_properties_table.hpp"
#include "migrations/2022_05_11_171000_create_torrent_tags_table.hpp"
#include "migrations/2022_05_11_171100_create_tag_torrent_table.hpp"
#include "migrations/2022_05_11_171200_create_tag_properties_table.hpp"
#include "migrations/2022_05_11_171300_create_types_table.hpp"
#include "migrations/2022_05_11_171400_create_datetime_table.hpp"
#include "seeders/databaseseeder.hpp"
using Orm::DatabaseManager;
using Orm::DB;
using ConfigUtils = Orm::Utils::Configuration;
using TomApplication = Tom::Application;
using namespace Migrations; // NOLINT(google-build-using-namespace)
using namespace Seeders; // NOLINT(google-build-using-namespace)
/*! Build the database manager instance and add a database connection. */
std::shared_ptr<DatabaseManager> setupManager();
/*! c++ main function. */
int main(int argc, char *argv[])
{
try {
// Ownership of the shared_ptr()
auto db = setupManager();
return TomApplication(argc, argv, std::move(db), "TOM_TESTDATA_ENV")
// TODO tests, add migration for createDatabase if not exists and remove DB creation from the workflows? silverqx
.migrations<CreateUsersTable,
CreateRolesTable,
CreateRoleUserTable,
CreateUserPhonesTable,
CreateSettingsTable,
CreateTorrentsTable,
CreateTorrentPeersTable,
CreateTorrentPreviewableFilesTable,
CreateTorrentPreviewableFilePropertiesTable,
CreateFilePropertyPropertiesTable,
CreateTorrentTagsTable,
CreateTagTorrentTable,
CreateTagPropertiesTable,
CreateTypesTable,
CreateDatetimeTable>()
.seeders<DatabaseSeeder>()
// Fire it up 🔥🚀✨
.run();
} catch (const std::exception &e) {
TomApplication::logException(e);
}
return EXIT_FAILURE;
}
std::shared_ptr<DatabaseManager> setupManager()
{
using namespace Orm::Constants; // NOLINT(google-build-using-namespace)
// Ownership of the shared_ptr()
return DB::create({
// MySQL connection
{QStringLiteral("tinyorm_testdata_tom_mysql"), { // shell:connection
{driver_, QMYSQL},
{host_, qEnvironmentVariable("DB_MYSQL_HOST", H127001)},
{port_, qEnvironmentVariable("DB_MYSQL_PORT", P3306)},
{database_, qEnvironmentVariable("DB_MYSQL_DATABASE", EMPTY)},
{username_, qEnvironmentVariable("DB_MYSQL_USERNAME", EMPTY)},
{password_, qEnvironmentVariable("DB_MYSQL_PASSWORD", EMPTY)},
{charset_, qEnvironmentVariable("DB_MYSQL_CHARSET", UTF8MB4)},
{collation_, qEnvironmentVariable("DB_MYSQL_COLLATION", UTF8MB40900aici)},
{timezone_, TZ00},
// Specifies what time zone all QDateTime-s will have
{qt_timezone, QVariant::fromValue(Qt::UTC)},
{prefix_, EMPTY},
{prefix_indexes, true},
{strict_, true},
{isolation_level, QStringLiteral("REPEATABLE READ")},
{engine_, InnoDB},
{Version, {}}, // Autodetect
{options_, ConfigUtils::mysqlSslOptions()},
}},
// PostgreSQL connection
{QStringLiteral("tinyorm_testdata_tom_postgres"), { // shell:connection
{driver_, QPSQL},
{application_name, QStringLiteral("tom_testdata")},
{host_, qEnvironmentVariable("DB_PGSQL_HOST", H127001)},
{port_, qEnvironmentVariable("DB_PGSQL_PORT", P5432)},
{database_, qEnvironmentVariable("DB_PGSQL_DATABASE", EMPTY)},
{search_path, qEnvironmentVariable("DB_PGSQL_SEARCHPATH", PUBLIC)},
{username_, qEnvironmentVariable("DB_PGSQL_USERNAME", postgres_)},
{password_, qEnvironmentVariable("DB_PGSQL_PASSWORD", EMPTY)},
{charset_, qEnvironmentVariable("DB_PGSQL_CHARSET", UTF8)},
{timezone_, UTC},
// Specifies what time zone all QDateTime-s will have
{qt_timezone, QVariant::fromValue(Qt::UTC)},
{prefix_, EMPTY},
{prefix_indexes, true},
// {isolation_level, QStringLiteral("REPEATABLE READ")}, // Postgres default is READ COMMITTED
// {synchronous_commit, QStringLiteral("off")}, // Postgres default is on
// ConnectionFactory provides a default value for this (for reference only)
// {dont_drop, QStringList {spatial_ref_sys}},
{options_, ConfigUtils::postgresSslOptions()},
}},
// SQLite connection
{QStringLiteral("tinyorm_testdata_tom_sqlite"), { // shell:connection
{driver_, QSQLITE},
{database_, qEnvironmentVariable("DB_SQLITE_DATABASE", {})},
{foreign_key_constraints, true},
{check_database_exists, true},
// Specifies what time zone all QDateTime-s will have
{qt_timezone, QVariant::fromValue(Qt::UTC)},
/* Return a QDateTime with the correct time zone instead of the QString,
only works when the qt_timezone isn't set to the DontConvert. */
{return_qdatetime, true},
{prefix_, EMPTY},
}}
},
/* Because the default connection name is not defined, then will be needed
to provide the connection name using the --database=xyz argument. */
{});
}