|
9 | 9 | std::mutex dbCrit; |
10 | 10 | sqlite3 *db; |
11 | 11 |
|
| 12 | +/* |
| 13 | + * When migrating from DB version 3 to 4, we change the username column |
| 14 | + * to be case-insensitive. This function ensures there aren't any |
| 15 | + * duplicates, e.g. username and USERNAME, before doing the migration. |
| 16 | + * I handled this in the code itself rather than the migration file just so |
| 17 | + * we can have a more detailed error message than what SQLite provides. |
| 18 | + */ |
| 19 | +static void checkCaseSensitiveDupes() { |
| 20 | + const char* sql = "SELECT Login, COUNT(*) FROM Accounts GROUP BY LOWER(Login) HAVING COUNT(*) > 1;"; |
| 21 | + |
| 22 | + sqlite3_stmt* stmt; |
| 23 | + sqlite3_prepare_v2(db, sql, -1, &stmt, NULL); |
| 24 | + int stat = sqlite3_step(stmt); |
| 25 | + |
| 26 | + if (stat == SQLITE_DONE) { |
| 27 | + // no rows returned, so we're good |
| 28 | + sqlite3_finalize(stmt); |
| 29 | + return; |
| 30 | + } else if (stat != SQLITE_ROW) { |
| 31 | + std::cout << "[FATAL] Failed to check for duplicate accounts: " << sqlite3_errmsg(db) << std::endl; |
| 32 | + sqlite3_finalize(stmt); |
| 33 | + exit(1); |
| 34 | + } |
| 35 | + |
| 36 | + std::cout << "[FATAL] Case-sensitive duplicates detected in the Login column." << std::endl; |
| 37 | + std::cout << "Either manually delete/rename the offending accounts, or run the pruning script:" << std::endl; |
| 38 | + std::cout << "https://github.com/OpenFusionProject/scripts/tree/main/db_migration/caseinsens.py" << std::endl; |
| 39 | + sqlite3_finalize(stmt); |
| 40 | + exit(1); |
| 41 | +} |
| 42 | + |
12 | 43 | static void createMetaTable() { |
13 | 44 | std::lock_guard<std::mutex> lock(dbCrit); // XXX |
14 | 45 |
|
@@ -143,6 +174,10 @@ static void checkMetaTable() { |
143 | 174 | } |
144 | 175 |
|
145 | 176 | while (dbVersion != DATABASE_VERSION) { |
| 177 | + // need to run this before we do any migration logic |
| 178 | + if (dbVersion == 3) |
| 179 | + checkCaseSensitiveDupes(); |
| 180 | + |
146 | 181 | // db migrations |
147 | 182 | std::cout << "[INFO] Migrating Database to Version " << dbVersion + 1 << std::endl; |
148 | 183 |
|
|
0 commit comments