    updated-record is the record as currently in the db and key-failures
    is a map of {key values} for unique keys which were requested but
    could not be acquired "
-  [^Session session ^Entity entity record opts]
+  [session :- Session
+   entity :- Entity
+   record :- t/MaybeRecordSchema
+   opts :- UpsertOptsSchema]
    (with-context deferred-context
      (mlet [:let [[record] (t/run-callbacks entity :before-save [record])]
 

        (for [kv stale-kvs]
          (delete-record session entity t kv)))))))

(defn lookup-record-seq
  "returns a seq of tuples [table lookup-record]"
  [model old-record record]
  (mapcat
   identity
   (for [t (t/mutable-lookup-tables model)]

            to
            unique-key-tables)))

(s/defn has-lookups?
  [entity :- Entity]
  (boolean
   (or (not-empty (t/mutable-secondary-tables entity))
       (not-empty (t/mutable-lookup-tables entity)))))

(s/defn update-secondaries-and-lookups
  "update non-LWT secondary and lookup entries"

  [session :- Session
   entity :- Entity
   old-record :- t/MaybeRecordSchema
   updated-record-with-keys :- t/MaybeRecordSchema]
  (with-context deferred-context
    (mlet [stale-secondary-responses (delete-stale-secondaries
                                      session
                                      entity
                                      old-record
                                      updated-record-with-keys)

           stale-lookup-responses (delete-stale-lookups
                                   session
                                   entity
                                   old-record
                                   updated-record-with-keys)

           secondary-reponses (upsert-secondaries
                               session
                               entity
                               updated-record-with-keys)

           lookup-responses (upsert-lookups
                             session
                             entity
                             old-record
                             updated-record-with-keys)]

      (return updated-record-with-keys))))

(s/defschema UpsertOptsSchema
  (s/maybe
   {(s/optional-key :if-not-exists) s/Bool
    (s/optional-key :only-if) [s/Any]
    (s/optional-key :columns) [(s/one s/Keyword :column) s/Keyword]
    (s/optional-key :where) [s/Any]
    (s/optional-key :using) {(s/optional-key :timestamp) s/Int
                             (s/optional-key :ttl) s/Int}}))

(s/defn upsert*
  "upsert a single instance, upserting primary, secondary, unique-key and
   lookup records as required and deleting stale secondary, unique-key and
   lookup records
