From 7233f49b708ae48721825380861ff3f60817d002 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Tue, 14 May 2024 22:22:34 +0000 Subject: fix(database): use sqlite timestamp format --- src/database.gleam | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'src') 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, ) -- cgit v1.2.3