From 131c9ed65f8560a7762f60204073d31411e89ae1 Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 8 Apr 2024 17:23:26 +0800 Subject: [PATCH 1/6] chore: use join --- libs/database/src/workspace.rs | 26 +++++++++++++------------- tests/main.rs | 1 - 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/libs/database/src/workspace.rs b/libs/database/src/workspace.rs index 9cf2fa6f..00006796 100644 --- a/libs/database/src/workspace.rs +++ b/libs/database/src/workspace.rs @@ -588,19 +588,19 @@ pub async fn select_all_user_workspaces<'a, E: Executor<'a, Database = Postgres> AFWorkspaceRow, r#" SELECT - workspace_id, - database_storage_id, - owner_uid, - (SELECT name FROM public.af_user WHERE uid = owner_uid) AS owner_name, - created_at, - workspace_type, - deleted_at, - workspace_name, - icon - FROM public.af_workspace - WHERE workspace_id IN ( - SELECT workspace_id FROM public.af_workspace_member - WHERE af_workspace_member.uid = (SELECT uid FROM public.af_user WHERE uuid = $1) + w.workspace_id, + w.database_storage_id, + w.owner_uid, + (SELECT name FROM public.af_user WHERE uid = w.owner_uid) AS owner_name, + w.created_at, + w.workspace_type, + w.deleted_at, + w.workspace_name, + w.icon + FROM af_workspace w + JOIN af_workspace_member wm ON w.workspace_id = wm.workspace_id + WHERE wm.uid = ( + SELECT uid FROM public.af_user WHERE uuid = $1 ); "#, user_uuid diff --git a/tests/main.rs b/tests/main.rs index 83983fdb..7b2a6aff 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -1,6 +1,5 @@ mod collab; mod collab_snapshot; -// mod collab_stream; mod gotrue; mod user; mod websocket; From c4c8911af2fa4b3a70a82a714691ae31b169c838 Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 8 Apr 2024 18:23:25 +0800 Subject: [PATCH 2/6] chore: add test --- libs/workspace-template/Cargo.toml | 10 +++---- tests/main.rs | 1 + tests/pg_sql/mod.rs | 2 ++ tests/pg_sql/util.rs | 44 ++++++++++++++++++++++++++++++ tests/pg_sql/workspace_test.rs | 17 ++++++++++++ 5 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 tests/pg_sql/mod.rs create mode 100644 tests/pg_sql/util.rs create mode 100644 tests/pg_sql/workspace_test.rs diff --git a/libs/workspace-template/Cargo.toml b/libs/workspace-template/Cargo.toml index 13e5634d..9f3f1f89 100644 --- a/libs/workspace-template/Cargo.toml +++ b/libs/workspace-template/Cargo.toml @@ -9,10 +9,10 @@ crate-type = ["cdylib", "rlib"] [dependencies] bytes.workspace = true -collab = { version = "0.1.0"} -collab-folder = { version = "0.1.0"} -collab-document = { version = "0.1.0"} -collab-entity = { version = "0.1.0"} +collab = { version = "0.1.0" } +collab-folder = { version = "0.1.0" } +collab-document = { version = "0.1.0" } +collab-entity = { version = "0.1.0" } collab-database = { version = "0.1.0" } async-trait = "0.1.77" anyhow.workspace = true @@ -24,4 +24,4 @@ nanoid = "0.4.0" serde = { version = "1.0.195", features = ["derive"] } [target.'cfg(target_arch = "wasm32")'.dependencies] -getrandom = { version = "0.2", features = ["js"]} +getrandom = { version = "0.2", features = ["js"] } diff --git a/tests/main.rs b/tests/main.rs index 7b2a6aff..8980c7a0 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -1,6 +1,7 @@ mod collab; mod collab_snapshot; mod gotrue; +mod pg_sql; mod user; mod websocket; mod workspace; diff --git a/tests/pg_sql/mod.rs b/tests/pg_sql/mod.rs new file mode 100644 index 00000000..7cac0f51 --- /dev/null +++ b/tests/pg_sql/mod.rs @@ -0,0 +1,2 @@ +mod util; +mod workspace_test; diff --git a/tests/pg_sql/util.rs b/tests/pg_sql/util.rs new file mode 100644 index 00000000..72d244c7 --- /dev/null +++ b/tests/pg_sql/util.rs @@ -0,0 +1,44 @@ +use lazy_static::lazy_static; +use snowflake::Snowflake; +use sqlx::PgPool; +use tokio::sync::RwLock; +use uuid::Uuid; + +pub async fn setup_db(pool: &PgPool) -> anyhow::Result<()> { + // Have to manually create schema and tables managed by gotrue but referenced by our + // migration scripts. + sqlx::query(r#"create schema auth"#).execute(pool).await?; + sqlx::query( + r#"create table auth.users( +id uuid NOT NULL UNIQUE, +deleted_at timestamptz null, +CONSTRAINT users_pkey PRIMARY KEY (id) +)"#, + ) + .execute(pool) + .await?; + + sqlx::migrate!("./migrations") + .set_ignore_missing(true) + .run(pool) + .await + .unwrap(); + Ok(()) +} + +lazy_static! { + pub static ref ID_GEN: RwLock = RwLock::new(Snowflake::new(1)); +} + +pub async fn test_create_user( + pool: &PgPool, + user_uuid: Uuid, + email: &str, + name: &str, +) -> anyhow::Result<()> { + let uid = ID_GEN.write().await.next_id(); + database::user::create_user(pool, uid, &user_uuid, &email, &name) + .await + .unwrap(); + Ok(()) +} diff --git a/tests/pg_sql/workspace_test.rs b/tests/pg_sql/workspace_test.rs new file mode 100644 index 00000000..e56143de --- /dev/null +++ b/tests/pg_sql/workspace_test.rs @@ -0,0 +1,17 @@ +use crate::pg_sql::util::{setup_db, test_create_user}; +use sqlx::PgPool; + +#[sqlx::test(migrations = false)] +async fn basic_test(pool: PgPool) -> sqlx::Result<()> { + setup_db(&pool).await.unwrap(); + + let user_uuid = uuid::Uuid::new_v4(); + let name = user_uuid.to_string(); + let email = format!("{}@appflowy.io", name); + + test_create_user(&pool, user_uuid, &email, &name) + .await + .unwrap(); + + Ok(()) +} From 7fda6119e1de32995075aa3438892507ec31b916 Mon Sep 17 00:00:00 2001 From: Zack Fu Zi Xiang Date: Mon, 8 Apr 2024 18:42:11 +0800 Subject: [PATCH 3/6] chore: cargo clippy --- ...fab3a5a67eef820f9885c72117364c683fb4a.json | 70 ------------------- tests/pg_sql/util.rs | 2 +- 2 files changed, 1 insertion(+), 71 deletions(-) delete mode 100644 .sqlx/query-71093ffc23294c01b80af7b1c43fab3a5a67eef820f9885c72117364c683fb4a.json diff --git a/.sqlx/query-71093ffc23294c01b80af7b1c43fab3a5a67eef820f9885c72117364c683fb4a.json b/.sqlx/query-71093ffc23294c01b80af7b1c43fab3a5a67eef820f9885c72117364c683fb4a.json deleted file mode 100644 index 2581b380..00000000 --- a/.sqlx/query-71093ffc23294c01b80af7b1c43fab3a5a67eef820f9885c72117364c683fb4a.json +++ /dev/null @@ -1,70 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n SELECT\n workspace_id,\n database_storage_id,\n owner_uid,\n (SELECT name FROM public.af_user WHERE uid = owner_uid) AS owner_name,\n created_at,\n workspace_type,\n deleted_at,\n workspace_name,\n icon\n FROM public.af_workspace\n WHERE workspace_id IN (\n SELECT workspace_id FROM public.af_workspace_member\n WHERE af_workspace_member.uid = (SELECT uid FROM public.af_user WHERE uuid = $1)\n );\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "workspace_id", - "type_info": "Uuid" - }, - { - "ordinal": 1, - "name": "database_storage_id", - "type_info": "Uuid" - }, - { - "ordinal": 2, - "name": "owner_uid", - "type_info": "Int8" - }, - { - "ordinal": 3, - "name": "owner_name", - "type_info": "Text" - }, - { - "ordinal": 4, - "name": "created_at", - "type_info": "Timestamptz" - }, - { - "ordinal": 5, - "name": "workspace_type", - "type_info": "Int4" - }, - { - "ordinal": 6, - "name": "deleted_at", - "type_info": "Timestamptz" - }, - { - "ordinal": 7, - "name": "workspace_name", - "type_info": "Text" - }, - { - "ordinal": 8, - "name": "icon", - "type_info": "Text" - } - ], - "parameters": { - "Left": [ - "Uuid" - ] - }, - "nullable": [ - false, - false, - false, - null, - true, - false, - true, - true, - false - ] - }, - "hash": "71093ffc23294c01b80af7b1c43fab3a5a67eef820f9885c72117364c683fb4a" -} diff --git a/tests/pg_sql/util.rs b/tests/pg_sql/util.rs index 72d244c7..898e90c1 100644 --- a/tests/pg_sql/util.rs +++ b/tests/pg_sql/util.rs @@ -37,7 +37,7 @@ pub async fn test_create_user( name: &str, ) -> anyhow::Result<()> { let uid = ID_GEN.write().await.next_id(); - database::user::create_user(pool, uid, &user_uuid, &email, &name) + database::user::create_user(pool, uid, &user_uuid, email, &name) .await .unwrap(); Ok(()) From da35d6d581fa566ca371ad194e000b43ab05b7e0 Mon Sep 17 00:00:00 2001 From: Zack Fu Zi Xiang Date: Mon, 8 Apr 2024 18:48:18 +0800 Subject: [PATCH 4/6] chore: cargo deny check advisories --- Cargo.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eff26b3d..8c953fe2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,7 +98,7 @@ dependencies = [ "encoding_rs", "flate2", "futures-core", - "h2 0.3.24", + "h2 0.3.26", "http 0.2.11", "httparse", "httpdate", @@ -887,7 +887,7 @@ dependencies = [ "derive_more", "futures-core", "futures-util", - "h2 0.3.24", + "h2 0.3.26", "http 0.2.11", "itoa", "log", @@ -2825,9 +2825,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.3.24" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb2c4422095b67ee78da96fbb51a4cc413b3b25883c7717ff7ca1ab31022c9c9" +checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" dependencies = [ "bytes", "fnv", @@ -3069,7 +3069,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2 0.3.24", + "h2 0.3.26", "http 0.2.11", "http-body 0.4.6", "httparse", @@ -4858,7 +4858,7 @@ dependencies = [ "encoding_rs", "futures-core", "futures-util", - "h2 0.3.24", + "h2 0.3.26", "http 0.2.11", "http-body 0.4.6", "hyper 0.14.28", From 583edfdb72a5db5d0b1d5ebcc327c1bf09050562 Mon Sep 17 00:00:00 2001 From: Zack Fu Zi Xiang Date: Mon, 8 Apr 2024 19:11:41 +0800 Subject: [PATCH 5/6] chore: cargo clippy --- tests/pg_sql/util.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/pg_sql/util.rs b/tests/pg_sql/util.rs index 898e90c1..6ce5fb84 100644 --- a/tests/pg_sql/util.rs +++ b/tests/pg_sql/util.rs @@ -37,7 +37,7 @@ pub async fn test_create_user( name: &str, ) -> anyhow::Result<()> { let uid = ID_GEN.write().await.next_id(); - database::user::create_user(pool, uid, &user_uuid, email, &name) + database::user::create_user(pool, uid, &user_uuid, email, name) .await .unwrap(); Ok(()) From 768d6838781a3af9e019628da1cb6f84819b7906 Mon Sep 17 00:00:00 2001 From: Zack Fu Zi Xiang Date: Tue, 9 Apr 2024 03:10:12 +0800 Subject: [PATCH 6/6] fix: add user to auth user --- tests/pg_sql/util.rs | 25 ++++++++++++++++++++----- tests/pg_sql/workspace_test.rs | 3 ++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/tests/pg_sql/util.rs b/tests/pg_sql/util.rs index 6ce5fb84..848217b7 100644 --- a/tests/pg_sql/util.rs +++ b/tests/pg_sql/util.rs @@ -9,11 +9,13 @@ pub async fn setup_db(pool: &PgPool) -> anyhow::Result<()> { // migration scripts. sqlx::query(r#"create schema auth"#).execute(pool).await?; sqlx::query( - r#"create table auth.users( -id uuid NOT NULL UNIQUE, -deleted_at timestamptz null, -CONSTRAINT users_pkey PRIMARY KEY (id) -)"#, + r#" + CREATE TABLE auth.users( + id uuid NOT NULL UNIQUE, + deleted_at timestamptz null, + CONSTRAINT users_pkey PRIMARY KEY (id) + ) + "#, ) .execute(pool) .await?; @@ -26,6 +28,19 @@ CONSTRAINT users_pkey PRIMARY KEY (id) Ok(()) } +pub async fn insert_auth_user(pool: &PgPool, user_uuid: Uuid) -> anyhow::Result<()> { + sqlx::query( + r#" + INSERT INTO auth.users (id) + VALUES ($1) + "#, + ) + .bind(user_uuid) + .execute(pool) + .await?; + Ok(()) +} + lazy_static! { pub static ref ID_GEN: RwLock = RwLock::new(Snowflake::new(1)); } diff --git a/tests/pg_sql/workspace_test.rs b/tests/pg_sql/workspace_test.rs index e56143de..e7a066a3 100644 --- a/tests/pg_sql/workspace_test.rs +++ b/tests/pg_sql/workspace_test.rs @@ -1,4 +1,4 @@ -use crate::pg_sql::util::{setup_db, test_create_user}; +use crate::pg_sql::util::{insert_auth_user, setup_db, test_create_user}; use sqlx::PgPool; #[sqlx::test(migrations = false)] @@ -9,6 +9,7 @@ async fn basic_test(pool: PgPool) -> sqlx::Result<()> { let name = user_uuid.to_string(); let email = format!("{}@appflowy.io", name); + insert_auth_user(&pool, user_uuid).await.unwrap(); test_create_user(&pool, user_uuid, &email, &name) .await .unwrap();