|
18 | 18 | #include <stdint.h>
|
19 | 19 |
|
20 | 20 | static const char* const DATABASE_FILENAME = "wallet.dat";
|
| 21 | +static constexpr int32_t WALLET_SCHEMA_VERSION = 0; |
21 | 22 |
|
22 | 23 | static Mutex g_sqlite_mutex;
|
23 | 24 | static int g_sqlite_count GUARDED_BY(g_sqlite_mutex) = 0;
|
@@ -137,6 +138,27 @@ bool SQLiteDatabase::Verify(bilingual_str& error)
|
137 | 138 | return false;
|
138 | 139 | }
|
139 | 140 |
|
| 141 | + // Check our schema version |
| 142 | + sqlite3_stmt* user_ver_stmt{nullptr}; |
| 143 | + ret = sqlite3_prepare_v2(m_db, "PRAGMA user_version", -1, &user_ver_stmt, nullptr); |
| 144 | + if (ret != SQLITE_OK) { |
| 145 | + sqlite3_finalize(user_ver_stmt); |
| 146 | + error = strprintf(_("SQLiteDatabase: Failed to prepare the statement to fetch sqlite wallet schema version: %s"), sqlite3_errstr(ret)); |
| 147 | + return false; |
| 148 | + } |
| 149 | + ret = sqlite3_step(user_ver_stmt); |
| 150 | + if (ret != SQLITE_ROW) { |
| 151 | + sqlite3_finalize(user_ver_stmt); |
| 152 | + error = strprintf(_("SQLiteDatabase: Failed to fetch sqlite wallet schema version: %s"), sqlite3_errstr(ret)); |
| 153 | + return false; |
| 154 | + } |
| 155 | + int32_t user_ver = sqlite3_column_int(user_ver_stmt, 0); |
| 156 | + sqlite3_finalize(user_ver_stmt); |
| 157 | + if (user_ver != WALLET_SCHEMA_VERSION) { |
| 158 | + error = strprintf(_("SQLiteDatabase: Unknown sqlite wallet schema version %d. Only version %d is supported"), user_ver, WALLET_SCHEMA_VERSION); |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
140 | 162 | sqlite3_stmt* stmt{nullptr};
|
141 | 163 | ret = sqlite3_prepare_v2(m_db, "PRAGMA integrity_check", -1, &stmt, nullptr);
|
142 | 164 | if (ret != SQLITE_OK) {
|
@@ -246,6 +268,13 @@ void SQLiteDatabase::Open()
|
246 | 268 | if (ret != SQLITE_OK) {
|
247 | 269 | throw std::runtime_error(strprintf("SQLiteDatabase: Failed to set the application id: %s\n", sqlite3_errstr(ret)));
|
248 | 270 | }
|
| 271 | + |
| 272 | + // Set the user version |
| 273 | + std::string set_user_ver = strprintf("PRAGMA user_version = %d", WALLET_SCHEMA_VERSION); |
| 274 | + ret = sqlite3_exec(m_db, set_user_ver.c_str(), nullptr, nullptr, nullptr); |
| 275 | + if (ret != SQLITE_OK) { |
| 276 | + throw std::runtime_error(strprintf("SQLiteDatabase: Failed to set the wallet schema version: %s\n", sqlite3_errstr(ret))); |
| 277 | + } |
249 | 278 | }
|
250 | 279 | }
|
251 | 280 |
|
|
0 commit comments