Skip to content

Commit dc7305d

Browse files
committed
change hb id to bigint + fix time type?
1 parent 3c6209a commit dc7305d

File tree

6 files changed

+66
-9
lines changed

6 files changed

+66
-9
lines changed

rustytime/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rustytime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "rustytime-server"
33
description = "🕒 blazingly fast time tracking for developers"
4-
version = "0.5.5"
4+
version = "0.5.6"
55
edition = "2024"
66
authors = ["ImShyMike"]
77
readme = "../README.md"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Disable compression
2+
ALTER TABLE heartbeats SET (timescaledb.compress = false);
3+
4+
-- Drop default
5+
ALTER TABLE heartbeats ALTER COLUMN id DROP DEFAULT;
6+
7+
-- Revert to INTEGER
8+
ALTER TABLE heartbeats ALTER COLUMN id TYPE INTEGER;
9+
10+
-- Drop sequence
11+
DROP SEQUENCE IF EXISTS heartbeats_id_seq;
12+
13+
-- Re-enable compression
14+
ALTER TABLE heartbeats SET (timescaledb.compress = true);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
-- 0. Decompress all compressed chunks
2+
DO $$
3+
DECLARE
4+
r RECORD;
5+
BEGIN
6+
FOR r IN
7+
SELECT
8+
c.schema_name AS chunk_schema,
9+
c.table_name AS chunk_name
10+
FROM _timescaledb_catalog.hypertable h
11+
JOIN _timescaledb_catalog.chunk c ON c.hypertable_id = h.id
12+
WHERE h.table_name = 'heartbeats'
13+
AND c.compressed_chunk_id IS NOT NULL
14+
LOOP
15+
EXECUTE format('SELECT decompress_chunk(''%I.%I'')', r.chunk_schema, r.chunk_name);
16+
END LOOP;
17+
END
18+
$$;
19+
20+
-- 1. Disable compression temporarily
21+
ALTER TABLE heartbeats SET (timescaledb.compress = false);
22+
23+
-- 2. Change column type from INTEGER -> BIGINT
24+
ALTER TABLE heartbeats ALTER COLUMN id TYPE BIGINT;
25+
26+
-- 3. Ensure the sequence exists and is attached
27+
DO $$
28+
BEGIN
29+
IF NOT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'heartbeats_id_seq') THEN
30+
CREATE SEQUENCE heartbeats_id_seq OWNED BY heartbeats.id;
31+
END IF;
32+
END
33+
$$;
34+
35+
ALTER TABLE heartbeats ALTER COLUMN id SET DEFAULT nextval('heartbeats_id_seq');
36+
37+
-- 4. Re-enable compression
38+
ALTER TABLE heartbeats SET (timescaledb.compress = true);

rustytime/src/models/heartbeat.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use diesel::prelude::*;
44
use diesel::sql_types::{BigInt, Date, Nullable, Text};
55
use ipnetwork::IpNetwork;
66
use serde::{Deserialize, Serialize};
7-
use serde_json::Value;
87

98
use crate::schema::heartbeats;
109
use crate::utils::http::parse_user_agent;
@@ -167,11 +166,11 @@ pub enum HeartbeatApiResponseVariant {
167166

168167
#[derive(Serialize, Debug)]
169168
pub struct HeartbeatResponse {
170-
pub id: String,
169+
pub id: i64,
171170
pub entity: String,
172171
#[serde(rename = "type")]
173172
pub type_: String,
174-
pub time: DateTime<Utc>,
173+
pub time: i64,
175174
}
176175

177176
#[derive(Serialize, Debug)]
@@ -191,7 +190,7 @@ pub struct BulkResponseItem(pub HeartbeatResponse, pub u16);
191190
#[diesel(table_name = heartbeats)]
192191
#[diesel(check_for_backend(diesel::pg::Pg))]
193192
pub struct Heartbeat {
194-
pub id: i32,
193+
pub id: i64,
195194
pub time: DateTime<Utc>,
196195
pub created_at: DateTime<Utc>,
197196
pub user_id: i32,
@@ -431,13 +430,19 @@ impl NewHeartbeat {
431430
sanitized.into_new_heartbeat(user_id, ip_address, headers)
432431
}
433432
}
433+
434434
impl From<Heartbeat> for HeartbeatResponse {
435435
fn from(heartbeat: Heartbeat) -> Self {
436+
let time = heartbeat
437+
.time
438+
.timestamp_nanos_opt()
439+
.unwrap_or_else(|| heartbeat.time.timestamp() * 1_000_000_000);
440+
436441
Self {
437-
id: heartbeat.id.to_string(),
442+
id: heartbeat.id,
438443
entity: heartbeat.entity,
439444
type_: heartbeat.type_,
440-
time: heartbeat.time,
445+
time,
441446
}
442447
}
443448
}

rustytime/src/schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
diesel::table! {
44
heartbeats (user_id, time) {
5-
id -> Int4,
5+
id -> Int8,
66
time -> Timestamptz,
77
created_at -> Timestamptz,
88
user_id -> Int4,

0 commit comments

Comments
 (0)