Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tenants update on conflict handling #298

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/stores/tenant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,11 @@ impl TenantStore for PgPool {
#[instrument(skip(self))]
async fn create_tenant(&self, params: TenantUpdateParams) -> Result<Tenant> {
let res = sqlx::query_as::<sqlx::postgres::Postgres, Tenant>(
"INSERT INTO public.tenants (id) VALUES ($1) RETURNING *;",
"INSERT INTO public.tenants (id)
VALUES ($1)
ON CONFLICT (id)
DO UPDATE SET updated_at = NOW()
RETURNING *",
)
.bind(params.id)
.fetch_one(self)
Expand All @@ -314,7 +318,7 @@ impl TenantStore for PgPool {
#[instrument(skip(self))]
async fn update_tenant(&self, id: &str, params: TenantUpdateParams) -> Result<Tenant> {
let res = sqlx::query_as::<sqlx::postgres::Postgres, Tenant>(
"UPDATE public.tenants SET id = $2 WHERE id = $1 RETURNING *;",
"UPDATE public.tenants SET id = $2, updated_at = NOW() WHERE id = $1 RETURNING *;",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose even of this function? It looks like this is never called anyway. Should just remove as id shouldn't be updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was added a long ago, probably the author expect to use it somehow. I'll remove this rudiment.

)
.bind(id)
.bind(params.id)
Expand All @@ -327,7 +331,8 @@ impl TenantStore for PgPool {
#[instrument(skip(self))]
async fn update_tenant_fcm(&self, id: &str, params: TenantFcmUpdateParams) -> Result<Tenant> {
let res = sqlx::query_as::<sqlx::postgres::Postgres, Tenant>(
"UPDATE public.tenants SET fcm_api_key = $2 WHERE id = $1 RETURNING *;",
"UPDATE public.tenants SET fcm_api_key = $2, updated_at = NOW() WHERE id = $1 \
RETURNING *;",
)
.bind(id)
.bind(params.fcm_api_key)
Expand All @@ -340,7 +345,8 @@ impl TenantStore for PgPool {
#[instrument(skip(self))]
async fn update_tenant_apns(&self, id: &str, params: TenantApnsUpdateParams) -> Result<Tenant> {
let res = sqlx::query_as::<sqlx::postgres::Postgres, Tenant>(
"UPDATE public.tenants SET apns_topic = $2 WHERE id = $1 RETURNING *;",
"UPDATE public.tenants SET apns_topic = $2, updated_at = NOW() WHERE id = $1 \
RETURNING *;",
)
.bind(id)
.bind(params.apns_topic)
Expand All @@ -363,7 +369,7 @@ impl TenantStore for PgPool {
} => sqlx::query_as::<sqlx::postgres::Postgres, Tenant>(
"UPDATE public.tenants SET apns_type = 'certificate'::apns_type, apns_certificate \
= $2, apns_certificate_password = $3, apns_pkcs8_pem = null, apns_team_id = \
null, apns_key_id = null WHERE id = $1 RETURNING *;",
null, apns_key_id = null, updated_at = NOW() WHERE id = $1 RETURNING *;",
)
.bind(id)
.bind(apns_certificate)
Expand All @@ -375,7 +381,7 @@ impl TenantStore for PgPool {
} => sqlx::query_as::<sqlx::postgres::Postgres, Tenant>(
"UPDATE public.tenants SET apns_type = 'token'::apns_type, apns_pkcs8_pem = $2, \
apns_team_id = $3, apns_key_id = $4, apns_certificate = null, \
apns_certificate_password = null WHERE id = $1 RETURNING *;",
apns_certificate_password = null, updated_at = NOW() WHERE id = $1 RETURNING *;",
)
.bind(id)
.bind(apns_pkcs8_pem)
Expand All @@ -391,7 +397,7 @@ impl TenantStore for PgPool {
#[instrument(skip(self))]
async fn suspend_tenant(&self, id: &str, reason: &str) -> Result<()> {
let mut query_builder = sqlx::QueryBuilder::new(
"UPDATE public.tenants SET suspended = true, suspended_reason =",
"UPDATE public.tenants SET suspended = true, updated_at = NOW(), suspended_reason =",
);
query_builder.push_bind(reason);
query_builder.push(" WHERE id = ");
Expand All @@ -406,7 +412,8 @@ impl TenantStore for PgPool {
#[instrument(skip(self))]
async fn unsuspend_tenant(&self, id: &str) -> Result<()> {
let mut query_builder = sqlx::QueryBuilder::new(
"UPDATE public.tenants SET suspended = false, suspended_reason = null WHERE id = ",
"UPDATE public.tenants SET suspended = false, updated_at = NOW(), suspended_reason = \
null WHERE id = ",
);
query_builder.push_bind(id);
let query = query_builder.build();
Expand Down
Loading