Also use exponential backoff for a locked db in the initial database schema, and support passing a QString to exec()
This commit is contained in:
parent
8b6ff0a865
commit
296f37262d
@ -56,11 +56,13 @@ QSqlDatabase get_thread_connection() {
|
|||||||
return thread_db;
|
return thread_db;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ci_query_exec(QSqlQuery* query) {
|
bool ci_query_exec(QSqlQuery* query, const QString query_string) {
|
||||||
bool passed = false;
|
bool passed = false;
|
||||||
int attempt = 0;
|
int attempt = 0;
|
||||||
while (!passed) {
|
while (!passed) {
|
||||||
passed = query->exec();
|
if (query_string.isEmpty()) passed = query->exec();
|
||||||
|
else passed = query->exec(query_string);
|
||||||
|
|
||||||
if (passed) return true;
|
if (passed) return true;
|
||||||
attempt++;
|
attempt++;
|
||||||
|
|
||||||
@ -79,9 +81,9 @@ bool init_database(const QString& database_path) {
|
|||||||
// Apply PRAGMAs
|
// Apply PRAGMAs
|
||||||
{
|
{
|
||||||
QSqlQuery pragma_query(get_thread_connection());
|
QSqlQuery pragma_query(get_thread_connection());
|
||||||
pragma_query.exec("PRAGMA journal_mode = WAL;");
|
ci_query_exec(&pragma_query, "PRAGMA journal_mode = WAL;");
|
||||||
pragma_query.exec("PRAGMA synchronous = NORMAL;");
|
ci_query_exec(&pragma_query, "PRAGMA synchronous = NORMAL;");
|
||||||
pragma_query.exec("PRAGMA foreign_keys = ON;");
|
ci_query_exec(&pragma_query, "PRAGMA foreign_keys = ON;");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the schema creation (or migration) statements
|
// Run the schema creation (or migration) statements
|
||||||
@ -200,7 +202,7 @@ bool init_database(const QString& database_path) {
|
|||||||
for (const QString &statement : sql_statements) {
|
for (const QString &statement : sql_statements) {
|
||||||
QSqlQuery query(get_thread_connection());
|
QSqlQuery query(get_thread_connection());
|
||||||
QString trimmed = statement.trimmed();
|
QString trimmed = statement.trimmed();
|
||||||
if (!trimmed.isEmpty() && !query.exec(trimmed)) {
|
if (!trimmed.isEmpty() && !ci_query_exec(&query, trimmed)) {
|
||||||
qDebug() << "Failed to execute SQL: " << trimmed
|
qDebug() << "Failed to execute SQL: " << trimmed
|
||||||
<< "\nError: " << query.lastError().text();
|
<< "\nError: " << query.lastError().text();
|
||||||
return false;
|
return false;
|
||||||
|
@ -18,9 +18,10 @@
|
|||||||
|
|
||||||
#include <QSqlDatabase>
|
#include <QSqlDatabase>
|
||||||
#include <QSqlQuery>
|
#include <QSqlQuery>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
QSqlDatabase get_thread_connection();
|
QSqlDatabase get_thread_connection();
|
||||||
bool ci_query_exec(QSqlQuery* query);
|
bool ci_query_exec(QSqlQuery* query, const QString query_string = "");
|
||||||
bool init_database(const QString& database_path);
|
bool init_database(const QString& database_path);
|
||||||
|
|
||||||
#endif // DB_COMMON_H
|
#endif // DB_COMMON_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user