-41
src/db.zig
-41
src/db.zig
···
80
80
};
81
81
82
82
std.debug.print("database initialized\n", .{});
83
-
84
-
// fix timestamps for posts that were indexed before TID parsing was added
85
-
migrateTimestamps();
86
-
}
87
-
88
-
fn migrateTimestamps() void {
89
-
const conn = db orelse return;
90
-
91
-
// check if migration needed (any post with timestamp far in the future relative to its TID)
92
-
var rows = conn.rows("SELECT uri, indexed_at FROM posts LIMIT 100", .{}) catch return;
93
-
defer rows.deinit();
94
-
95
-
var needs_fix: usize = 0;
96
-
while (rows.next()) |row| {
97
-
const uri = row.text(0);
98
-
const stored_ts = row.int(1);
99
-
if (parseTimestampFromUri(uri)) |tid_ts| {
100
-
// if stored timestamp differs from TID timestamp by more than 1 hour, it's wrong
101
-
const diff = if (stored_ts > tid_ts) stored_ts - tid_ts else tid_ts - stored_ts;
102
-
if (diff > 3600000) needs_fix += 1;
103
-
}
104
-
}
105
-
106
-
if (needs_fix == 0) return;
107
-
108
-
std.debug.print("migrating {d} posts with incorrect timestamps...\n", .{needs_fix});
109
-
110
-
// fetch all posts and update timestamps
111
-
var all_rows = conn.rows("SELECT uri FROM posts", .{}) catch return;
112
-
defer all_rows.deinit();
113
-
114
-
var fixed: usize = 0;
115
-
while (all_rows.next()) |row| {
116
-
const uri = row.text(0);
117
-
if (parseTimestampFromUri(uri)) |tid_ts| {
118
-
conn.exec("UPDATE posts SET indexed_at = ? WHERE uri = ?", .{ tid_ts, uri }) catch continue;
119
-
fixed += 1;
120
-
}
121
-
}
122
-
123
-
std.debug.print("fixed {d} post timestamps\n", .{fixed});
124
83
}
125
84
126
85
pub fn addPost(uri: []const u8, cid: []const u8) !void {