Skip to content

Commit 76138e9

Browse files
authored
feature(pedm): SQL initialization (#1302)
- adds version tracking in the `pedm_schema_version` table - adds table initialization - error checking is strict and initialization only happens if the version table is not found - adds SQLite error handling for microsecond timestamp conversion to/from Chrono - fixes a bug in the libSQL schema where datetimes were stored incorrectly - they were being stored fractionally, instead of the full microseconds since epoch - adds `/about` endpoint to get basic info about the running application - I used this to ensure that the libsql date handling is functional
1 parent cdbe0b7 commit 76138e9

16 files changed

Lines changed: 543 additions & 223 deletions

File tree

Cargo.lock

Lines changed: 23 additions & 96 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/devolutions-pedm/Cargo.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ anyhow = "1.0"
1414
axum = { version = "0.8", default-features = false, features = ["http1", "json", "tokio", "query", "tracing", "tower-log", "form", "original-uri", "matched-path"] }
1515
base16ct = { version = "0.2", features = ["std", "alloc"] }
1616
base64 = "0.22"
17+
chrono = { version = "0.4", features = ["serde"] }
1718
digest = "0.10"
1819
hyper = { version = "1.3", features = ["server"] }
1920
hyper-util = { version = "0.1", features = ["tokio"] }
20-
schemars = "0.8"
21+
schemars = { version = "0.8", features = ["chrono"] }
2122
serde = "1.0"
2223
serde_json = "1.0"
2324
sha1 = "0.10"
@@ -39,10 +40,10 @@ uuid = "1"
3940
dunce = "1.0"
4041
tower = "0.5"
4142
futures-util = "0.3"
42-
libsql = { version = "0.9", optional = true, features = [ "core", "stream"] }
43-
tokio-postgres = { version = "0.7", optional = true }
44-
bb8 = { version = "0.9.0", optional = true }
45-
bb8-postgres = { version = "0.9.0", optional = true }
43+
libsql = { version = "0.9", optional = true, default-features = false, features = [ "core", "sync"] }
44+
tokio-postgres = { version = "0.7", optional = true, features = ["with-chrono-0_4"] }
45+
bb8 = { version = "0.9", optional = true }
46+
bb8-postgres = { version = "0.9", optional = true }
4647

4748
[features]
4849
default = ["libsql"]
Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
1-
/* In SQLite, we store time as integer with microsecond precision. This is the same precision used by TIMESTAMPTZ in Postgres. */
1+
/* In SQLite, we store time as an 8-byte integer (i64) with microsecond precision. This matches TIMESTAMPTZ in Postgres.
2+
Use `chrono::DateTime::timestamp_micros` when inserting or fetching timestamps in Rust.
3+
*/
24

3-
CREATE TABLE pedm_run (
4-
id INTEGER PRIMARY KEY AUTOINCREMENT,
5-
start_time INTEGER NOT NULL DEFAULT (CAST(strftime('%f', 'now') * 1000000 AS INTEGER)),
6-
pipe_name TEXT NOT NULL
5+
CREATE TABLE IF NOT EXISTS version
6+
(
7+
version integer PRIMARY KEY,
8+
updated_at integer NOT NULL DEFAULT (
9+
CAST(strftime('%s', 'now') AS integer) * 1000000 + CAST(strftime('%f', 'now') * 1000000 AS integer) % 1000000
10+
)
711
);
812

9-
CREATE TABLE http_request (
10-
id INTEGER PRIMARY KEY,
11-
at INTEGER NOT NULL DEFAULT (CAST(strftime('%f', 'now') * 1000000 AS INTEGER)),
12-
method TEXT NOT NULL,
13-
path TEXT NOT NULL,
14-
status_code INTEGER NOT NULL
13+
CREATE TABLE IF NOT EXISTS run
14+
(
15+
id integer PRIMARY KEY AUTOINCREMENT,
16+
start_time integer NOT NULL DEFAULT (
17+
CAST(strftime('%s', 'now') AS integer) * 1000000 + CAST(strftime('%f', 'now') * 1000000 AS integer) % 1000000
18+
),
19+
pipe_name text NOT NULL
1520
);
1621

17-
CREATE TABLE elevate_tmp_request (
18-
req_id INTEGER PRIMARY KEY,
19-
seconds INTEGER NOT NULL
22+
CREATE TABLE IF NOT EXISTS http_request
23+
(
24+
id integer PRIMARY KEY,
25+
at integer NOT NULL DEFAULT (
26+
CAST(strftime('%s', 'now') AS integer) * 1000000 + CAST(strftime('%f', 'now') * 1000000 AS integer) % 1000000
27+
),
28+
method text NOT NULL,
29+
path text NOT NULL,
30+
status_code integer NOT NULL
2031
);
32+
33+
CREATE TABLE IF NOT EXISTS elevate_tmp_request
34+
(
35+
req_id integer PRIMARY KEY,
36+
seconds integer NOT NULL
37+
);
38+
39+
INSERT INTO version (version) VALUES (0) ON CONFLICT DO NOTHING;

0 commit comments

Comments
 (0)