Fix attachment positions

It worked for new items, because the implicit offset was 0. when
adding archives to existing items, there are already attachments and
the new attachments are added to the end. This won't work if files are
added concurrently, because there is no quick and reliable way to
determine the offset then.
This commit is contained in:
Eike Kettner
2020-05-24 14:58:18 +02:00
parent 1dde43e092
commit 4694433e38
4 changed files with 95 additions and 33 deletions

View File

@ -101,4 +101,6 @@ case class Column(name: String, ns: String = "", alias: String = "") {
def asc: Fragment =
f ++ fr"asc"
def max: Fragment =
fr"MAX(" ++ f ++ fr")"
}

View File

@ -38,8 +38,10 @@ object RAttachment {
fr"${v.id},${v.itemId},${v.fileId.id},${v.position},${v.created},${v.name}"
).update.run
def countOnItem(id: Ident): ConnectionIO[Int] =
selectCount(itemId, table, itemId.is(id)).query[Int].unique
def nextPosition(id: Ident): ConnectionIO[Int] =
for {
max <- selectSimple(position.max, table, itemId.is(id)).query[Option[Int]].unique
} yield max.map(_ + 1).getOrElse(0)
def updateFileIdAndName(
attachId: Ident,