From 143b795b926b643f160c26cc994a557dc441d22b Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Wed, 22 Feb 2023 18:06:22 +0100 Subject: [PATCH 1/2] Fix sql migration error --- backend/migrations/2022-12-03-124501_init/up.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/migrations/2022-12-03-124501_init/up.sql b/backend/migrations/2022-12-03-124501_init/up.sql index ad3e1c0..49ae6d2 100644 --- a/backend/migrations/2022-12-03-124501_init/up.sql +++ b/backend/migrations/2022-12-03-124501_init/up.sql @@ -2,6 +2,7 @@ CREATE TABLE schoolyears ( id SERIAL PRIMARY KEY, untis_id INTEGER NOT NULL UNIQUE, name VARCHAR NOT NULL, + active BOOLEAN NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP ); @@ -15,7 +16,7 @@ CREATE TABLE tenants ( untis_id INTEGER NOT NULL UNIQUE, schoolyear_id INTEGER NOT NULL REFERENCES schoolyears(id), name VARCHAR NOT NULL, - active BOOLEAN NOT NULL DEFAULT FALSE, + active BOOLEAN NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP ); From b2c5dc2917512f6bd67bba0094da7860c3826a91 Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Wed, 22 Feb 2023 18:11:04 +0100 Subject: [PATCH 2/2] Update --- backend/src/db/models.rs | 2 ++ backend/src/db/schema.rs | 1 + backend/src/worker/update_info.rs | 15 +++++++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/backend/src/db/models.rs b/backend/src/db/models.rs index caeb751..75fe6b8 100644 --- a/backend/src/db/models.rs +++ b/backend/src/db/models.rs @@ -10,6 +10,7 @@ pub struct Schoolyear { pub id: i32, pub untis_id: i32, pub name: String, + pub active: bool, pub created_at: NaiveDateTime, pub updated_at: Option, } @@ -19,6 +20,7 @@ pub struct Schoolyear { pub struct NewSchoolyear<'a> { pub untis_id: i32, pub name: &'a str, + pub active: bool, } #[derive(Identifiable, Queryable, Associations, Debug)] diff --git a/backend/src/db/schema.rs b/backend/src/db/schema.rs index c27d85c..4a16323 100644 --- a/backend/src/db/schema.rs +++ b/backend/src/db/schema.rs @@ -13,6 +13,7 @@ diesel::table! { id -> Integer, untis_id -> Integer, name -> VarChar, + active -> Bool, created_at -> Timestamp, updated_at -> Nullable, } diff --git a/backend/src/worker/update_info.rs b/backend/src/worker/update_info.rs index 64aa6b5..e3db80e 100644 --- a/backend/src/worker/update_info.rs +++ b/backend/src/worker/update_info.rs @@ -24,15 +24,26 @@ async fn fetch_schoolyears(client: &untis::Client, db_conn: &mut PgConnection) - .map(|y| db::models::NewSchoolyear { untis_id: y.id, name: &y.name, + active: false, }) .collect::>(), ) .execute(db_conn)?; - Ok(db::schema::schoolyears::table + let id = db::schema::schoolyears::table .filter(db::schema::schoolyears::untis_id.eq(client.current_schoolyear().await?.id)) .select(db::schema::schoolyears::id) - .first(db_conn)?) + .first(db_conn)?; + + diesel::update(db::schema::schoolyears::table) + .set(db::schema::schoolyears::active.eq(false)) + .execute(db_conn)?; + diesel::update(db::schema::schoolyears::table) + .filter(db::schema::schoolyears::id.eq(id)) + .set(db::schema::schoolyears::active.eq(true)) + .execute(db_conn)?; + + Ok(id) } async fn fetch_current_tenant(