Skip to content

Commit 22aa731

Browse files
committed
Use Ecto.Query select to only select the variant field
1 parent be4a2df commit 22aa731

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

lib/upload/blob.ex

+16-9
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ defmodule Upload.Blob do
44
55
The checksum field is a MD5 hash of the blob.
66
"""
7-
87
use Ecto.Schema
8+
99
import Ecto.Changeset
10+
import Ecto.Query
1011

1112
alias Upload.Stat
1213

@@ -130,14 +131,20 @@ defmodule Upload.Blob do
130131
changeset
131132

132133
original_blob_id ->
133-
if repo.get(__MODULE__, original_blob_id).variant do
134-
add_error(
135-
changeset,
136-
:original_blob_id,
137-
"Can not set original_blob_id to a variant blob."
138-
)
139-
else
140-
changeset
134+
__MODULE__
135+
|> where([blob], blob.id == ^original_blob_id)
136+
|> select([blob], blob.variant)
137+
|> repo.one()
138+
|> case do
139+
nil ->
140+
changeset
141+
142+
_variant ->
143+
add_error(
144+
changeset,
145+
:original_blob_id,
146+
"Can not set original_blob_id to a variant blob."
147+
)
141148
end
142149
end
143150
end

0 commit comments

Comments
 (0)