Add updated column to some tables

This commit is contained in:
Eike Kettner 2020-08-24 16:09:11 +02:00
parent 96d2f948f2
commit de5b33c40d
7 changed files with 156 additions and 53 deletions

View File

@ -341,6 +341,7 @@ trait Conversions {
v.address.city, v.address.city,
v.address.country, v.address.country,
v.notes, v.notes,
now,
now now
) )
} yield OOrganization.OrgAndContacts(org, cont) } yield OOrganization.OrgAndContacts(org, cont)
@ -353,6 +354,7 @@ trait Conversions {
def contacts(oid: Ident) = def contacts(oid: Ident) =
v.contacts.traverse(c => newContact(c, oid.some, None)) v.contacts.traverse(c => newContact(c, oid.some, None))
for { for {
now <- Timestamp.current[F]
cont <- contacts(v.id) cont <- contacts(v.id)
org = ROrganization( org = ROrganization(
v.id, v.id,
@ -363,7 +365,8 @@ trait Conversions {
v.address.city, v.address.city,
v.address.country, v.address.country,
v.notes, v.notes,
v.created v.created,
now
) )
} yield OOrganization.OrgAndContacts(org, cont) } yield OOrganization.OrgAndContacts(org, cont)
} }
@ -398,6 +401,7 @@ trait Conversions {
v.address.country, v.address.country,
v.notes, v.notes,
v.concerning, v.concerning,
now,
now now
) )
} yield OOrganization.PersonAndContacts(org, cont) } yield OOrganization.PersonAndContacts(org, cont)
@ -410,6 +414,7 @@ trait Conversions {
def contacts(pid: Ident) = def contacts(pid: Ident) =
v.contacts.traverse(c => newContact(c, None, pid.some)) v.contacts.traverse(c => newContact(c, None, pid.some))
for { for {
now <- Timestamp.current[F]
cont <- contacts(v.id) cont <- contacts(v.id)
org = RPerson( org = RPerson(
v.id, v.id,
@ -421,7 +426,8 @@ trait Conversions {
v.address.country, v.address.country,
v.notes, v.notes,
v.concerning, v.concerning,
v.created v.created,
now
) )
} yield OOrganization.PersonAndContacts(org, cont) } yield OOrganization.PersonAndContacts(org, cont)
} }
@ -536,11 +542,11 @@ trait Conversions {
def newEquipment[F[_]: Sync](e: Equipment, cid: Ident): F[REquipment] = def newEquipment[F[_]: Sync](e: Equipment, cid: Ident): F[REquipment] =
timeId.map({ timeId.map({
case (id, now) => case (id, now) =>
REquipment(id, cid, e.name, now) REquipment(id, cid, e.name, now, now)
}) })
def changeEquipment(e: Equipment, cid: Ident): REquipment = def changeEquipment[F[_]: Sync](e: Equipment, cid: Ident): F[REquipment] =
REquipment(e.id, cid, e.name, e.created) Timestamp.current[F].map(now => REquipment(e.id, cid, e.name, e.created, now))
// idref // idref

View File

@ -39,10 +39,10 @@ object EquipmentRoutes {
case req @ PUT -> Root => case req @ PUT -> Root =>
for { for {
data <- req.as[Equipment] data <- req.as[Equipment]
equip = changeEquipment(data, user.account.collective) equip <- changeEquipment(data, user.account.collective)
res <- backend.equipment.update(equip) res <- backend.equipment.update(equip)
resp <- Ok(basicResult(res, "Equipment updated.")) resp <- Ok(basicResult(res, "Equipment updated."))
} yield resp } yield resp
case DELETE -> Root / Ident(id) => case DELETE -> Root / Ident(id) =>

View File

@ -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;

View File

@ -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;

View File

@ -7,7 +7,13 @@ import docspell.store.impl._
import doobie._ import doobie._
import doobie.implicits._ 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 { object REquipment {
@ -18,25 +24,32 @@ object REquipment {
val cid = Column("cid") val cid = Column("cid")
val name = Column("name") val name = Column("name")
val created = Column("created") 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._ import Columns._
def insert(v: REquipment): ConnectionIO[Int] = { 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 sql.update.run
} }
def update(v: REquipment): ConnectionIO[Int] = { def update(v: REquipment): ConnectionIO[Int] = {
val sql = updateRow( def sql(now: Timestamp) =
table, updateRow(
and(eid.is(v.eid), cid.is(v.cid)), table,
commas( and(eid.is(v.eid), cid.is(v.cid)),
cid.setTo(v.cid), commas(
name.setTo(v.name) cid.setTo(v.cid),
name.setTo(v.name),
updated.setTo(now)
)
) )
) for {
sql.update.run now <- Timestamp.current[ConnectionIO]
n <- sql(now).update.run
} yield n
} }
def existsByName(coll: Ident, ename: String): ConnectionIO[Boolean] = { def existsByName(coll: Ident, ename: String): ConnectionIO[Boolean] = {

View File

@ -19,7 +19,8 @@ case class ROrganization(
city: String, city: String,
country: String, country: String,
notes: Option[String], notes: Option[String],
created: Timestamp created: Timestamp,
updated: Timestamp
) {} ) {}
object ROrganization { object ROrganization {
@ -38,7 +39,8 @@ object ROrganization {
val country = Column("country") val country = Column("country")
val notes = Column("notes") val notes = Column("notes")
val created = Column("created") 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._ import Columns._
@ -47,26 +49,31 @@ object ROrganization {
val sql = insertRow( val sql = insertRow(
table, table,
all, 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 sql.update.run
} }
def update(v: ROrganization): ConnectionIO[Int] = { def update(v: ROrganization): ConnectionIO[Int] = {
val sql = updateRow( def sql(now: Timestamp) =
table, updateRow(
and(oid.is(v.oid), cid.is(v.cid)), table,
commas( and(oid.is(v.oid), cid.is(v.cid)),
cid.setTo(v.cid), commas(
name.setTo(v.name), cid.setTo(v.cid),
street.setTo(v.street), name.setTo(v.name),
zip.setTo(v.zip), street.setTo(v.street),
city.setTo(v.city), zip.setTo(v.zip),
country.setTo(v.country), city.setTo(v.city),
notes.setTo(v.notes) country.setTo(v.country),
notes.setTo(v.notes),
updated.setTo(now)
)
) )
) for {
sql.update.run now <- Timestamp.current[ConnectionIO]
n <- sql(now).update.run
} yield n
} }
def existsByName(coll: Ident, oname: String): ConnectionIO[Boolean] = def existsByName(coll: Ident, oname: String): ConnectionIO[Boolean] =

View File

@ -20,7 +20,8 @@ case class RPerson(
country: String, country: String,
notes: Option[String], notes: Option[String],
concerning: Boolean, concerning: Boolean,
created: Timestamp created: Timestamp,
updated: Timestamp
) {} ) {}
object RPerson { object RPerson {
@ -40,7 +41,20 @@ object RPerson {
val notes = Column("notes") val notes = Column("notes")
val concerning = Column("concerning") val concerning = Column("concerning")
val created = Column("created") 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._ import Columns._
@ -49,27 +63,32 @@ object RPerson {
val sql = insertRow( val sql = insertRow(
table, table,
all, 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 sql.update.run
} }
def update(v: RPerson): ConnectionIO[Int] = { def update(v: RPerson): ConnectionIO[Int] = {
val sql = updateRow( def sql(now: Timestamp) =
table, updateRow(
and(pid.is(v.pid), cid.is(v.cid)), table,
commas( and(pid.is(v.pid), cid.is(v.cid)),
cid.setTo(v.cid), commas(
name.setTo(v.name), cid.setTo(v.cid),
street.setTo(v.street), name.setTo(v.name),
zip.setTo(v.zip), street.setTo(v.street),
city.setTo(v.city), zip.setTo(v.zip),
country.setTo(v.country), city.setTo(v.city),
concerning.setTo(v.concerning), country.setTo(v.country),
notes.setTo(v.notes) concerning.setTo(v.concerning),
notes.setTo(v.notes),
updated.setTo(now)
)
) )
) for {
sql.update.run now <- Timestamp.current[ConnectionIO]
n <- sql(now).update.run
} yield n
} }
def existsByName(coll: Ident, pname: String): ConnectionIO[Boolean] = def existsByName(coll: Ident, pname: String): ConnectionIO[Boolean] =