Add creation date to allow consistent ordering

This commit is contained in:
Simon Bruder 2024-07-11 21:50:03 +02:00
parent 00d7647187
commit 4f230af334
Signed by: simon
GPG key ID: 347FF8699CDA0776
21 changed files with 158 additions and 16 deletions

View file

@ -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
]
},

View file

@ -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
]
},

View file

@ -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
]
},

View file

@ -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"

View file

@ -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"

View file

@ -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"
}

View file

@ -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"
}

View file

@ -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"
}

View file

@ -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"

View file

@ -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
]
},

View file

@ -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
]
},

View file

@ -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"

View file

@ -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
]
},

6
Cargo.lock generated
View file

@ -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",

View file

@ -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]

View file

@ -0,0 +1,9 @@
-- SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
--
-- SPDX-License-Identifier: AGPL-3.0-or-later
ALTER TABLE items
DROP created_at;
ALTER TABLE item_classes
DROP created_at;

View file

@ -0,0 +1,21 @@
-- SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
--
-- 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;

View file

@ -95,6 +95,11 @@ async fn show_item_class(
#[get("/item-classes")]
async fn list_item_classes(pool: web::Data<PgPool>) -> actix_web::Result<impl Responder> {
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<PgPool>) -> actix_web::Result<impl Re
}
}
tbody {
@for item_class in item_classes.values() {
@for item_class in item_classes_ids {
@let item_class = item_classes.get(&item_class).unwrap();
tr {
td { a href={ "/item-class/" (item_class.id) } { (item_class.name) } }
td {

View file

@ -28,7 +28,9 @@ pub async fn get(pool: &PgPool, id: Uuid) -> Result<Item, sqlx::Error> {
}
pub async fn get_all(pool: &PgPool) -> Result<Vec<Item>, 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<HashMap<Uuid, Item>, sqlx::Error> {

View file

@ -27,7 +27,7 @@ pub async fn get(pool: &PgPool, id: Uuid) -> Result<ItemClass, sqlx::Error> {
}
pub async fn get_all(pool: &PgPool) -> Result<Vec<ItemClass>, sqlx::Error> {
query_as!(ItemClass, "SELECT * FROM item_classes")
query_as!(ItemClass, "SELECT * FROM item_classes ORDER BY created_at")
.fetch_all(pool)
.await
}

View file

@ -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<String>,
pub parent: Option<Uuid>,
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<Uuid>,
#[serde(with = "time::serde::iso8601")]
pub created_at: OffsetDateTime,
}
#[derive(Debug, Deserialize)]