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
This commit is contained in:
Cory McWilliams 2022-02-12 02:51:43 +00:00
parent 483638a7e6
commit 1bdd67d659

View File

@ -74,10 +74,11 @@ void tf_ssb_db_init(tf_ssb_t* ssb)
sqlite3_stmt* statement = NULL; sqlite3_stmt* statement = NULL;
if (sqlite3_prepare(db, "PRAGMA table_info(messages)", -1, &statement, NULL) == SQLITE_OK) 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* 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) if (name && type && strcmp(name, "timestamp") == 0 && strcmp(type, "INTEGER") == 0)
{ {
need_convert_timestamp_to_real = true; 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) 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, "ALTER TABLE messages ADD COLUMN timestamp_real REAL");
_tf_ssb_db_exec(db, "UPDATE messages SET timestamp_real = timestamp"); _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, "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) 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"); _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 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("%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_FreeCString(context, jv);
JS_FreeValue(context, j); JS_FreeValue(context, j);