Skip to content

Commit

Permalink
remove supabase_project_ref and prefix from tenant
Browse files Browse the repository at this point in the history
  • Loading branch information
imor committed Aug 29, 2024
1 parent a8126e9 commit 04b7927
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 202 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

2 changes: 0 additions & 2 deletions api/migrations/20240819111151_create_tenants.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
create table
public.tenants (
id bigint generated always as identity primary key,
supabase_project_ref text,
prefix text not null,
name text not null
);
23 changes: 5 additions & 18 deletions api/src/db/tenants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,16 @@ use sqlx::PgPool;
pub struct Tenant {
pub id: i64,
pub name: String,
pub supabase_project_ref: Option<String>,
pub prefix: String,
}

pub async fn create_tenant(
pool: &PgPool,
tenant_name: &str,
supabase_project_ref: Option<&str>,
prefix: &str,
) -> Result<i64, sqlx::Error> {
pub async fn create_tenant(pool: &PgPool, tenant_name: &str) -> Result<i64, sqlx::Error> {
let record = sqlx::query!(
r#"
insert into tenants (name, supabase_project_ref, prefix)
values ($1, $2, $3)
insert into tenants (name)
values ($1)
returning id
"#,
tenant_name,
supabase_project_ref,
prefix
)
.fetch_one(pool)
.await?;
Expand All @@ -32,7 +23,7 @@ pub async fn create_tenant(
pub async fn read_tenant(pool: &PgPool, tenant_id: i64) -> Result<Option<Tenant>, sqlx::Error> {
let record = sqlx::query!(
r#"
select id, name, supabase_project_ref, prefix
select id, name
from tenants
where id = $1
"#,
Expand All @@ -44,8 +35,6 @@ pub async fn read_tenant(pool: &PgPool, tenant_id: i64) -> Result<Option<Tenant>
Ok(record.map(|r| Tenant {
id: r.id,
name: r.name,
supabase_project_ref: r.supabase_project_ref,
prefix: r.prefix,
}))
}

Expand Down Expand Up @@ -88,7 +77,7 @@ pub async fn delete_tenant(pool: &PgPool, tenant_id: i64) -> Result<Option<i64>,
pub async fn read_all_tenants(pool: &PgPool) -> Result<Vec<Tenant>, sqlx::Error> {
let mut record = sqlx::query!(
r#"
select id, name, supabase_project_ref, prefix
select id, name
from tenants
"#,
)
Expand All @@ -100,8 +89,6 @@ pub async fn read_all_tenants(pool: &PgPool) -> Result<Vec<Tenant>, sqlx::Error>
.map(|r| Tenant {
id: r.id,
name: r.name,
supabase_project_ref: r.supabase_project_ref,
prefix: r.prefix,
})
.collect())
}
20 changes: 2 additions & 18 deletions api/src/routes/tenants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use thiserror::Error;

use crate::{db, utils::generate_random_alpha_str};
use crate::db;

#[derive(Deserialize)]
struct PostTenantRequest {
name: String,
supabase_project_ref: Option<String>,
}

#[derive(Serialize)]
Expand Down Expand Up @@ -44,8 +43,6 @@ impl ResponseError for TenantError {
struct GetTenantResponse {
id: i64,
name: String,
supabase_project_ref: Option<String>,
prefix: String,
}

#[post("/tenants")]
Expand All @@ -55,16 +52,7 @@ pub async fn create_tenant(
) -> Result<impl Responder, TenantError> {
let tenant = tenant.0;
let name = tenant.name;
let spr = tenant.supabase_project_ref;
let id = match spr {
Some(spr) => {
db::tenants::create_tenant(&pool, &name, Some(spr.as_str()), spr.as_str()).await?
}
None => {
let prefix = generate_random_alpha_str(20);
db::tenants::create_tenant(&pool, &name, None, &prefix).await?
}
};
let id = db::tenants::create_tenant(&pool, &name).await?;
let response = PostTenantResponse { id };
Ok(Json(response))
}
Expand All @@ -80,8 +68,6 @@ pub async fn read_tenant(
.map(|t| GetTenantResponse {
id: t.id,
name: t.name,
supabase_project_ref: t.supabase_project_ref,
prefix: t.prefix,
})
.ok_or(TenantError::NotFound(tenant_id))?;
Ok(Json(response))
Expand Down Expand Up @@ -120,8 +106,6 @@ pub async fn read_all_tenants(pool: Data<PgPool>) -> Result<impl Responder, Tena
.map(|t| GetTenantResponse {
id: t.id,
name: t.name,
supabase_project_ref: t.supabase_project_ref,
prefix: t.prefix,
})
.collect();
Ok(Json(response))
Expand Down
48 changes: 2 additions & 46 deletions api/tests/api/tenants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use api::utils::generate_random_alpha_str;
use reqwest::StatusCode;

use crate::test_app::{
Expand All @@ -11,10 +10,7 @@ pub async fn create_tenant(app: &TestApp) -> i64 {
}

pub async fn create_tenant_with_name(app: &TestApp, name: String) -> i64 {
let tenant = CreateTenantRequest {
name,
supabase_project_ref: None,
};
let tenant = CreateTenantRequest { name };
let response = app.create_tenant(&tenant).await;
let response: CreateTenantResponse = response
.json()
Expand All @@ -24,48 +20,13 @@ pub async fn create_tenant_with_name(app: &TestApp, name: String) -> i64 {
}

#[tokio::test]
async fn tenant_can_be_created_with_supabase_project_ref() {
// Arrange
let app = spawn_app().await;

// Act
let supabase_project_ref = Some(generate_random_alpha_str(20));
let tenant = CreateTenantRequest {
name: "NewTenant".to_string(),
supabase_project_ref,
};
let response = app.create_tenant(&tenant).await;

// Assert
assert!(response.status().is_success());
let response: CreateTenantResponse = response
.json()
.await
.expect("failed to deserialize response");
assert_eq!(response.id, 1);

let tenant_id = response.id;
let response = app.read_tenant(tenant_id).await;
let response: TenantResponse = response
.json()
.await
.expect("failed to deserialize response");

assert_eq!(response.id, tenant_id);
assert_eq!(response.name, tenant.name);
assert_eq!(response.supabase_project_ref, tenant.supabase_project_ref);
assert_eq!(response.prefix, tenant.supabase_project_ref.unwrap());
}

#[tokio::test]
async fn tenant_can_be_created_without_supabase_project_ref() {
async fn tenant_can_be_created() {
// Arrange
let app = spawn_app().await;

// Act
let tenant = CreateTenantRequest {
name: "NewTenant".to_string(),
supabase_project_ref: None,
};
let response = app.create_tenant(&tenant).await;

Expand All @@ -86,8 +47,6 @@ async fn tenant_can_be_created_without_supabase_project_ref() {

assert_eq!(response.id, tenant_id);
assert_eq!(response.name, tenant.name);
assert_eq!(response.supabase_project_ref, None);
assert!(response.prefix.len() == 20);
}

#[tokio::test]
Expand All @@ -96,7 +55,6 @@ async fn an_existing_tenant_can_be_read() {
let app = spawn_app().await;
let tenant = CreateTenantRequest {
name: "NewTenant".to_string(),
supabase_project_ref: None,
};
let response = app.create_tenant(&tenant).await;
let response: CreateTenantResponse = response
Expand Down Expand Up @@ -136,7 +94,6 @@ async fn an_existing_tenant_can_be_updated() {
let app = spawn_app().await;
let tenant = CreateTenantRequest {
name: "NewTenant".to_string(),
supabase_project_ref: None,
};
let response = app.create_tenant(&tenant).await;
let response: CreateTenantResponse = response
Expand Down Expand Up @@ -183,7 +140,6 @@ async fn an_existing_tenant_can_be_deleted() {
let app = spawn_app().await;
let tenant = CreateTenantRequest {
name: "NewTenant".to_string(),
supabase_project_ref: None,
};
let response = app.create_tenant(&tenant).await;
let response: CreateTenantResponse = response
Expand Down
Loading

0 comments on commit 04b7927

Please sign in to comment.