From de5b33c40ddc50cd901eaf6e2d8d2f8f07290562 Mon Sep 17 00:00:00 2001 From: Eike Kettner Date: Mon, 24 Aug 2020 16:09:11 +0200 Subject: [PATCH] Add `updated` column to some tables --- .../restserver/conv/Conversions.scala | 16 ++++-- .../restserver/routes/EquipmentRoutes.scala | 8 +-- .../mariadb/V1.9.0__updated_column.sql | 29 ++++++++++ .../postgresql/V1.9.0__updated_column.sql | 29 ++++++++++ .../docspell/store/records/REquipment.scala | 35 ++++++++---- .../store/records/ROrganization.scala | 39 ++++++++------ .../docspell/store/records/RPerson.scala | 53 +++++++++++++------ 7 files changed, 156 insertions(+), 53 deletions(-) create mode 100644 modules/store/src/main/resources/db/migration/mariadb/V1.9.0__updated_column.sql create mode 100644 modules/store/src/main/resources/db/migration/postgresql/V1.9.0__updated_column.sql diff --git a/modules/restserver/src/main/scala/docspell/restserver/conv/Conversions.scala b/modules/restserver/src/main/scala/docspell/restserver/conv/Conversions.scala index f2f131f0..539ec3eb 100644 --- a/modules/restserver/src/main/scala/docspell/restserver/conv/Conversions.scala +++ b/modules/restserver/src/main/scala/docspell/restserver/conv/Conversions.scala @@ -341,6 +341,7 @@ trait Conversions { v.address.city, v.address.country, v.notes, + now, now ) } yield OOrganization.OrgAndContacts(org, cont) @@ -353,6 +354,7 @@ trait Conversions { def contacts(oid: Ident) = v.contacts.traverse(c => newContact(c, oid.some, None)) for { + now <- Timestamp.current[F] cont <- contacts(v.id) org = ROrganization( v.id, @@ -363,7 +365,8 @@ trait Conversions { v.address.city, v.address.country, v.notes, - v.created + v.created, + now ) } yield OOrganization.OrgAndContacts(org, cont) } @@ -398,6 +401,7 @@ trait Conversions { v.address.country, v.notes, v.concerning, + now, now ) } yield OOrganization.PersonAndContacts(org, cont) @@ -410,6 +414,7 @@ trait Conversions { def contacts(pid: Ident) = v.contacts.traverse(c => newContact(c, None, pid.some)) for { + now <- Timestamp.current[F] cont <- contacts(v.id) org = RPerson( v.id, @@ -421,7 +426,8 @@ trait Conversions { v.address.country, v.notes, v.concerning, - v.created + v.created, + now ) } yield OOrganization.PersonAndContacts(org, cont) } @@ -536,11 +542,11 @@ trait Conversions { def newEquipment[F[_]: Sync](e: Equipment, cid: Ident): F[REquipment] = timeId.map({ case (id, now) => - REquipment(id, cid, e.name, now) + REquipment(id, cid, e.name, now, now) }) - def changeEquipment(e: Equipment, cid: Ident): REquipment = - REquipment(e.id, cid, e.name, e.created) + def changeEquipment[F[_]: Sync](e: Equipment, cid: Ident): F[REquipment] = + Timestamp.current[F].map(now => REquipment(e.id, cid, e.name, e.created, now)) // idref diff --git a/modules/restserver/src/main/scala/docspell/restserver/routes/EquipmentRoutes.scala b/modules/restserver/src/main/scala/docspell/restserver/routes/EquipmentRoutes.scala index edfc7521..a8db67ba 100644 --- a/modules/restserver/src/main/scala/docspell/restserver/routes/EquipmentRoutes.scala +++ b/modules/restserver/src/main/scala/docspell/restserver/routes/EquipmentRoutes.scala @@ -39,10 +39,10 @@ object EquipmentRoutes { case req @ PUT -> Root => for { - data <- req.as[Equipment] - equip = changeEquipment(data, user.account.collective) - res <- backend.equipment.update(equip) - resp <- Ok(basicResult(res, "Equipment updated.")) + data <- req.as[Equipment] + equip <- changeEquipment(data, user.account.collective) + res <- backend.equipment.update(equip) + resp <- Ok(basicResult(res, "Equipment updated.")) } yield resp case DELETE -> Root / Ident(id) => diff --git a/modules/store/src/main/resources/db/migration/mariadb/V1.9.0__updated_column.sql b/modules/store/src/main/resources/db/migration/mariadb/V1.9.0__updated_column.sql new file mode 100644 index 00000000..72b6b152 --- /dev/null +++ b/modules/store/src/main/resources/db/migration/mariadb/V1.9.0__updated_column.sql @@ -0,0 +1,29 @@ +-- organization +ALTER TABLE `organization` +ADD COLUMN (`updated` timestamp); + +UPDATE `organization` SET `updated` = `created`; + +ALTER TABLE `organization` +MODIFY `updated` timestamp NOT NULL; + +-- person +ALTER TABLE `person` +MODIFY `created` timestamp; + +ALTER TABLE `person` +ADD COLUMN (`updated` timestamp); + +UPDATE `person` SET `updated` = `created`; + +ALTER TABLE `person` +MODIFY `updated` timestamp NOT NULL; + +-- equipment +ALTER TABLE `equipment` +ADD COLUMN (`updated` timestamp); + +UPDATE `equipment` SET `updated` = `created`; + +ALTER TABLE `equipment` +MODIFY `updated` timestamp NOT NULL; diff --git a/modules/store/src/main/resources/db/migration/postgresql/V1.9.0__updated_column.sql b/modules/store/src/main/resources/db/migration/postgresql/V1.9.0__updated_column.sql new file mode 100644 index 00000000..34c57718 --- /dev/null +++ b/modules/store/src/main/resources/db/migration/postgresql/V1.9.0__updated_column.sql @@ -0,0 +1,29 @@ +-- organization +ALTER TABLE "organization" +ADD COLUMN "updated" timestamp; + +UPDATE "organization" SET "updated" = "created"; + +ALTER TABLE "organization" +ALTER COLUMN "updated" SET NOT NULL; + +-- person +ALTER TABLE "person" ALTER COLUMN "created" + TYPE timestamp USING(to_timestamp("created", 'YYYY-MM-DD HH24:MI:SS')::timestamp); + +ALTER TABLE "person" +ADD COLUMN "updated" timestamp; + +UPDATE "person" SET "updated" = "created"; + +ALTER TABLE "person" +ALTER COLUMN "updated" SET NOT NULL; + +-- equipment +ALTER TABLE "equipment" +ADD COLUMN "updated" timestamp; + +UPDATE "equipment" SET "updated" = "created"; + +ALTER TABLE "equipment" +ALTER COLUMN "updated" SET NOT NULL; diff --git a/modules/store/src/main/scala/docspell/store/records/REquipment.scala b/modules/store/src/main/scala/docspell/store/records/REquipment.scala index 78d2e7f8..3a7f6d2f 100644 --- a/modules/store/src/main/scala/docspell/store/records/REquipment.scala +++ b/modules/store/src/main/scala/docspell/store/records/REquipment.scala @@ -7,7 +7,13 @@ import docspell.store.impl._ import doobie._ import doobie.implicits._ -case class REquipment(eid: Ident, cid: Ident, name: String, created: Timestamp) {} +case class REquipment( + eid: Ident, + cid: Ident, + name: String, + created: Timestamp, + updated: Timestamp +) {} object REquipment { @@ -18,25 +24,32 @@ object REquipment { val cid = Column("cid") val name = Column("name") val created = Column("created") - val all = List(eid, cid, name, created) + val updated = Column("updated") + val all = List(eid, cid, name, created, updated) } import Columns._ def insert(v: REquipment): ConnectionIO[Int] = { - val sql = insertRow(table, all, fr"${v.eid},${v.cid},${v.name},${v.created}") + val sql = + insertRow(table, all, fr"${v.eid},${v.cid},${v.name},${v.created},${v.updated}") sql.update.run } def update(v: REquipment): ConnectionIO[Int] = { - val sql = updateRow( - table, - and(eid.is(v.eid), cid.is(v.cid)), - commas( - cid.setTo(v.cid), - name.setTo(v.name) + def sql(now: Timestamp) = + updateRow( + table, + and(eid.is(v.eid), cid.is(v.cid)), + commas( + cid.setTo(v.cid), + name.setTo(v.name), + updated.setTo(now) + ) ) - ) - sql.update.run + for { + now <- Timestamp.current[ConnectionIO] + n <- sql(now).update.run + } yield n } def existsByName(coll: Ident, ename: String): ConnectionIO[Boolean] = { diff --git a/modules/store/src/main/scala/docspell/store/records/ROrganization.scala b/modules/store/src/main/scala/docspell/store/records/ROrganization.scala index 17fe4845..8eb07e29 100644 --- a/modules/store/src/main/scala/docspell/store/records/ROrganization.scala +++ b/modules/store/src/main/scala/docspell/store/records/ROrganization.scala @@ -19,7 +19,8 @@ case class ROrganization( city: String, country: String, notes: Option[String], - created: Timestamp + created: Timestamp, + updated: Timestamp ) {} object ROrganization { @@ -38,7 +39,8 @@ object ROrganization { val country = Column("country") val notes = Column("notes") val created = Column("created") - val all = List(oid, cid, name, street, zip, city, country, notes, created) + val updated = Column("updated") + val all = List(oid, cid, name, street, zip, city, country, notes, created, updated) } import Columns._ @@ -47,26 +49,31 @@ object ROrganization { val sql = insertRow( table, all, - fr"${v.oid},${v.cid},${v.name},${v.street},${v.zip},${v.city},${v.country},${v.notes},${v.created}" + fr"${v.oid},${v.cid},${v.name},${v.street},${v.zip},${v.city},${v.country},${v.notes},${v.created},${v.updated}" ) sql.update.run } def update(v: ROrganization): ConnectionIO[Int] = { - val sql = updateRow( - table, - and(oid.is(v.oid), cid.is(v.cid)), - commas( - cid.setTo(v.cid), - name.setTo(v.name), - street.setTo(v.street), - zip.setTo(v.zip), - city.setTo(v.city), - country.setTo(v.country), - notes.setTo(v.notes) + def sql(now: Timestamp) = + updateRow( + table, + and(oid.is(v.oid), cid.is(v.cid)), + commas( + cid.setTo(v.cid), + name.setTo(v.name), + street.setTo(v.street), + zip.setTo(v.zip), + city.setTo(v.city), + country.setTo(v.country), + notes.setTo(v.notes), + updated.setTo(now) + ) ) - ) - sql.update.run + for { + now <- Timestamp.current[ConnectionIO] + n <- sql(now).update.run + } yield n } def existsByName(coll: Ident, oname: String): ConnectionIO[Boolean] = diff --git a/modules/store/src/main/scala/docspell/store/records/RPerson.scala b/modules/store/src/main/scala/docspell/store/records/RPerson.scala index eb9a9872..0c2bdcd9 100644 --- a/modules/store/src/main/scala/docspell/store/records/RPerson.scala +++ b/modules/store/src/main/scala/docspell/store/records/RPerson.scala @@ -20,7 +20,8 @@ case class RPerson( country: String, notes: Option[String], concerning: Boolean, - created: Timestamp + created: Timestamp, + updated: Timestamp ) {} object RPerson { @@ -40,7 +41,20 @@ object RPerson { val notes = Column("notes") val concerning = Column("concerning") val created = Column("created") - val all = List(pid, cid, name, street, zip, city, country, notes, concerning, created) + val updated = Column("updated") + val all = List( + pid, + cid, + name, + street, + zip, + city, + country, + notes, + concerning, + created, + updated + ) } import Columns._ @@ -49,27 +63,32 @@ object RPerson { val sql = insertRow( table, all, - fr"${v.pid},${v.cid},${v.name},${v.street},${v.zip},${v.city},${v.country},${v.notes},${v.concerning},${v.created}" + fr"${v.pid},${v.cid},${v.name},${v.street},${v.zip},${v.city},${v.country},${v.notes},${v.concerning},${v.created},${v.updated}" ) sql.update.run } def update(v: RPerson): ConnectionIO[Int] = { - val sql = updateRow( - table, - and(pid.is(v.pid), cid.is(v.cid)), - commas( - cid.setTo(v.cid), - name.setTo(v.name), - street.setTo(v.street), - zip.setTo(v.zip), - city.setTo(v.city), - country.setTo(v.country), - concerning.setTo(v.concerning), - notes.setTo(v.notes) + def sql(now: Timestamp) = + updateRow( + table, + and(pid.is(v.pid), cid.is(v.cid)), + commas( + cid.setTo(v.cid), + name.setTo(v.name), + street.setTo(v.street), + zip.setTo(v.zip), + city.setTo(v.city), + country.setTo(v.country), + concerning.setTo(v.concerning), + notes.setTo(v.notes), + updated.setTo(now) + ) ) - ) - sql.update.run + for { + now <- Timestamp.current[ConnectionIO] + n <- sql(now).update.run + } yield n } def existsByName(coll: Ident, pname: String): ConnectionIO[Boolean] =