aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-05-14 15:22:34 -0700
committerFuwn <[email protected]>2024-05-14 15:22:39 -0700
commite64b39d88be011009485a0e0faa2bbf323dc7714 (patch)
treebe099ef849267c1f46695746a90c99d0a2da4a95 /src
parentd686a39bd5a0474f4a6c122e1ec54300cf0b4f7f (diff)
downloadmayu-e64b39d88be011009485a0e0faa2bbf323dc7714.tar.xz
mayu-e64b39d88be011009485a0e0faa2bbf323dc7714.zip
fix(database): use sqlite timestamp format
Diffstat (limited to 'src')
-rw-r--r--src/database.gleam21
1 files changed, 10 insertions, 11 deletions
diff --git a/src/database.gleam b/src/database.gleam
index 34400ce..1e26a31 100644
--- a/src/database.gleam
+++ b/src/database.gleam
@@ -1,5 +1,6 @@
import birl
import gleam/dynamic
+import gleam/string
import sqlight
pub type Counter {
@@ -24,11 +25,6 @@ pub fn setup(connection) {
"alter table tb_count add column " <> name <> " text;",
connection,
)
- let _ =
- sqlight.exec(
- "update tb_count set " <> name <> " = current_timestamp;",
- connection,
- )
Nil
}
@@ -48,24 +44,27 @@ pub fn add_counter(connection, name) {
)
}
+fn sqlite_now() {
+ birl.to_iso8601(birl.utc_now())
+ |> string.slice(0, 19)
+ |> string.replace("T", " ")
+}
+
pub fn get_counter(connection, name) {
case name {
"demo" -> Counter("demo", 0_123_456_789, "", "")
_ -> {
let assert Ok(_) =
sqlight.query(
- "insert or ignore into tb_count (name) values (?);",
- with: [sqlight.text(name)],
+ "insert or ignore into tb_count (name, created_at) values (?, ?);",
+ with: [sqlight.text(name), sqlight.text(sqlite_now())],
on: connection,
expecting: dynamic.optional(dynamic.int),
)
let assert Ok(_) =
sqlight.query(
"update tb_count set num = num + 1, updated_at = ? where name = ?;",
- with: [
- sqlight.text(birl.to_iso8601(birl.utc_now())),
- sqlight.text(name),
- ],
+ with: [sqlight.text(sqlite_now()), sqlight.text(name)],
on: connection,
expecting: dynamic.int,
)