From 2a2e18c0af38a199f258c3454df0ebc208a2bfc8 Mon Sep 17 00:00:00 2001 From: Collin Beczak <88843144+CollinBeczak@users.noreply.github.com> Date: Tue, 13 Aug 2024 18:26:06 -0500 Subject: [PATCH] add users.avatar_url constraint and default (#1141) --- .../repository/LeaderboardRepository.scala | 4 ++-- .../framework/repository/UserRepository.scala | 8 ++++---- conf/evolutions/default/95.sql | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 conf/evolutions/default/95.sql diff --git a/app/org/maproulette/framework/repository/LeaderboardRepository.scala b/app/org/maproulette/framework/repository/LeaderboardRepository.scala index 325f92a7..37377afa 100644 --- a/app/org/maproulette/framework/repository/LeaderboardRepository.scala +++ b/app/org/maproulette/framework/repository/LeaderboardRepository.scala @@ -44,7 +44,7 @@ class LeaderboardRepository @Inject() (override val db: Database) extends Reposi ): RowParser[LeaderboardUser] = { get[Long]("user_id") ~ get[String]("user_name") ~ - get[String]("user_avatar_url").? ~ + get[String]("user_avatar_url") ~ get[Int]("user_score") ~ get[Int]("user_ranking") ~ get[DateTime]("created").? ~ @@ -61,7 +61,7 @@ class LeaderboardRepository @Inject() (override val db: Database) extends Reposi new LeaderboardUser( userId, name, - avatarURL.getOrElse(""), + avatarURL, score, rank, completedTasks, diff --git a/app/org/maproulette/framework/repository/UserRepository.scala b/app/org/maproulette/framework/repository/UserRepository.scala index eba79107..ade689ba 100644 --- a/app/org/maproulette/framework/repository/UserRepository.scala +++ b/app/org/maproulette/framework/repository/UserRepository.scala @@ -466,10 +466,10 @@ object UserRepository { get[Long]("users.id") ~ get[Long]("users.osm_id") ~ get[String]("users.name") ~ - get[Option[String]]("users.avatar_url") ~ + get[String]("users.avatar_url") ~ get[List[Int]]("roles") map { case projectId ~ userId ~ osmId ~ displayName ~ avatarURL ~ roles => - ProjectManager(projectId, userId, osmId, displayName, avatarURL.getOrElse(""), roles) + ProjectManager(projectId, userId, osmId, displayName, avatarURL, roles) } } @@ -493,7 +493,7 @@ object UserRepository { get[DateTime]("users.osm_created") ~ get[String]("users.name") ~ get[Option[String]]("users.description") ~ - get[Option[String]]("users.avatar_url") ~ + get[String]("users.avatar_url") ~ get[Option[String]]("home") ~ get[Option[String]]("users.api_key") ~ get[String]("users.oauth_token") ~ @@ -544,7 +544,7 @@ object UserRepository { osmId, displayName, description.getOrElse(""), - avatarURL.getOrElse(""), + avatarURL, Location(locationWKT.getX, locationWKT.getY), osmCreated, oauthToken diff --git a/conf/evolutions/default/95.sql b/conf/evolutions/default/95.sql new file mode 100644 index 00000000..53e9086c --- /dev/null +++ b/conf/evolutions/default/95.sql @@ -0,0 +1,16 @@ +-- --- !Ups +-- Update any existing null avatar_url values to the default +UPDATE users +SET avatar_url = '/assets/images/user_no_image.png' +WHERE avatar_url IS NULL; + +-- Alter the table to set avatar_url as NOT NULL with a default value +ALTER TABLE IF EXISTS users +ALTER COLUMN avatar_url SET DEFAULT '/assets/images/user_no_image.png', +ALTER COLUMN avatar_url SET NOT NULL; + +-- --- !Downs +-- Revert the avatar_url column to allow null values and remove the default value +ALTER TABLE IF EXISTS users +ALTER COLUMN avatar_url DROP NOT NULL, +ALTER COLUMN avatar_url DROP DEFAULT;