-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.cpp
194 lines (177 loc) · 10 KB
/
main.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <orm/db.hpp>
#include <orm/utils/configuration.hpp>
#include <tom/application.hpp>
#include "migrations/2014_10_12_000000_create_posts_table.hpp"
#include "migrations/2014_10_12_100000_add_factor_column_to_posts_table.hpp"
#include "migrations/2014_10_12_200000_create_properties_table.hpp"
#include "migrations/2014_10_12_300000_create_phones_table.hpp"
#include "seeders/databaseseeder.hpp"
using Orm::DatabaseManager;
using Orm::DB;
using Orm::QtTimeZoneConfig;
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)
/*! Create the database manager instance and add a database connection. */
std::shared_ptr<DatabaseManager> setupDatabaseManager();
/*! C++ main function. */
int main(int argc, char *argv[])
{
try {
// Ownership of the shared_ptr()
auto db = setupDatabaseManager();
return TomApplication(argc, argv, std::move(db), "TOM_EXAMPLE_ENV",
QStringLiteral("migrations_example"))
/* Default migrations path for the make:migration command, the path
can be absolute or relative (to the pwd at runtime). */
// .migrationsPath("database/migrations")
// .migrationsPath(std::filesystem::current_path() / "database" / "migrations")
/* Migration classes can be named in two formats, StudlyCase without
the datetime prefix and snake_case with the datetime prefix.
If the StudlyCase name is used then the T_MIGRATION macro has to be
also used in the migration class. */
.migrations<CreatePostsTable,
// _2014_10_12_000000_create_posts_table,
AddFactorColumnToPostsTable,
CreatePropertiesTable,
CreatePhonesTable>()
/* Seeder classes, the DatabaseSeeder is the default/root seeder class,
it must always exist if the --class command-line argument is not
provided, or you can provide a custom name through the --class
argument.
The order of seeder classes doesn't matter, they will be called
in the order defined by the call<>() method inside the seeders
themselves. */
.seeders<DatabaseSeeder>()
// Fire it up 🔥🚀✨
.run();
} catch (const std::exception &e) {
TomApplication::logException(e);
}
return EXIT_FAILURE;
}
std::shared_ptr<DatabaseManager> setupDatabaseManager()
{
using namespace Orm::Constants; // NOLINT(google-build-using-namespace)
// Ownership of the shared_ptr()
return DB::create({
#if !defined(PROJECT_TINYDRIVERS) || defined(TINYDRIVERS_MYSQL_DRIVER)
// MySQL connection
{QStringLiteral("tinyorm_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)},
// SYSTEM - set the time zone to your local MySQL server time zone
{timezone_, TZ00},
/* Specifies what time zone all QDateTime-s will have, the overridden default is
the QTimeZone::UTC, set to the QTimeZone::LocalTime or
QtTimeZoneType::DontConvert to use the system local time. */
{qt_timezone, QVariant::fromValue(QtTimeZoneConfig::utc())},
{prefix_, EMPTY},
{prefix_indexes, false},
{strict_, true},
{isolation_level, QStringLiteral("REPEATABLE READ")}, // MySQL default is REPEATABLE READ for InnoDB
{engine_, InnoDB},
{Version, {}}, // Autodetect
{options_, ConfigUtils::mysqlSslOptions()},
}},
// MariaDB connection
{QStringLiteral("tinyorm_tom_maria"), { // shell:connection
{driver_, QMYSQL},
{host_, qEnvironmentVariable("DB_MARIA_HOST", H127001)},
{port_, qEnvironmentVariable("DB_MARIA_PORT", P3306)},
{database_, qEnvironmentVariable("DB_MARIA_DATABASE", EMPTY)},
{username_, qEnvironmentVariable("DB_MARIA_USERNAME", EMPTY)},
{password_, qEnvironmentVariable("DB_MARIA_PASSWORD", EMPTY)},
{charset_, qEnvironmentVariable("DB_MARIA_CHARSET", UTF8MB4)},
{collation_, qEnvironmentVariable("DB_MARIA_COLLATION",
UTF8MB4Uca1400aici)},
// SYSTEM - set the time zone to your local MySQL server time zone
{timezone_, TZ00},
/* Specifies what time zone all QDateTime-s will have, the overridden default
is the QTimeZone::UTC, set to the QTimeZone::LocalTime or
QtTimeZoneType::DontConvert to use the system local time. */
{qt_timezone, QVariant::fromValue(QtTimeZoneConfig::utc())},
{prefix_, EMPTY},
{prefix_indexes, false},
{strict_, true},
{isolation_level, QStringLiteral("REPEATABLE READ")}, // MySQL default is REPEATABLE READ for InnoDB
{engine_, InnoDB},
{Version, {}}, // Autodetect
{options_, ConfigUtils::mariaSslOptions()},
}},
#endif
#if !defined(PROJECT_TINYDRIVERS) || defined(TINYDRIVERS_PSQL_DRIVER)
// PostgreSQL connection
{QStringLiteral("tinyorm_tom_postgres"), { // shell:connection
{driver_, QPSQL},
{application_name, QStringLiteral("tom")},
{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)},
// LOCAL/DEFAULT - set the time zone to your local PostgreSQL server time zone
{timezone_, UTC},
/* Specifies what time zone all QDateTime-s will have, the overridden default
is the QTimeZone::UTC, set to the QTimeZone::LocalTime or
QtTimeZoneType::DontConvert to use the system local time. */
{qt_timezone, QVariant::fromValue(QtTimeZoneConfig::utc())},
// Examples of qt_timezone
// {qt_timezone, QVariant::fromValue(QTimeZone("Europe/Bratislava"))},
// {qt_timezone, "Europe/Prague"}, // Will be converted to QTimeZone("Europe/Prague")
// {qt_timezone, QVariant::fromValue(QTimeZone("UTC+04"))},
// {qt_timezone, "-03:00"},
// {qt_timezone, 3600}, // Offset from UTC
// {qt_timezone, QVariant::fromValue(QTimeZone::LocalTime)}, // To support Qt <v6.5 use Orm::QtTimeZoneConfig::utc/localTime()
// {qt_timezone, {}}, // The same as QTimeZone::UTC
{prefix_, EMPTY},
{prefix_indexes, false},
// {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()},
}},
#endif
#if !defined(PROJECT_TINYDRIVERS) || defined(TINYDRIVERS_SQLITE_DRIVER)
// SQLite connection
{QStringLiteral("tinyorm_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, the overridden default
is the QTimeZone::UTC, set to the QTimeZone::LocalTime or
QtTimeZoneType::DontConvert to use the system local time. */
{qt_timezone, QVariant::fromValue(QtTimeZoneConfig::utc())},
/* Return a QDateTime/QDate 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},
{prefix_indexes, false},
}}
#endif
},
// MySQL as the default database connection
QStringLiteral("tinyorm_tom_mysql"));
}
/* Alternative syntax to instantiate migration classes. */
// return TomApplication(argc, argv, std::move(db), "TOM_EXAMPLE_ENV",
// QStringLiteral("migrations_table")
// {
// std::make_shared<CreatePostsTable>(),
// std::make_shared<AddFactorColumnToPostsTable>(),
// std::make_shared<CreatePropertiesTable>(),
// std::make_shared<CreatePhonesTable>(),
// })
// // Fire it up 🔥🚀✨
// .run();