Skip to content

Commit

Permalink
Add created_at / updated_at on relevant objects
Browse files Browse the repository at this point in the history
Part of #29 as well as the ongoing room templates work
  • Loading branch information
Eijebong committed Nov 27, 2024
1 parent d07104f commit 4c7e036
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
16 changes: 16 additions & 0 deletions migrations/2024-11-27-092030_add_created_updated_at/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- This file should undo anything in `up.sql`

ALTER TABLE rooms DROP COLUMN created_at;
ALTER TABLE yamls DROP COLUMN created_at;
ALTER TABLE room_templates DROP COLUMN created_at;

ALTER TABLE rooms DROP COLUMN updated_at;
ALTER TABLE yamls DROP COLUMN updated_at;
ALTER TABLE room_templates DROP COLUMN updated_at;

DROP TRIGGER set_updated_at ON rooms;
DROP TRIGGER set_updated_at ON yamls;
DROP TRIGGER set_updated_at ON room_templates;

DROP FUNCTION set_updated_at;

25 changes: 25 additions & 0 deletions migrations/2024-11-27-092030_add_created_updated_at/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Your SQL goes here

ALTER TABLE rooms ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT NOW();
ALTER TABLE yamls ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT NOW();
ALTER TABLE room_templates ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT NOW();

ALTER TABLE rooms ADD COLUMN updated_at TIMESTAMP NOT NULL DEFAULT NOW();
ALTER TABLE yamls ADD COLUMN updated_at TIMESTAMP NOT NULL DEFAULT NOW();
ALTER TABLE room_templates ADD COLUMN updated_at TIMESTAMP NOT NULL DEFAULT NOW();

CREATE OR REPLACE FUNCTION set_updated_at() RETURNS trigger AS $$
BEGIN
IF (
NEW IS DISTINCT FROM OLD AND
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
) THEN
NEW.updated_at := current_timestamp;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER set_updated_at BEFORE UPDATE ON rooms FOR EACH ROW EXECUTE PROCEDURE set_updated_at();
CREATE TRIGGER set_updated_at BEFORE UPDATE ON yamls FOR EACH ROW EXECUTE PROCEDURE set_updated_at();
CREATE TRIGGER set_updated_at BEFORE UPDATE ON room_templates FOR EACH ROW EXECUTE PROCEDURE set_updated_at();
69 changes: 66 additions & 3 deletions src/db/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub struct RoomSettings {
pub yaml_limit_bypass_list: Vec<i64>,
pub manifest: Json<Manifest>,
pub show_apworlds: bool,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}

pub type Room = GenericRoom<RoomId>;
Expand All @@ -72,8 +74,43 @@ impl<DB: Backend> Selectable<DB> for GenericRoom<RoomTemplateId> {
}
}

impl<T: Clone, DB: Backend, ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7, ST8, ST9, ST10, ST11>
Queryable<(ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7, ST8, ST9, ST10, ST11), DB> for GenericRoom<T>
impl<
T: Clone,
DB: Backend,
ST0,
ST1,
ST2,
ST3,
ST4,
ST5,
ST6,
ST7,
ST8,
ST9,
ST10,
ST11,
ST12,
ST13,
>
Queryable<
(
ST0,
ST1,
ST2,
ST3,
ST4,
ST5,
ST6,
ST7,
ST8,
ST9,
ST10,
ST11,
ST12,
ST13,
),
DB,
> for GenericRoom<T>
where
(
T,
Expand All @@ -88,7 +125,27 @@ where
Vec<i64>,
Json<Manifest>,
bool,
): FromStaticSqlRow<(ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7, ST8, ST9, ST10, ST11), DB>,
NaiveDateTime,
NaiveDateTime,
): FromStaticSqlRow<
(
ST0,
ST1,
ST2,
ST3,
ST4,
ST5,
ST6,
ST7,
ST8,
ST9,
ST10,
ST11,
ST12,
ST13,
),
DB,
>,
{
type Row = (
T,
Expand All @@ -103,6 +160,8 @@ where
Vec<i64>,
Json<Manifest>,
bool,
NaiveDateTime,
NaiveDateTime,
);

fn build(row: Self::Row) -> diesel::deserialize::Result<Self> {
Expand All @@ -120,6 +179,8 @@ where
yaml_limit_bypass_list: row.9,
manifest: row.10,
show_apworlds: row.11,
created_at: row.12,
updated_at: row.13,
},
})
}
Expand All @@ -139,6 +200,8 @@ impl RoomSettings {
yaml_limit_bypass_list: vec![],
manifest: Json(Manifest::from_index_with_latest_versions(index)?),
show_apworlds: true,
created_at: Self::default_close_date()?,
updated_at: Self::default_close_date()?,
})
}

Expand Down
6 changes: 6 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ diesel::table! {
yaml_limit_bypass_list -> Array<Int8>,
manifest -> Jsonb,
show_apworlds -> Bool,
created_at -> Timestamp,
updated_at -> Timestamp,
}
}

Expand All @@ -47,6 +49,8 @@ diesel::table! {
yaml_limit_bypass_list -> Array<Int8>,
manifest -> Jsonb,
show_apworlds -> Bool,
created_at -> Timestamp,
updated_at -> Timestamp,
}
}

Expand All @@ -62,6 +66,8 @@ diesel::table! {
game -> Varchar,
owner_id -> Int8,
features -> Json,
created_at -> Timestamp,
updated_at -> Timestamp,
}
}

Expand Down

0 comments on commit 4c7e036

Please sign in to comment.