diff --git a/.ds.baseline b/.ds.baseline index 148542232..2baf278e1 100644 --- a/.ds.baseline +++ b/.ds.baseline @@ -349,7 +349,7 @@ "filename": "tests/app/user/test_rest.py", "hashed_secret": "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33", "is_verified": false, - "line_number": 858, + "line_number": 864, "is_secret": false } ], @@ -384,5 +384,5 @@ } ] }, - "generated_at": "2024-11-15T18:43:06Z" + "generated_at": "2024-12-19T19:09:50Z" } diff --git a/app/celery/scheduled_tasks.py b/app/celery/scheduled_tasks.py index f51b2d994..1a2485904 100644 --- a/app/celery/scheduled_tasks.py +++ b/app/celery/scheduled_tasks.py @@ -159,6 +159,7 @@ def replay_created_notifications(): @notify_celery.task(name="check-for-missing-rows-in-completed-jobs") def check_for_missing_rows_in_completed_jobs(): + jobs = find_jobs_with_missing_rows() for job in jobs: ( diff --git a/app/config.py b/app/config.py index 12159e289..ce7b632ea 100644 --- a/app/config.py +++ b/app/config.py @@ -208,7 +208,7 @@ class Config(object): }, "check-for-missing-rows-in-completed-jobs": { "task": "check-for-missing-rows-in-completed-jobs", - "schedule": crontab(minute="*/10"), + "schedule": crontab(minute="*/2"), "options": {"queue": QueueNames.PERIODIC}, }, "replay-created-notifications": { diff --git a/app/dao/annual_billing_dao.py b/app/dao/annual_billing_dao.py index 306a2dd86..8b6d092f4 100644 --- a/app/dao/annual_billing_dao.py +++ b/app/dao/annual_billing_dao.py @@ -29,8 +29,8 @@ def dao_create_or_update_annual_billing_for_year( def dao_get_annual_billing(service_id): stmt = ( select(AnnualBilling) - .filter_by( - service_id=service_id, + .where( + AnnualBilling.service_id == service_id, ) .order_by(AnnualBilling.financial_year_start) ) @@ -57,8 +57,9 @@ def dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start=No if not financial_year_start: financial_year_start = get_current_calendar_year_start_year() - stmt = select(AnnualBilling).filter_by( - service_id=service_id, financial_year_start=financial_year_start + stmt = select(AnnualBilling).where( + AnnualBilling.service_id == service_id, + AnnualBilling.financial_year_start == financial_year_start, ) return db.session.execute(stmt).scalars().first() @@ -66,8 +67,8 @@ def dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start=No def dao_get_all_free_sms_fragment_limit(service_id): stmt = ( select(AnnualBilling) - .filter_by( - service_id=service_id, + .where( + AnnualBilling.service_id == service_id, ) .order_by(AnnualBilling.financial_year_start) ) diff --git a/app/dao/api_key_dao.py b/app/dao/api_key_dao.py index ebfdcb43e..205b0fb8c 100644 --- a/app/dao/api_key_dao.py +++ b/app/dao/api_key_dao.py @@ -43,7 +43,7 @@ def get_model_api_keys(service_id, id=None): select(ApiKey).where( ApiKey.id == id, ApiKey.service_id == service_id, - ApiKey.expiry_date == None, # noqa + ApiKey.expiry_date == None, # noqa ) ) .scalars() @@ -88,7 +88,9 @@ def get_unsigned_secret(key_id): """ api_key = ( db.session.execute( - select(ApiKey).where(ApiKey.id == key_id, ApiKey.expiry_date == None) # noqa + select(ApiKey).where( + ApiKey.id == key_id, ApiKey.expiry_date == None # noqa + ) ) .scalars() .one() diff --git a/app/dao/complaint_dao.py b/app/dao/complaint_dao.py index 63b7487fb..d50c0aa0c 100644 --- a/app/dao/complaint_dao.py +++ b/app/dao/complaint_dao.py @@ -33,7 +33,7 @@ def fetch_paginated_complaints(page=1): def fetch_complaints_by_service(service_id): stmt = ( select(Complaint) - .filter_by(service_id=service_id) + .where(Complaint.service_id == service_id) .order_by(desc(Complaint.created_at)) ) return db.session.execute(stmt).scalars().all() diff --git a/app/dao/email_branding_dao.py b/app/dao/email_branding_dao.py index 61dc2a46b..bb41ceadf 100644 --- a/app/dao/email_branding_dao.py +++ b/app/dao/email_branding_dao.py @@ -11,7 +11,9 @@ def dao_get_email_branding_options(): def dao_get_email_branding_by_id(email_branding_id): return ( - db.session.execute(select(EmailBranding).filter_by(id=email_branding_id)) + db.session.execute( + select(EmailBranding).where(EmailBranding.id == email_branding_id) + ) .scalars() .one() ) @@ -19,7 +21,9 @@ def dao_get_email_branding_by_id(email_branding_id): def dao_get_email_branding_by_name(email_branding_name): return ( - db.session.execute(select(EmailBranding).filter_by(name=email_branding_name)) + db.session.execute( + select(EmailBranding).where(EmailBranding.name == email_branding_name) + ) .scalars() .first() ) diff --git a/app/dao/inbound_sms_dao.py b/app/dao/inbound_sms_dao.py index feb967b54..e9a84ffa3 100644 --- a/app/dao/inbound_sms_dao.py +++ b/app/dao/inbound_sms_dao.py @@ -180,7 +180,9 @@ def delete_inbound_sms_older_than_retention(): def dao_get_inbound_sms_by_id(service_id, inbound_id): - stmt = select(InboundSms).filter_by(id=inbound_id, service_id=service_id) + stmt = select(InboundSms).where( + InboundSms.id == inbound_id, InboundSms.service_id == service_id + ) return db.session.execute(stmt).scalars().one() diff --git a/app/dao/invited_org_user_dao.py b/app/dao/invited_org_user_dao.py index e817f405e..823e9a8f4 100644 --- a/app/dao/invited_org_user_dao.py +++ b/app/dao/invited_org_user_dao.py @@ -15,8 +15,9 @@ def save_invited_org_user(invited_org_user): def get_invited_org_user(organization_id, invited_org_user_id): return ( db.session.execute( - select(InvitedOrganizationUser).filter_by( - organization_id=organization_id, id=invited_org_user_id + select(InvitedOrganizationUser).where( + InvitedOrganizationUser.organization_id == organization_id, + InvitedOrganizationUser.id == invited_org_user_id, ) ) .scalars() @@ -27,7 +28,9 @@ def get_invited_org_user(organization_id, invited_org_user_id): def get_invited_org_user_by_id(invited_org_user_id): return ( db.session.execute( - select(InvitedOrganizationUser).filter_by(id=invited_org_user_id) + select(InvitedOrganizationUser).where( + InvitedOrganizationUser.id == invited_org_user_id + ) ) .scalars() .one() @@ -37,7 +40,9 @@ def get_invited_org_user_by_id(invited_org_user_id): def get_invited_org_users_for_organization(organization_id): return ( db.session.execute( - select(InvitedOrganizationUser).filter_by(organization_id=organization_id) + select(InvitedOrganizationUser).where( + InvitedOrganizationUser.organization_id == organization_id + ) ) .scalars() .all() diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index ddec26956..ae6dec628 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -39,7 +39,7 @@ def dao_get_notification_outcomes_for_job(service_id, job_id): def dao_get_job_by_service_id_and_job_id(service_id, job_id): - stmt = select(Job).filter_by(service_id=service_id, id=job_id) + stmt = select(Job).where(Job.service_id == service_id, Job.id == job_id) return db.session.execute(stmt).scalars().one() @@ -97,7 +97,7 @@ def dao_get_scheduled_job_stats( def dao_get_job_by_id(job_id): - stmt = select(Job).filter_by(id=job_id) + stmt = select(Job).where(Job.id == job_id) return db.session.execute(stmt).scalars().one() diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index d74e85ba9..ed60de791 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -209,7 +209,9 @@ def get_notifications_for_job( if page_size is None: page_size = current_app.config["PAGE_SIZE"] - stmt = select(Notification).filter_by(service_id=service_id, job_id=job_id) + stmt = select(Notification).where( + Notification.service_id == service_id, Notification.job_id == job_id + ) stmt = _filter_query(stmt, filter_dict) stmt = stmt.order_by(asc(Notification.job_row_number)) @@ -223,30 +225,35 @@ def get_notifications_for_job( def dao_get_notification_count_for_job_id(*, job_id): - stmt = select(func.count(Notification.id)).filter_by(job_id=job_id) + stmt = select(func.count(Notification.id)).where(Notification.job_id == job_id) return db.session.execute(stmt).scalar() def dao_get_notification_count_for_service(*, service_id): - stmt = select(func.count(Notification.id)).filter_by(service_id=service_id) + stmt = select(func.count(Notification.id)).where( + Notification.service_id == service_id + ) return db.session.execute(stmt).scalar() def dao_get_failed_notification_count(): - stmt = select(func.count(Notification.id)).filter_by( - status=NotificationStatus.FAILED + stmt = select(func.count(Notification.id)).where( + Notification.status == NotificationStatus.FAILED ) return db.session.execute(stmt).scalar() def get_notification_with_personalisation(service_id, notification_id, key_type): - filter_dict = {"service_id": service_id, "id": notification_id} + filter_dict = { + "Notification.service_id": service_id, + "Notification.id": notification_id, + } if key_type: - filter_dict["key_type"] = key_type + filter_dict["Notification.key_type"] = key_type stmt = ( select(Notification) - .filter_by(**filter_dict) + .where(**filter_dict) .options(joinedload(Notification.template)) ) return db.session.execute(stmt).scalars().one() diff --git a/app/dao/organization_dao.py b/app/dao/organization_dao.py index 668ac6c25..cd03e9112 100644 --- a/app/dao/organization_dao.py +++ b/app/dao/organization_dao.py @@ -27,17 +27,19 @@ def dao_count_organizations_with_live_services(): def dao_get_organization_services(organization_id): - stmt = select(Organization).filter_by(id=organization_id) + stmt = select(Organization).where(Organization.id == organization_id) return db.session.execute(stmt).scalars().one().services def dao_get_organization_live_services(organization_id): - stmt = select(Service).filter_by(organization_id=organization_id, restricted=False) + stmt = select(Service).where( + Service.organization_id == organization_id, Service.restricted == False # noqa + ) return db.session.execute(stmt).scalars().all() def dao_get_organization_by_id(organization_id): - stmt = select(Organization).filter_by(id=organization_id) + stmt = select(Organization).where(Organization.id == organization_id) return db.session.execute(stmt).scalars().one() @@ -49,14 +51,18 @@ def dao_get_organization_by_email_address(email_address): if email_address.endswith( "@{}".format(domain.domain) ) or email_address.endswith(".{}".format(domain.domain)): - stmt = select(Organization).filter_by(id=domain.organization_id) + stmt = select(Organization).where(Organization.id == domain.organization_id) return db.session.execute(stmt).scalars().one() return None def dao_get_organization_by_service_id(service_id): - stmt = select(Organization).join(Organization.services).filter_by(id=service_id) + stmt = ( + select(Organization) + .join(Organization.services) + .where(Organization.id == service_id) + ) return db.session.execute(stmt).scalars().first() @@ -74,7 +80,7 @@ def dao_update_organization(organization_id, **kwargs): num_updated = db.session.execute(stmt).rowcount if isinstance(domains, list): - stmt = delete(Domain).filter_by(organization_id=organization_id) + stmt = delete(Domain).where(Domain.organization_id == organization_id) db.session.execute(stmt) db.session.bulk_save_objects( [ @@ -108,7 +114,7 @@ def _update_organization_services(organization, attribute, only_where_none=True) @autocommit @version_class(Service) def dao_add_service_to_organization(service, organization_id): - stmt = select(Organization).filter_by(id=organization_id) + stmt = select(Organization).where(Organization.id == organization_id) organization = db.session.execute(stmt).scalars().one() service.organization_id = organization_id @@ -130,7 +136,7 @@ def dao_get_users_for_organization(organization_id): @autocommit def dao_add_user_to_organization(organization_id, user_id): organization = dao_get_organization_by_id(organization_id) - stmt = select(User).filter_by(id=user_id) + stmt = select(User).where(User.id == user_id) user = db.session.execute(stmt).scalars().one() user.organizations.append(organization) db.session.add(organization) diff --git a/app/dao/permissions_dao.py b/app/dao/permissions_dao.py index 24503fa70..4bec2193e 100644 --- a/app/dao/permissions_dao.py +++ b/app/dao/permissions_dao.py @@ -17,12 +17,14 @@ class PermissionDAO(DAOClass): def remove_user_service_permissions(self, user, service): db.session.execute( - delete(self.Meta.model).filter_by(user=user, service=service) + delete(self.Meta.model).where( + self.Meta.model.user == user, self.Meta.model.service == service + ) ) db.session.commit() def remove_user_service_permissions_for_all_services(self, user): - db.session.execute(delete(self.Meta.model).filter_by(user=user)) + db.session.execute(delete(self.Meta.model).where(self.Meta.model.user == user)) db.session.commit() def set_user_service_permission( @@ -53,9 +55,9 @@ class PermissionDAO(DAOClass): return ( db.session.execute( select(self.Meta.model) - .filter_by(user_id=user_id) + .where(self.Meta.model.user_id == user_id) .join(Permission.service) - .filter_by(active=True) + .where(Permission.active == True) # noqa ) .scalars() .all() @@ -65,9 +67,9 @@ class PermissionDAO(DAOClass): return ( db.session.execute( select(self.Meta.model) - .filter_by(user_id=user_id) + .where(self.Meta.model.user_id == user_id) .join(Permission.service) - .filter_by(active=True, id=service_id) + .where(Permission.active == True, Permission.id == service_id) # noqa ) .scalars() .all() diff --git a/app/dao/service_callback_api_dao.py b/app/dao/service_callback_api_dao.py index d65e341ef..4c81b5c5f 100644 --- a/app/dao/service_callback_api_dao.py +++ b/app/dao/service_callback_api_dao.py @@ -33,8 +33,9 @@ def reset_service_callback_api( def get_service_callback_api(service_callback_api_id, service_id): return ( db.session.execute( - select(ServiceCallbackApi).filter_by( - id=service_callback_api_id, service_id=service_id + select(ServiceCallbackApi).where( + ServiceCallbackApi.id == service_callback_api_id, + ServiceCallbackApi.service_id == service_id, ) ) .scalars() @@ -45,9 +46,9 @@ def get_service_callback_api(service_callback_api_id, service_id): def get_service_delivery_status_callback_api_for_service(service_id): return ( db.session.execute( - select(ServiceCallbackApi).filter_by( - service_id=service_id, - callback_type=CallbackType.DELIVERY_STATUS, + select(ServiceCallbackApi).where( + ServiceCallbackApi.service_id == service_id, + ServiceCallbackApi.callback_type == CallbackType.DELIVERY_STATUS, ) ) .scalars() @@ -58,9 +59,9 @@ def get_service_delivery_status_callback_api_for_service(service_id): def get_service_complaint_callback_api_for_service(service_id): return ( db.session.execute( - select(ServiceCallbackApi).filter_by( - service_id=service_id, - callback_type=CallbackType.COMPLAINT, + select(ServiceCallbackApi).where( + ServiceCallbackApi.service_id == service_id, + ServiceCallbackApi.callback_type == CallbackType.COMPLAINT, ) ) .scalars() diff --git a/app/dao/service_email_reply_to_dao.py b/app/dao/service_email_reply_to_dao.py index ff1991238..56e98f6a4 100644 --- a/app/dao/service_email_reply_to_dao.py +++ b/app/dao/service_email_reply_to_dao.py @@ -73,7 +73,10 @@ def update_reply_to_email_address(service_id, reply_to_id, email_address, is_def def archive_reply_to_email_address(service_id, reply_to_id): reply_to_archive = ( db.session.execute( - select(ServiceEmailReplyTo).filter_by(id=reply_to_id, service_id=service_id) + select(ServiceEmailReplyTo).where( + ServiceEmailReplyTo.id == reply_to_id, + ServiceEmailReplyTo.service_id == service_id, + ) ) .scalars() .one() diff --git a/app/dao/service_inbound_api_dao.py b/app/dao/service_inbound_api_dao.py index af9c3689b..45efaefd7 100644 --- a/app/dao/service_inbound_api_dao.py +++ b/app/dao/service_inbound_api_dao.py @@ -32,8 +32,9 @@ def reset_service_inbound_api( def get_service_inbound_api(service_inbound_api_id, service_id): return ( db.session.execute( - select(ServiceInboundApi).filter_by( - id=service_inbound_api_id, service_id=service_id + select(ServiceInboundApi).where( + ServiceInboundApi.id == service_inbound_api_id, + ServiceInboundApi.service_id == service_id, ) ) .scalars() @@ -43,7 +44,9 @@ def get_service_inbound_api(service_inbound_api_id, service_id): def get_service_inbound_api_for_service(service_id): return ( - db.session.execute(select(ServiceInboundApi).filter_by(service_id=service_id)) + db.session.execute( + select(ServiceInboundApi).where(ServiceInboundApi.service_id == service_id) + ) .scalars() .first() ) diff --git a/app/dao/service_sms_sender_dao.py b/app/dao/service_sms_sender_dao.py index e9597c1a1..e2d244c52 100644 --- a/app/dao/service_sms_sender_dao.py +++ b/app/dao/service_sms_sender_dao.py @@ -17,8 +17,10 @@ def insert_service_sms_sender(service, sms_sender): def dao_get_service_sms_senders_by_id(service_id, service_sms_sender_id): - stmt = select(ServiceSmsSender).filter_by( - id=service_sms_sender_id, service_id=service_id, archived=False + stmt = select(ServiceSmsSender).where( + ServiceSmsSender.id == service_sms_sender_id, + ServiceSmsSender.service_id == service_id, + ServiceSmsSender.archived == False, # noqa ) return db.session.execute(stmt).scalars().one() @@ -27,7 +29,10 @@ def dao_get_sms_senders_by_service_id(service_id): stmt = ( select(ServiceSmsSender) - .filter_by(service_id=service_id, archived=False) + .where( + ServiceSmsSender.service_id == service_id, + ServiceSmsSender.archived == False, # noqa + ) .order_by(desc(ServiceSmsSender.is_default)) ) return db.session.execute(stmt).scalars().all() @@ -87,7 +92,10 @@ def update_existing_sms_sender_with_inbound_number( def archive_sms_sender(service_id, sms_sender_id): sms_sender_to_archive = ( db.session.execute( - select(ServiceSmsSender).filter_by(id=sms_sender_id, service_id=service_id) + select(ServiceSmsSender).where( + ServiceSmsSender.id == sms_sender_id, + ServiceSmsSender.service_id == service_id, + ) ) .scalars() .one() diff --git a/app/dao/service_user_dao.py b/app/dao/service_user_dao.py index cd2aeb5eb..43277fc93 100644 --- a/app/dao/service_user_dao.py +++ b/app/dao/service_user_dao.py @@ -6,7 +6,9 @@ from app.models import ServiceUser, User def dao_get_service_user(user_id, service_id): - stmt = select(ServiceUser).filter_by(user_id=user_id, service_id=service_id) + stmt = select(ServiceUser).where( + ServiceUser.user_id == user_id, ServiceUser.service_id == service_id + ) return db.session.execute(stmt).scalars().one_or_none() @@ -22,7 +24,7 @@ def dao_get_active_service_users(service_id): def dao_get_service_users_by_user_id(user_id): return ( - db.session.execute(select(ServiceUser).filter_by(user_id=user_id)) + db.session.execute(select(ServiceUser).where(ServiceUser.user_id == user_id)) .scalars() .all() ) diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index 31eaf2ef5..f6b3818f4 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -216,7 +216,9 @@ def dao_fetch_service_by_inbound_number(number): def dao_fetch_service_by_id_with_api_keys(service_id, only_active=False): stmt = ( - select(Service).filter_by(id=service_id).options(joinedload(Service.api_keys)) + select(Service) + .where(Service.id == service_id) + .options(joinedload(Service.api_keys)) ) if only_active: stmt = stmt.filter(Service.active) @@ -240,7 +242,7 @@ def dao_fetch_all_services_created_by_user(user_id): stmt = ( select(Service) - .filter_by(created_by_id=user_id) + .where(Service.created_by_id == user_id) .order_by(asc(Service.created_at)) ) @@ -392,24 +394,26 @@ def delete_service_and_all_associated_db_objects(service): db.session.execute(stmt) db.session.commit() - subq = select(Template.id).filter_by(service=service).subquery() + subq = select(Template.id).where(Service.service == service).subquery() stmt = delete(TemplateRedacted).filter(TemplateRedacted.template_id.in_(subq)) _delete_commit(stmt) - _delete_commit(delete(ServiceSmsSender).filter_by(service=service)) - _delete_commit(delete(ServiceEmailReplyTo).filter_by(service=service)) - _delete_commit(delete(InvitedUser).filter_by(service=service)) - _delete_commit(delete(Permission).filter_by(service=service)) - _delete_commit(delete(NotificationHistory).filter_by(service=service)) - _delete_commit(delete(Notification).filter_by(service=service)) - _delete_commit(delete(Job).filter_by(service=service)) - _delete_commit(delete(Template).filter_by(service=service)) - _delete_commit(delete(TemplateHistory).filter_by(service_id=service.id)) - _delete_commit(delete(ServicePermission).filter_by(service_id=service.id)) - _delete_commit(delete(ApiKey).filter_by(service=service)) - _delete_commit(delete(ApiKey.get_history_model()).filter_by(service_id=service.id)) - _delete_commit(delete(AnnualBilling).filter_by(service_id=service.id)) + _delete_commit(delete(ServiceSmsSender).where(Service.service == service)) + _delete_commit(delete(ServiceEmailReplyTo).where(Service.service == service)) + _delete_commit(delete(InvitedUser).where(Service.service == service)) + _delete_commit(delete(Permission).where(Service.service == service)) + _delete_commit(delete(NotificationHistory).where(Service.service == service)) + _delete_commit(delete(Notification).where(Service.service == service)) + _delete_commit(delete(Job).where(Service.service == service)) + _delete_commit(delete(Template).where(Service.service == service)) + _delete_commit(delete(TemplateHistory).where(Service.service_id == service.id)) + _delete_commit(delete(ServicePermission).where(Service.service_id == service.id)) + _delete_commit(delete(ApiKey).where(Service.service == service)) + _delete_commit( + delete(ApiKey.get_history_model()).where(Service.service_id == service.id) + ) + _delete_commit(delete(AnnualBilling).where(Service.service_id == service.id)) stmt = ( select(VerifyCode).join(User).filter(User.id.in_([x.id for x in service.users])) @@ -421,7 +425,7 @@ def delete_service_and_all_associated_db_objects(service): for user in users: user.organizations = [] service.users.remove(user) - _delete_commit(delete(Service.get_history_model()).filter_by(id=service.id)) + _delete_commit(delete(Service.get_history_model()).where(Service.id == service.id)) db.session.delete(service) db.session.commit() for user in users: diff --git a/app/dao/templates_dao.py b/app/dao/templates_dao.py index 7c5d7459e..c97e1fc10 100644 --- a/app/dao/templates_dao.py +++ b/app/dao/templates_dao.py @@ -46,21 +46,28 @@ def dao_redact_template(template, user_id): def dao_get_template_by_id_and_service_id(template_id, service_id, version=None): if version is not None: - stmt = select(TemplateHistory).filter_by( - id=template_id, hidden=False, service_id=service_id, version=version + stmt = select(TemplateHistory).where( + TemplateHistory.id == template_id, + TemplateHistory.hidden == False, # noqa + TemplateHistory.service_id == service_id, + TemplateHistory.version == version, ) return db.session.execute(stmt).scalars().one() - stmt = select(Template).filter_by( - id=template_id, hidden=False, service_id=service_id + stmt = select(Template).where( + Template.id == template_id, + Template.hidden == False, # noqa + Template.service_id == service_id, ) return db.session.execute(stmt).scalars().one() def dao_get_template_by_id(template_id, version=None): if version is not None: - stmt = select(TemplateHistory).filter_by(id=template_id, version=version) + stmt = select(TemplateHistory).where( + TemplateHistory.id == template_id, TemplateHistory.version == version + ) return db.session.execute(stmt).scalars().one() - stmt = select(Template).filter_by(id=template_id) + stmt = select(Template).where(Template.id == template_id) return db.session.execute(stmt).scalars().one() @@ -68,11 +75,11 @@ def dao_get_all_templates_for_service(service_id, template_type=None): if template_type is not None: stmt = ( select(Template) - .filter_by( - service_id=service_id, - template_type=template_type, - hidden=False, - archived=False, + .where( + Template.service_id == service_id, + Template.template_type == template_type, + Template.hidden == False, # noqa + Template.archived == False, # noqa ) .order_by( asc(Template.name), @@ -82,7 +89,11 @@ def dao_get_all_templates_for_service(service_id, template_type=None): return db.session.execute(stmt).scalars().all() stmt = ( select(Template) - .filter_by(service_id=service_id, hidden=False, archived=False) + .where( + Template.service_id == service_id, + Template.hidden == False, # noqa + Template.archived == False, # noqa + ) .order_by( asc(Template.name), asc(Template.template_type), @@ -94,10 +105,10 @@ def dao_get_all_templates_for_service(service_id, template_type=None): def dao_get_template_versions(service_id, template_id): stmt = ( select(TemplateHistory) - .filter_by( - service_id=service_id, - id=template_id, - hidden=False, + .where( + TemplateHistory.service_id == service_id, + TemplateHistory.id == template_id, + TemplateHistory.hidden == False, # noqa ) .order_by(desc(TemplateHistory.version)) ) diff --git a/app/dao/users_dao.py b/app/dao/users_dao.py index 690ecc7f9..f13974474 100644 --- a/app/dao/users_dao.py +++ b/app/dao/users_dao.py @@ -37,7 +37,7 @@ def get_login_gov_user(login_uuid, email_address): login.gov uuids are. Eventually the code that checks by email address should be removed. """ - stmt = select(User).filter_by(login_uuid=login_uuid) + stmt = select(User).where(User.login_uuid == login_uuid) user = db.session.execute(stmt).scalars().first() if user: if user.email_address != email_address: @@ -65,7 +65,7 @@ def get_login_gov_user(login_uuid, email_address): def save_user_attribute(usr, update_dict=None): - db.session.query(User).filter_by(id=usr.id).update(update_dict or {}) + db.session.query(User).where(User.id == usr.id).update(update_dict or {}) db.session.commit() @@ -82,7 +82,7 @@ def save_model_user( user.email_access_validated_at = utc_now() if update_dict: _remove_values_for_keys_if_present(update_dict, ["id", "password_changed_at"]) - db.session.query(User).filter_by(id=user.id).update(update_dict or {}) + db.session.query(User).where(User.id == user.id).update(update_dict or {}) else: db.session.add(user) db.session.commit() @@ -105,7 +105,7 @@ def get_user_code(user, code, code_type): # time searching for the correct code. stmt = ( select(VerifyCode) - .filter_by(user=user, code_type=code_type) + .where(VerifyCode.user == user, VerifyCode.code_type == code_type) .order_by(VerifyCode.created_at.desc()) ) codes = db.session.execute(stmt).scalars().all() @@ -135,7 +135,7 @@ def delete_model_user(user): def delete_user_verify_codes(user): - stmt = delete(VerifyCode).filter_by(user=user) + stmt = delete(VerifyCode).where(VerifyCode.user == user) db.session.execute(stmt) db.session.commit() @@ -152,7 +152,7 @@ def count_user_verify_codes(user): def get_user_by_id(user_id=None): if user_id: - stmt = select(User).filter_by(id=user_id) + stmt = select(User).where(User.id == user_id) return db.session.execute(stmt).scalars().one() return get_users() diff --git a/app/service/rest.py b/app/service/rest.py index 60083485f..533bf1bff 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -422,14 +422,20 @@ def get_service_history(service_id): ) service_history = ( - db.session.execute(select(Service.get_history_model()).filter_by(id=service_id)) + db.session.execute( + select(Service.get_history_model()).where( + Service.get_history_model().id == service_id + ) + ) .scalars() .all() ) service_data = service_history_schema.dump(service_history, many=True) api_key_history = ( db.session.execute( - select(ApiKey.get_history_model()).filter_by(service_id=service_id) + select(ApiKey.get_history_model()).where( + ApiKey.get_history_model().service_id == service_id + ) ) .scalars() .all() @@ -437,7 +443,9 @@ def get_service_history(service_id): api_keys_data = api_key_history_schema.dump(api_key_history, many=True) template_history = ( - db.session.execute(select(TemplateHistory).filter_by(service_id=service_id)) + db.session.execute( + select(TemplateHistory).where(TemplateHistory.service_id == service_id) + ) .scalars() .all() ) diff --git a/tests/__init__.py b/tests/__init__.py index 47c911386..6ea1ba94b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -15,7 +15,9 @@ def create_service_authorization_header(service_id, key_type=KeyType.NORMAL): client_id = str(service_id) secrets = ( db.session.execute( - select(ApiKey).filter_by(service_id=service_id, key_type=key_type) + select(ApiKey).where( + ApiKey.service_id == service_id, ApiKey.key_type == key_type + ) ) .scalars() .all() diff --git a/tests/app/dao/test_fact_notification_status_dao.py b/tests/app/dao/test_fact_notification_status_dao.py index fd97496e3..5b9a7d695 100644 --- a/tests/app/dao/test_fact_notification_status_dao.py +++ b/tests/app/dao/test_fact_notification_status_dao.py @@ -1130,7 +1130,10 @@ def test_update_fact_notification_status_respects_gmt_bst( stmt = ( select(func.count()) .select_from(FactNotificationStatus) - .filter_by(service_id=sample_service.id, local_date=process_day) + .where( + FactNotificationStatus.service_id == sample_service.id, + FactNotificationStatus.local_date == process_day, + ) ) result = db.session.execute(stmt) assert result.rowcount == expected_count diff --git a/tests/app/dao/test_organization_dao.py b/tests/app/dao/test_organization_dao.py index fb2e01d85..773c14bd6 100644 --- a/tests/app/dao/test_organization_dao.py +++ b/tests/app/dao/test_organization_dao.py @@ -180,8 +180,9 @@ def test_update_organization_updates_the_service_org_type_if_org_type_is_provide assert sample_organization.organization_type == OrganizationType.FEDERAL assert sample_service.organization_type == OrganizationType.FEDERAL - stmt = select(Service.get_history_model()).filter_by( - id=sample_service.id, version=2 + stmt = select(Service.get_history_model()).where( + Service.get_history_model().id == sample_service.id, + Service.get_history_model().version == 2, ) assert ( db.session.execute(stmt).scalars().one().organization_type @@ -234,8 +235,9 @@ def test_add_service_to_organization(sample_service, sample_organization): assert sample_organization.services[0].id == sample_service.id assert sample_service.organization_type == sample_organization.organization_type - stmt = select(Service.get_history_model()).filter_by( - id=sample_service.id, version=2 + stmt = select(Service.get_history_model()).where( + Service.get_history_model().id == sample_service.id, + Service.get_history_model().version == 2, ) assert ( db.session.execute(stmt).scalars().one().organization_type diff --git a/tests/app/dao/test_service_callback_api_dao.py b/tests/app/dao/test_service_callback_api_dao.py index 1bff31f67..30b1567bd 100644 --- a/tests/app/dao/test_service_callback_api_dao.py +++ b/tests/app/dao/test_service_callback_api_dao.py @@ -39,7 +39,9 @@ def test_save_service_callback_api(sample_service): versioned = ( db.session.execute( - select(ServiceCallbackApi.get_history_model()).filter_by(id=callback_api.id) + select(ServiceCallbackApi.get_history_model()).where( + ServiceCallbackApi.get_history_model().id == callback_api.id + ) ) .scalars() .one() @@ -147,8 +149,8 @@ def test_update_service_callback_api(sample_service): versioned_results = ( db.session.execute( - select(ServiceCallbackApi.get_history_model()).filter_by( - id=saved_callback_api.id + select(ServiceCallbackApi.get_history_model()).where( + ServiceCallbackApi.get_history_model().id == saved_callback_api.id ) ) .scalars() diff --git a/tests/app/dao/test_service_inbound_api_dao.py b/tests/app/dao/test_service_inbound_api_dao.py index 232d256dd..c0a4a4245 100644 --- a/tests/app/dao/test_service_inbound_api_dao.py +++ b/tests/app/dao/test_service_inbound_api_dao.py @@ -38,7 +38,9 @@ def test_save_service_inbound_api(sample_service): versioned = ( db.session.execute( - select(ServiceInboundApi.get_history_model()).filter_by(id=inbound_api.id) + select(ServiceInboundApi.get_history_model()).where( + ServiceInboundApi.get_history_model().id == inbound_api.id + ) ) .scalars() .one() @@ -95,8 +97,8 @@ def test_update_service_inbound_api(sample_service): versioned_results = ( db.session.execute( - select(ServiceInboundApi.get_history_model()).filter_by( - id=saved_inbound_api.id + select(ServiceInboundApi.get_history_model()).where( + ServiceInboundApi.get_history_model().id == saved_inbound_api.id ) ) .scalars() diff --git a/tests/app/dao/test_service_sms_sender_dao.py b/tests/app/dao/test_service_sms_sender_dao.py index 10bfd21f4..21853e61f 100644 --- a/tests/app/dao/test_service_sms_sender_dao.py +++ b/tests/app/dao/test_service_sms_sender_dao.py @@ -126,7 +126,7 @@ def test_dao_add_sms_sender_for_service_switches_default(notify_db_session): def test_dao_update_service_sms_sender(notify_db_session): service = create_service() - stmt = select(ServiceSmsSender).filter_by(service_id=service.id) + stmt = select(ServiceSmsSender).where(ServiceSmsSender.service_id == service.id) service_sms_senders = db.session.execute(stmt).scalars().all() assert len(service_sms_senders) == 1 sms_sender_to_update = service_sms_senders[0] @@ -137,7 +137,7 @@ def test_dao_update_service_sms_sender(notify_db_session): is_default=True, sms_sender="updated", ) - stmt = select(ServiceSmsSender).filter_by(service_id=service.id) + stmt = select(ServiceSmsSender).where(ServiceSmsSender.service_id == service.id) sms_senders = db.session.execute(stmt).scalars().all() assert len(sms_senders) == 1 assert sms_senders[0].is_default @@ -159,7 +159,7 @@ def test_dao_update_service_sms_sender_switches_default(notify_db_session): is_default=True, sms_sender="updated", ) - stmt = select(ServiceSmsSender).filter_by(service_id=service.id) + stmt = select(ServiceSmsSender).where(ServiceSmsSender.service_id == service.id) sms_senders = db.session.execute(stmt).scalars().all() expected = {("testing", False), ("updated", True)} @@ -191,7 +191,7 @@ def test_update_existing_sms_sender_with_inbound_number(notify_db_session): service = create_service() inbound_number = create_inbound_number(number="12345", service_id=service.id) - stmt = select(ServiceSmsSender).filter_by(service_id=service.id) + stmt = select(ServiceSmsSender).where(ServiceSmsSender.service_id == service.id) existing_sms_sender = db.session.execute(stmt).scalars().one() sms_sender = update_existing_sms_sender_with_inbound_number( service_sms_sender=existing_sms_sender, @@ -208,7 +208,7 @@ def test_update_existing_sms_sender_with_inbound_number_raises_exception_if_inbo notify_db_session, ): service = create_service() - stmt = select(ServiceSmsSender).filter_by(service_id=service.id) + stmt = select(ServiceSmsSender).where(ServiceSmsSender.service_id == service.id) existing_sms_sender = db.session.execute(stmt).scalars().one() with pytest.raises(expected_exception=SQLAlchemyError): update_existing_sms_sender_with_inbound_number( diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index 61fe99419..cb82c929c 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -746,9 +746,13 @@ def test_update_service_creates_a_history_record_with_current_data(notify_db_ses service_from_db = _get_first_service() assert service_from_db.version == 2 - stmt = select(Service.get_history_model()).filter_by(name="service_name") + stmt = select(Service.get_history_model()).where( + Service.get_history_model().name == "service_name" + ) assert db.session.execute(stmt).scalars().one().version == 1 - stmt = select(Service.get_history_model()).filter_by(name="updated_service_name") + stmt = select(Service.get_history_model()).where( + Service.get_history_model().name == "updated_service_name" + ) assert db.session.execute(stmt).scalars().one().version == 2 @@ -819,7 +823,7 @@ def test_update_service_permission_creates_a_history_record_with_current_data( stmt = ( select(Service.get_history_model()) - .filter_by(name="service_name") + .where(Service.get_history_model().name == "service_name") .order_by("version") ) history = db.session.execute(stmt).scalars().all() @@ -920,7 +924,9 @@ def test_add_existing_user_to_another_service_doesnot_change_old_permissions( dao_create_service(service_one, user) assert user.id == service_one.users[0].id - stmt = select(Permission).filter_by(service=service_one, user=user) + stmt = select(Permission).where( + Permission.service == service_one, Permission.user == user + ) test_user_permissions = db.session.execute(stmt).all() assert len(test_user_permissions) == 7 @@ -941,10 +947,14 @@ def test_add_existing_user_to_another_service_doesnot_change_old_permissions( dao_create_service(service_two, other_user) assert other_user.id == service_two.users[0].id - stmt = select(Permission).filter_by(service=service_two, user=other_user) + stmt = select(Permission).where( + Permission.service == service_two, Permission.user == other_user + ) other_user_permissions = db.session.execute(stmt).all() assert len(other_user_permissions) == 7 - stmt = select(Permission).filter_by(service=service_one, user=other_user) + stmt = select(Permission).where( + Permission.service == service_one, Permission.user == other_user + ) other_user_service_one_permissions = db.session.execute(stmt).all() assert len(other_user_service_one_permissions) == 0 @@ -955,11 +965,15 @@ def test_add_existing_user_to_another_service_doesnot_change_old_permissions( permissions.append(Permission(permission=p)) dao_add_user_to_service(service_one, other_user, permissions=permissions) - stmt = select(Permission).filter_by(service=service_one, user=other_user) + stmt = select(Permission).where( + Permission.service == service_one, Permission.user == other_user + ) other_user_service_one_permissions = db.session.execute(stmt).all() assert len(other_user_service_one_permissions) == 2 - stmt = select(Permission).filter_by(service=service_two, user=other_user) + stmt = select(Permission).where( + Permission.service == service_two, Permission.user == other_user + ) other_user_service_two_permissions = db.session.execute(stmt).all() assert len(other_user_service_two_permissions) == 7 diff --git a/tests/app/dao/test_templates_dao.py b/tests/app/dao/test_templates_dao.py index 734a29c0a..e37248de7 100644 --- a/tests/app/dao/test_templates_dao.py +++ b/tests/app/dao/test_templates_dao.py @@ -334,9 +334,9 @@ def test_update_template_creates_a_history_record_with_current_data( assert template_from_db.version == 2 - stmt = select(TemplateHistory).filter_by(name="Sample Template") + stmt = select(TemplateHistory).where(TemplateHistory.name == "Sample Template") assert db.session.execute(stmt).scalars().one().version == 1 - stmt = select(TemplateHistory).filter_by(name="new name") + stmt = select(TemplateHistory).where(TemplateHistory.name == "new name") assert db.session.execute(stmt).scalars().one().version == 2 diff --git a/tests/app/delivery/test_send_to_providers.py b/tests/app/delivery/test_send_to_providers.py index 91970e968..c7f404324 100644 --- a/tests/app/delivery/test_send_to_providers.py +++ b/tests/app/delivery/test_send_to_providers.py @@ -111,7 +111,9 @@ def test_should_send_personalised_template_to_correct_sms_provider_and_persist( ) notification = ( - db.session.execute(select(Notification).filter_by(id=db_notification.id)) + db.session.execute( + select(Notification).where(Notification.id == db_notification.id) + ) .scalars() .one() ) @@ -159,7 +161,9 @@ def test_should_send_personalised_template_to_correct_email_provider_and_persist ) notification = ( - db.session.execute(select(Notification).filter_by(id=db_notification.id)) + db.session.execute( + select(Notification).where(Notification.id == db_notification.id) + ) .scalars() .one() ) diff --git a/tests/app/service/test_api_key_endpoints.py b/tests/app/service/test_api_key_endpoints.py index f5a8af007..091910224 100644 --- a/tests/app/service/test_api_key_endpoints.py +++ b/tests/app/service/test_api_key_endpoints.py @@ -29,7 +29,7 @@ def test_api_key_should_create_new_api_key_for_service(notify_api, sample_servic assert "data" in json.loads(response.get_data(as_text=True)) saved_api_key = ( db.session.execute( - select(ApiKey).filter_by(service_id=sample_service.id) + select(ApiKey).where(ApiKey.service_id == sample_service.id) ) .scalars() .first() diff --git a/tests/app/service/test_archived_service.py b/tests/app/service/test_archived_service.py index 5f97c2989..2e32a1982 100644 --- a/tests/app/service/test_archived_service.py +++ b/tests/app/service/test_archived_service.py @@ -88,7 +88,7 @@ def test_deactivating_service_creates_history(archived_service): history = ( db.session.execute( select(ServiceHistory) - .filter_by(id=archived_service.id) + .where(ServiceHistory.id == archived_service.id) .order_by(ServiceHistory.version.desc()) ) .scalars() diff --git a/tests/app/service/test_suspend_resume_service.py b/tests/app/service/test_suspend_resume_service.py index ad036b414..a59345f9b 100644 --- a/tests/app/service/test_suspend_resume_service.py +++ b/tests/app/service/test_suspend_resume_service.py @@ -81,7 +81,7 @@ def test_service_history_is_created(client, sample_service, action, original_sta history = ( db.session.execute( select(ServiceHistory) - .filter_by(id=sample_service.id) + .where(ServiceHistory.id == sample_service.id) .order_by(ServiceHistory.version.desc()) ) .scalars() diff --git a/tests/app/user/test_rest.py b/tests/app/user/test_rest.py index bd62bc640..0bd74b2b3 100644 --- a/tests/app/user/test_rest.py +++ b/tests/app/user/test_rest.py @@ -119,7 +119,7 @@ def test_post_user(admin_request, notify_db_session): user = ( db.session.execute( - select(User).filter_by(email_address="user@digital.fake.gov") + select(User).where(User.email_address == "user@digital.fake.gov") ) .scalars() .first() @@ -146,7 +146,7 @@ def test_post_user_without_auth_type(admin_request, notify_db_session): user = ( db.session.execute( - select(User).filter_by(email_address="user@digital.fake.gov") + select(User).where(User.email_address == "user@digital.fake.gov") ) .scalars() .first() @@ -494,7 +494,9 @@ def test_set_user_permissions(admin_request, sample_user, sample_service): permission = ( db.session.execute( - select(Permission).filter_by(permission=PermissionType.MANAGE_SETTINGS) + select(Permission).where( + Permission.permission == PermissionType.MANAGE_SETTINGS + ) ) .scalars() .first() @@ -521,7 +523,9 @@ def test_set_user_permissions_multiple(admin_request, sample_user, sample_servic permission = ( db.session.execute( - select(Permission).filter_by(permission=PermissionType.MANAGE_SETTINGS) + select(Permission).where( + Permission.permission == PermissionType.MANAGE_SETTINGS + ) ) .scalars() .first() @@ -531,7 +535,9 @@ def test_set_user_permissions_multiple(admin_request, sample_user, sample_servic assert permission.permission == PermissionType.MANAGE_SETTINGS permission = ( db.session.execute( - select(Permission).filter_by(permission=PermissionType.MANAGE_TEMPLATES) + select(Permission).where( + Permission.permission == PermissionType.MANAGE_TEMPLATES + ) ) .scalars() .first()