|
11 | 11 | #include <unistd.h>
|
12 | 12 |
|
13 | 13 | #include "db_connection.h"
|
| 14 | +#include "cflock.h" |
14 | 15 | #include "sqlite_database_scheme.h"
|
15 | 16 | #include "sqlite_database_scheme_updates.h"
|
16 | 17 | #include "qexcdatabase.h"
|
| 18 | +#include "qfilethrow.h" |
17 | 19 | #include "qsqlquerythrow.h"
|
18 | 20 | #include "logger.h"
|
19 | 21 | #include "app.h"
|
|
22 | 24 |
|
23 | 25 | static QSqlDatabase* g_db = nullptr;
|
24 | 26 |
|
| 27 | +static bool versionTableExists(QSqlQueryThrow& query){ |
| 28 | + logDebug << "checking for version table..."; |
| 29 | + query.exec("SELECT name FROM sqlite_master WHERE type='table' AND name='version'"); |
| 30 | + return query.next(); |
| 31 | +} |
| 32 | + |
25 | 33 | static QVersionNumber queryVersion(QSqlQueryThrow& query){
|
26 | 34 | query.exec("select ver from version");
|
27 | 35 | query.next(true);
|
@@ -88,55 +96,76 @@ static void handleDifferentVersions(const QVersionNumber& dbVersion,
|
88 | 96 |
|
89 | 97 | }
|
90 | 98 |
|
91 |
| -/// @param versionAction: if true, update version, if needed |
92 |
| -/// @throws QExcDatabase |
93 |
| -static void openAndPrepareSqliteDb() |
94 |
| -{ |
95 |
| - const QString appDataLoc = db_connection::mkDbPath(); |
96 |
| - const QString dbPath = appDataLoc + "/database.db"; |
97 |
| - |
98 |
| - bool pathExisted = QFileInfo::exists(dbPath); |
99 |
| - g_db->setDatabaseName(dbPath); |
100 |
| - if(! g_db->open()) { |
101 |
| - throw QExcDatabase(__func__, g_db->lastError()); |
102 |
| - } |
103 |
| - |
104 |
| - QSqlQueryThrow query(*g_db); |
105 |
| - // Allow for delete queries with cascades |
106 |
| - |
| 99 | +static void |
| 100 | +createOrUpDateDb(QSqlQueryThrow &query, CFlock& lock){ |
| 101 | + logDebug << "about to lockExclusive database for scheme update..."; |
107 | 102 | // quoting sqlite.org/foreignkeys.html
|
108 | 103 | // "It is not possible to enable or disable foreign key constraints in the
|
109 | 104 | // middle of a multi-statement transaction (when SQLite is not in autocommit mode)"
|
110 |
| - // The scheme-updates require foreign_keys=OFF, so enable below |
| 105 | + // The scheme-updates require foreign_keys=OFF, so call below pragma: |
111 | 106 | query.exec("PRAGMA foreign_keys=OFF");
|
112 |
| - |
| 107 | + lock.lockExclusive(); |
113 | 108 | query.transaction();
|
114 | 109 |
|
115 |
| - if(! pathExisted){ |
116 |
| - // Initially create the database |
| 110 | + if(! versionTableExists(query)){ |
117 | 111 | logInfo << qtr("Creating new sqlite database");
|
118 |
| - QStringList statements = QString(SQLITE_DATABASE_SCHEME).split(';', QString::SkipEmptyParts); |
119 |
| - |
| 112 | + QStringList statements = QString( |
| 113 | + SQLITE_DATABASE_SCHEME).split(';', QString::SkipEmptyParts); |
120 | 114 | for(const QString& stmt : statements){
|
121 | 115 | query.exec(stmt);
|
122 | 116 | }
|
123 |
| - QFile dbfile(appDataLoc); |
124 |
| - if(! dbfile.setPermissions( |
125 |
| - QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner)){ |
126 |
| - logWarning << qtr("Failed to initially set permissions on the database-directory " |
127 |
| - "at %1: %2. Other users might be able " |
| 117 | + |
| 118 | + QFile dbDir(db_connection::getDatabaseDir()); |
| 119 | + if(! dbDir.setPermissions( |
| 120 | + QFileDevice::ReadOwner|QFileDevice::WriteOwner|QFileDevice::ExeOwner)){ |
| 121 | + logWarning << qtr("Failed to initially set permissions on the database-" |
| 122 | + "directory at %1: %2. Other users might be able " |
128 | 123 | "to browse your command history...")
|
129 |
| - .arg(dbPath, dbfile.errorString()); |
| 124 | + .arg(db_connection::getDatabaseDir(), dbDir.errorString()); |
130 | 125 | }
|
131 | 126 | }
|
132 | 127 | const auto dbVersion = queryVersion(query);
|
133 |
| - logDebug << "current db-version" << dbVersion.toString(); |
134 | 128 | if(dbVersion != app::version()){
|
135 | 129 | handleDifferentVersions(dbVersion, query);
|
136 | 130 | }
|
137 |
| - |
138 | 131 | query.commit();
|
139 | 132 | // outside of transaction (see above):
|
| 133 | + // Allow for delete queries with cascades |
| 134 | + query.exec("PRAGMA foreign_keys=ON"); |
| 135 | +} |
| 136 | + |
| 137 | +/// @throws QExcDatabase |
| 138 | +static void openAndPrepareSqliteDb() |
| 139 | +{ |
| 140 | + const QString appDataLoc = db_connection::mkDbPath(); |
| 141 | + const QString dbPath = appDataLoc + "/database.db"; |
| 142 | + QFileThrow lockfile(appDataLoc + "/.shournal-dblock"); |
| 143 | + lockfile.open(QFile::OpenModeFlag::ReadWrite); |
| 144 | + // Lock to allow for concurrent database scheme updates. First, we lock shared, |
| 145 | + // upgrading to exclusive on scheme update. Note that for some reason concurrent |
| 146 | + // processes executing "PRAGMA locking_mode=EXCLUSIVE; BEGIN EXCLUSIVE;" deadlocked |
| 147 | + // during integration tests, so be careful with that directive. |
| 148 | + CFlock lock(lockfile.handle()); |
| 149 | + lock.lockShared(); |
| 150 | + g_db->setDatabaseName(dbPath); |
| 151 | + if(! g_db->open()) { |
| 152 | + throw QExcDatabase(__func__, g_db->lastError()); |
| 153 | + } |
| 154 | + |
| 155 | + QSqlQueryThrow query(*g_db); |
| 156 | + if(! versionTableExists(query)){ |
| 157 | + logDebug << "version table did not exist yet.."; |
| 158 | + createOrUpDateDb(query, lock); |
| 159 | + } else { |
| 160 | + const auto dbVersion = queryVersion(query); |
| 161 | + logDebug << "current db-version" << dbVersion.toString(); |
| 162 | + if(dbVersion != app::version()){ |
| 163 | + createOrUpDateDb(query, lock); |
| 164 | + } |
| 165 | + } |
| 166 | + lock.unlock(); |
| 167 | + |
| 168 | + // Allow for delete queries with cascades |
140 | 169 | query.exec("PRAGMA foreign_keys=ON");
|
141 | 170 | }
|
142 | 171 |
|
|
0 commit comments