From 375fa7740e7e36503ab7ac4482348782533235e5 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Thu, 11 Jul 2024 21:50:03 +0200 Subject: [PATCH] Add creation date to allow consistent ordering --- ...9c2b0791f3f2c4c0ae4777b2a2c309410d0ec.json | 6 ++++++ ...c7ee7520478ff4f5886b4523fb0dc4df0e204.json | 6 ++++++ ...f28fd05f18706f70220b6ebb7354d2a0a9e3b.json | 6 ++++++ ...e5da76e4cf488eedd4704019b4f14b6fbafb2.json | 8 ++++++- ...3dc0b18360644234fb01b24c4c047bebb3a10.json | 8 ++++++- ...a92283151d67f52fbb22e5d976b1c6a5c367.json} | 18 +++++++++++++--- ...0ce194ede48a1c46326315cae9f07347597f6.json | 20 ++++++++++++++++++ ...b413f804c6f3e082a818b67ebab0938acb39.json} | 10 ++++----- ...beacf50ebf5fbfead379340f9f72614958e78.json | 8 ++++++- ...4c85cfc7db545724d94c1a467b2c0c74b6b6e.json | 6 ++++++ ...fbc5433e972b5522b367098afe6cb90a30bf2.json | 6 ++++++ ...d0c0b86eb2322035e6a720e2e6d425072a8c1.json | 8 ++++++- ...57d6852d2d3f63dc055442125a6a92950729d.json | 6 ++++++ Cargo.lock | 6 ++++++ Cargo.toml | 3 ++- ...0711190709_add_creation_timestamp.down.sql | 9 ++++++++ ...240711190709_add_creation_timestamp.up.sql | 21 +++++++++++++++++++ src/frontend/item_class.rs | 8 ++++++- src/manage/item.rs | 4 +++- src/manage/item_class.rs | 2 +- src/models.rs | 5 +++++ 21 files changed, 158 insertions(+), 16 deletions(-) rename .sqlx/{query-dc197bce5f03ab1ab08391f54f960e63e935172b25e5a6835d51e786fb2f854e.json => query-58969747bdccae4a2d3ad8d8117aa92283151d67f52fbb22e5d976b1c6a5c367.json} (55%) create mode 100644 .sqlx/query-5cf503740d71431d8b9d256960c0ce194ede48a1c46326315cae9f07347597f6.json rename .sqlx/{query-467dcd7fd379750cbf30a190d06dc1b3e61b1bd7ef288fbf6fbedec52c276434.json => query-6e7b3389c47091d9fc8c7638b401b413f804c6f3e082a818b67ebab0938acb39.json} (69%) create mode 100644 migrations/20240711190709_add_creation_timestamp.down.sql create mode 100644 migrations/20240711190709_add_creation_timestamp.up.sql diff --git a/.sqlx/query-05452db872d5ae8574a979b07d19c2b0791f3f2c4c0ae4777b2a2c309410d0ec.json b/.sqlx/query-05452db872d5ae8574a979b07d19c2b0791f3f2c4c0ae4777b2a2c309410d0ec.json index d114826..92605eb 100644 --- a/.sqlx/query-05452db872d5ae8574a979b07d19c2b0791f3f2c4c0ae4777b2a2c309410d0ec.json +++ b/.sqlx/query-05452db872d5ae8574a979b07d19c2b0791f3f2c4c0ae4777b2a2c309410d0ec.json @@ -22,6 +22,11 @@ "ordinal": 3, "name": "class", "type_info": "Uuid" + }, + { + "ordinal": 4, + "name": "created_at", + "type_info": "Timestamptz" } ], "parameters": { @@ -35,6 +40,7 @@ false, true, true, + false, false ] }, diff --git a/.sqlx/query-1e9045f52c002a19b815351fee1c7ee7520478ff4f5886b4523fb0dc4df0e204.json b/.sqlx/query-1e9045f52c002a19b815351fee1c7ee7520478ff4f5886b4523fb0dc4df0e204.json index c5e700a..1217e4d 100644 --- a/.sqlx/query-1e9045f52c002a19b815351fee1c7ee7520478ff4f5886b4523fb0dc4df0e204.json +++ b/.sqlx/query-1e9045f52c002a19b815351fee1c7ee7520478ff4f5886b4523fb0dc4df0e204.json @@ -22,6 +22,11 @@ "ordinal": 3, "name": "class", "type_info": "Uuid" + }, + { + "ordinal": 4, + "name": "created_at", + "type_info": "Timestamptz" } ], "parameters": { @@ -33,6 +38,7 @@ false, true, true, + false, false ] }, diff --git a/.sqlx/query-2e9619ce6db1047e2f5447c4925f28fd05f18706f70220b6ebb7354d2a0a9e3b.json b/.sqlx/query-2e9619ce6db1047e2f5447c4925f28fd05f18706f70220b6ebb7354d2a0a9e3b.json index 6e1a26f..73a859f 100644 --- a/.sqlx/query-2e9619ce6db1047e2f5447c4925f28fd05f18706f70220b6ebb7354d2a0a9e3b.json +++ b/.sqlx/query-2e9619ce6db1047e2f5447c4925f28fd05f18706f70220b6ebb7354d2a0a9e3b.json @@ -22,6 +22,11 @@ "ordinal": 3, "name": "class", "type_info": "Uuid" + }, + { + "ordinal": 4, + "name": "created_at", + "type_info": "Timestamptz" } ], "parameters": { @@ -33,6 +38,7 @@ false, true, true, + false, false ] }, diff --git a/.sqlx/query-308962c26250f9312287a3f2f21e5da76e4cf488eedd4704019b4f14b6fbafb2.json b/.sqlx/query-308962c26250f9312287a3f2f21e5da76e4cf488eedd4704019b4f14b6fbafb2.json index 66eaa87..5c7a48e 100644 --- a/.sqlx/query-308962c26250f9312287a3f2f21e5da76e4cf488eedd4704019b4f14b6fbafb2.json +++ b/.sqlx/query-308962c26250f9312287a3f2f21e5da76e4cf488eedd4704019b4f14b6fbafb2.json @@ -17,6 +17,11 @@ "ordinal": 2, "name": "parent", "type_info": "Uuid" + }, + { + "ordinal": 3, + "name": "created_at", + "type_info": "Timestamptz" } ], "parameters": { @@ -27,7 +32,8 @@ "nullable": [ false, false, - true + true, + false ] }, "hash": "308962c26250f9312287a3f2f21e5da76e4cf488eedd4704019b4f14b6fbafb2" diff --git a/.sqlx/query-408a171b46d5c615ff846547f463dc0b18360644234fb01b24c4c047bebb3a10.json b/.sqlx/query-408a171b46d5c615ff846547f463dc0b18360644234fb01b24c4c047bebb3a10.json index ab0620b..62d1fb3 100644 --- a/.sqlx/query-408a171b46d5c615ff846547f463dc0b18360644234fb01b24c4c047bebb3a10.json +++ b/.sqlx/query-408a171b46d5c615ff846547f463dc0b18360644234fb01b24c4c047bebb3a10.json @@ -17,6 +17,11 @@ "ordinal": 2, "name": "parent", "type_info": "Uuid" + }, + { + "ordinal": 3, + "name": "created_at", + "type_info": "Timestamptz" } ], "parameters": { @@ -29,7 +34,8 @@ "nullable": [ false, false, - true + true, + false ] }, "hash": "408a171b46d5c615ff846547f463dc0b18360644234fb01b24c4c047bebb3a10" diff --git a/.sqlx/query-dc197bce5f03ab1ab08391f54f960e63e935172b25e5a6835d51e786fb2f854e.json b/.sqlx/query-58969747bdccae4a2d3ad8d8117aa92283151d67f52fbb22e5d976b1c6a5c367.json similarity index 55% rename from .sqlx/query-dc197bce5f03ab1ab08391f54f960e63e935172b25e5a6835d51e786fb2f854e.json rename to .sqlx/query-58969747bdccae4a2d3ad8d8117aa92283151d67f52fbb22e5d976b1c6a5c367.json index f354f52..40c6ee7 100644 --- a/.sqlx/query-dc197bce5f03ab1ab08391f54f960e63e935172b25e5a6835d51e786fb2f854e.json +++ b/.sqlx/query-58969747bdccae4a2d3ad8d8117aa92283151d67f52fbb22e5d976b1c6a5c367.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT * FROM item_classes", + "query": "SELECT * FROM items ORDER BY created_at", "describe": { "columns": [ { @@ -17,6 +17,16 @@ "ordinal": 2, "name": "parent", "type_info": "Uuid" + }, + { + "ordinal": 3, + "name": "class", + "type_info": "Uuid" + }, + { + "ordinal": 4, + "name": "created_at", + "type_info": "Timestamptz" } ], "parameters": { @@ -24,9 +34,11 @@ }, "nullable": [ false, + true, + true, false, - true + false ] }, - "hash": "dc197bce5f03ab1ab08391f54f960e63e935172b25e5a6835d51e786fb2f854e" + "hash": "58969747bdccae4a2d3ad8d8117aa92283151d67f52fbb22e5d976b1c6a5c367" } diff --git a/.sqlx/query-5cf503740d71431d8b9d256960c0ce194ede48a1c46326315cae9f07347597f6.json b/.sqlx/query-5cf503740d71431d8b9d256960c0ce194ede48a1c46326315cae9f07347597f6.json new file mode 100644 index 0000000..5f22e6d --- /dev/null +++ b/.sqlx/query-5cf503740d71431d8b9d256960c0ce194ede48a1c46326315cae9f07347597f6.json @@ -0,0 +1,20 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT id FROM item_classes ORDER BY created_at", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id", + "type_info": "Uuid" + } + ], + "parameters": { + "Left": [] + }, + "nullable": [ + false + ] + }, + "hash": "5cf503740d71431d8b9d256960c0ce194ede48a1c46326315cae9f07347597f6" +} diff --git a/.sqlx/query-467dcd7fd379750cbf30a190d06dc1b3e61b1bd7ef288fbf6fbedec52c276434.json b/.sqlx/query-6e7b3389c47091d9fc8c7638b401b413f804c6f3e082a818b67ebab0938acb39.json similarity index 69% rename from .sqlx/query-467dcd7fd379750cbf30a190d06dc1b3e61b1bd7ef288fbf6fbedec52c276434.json rename to .sqlx/query-6e7b3389c47091d9fc8c7638b401b413f804c6f3e082a818b67ebab0938acb39.json index 53d333c..be996de 100644 --- a/.sqlx/query-467dcd7fd379750cbf30a190d06dc1b3e61b1bd7ef288fbf6fbedec52c276434.json +++ b/.sqlx/query-6e7b3389c47091d9fc8c7638b401b413f804c6f3e082a818b67ebab0938acb39.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT * FROM items", + "query": "SELECT * FROM item_classes ORDER BY created_at", "describe": { "columns": [ { @@ -20,8 +20,8 @@ }, { "ordinal": 3, - "name": "class", - "type_info": "Uuid" + "name": "created_at", + "type_info": "Timestamptz" } ], "parameters": { @@ -29,10 +29,10 @@ }, "nullable": [ false, - true, + false, true, false ] }, - "hash": "467dcd7fd379750cbf30a190d06dc1b3e61b1bd7ef288fbf6fbedec52c276434" + "hash": "6e7b3389c47091d9fc8c7638b401b413f804c6f3e082a818b67ebab0938acb39" } diff --git a/.sqlx/query-7e2e1460a2c946ba909c50d4cf1beacf50ebf5fbfead379340f9f72614958e78.json b/.sqlx/query-7e2e1460a2c946ba909c50d4cf1beacf50ebf5fbfead379340f9f72614958e78.json index c7661e1..b9586ce 100644 --- a/.sqlx/query-7e2e1460a2c946ba909c50d4cf1beacf50ebf5fbfead379340f9f72614958e78.json +++ b/.sqlx/query-7e2e1460a2c946ba909c50d4cf1beacf50ebf5fbfead379340f9f72614958e78.json @@ -17,6 +17,11 @@ "ordinal": 2, "name": "parent", "type_info": "Uuid" + }, + { + "ordinal": 3, + "name": "created_at", + "type_info": "Timestamptz" } ], "parameters": { @@ -28,7 +33,8 @@ "nullable": [ false, false, - true + true, + false ] }, "hash": "7e2e1460a2c946ba909c50d4cf1beacf50ebf5fbfead379340f9f72614958e78" diff --git a/.sqlx/query-85528d16361a6e736773e8f8aba4c85cfc7db545724d94c1a467b2c0c74b6b6e.json b/.sqlx/query-85528d16361a6e736773e8f8aba4c85cfc7db545724d94c1a467b2c0c74b6b6e.json index 4553886..eb03f0f 100644 --- a/.sqlx/query-85528d16361a6e736773e8f8aba4c85cfc7db545724d94c1a467b2c0c74b6b6e.json +++ b/.sqlx/query-85528d16361a6e736773e8f8aba4c85cfc7db545724d94c1a467b2c0c74b6b6e.json @@ -22,6 +22,11 @@ "ordinal": 3, "name": "class", "type_info": "Uuid" + }, + { + "ordinal": 4, + "name": "created_at", + "type_info": "Timestamptz" } ], "parameters": { @@ -36,6 +41,7 @@ false, true, true, + false, false ] }, diff --git a/.sqlx/query-94958a3a57c3178e6b0de5723b1fbc5433e972b5522b367098afe6cb90a30bf2.json b/.sqlx/query-94958a3a57c3178e6b0de5723b1fbc5433e972b5522b367098afe6cb90a30bf2.json index 4266aae..44006c7 100644 --- a/.sqlx/query-94958a3a57c3178e6b0de5723b1fbc5433e972b5522b367098afe6cb90a30bf2.json +++ b/.sqlx/query-94958a3a57c3178e6b0de5723b1fbc5433e972b5522b367098afe6cb90a30bf2.json @@ -22,6 +22,11 @@ "ordinal": 3, "name": "class", "type_info": "Uuid" + }, + { + "ordinal": 4, + "name": "created_at", + "type_info": "Timestamptz" } ], "parameters": { @@ -33,6 +38,7 @@ false, true, true, + false, false ] }, diff --git a/.sqlx/query-c552c0a40bc8995cb95726a85f1d0c0b86eb2322035e6a720e2e6d425072a8c1.json b/.sqlx/query-c552c0a40bc8995cb95726a85f1d0c0b86eb2322035e6a720e2e6d425072a8c1.json index 1d1eaa6..0c7bcd6 100644 --- a/.sqlx/query-c552c0a40bc8995cb95726a85f1d0c0b86eb2322035e6a720e2e6d425072a8c1.json +++ b/.sqlx/query-c552c0a40bc8995cb95726a85f1d0c0b86eb2322035e6a720e2e6d425072a8c1.json @@ -17,6 +17,11 @@ "ordinal": 2, "name": "parent", "type_info": "Uuid" + }, + { + "ordinal": 3, + "name": "created_at", + "type_info": "Timestamptz" } ], "parameters": { @@ -27,7 +32,8 @@ "nullable": [ false, false, - true + true, + false ] }, "hash": "c552c0a40bc8995cb95726a85f1d0c0b86eb2322035e6a720e2e6d425072a8c1" diff --git a/.sqlx/query-e45160a73a18d870f592989349057d6852d2d3f63dc055442125a6a92950729d.json b/.sqlx/query-e45160a73a18d870f592989349057d6852d2d3f63dc055442125a6a92950729d.json index 1de7df6..a7b8b6a 100644 --- a/.sqlx/query-e45160a73a18d870f592989349057d6852d2d3f63dc055442125a6a92950729d.json +++ b/.sqlx/query-e45160a73a18d870f592989349057d6852d2d3f63dc055442125a6a92950729d.json @@ -22,6 +22,11 @@ "ordinal": 3, "name": "class", "type_info": "Uuid" + }, + { + "ordinal": 4, + "name": "created_at", + "type_info": "Timestamptz" } ], "parameters": { @@ -33,6 +38,7 @@ false, true, true, + false, false ] }, diff --git a/Cargo.lock b/Cargo.lock index 4dea82f..853b242 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -660,6 +660,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" dependencies = [ "powerfmt", + "serde", ] [[package]] @@ -1126,6 +1127,7 @@ dependencies = [ "serde", "sqlx", "thiserror", + "time", "uuid", ] @@ -1953,6 +1955,7 @@ dependencies = [ "smallvec", "sqlformat", "thiserror", + "time", "tokio", "tokio-stream", "tracing", @@ -2037,6 +2040,7 @@ dependencies = [ "sqlx-core", "stringprep", "thiserror", + "time", "tracing", "uuid", "whoami", @@ -2076,6 +2080,7 @@ dependencies = [ "sqlx-core", "stringprep", "thiserror", + "time", "tracing", "uuid", "whoami", @@ -2099,6 +2104,7 @@ dependencies = [ "percent-encoding", "serde", "sqlx-core", + "time", "tracing", "url", "urlencoding", diff --git a/Cargo.toml b/Cargo.toml index fc0e3ca..f6ce272 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,8 +21,9 @@ mime = "0.3.17" printpdf = "0.7.0" rust-fontconfig = "0.1.7" serde = { version = "1.0.203", features = ["serde_derive"] } -sqlx = { version = "0.7.4", features = ["runtime-tokio", "postgres", "uuid"] } +sqlx = { version = "0.7.4", features = ["runtime-tokio", "postgres", "uuid", "time"] } thiserror = "1.0.61" +time = { version = "0.3.36", features = ["serde"] } uuid = { version = "1.9.0", features = ["serde", "v4"] } [profile.dev.package.sqlx-macros] diff --git a/migrations/20240711190709_add_creation_timestamp.down.sql b/migrations/20240711190709_add_creation_timestamp.down.sql new file mode 100644 index 0000000..341d8e8 --- /dev/null +++ b/migrations/20240711190709_add_creation_timestamp.down.sql @@ -0,0 +1,9 @@ +-- SPDX-FileCopyrightText: 2024 Simon Bruder +-- +-- SPDX-License-Identifier: AGPL-3.0-or-later + +ALTER TABLE items + DROP created_at; + +ALTER TABLE item_classes + DROP created_at; diff --git a/migrations/20240711190709_add_creation_timestamp.up.sql b/migrations/20240711190709_add_creation_timestamp.up.sql new file mode 100644 index 0000000..eb16299 --- /dev/null +++ b/migrations/20240711190709_add_creation_timestamp.up.sql @@ -0,0 +1,21 @@ +-- SPDX-FileCopyrightText: 2024 Simon Bruder +-- +-- SPDX-License-Identifier: AGPL-3.0-or-later + +ALTER TABLE items + ADD created_at TIMESTAMP WITH TIME ZONE DEFAULT now(); + +UPDATE items + SET created_at = now(); + +ALTER TABLE items + ALTER created_at SET NOT NULL; + +ALTER TABLE item_classes + ADD created_at TIMESTAMP WITH TIME ZONE DEFAULT now(); + +UPDATE item_classes + SET created_at = now(); + +ALTER TABLE item_classes + ALTER created_at SET NOT NULL; diff --git a/src/frontend/item_class.rs b/src/frontend/item_class.rs index 13ea207..8198c79 100644 --- a/src/frontend/item_class.rs +++ b/src/frontend/item_class.rs @@ -95,6 +95,11 @@ async fn show_item_class( #[get("/item-classes")] async fn list_item_classes(pool: web::Data) -> actix_web::Result { + let item_classes_ids = sqlx::query_scalar!("SELECT id FROM item_classes ORDER BY created_at") + .fetch_all(pool.as_ref()) + .await + .map_err(error::ErrorInternalServerError)?; + let item_classes = manage::item_class::get_all_as_map(&pool) .await .map_err(error::ErrorInternalServerError)?; @@ -121,7 +126,8 @@ async fn list_item_classes(pool: web::Data) -> actix_web::Result Result { } pub async fn get_all(pool: &PgPool) -> Result, sqlx::Error> { - query_as!(Item, "SELECT * FROM items").fetch_all(pool).await + query_as!(Item, "SELECT * FROM items ORDER BY created_at") + .fetch_all(pool) + .await } pub async fn get_all_as_map(pool: &PgPool) -> Result, sqlx::Error> { diff --git a/src/manage/item_class.rs b/src/manage/item_class.rs index 2eecb33..2fc4bdf 100644 --- a/src/manage/item_class.rs +++ b/src/manage/item_class.rs @@ -27,7 +27,7 @@ pub async fn get(pool: &PgPool, id: Uuid) -> Result { } pub async fn get_all(pool: &PgPool) -> Result, sqlx::Error> { - query_as!(ItemClass, "SELECT * FROM item_classes") + query_as!(ItemClass, "SELECT * FROM item_classes ORDER BY created_at") .fetch_all(pool) .await } diff --git a/src/models.rs b/src/models.rs index d03ea3e..018b4f9 100644 --- a/src/models.rs +++ b/src/models.rs @@ -3,6 +3,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later use serde::{Deserialize, Serialize}; +use time::OffsetDateTime; use uuid::Uuid; #[derive(Clone, Debug, Serialize, sqlx::FromRow)] @@ -11,6 +12,8 @@ pub struct Item { pub name: Option, pub parent: Option, pub class: Uuid, + #[serde(with = "time::serde::iso8601")] + pub created_at: OffsetDateTime, } #[derive(Debug, Deserialize)] @@ -28,6 +31,8 @@ pub struct ItemClass { pub name: String, #[serde(skip_serializing_if = "Option::is_none")] pub parent: Option, + #[serde(with = "time::serde::iso8601")] + pub created_at: OffsetDateTime, } #[derive(Debug, Deserialize)]