From c5100f31e58393e6474fe85eb6dbfef04877d7f7 Mon Sep 17 00:00:00 2001 From: Chris Jean-Marie Date: Wed, 14 May 2025 02:23:31 +0000 Subject: [PATCH] Cleanup use statements --- backend/src/calendar.rs | 5 ++- backend/src/main.rs | 49 ++++++++++++++++++++++++++--- backend/src/routes.rs | 1 - backend/src/secret_gift_exchange.rs | 4 +-- 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/backend/src/calendar.rs b/backend/src/calendar.rs index ff785a3..0c8adba 100644 --- a/backend/src/calendar.rs +++ b/backend/src/calendar.rs @@ -1,11 +1,10 @@ use askama::Template; use axum::{ - body::Body, extract::{Path, Query, Request, State}, response::{Html, IntoResponse, Redirect, Response}, Extension + extract::{Path, Query, State}, response::{Html, IntoResponse, Redirect, Response}, Extension }; use http::StatusCode; use serde::{Deserialize, Serialize}; -use serde_json::json; -use sqlx::{types::Json, PgPool, Row}; +use sqlx::{PgPool, Row}; use crate::{ middlewares::is_authorized, diff --git a/backend/src/main.rs b/backend/src/main.rs index 4d427dd..53dacef 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -5,7 +5,7 @@ use axum::{ }; use dotenvy::var; use secret_gift_exchange::{giftexchange, giftexchange_save, giftexchanges}; -use sqlx::{migrate::Migrator, sqlite::SqlitePoolOptions, sqlite::SqliteRow, Row, SqlitePool}; +use sqlx::{migrate::Migrator, sqlite::SqlitePoolOptions, Row, SqlitePool}; use sqlx::{postgres::PgPoolOptions, PgPool}; use std::net::SocketAddr; use tower_http::services::ServeDir; @@ -25,7 +25,7 @@ use error_handling::AppError; use google_oauth::{google_auth_return, login, logout}; use middlewares::inject_user_data; use routes::{about, contact, dashboard, index, profile, user_profile, user_profile_account, useradmin}; -use user::{add_user_role, delete_user_role, user_account, UserData}; +use user::{add_user_role, delete_user_role, UserData}; use wishlist::{ user_wishlist, user_wishlist_add, user_wishlist_add_item, user_wishlist_bought_item, user_wishlist_delete_item, user_wishlist_edit_item, user_wishlist_received_item, @@ -51,10 +51,10 @@ async fn main() { static MIGRATOR: Migrator = sqlx::migrate!(); -/* MIGRATOR + MIGRATOR .run(&app_state.db_pool) .await - .expect("Failed to run migrations"); */ + .expect("Failed to run migrations"); // Copy from old sqlite database if it exists if let Ok(source_db_url) = var("SOURCE_DB_URL") { @@ -315,4 +315,45 @@ async fn copy_database(sdb_pool: &SqlitePool, db_pool: &PgPool) { println!("Error: {}", e); } } + + // Run migration scripts again + // Copy accounts(users) to profiles(people) + let result =sqlx::query(r#"insert into people (created_by, updated_by, email, name, family_name, given_name) + select created_by, updated_by, email, name, family_name, given_name from users + on conflict do nothing;"#) + .execute(db_pool) + .await; + + if let Err(e) = result { + println!("Error: {}", e); + } + + // Link accounts to profiles + let result = sqlx::query(r#"update users u set person_id = p.id from people p where p.email = u.email;"#) + .execute(db_pool) + .await; + + if let Err(e) = result { + println!("Error: {}", e); + } + + // Move wishlist items from accounts to profiles + let result = sqlx::query(r#"update wishlist_items wi set user_id = p.person_id from users p where p.id = wi.user_id;"#) + .execute(db_pool) + .await; + + if let Err(e) = result { + println!("Error: {}", e); + } + + // Copy normal role from accounts to profiles + let result = sqlx::query(r#"insert into user_roles (created_at, created_by, updated_at, updated_by, user_id, role_id) + select ur.created_at, ur.created_by, ur.updated_at, ur.updated_by, u.person_id, ur.role_id from user_roles ur join roles r on r.id = ur.role_id join users u on u.id = ur.user_id where r.name = 'normal' + on conflict do nothing;"#) + .execute(db_pool) + .await; + + if let Err(e) = result { + println!("Error: {}", e); + } } diff --git a/backend/src/routes.rs b/backend/src/routes.rs index b204681..93dbb06 100644 --- a/backend/src/routes.rs +++ b/backend/src/routes.rs @@ -5,7 +5,6 @@ use axum::{ Extension, }; use http::StatusCode; -use reqwest::redirect; use sqlx::PgPool; use uuid::Uuid; diff --git a/backend/src/secret_gift_exchange.rs b/backend/src/secret_gift_exchange.rs index ae865e1..5b68b9c 100644 --- a/backend/src/secret_gift_exchange.rs +++ b/backend/src/secret_gift_exchange.rs @@ -1,9 +1,9 @@ use askama::Template; use axum::{ body::{self, Body}, - extract::{FromRequest, Path, Request, State}, + extract::{Path, Request, State}, response::{Html, IntoResponse, Redirect, Response}, - Extension, Form, Json, RequestExt, + Extension, }; use http::{header::CONTENT_TYPE, StatusCode}; use serde::{Deserialize, Serialize};