From 1bdd67d659224b77e43b7716650ca1b9fbbdda0c Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 12 Feb 2022 02:51:43 +0000 Subject: [PATCH] Migration fixes, and make 'check' delete invalid data. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3836 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/ssb.db.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/ssb.db.c b/src/ssb.db.c index a90bbe7a..4836af24 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -74,10 +74,11 @@ void tf_ssb_db_init(tf_ssb_t* ssb) sqlite3_stmt* statement = NULL; if (sqlite3_prepare(db, "PRAGMA table_info(messages)", -1, &statement, NULL) == SQLITE_OK) { - while (sqlite3_step(statement) == SQLITE_ROW) + int result = SQLITE_OK; + while ((result = sqlite3_step(statement)) == SQLITE_ROW) { const char* name = (const char*)sqlite3_column_text(statement, 1); - const char* type = (const char*)sqlite3_column_text(statement, 1); + const char* type = (const char*)sqlite3_column_text(statement, 2); if (name && type && strcmp(name, "timestamp") == 0 && strcmp(type, "INTEGER") == 0) { need_convert_timestamp_to_real = true; @@ -92,13 +93,19 @@ void tf_ssb_db_init(tf_ssb_t* ssb) if (need_convert_timestamp_to_real) { + printf("Converting timestamp column from INTEGER to REAL.\n"); + _tf_ssb_db_exec(db, "BEGIN TRANSACTION"); + _tf_ssb_db_exec(db, "DROP INDEX IF EXISTS messages_author_timestamp_index"); _tf_ssb_db_exec(db, "ALTER TABLE messages ADD COLUMN timestamp_real REAL"); _tf_ssb_db_exec(db, "UPDATE messages SET timestamp_real = timestamp"); - _tf_ssb_db_exec(db, "ALTER TABLE messages DROP COLUMN timestamp REAL"); + _tf_ssb_db_exec(db, "ALTER TABLE messages DROP COLUMN timestamp"); _tf_ssb_db_exec(db, "ALTER TABLE messages RENAME COLUMN timestamp_real TO timestamp"); + _tf_ssb_db_exec(db, "CREATE INDEX IF NOT EXISTS messages_author_timestamp_index ON messages (author, timestamp)"); + _tf_ssb_db_exec(db, "COMMIT TRANSACTION"); } if (need_add_sequence_before_author) { + printf("Adding sequence_before_author column.\n"); _tf_ssb_db_exec(db, "ALTER TABLE messages ADD COLUMN sequence_before_author INTEGER"); } } @@ -639,6 +646,21 @@ bool tf_ssb_db_check(sqlite3* db, const char* check_author) else { printf("%s sequence=%" PRId64 " unable to verify signature for %s sequence_before_author=%d message=[%.*s]\n", author, sequence, id, sequence_before_author, (int)strlen(jv), jv); + + printf("Deleting author = %s sequence >= %" PRId64 ".\n", author, sequence); + sqlite3_stmt* delete_statement = NULL; + if (sqlite3_prepare(db, "DELETE FROM messages WHERE author = ? AND sequence >= ?", -1, &delete_statement, NULL) == SQLITE_OK) + { + if (sqlite3_bind_text(delete_statement, 1, author, -1, NULL) == SQLITE_OK && + sqlite3_bind_int64(delete_statement, 2, sequence) == SQLITE_OK) + { + if (sqlite3_step(delete_statement) != SQLITE_DONE) + { + printf("Error deleting author = %s sequence >= %" PRId64 ".\n", author, sequence); + } + } + sqlite3_finalize(delete_statement); + } } JS_FreeCString(context, jv); JS_FreeValue(context, j);