Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

37.7 KB
Raw Blame
(ns metabase-enterprise.remote-sync.db
  "Application database queries for the remote-sync module. Every function here is a direct Toucan 2 call with no
  additional logic, so no other namespace in the module runs a query itself."
  (:require
   [medley.core :as m]
   [metabase-enterprise.remote-sync.schema :as remote-sync.schema]
   [metabase.collections.schema :as collections.schema]
   [metabase.lib.schema.id :as lib.schema.id]
   [metabase.util.malli :as mu]
   [metabase.util.malli.schema :as ms]
   [toucan2.core :as t2]))

(def ^:private ConditionKey
  "The column keys used in the `:conditions` / `:cascade-filter` / `:removal-conditions` of a remote-sync model
  spec (see `metabase-enterprise.remote-sync.spec`)."
  [:enum :exploration_id :built_in_type :is_default :entity_id :collection_id :model_id :archived :archived_at])

(def ^:private Conditions
  "A map of column to value (possibly nil) or Toucan 2 operator-vector value, or nil for none."
  [:maybe [:map-of ConditionKey [:maybe [:or :string :int :boolean :keyword sequential?]]]])

(def ^:private RemovalOpts
  "The `:scope-key`, `:synced-collection-ids`, `:unscoped-conditions`, `:entity-ids`, and `:removal-conditions`
  describing which rows an import removes (see [[removal-exprs]])."
  [:map {:closed true}
   [:scope-key             [:maybe :keyword]]
   [:synced-collection-ids [:maybe [:or [:set ::lib.schema.id/collection] [:sequential ::lib.schema.id/collection]]]]
   [:unscoped-conditions   Conditions]
   [:entity-ids            [:maybe [:or [:set :string] [:sequential :string]]]]
   [:removal-conditions    Conditions]])

(def ^:private ModelId
  "A `:model_id`: the primary key of the referenced entity, or `-1` for the legacy Transforms root Collection row that
  startup converts."
  [:maybe [:or ms/PositiveInt [:= -1]]])

(mu/defn eligible-children
  "The instances of `model-key` whose `fk` column equals `model-id`, additionally matching `conditions` (a map of
  column to value or Toucan 2 operator-vector value, or nil for none)."
  [model-key   :- :keyword
   fk          :- :keyword
   model-id    :- ms/PositiveInt
   conditions  :- Conditions]
  (apply t2/select model-key fk model-id (mapcat identity conditions)))

(mu/defn row-exists? :- :boolean
  "Whether a `model` row has `column` equal to `id`."
  [model  :- :keyword
   column :- :keyword
   id     :- ms/PositiveInt]
  (t2/exists? model column id))

(mu/defn ids-where
  "The IDs of the instances of `model-key` matching `conditions` (a map of column to value or Toucan 2
  operator-vector value, or nil for every instance)."
  [model-key  :- :keyword
   conditions :- Conditions]
  (apply t2/select-fn-set :id model-key (mapcat identity conditions)))

(mu/defn existing-ids :- [:set ms/PositiveInt]
  "The IDs among `ids` of the existing instances of `model-key`."
  [model-key :- :keyword
   ids       :- [:sequential ms/PositiveInt]]
  (if (seq ids)
    (t2/select-pks-set model-key :id [:in ids])
    #{}))

(mu/defn instances-where
  "The instances of `model-key` matching `conditions` (a map of column to value or Toucan 2 operator-vector value,
  or nil for every instance)."
  [model-key  :- :keyword
   conditions :- Conditions]
  (apply t2/select model-key (mapcat identity conditions)))

(mu/defn entity-id-where :- [:maybe :string]
  "The `:entity_id` of the instance of `model-key` whose `column` equals `value`, or nil."
  [model-key :- :keyword
   column    :- [:enum :name :term]
   value     :- :string]
  (t2/select-one-fn :entity_id model-key column value))

(mu/defn count-where
  "The number of instances of `model-key` matching `conditions` (a map of column to value or Toucan 2
  operator-vector value, or nil for every instance)."
  [model-key  :- :keyword
   conditions :- Conditions]
  (apply t2/count model-key (mapcat identity conditions)))

(defn- removal-condition-exprs
  "Turns a spec's removal-conditions map into `:where` fragments: an `:entity_id` entry whose value is an
  `[op value]` pair becomes `[op :entity_id value]`; any other entry with a vector value becomes `[:in key value]`;
  a scalar value becomes `[:= key value]`."
  [removal-conditions]
  (for [[k v] removal-conditions]
    (cond
      (and (= k :entity_id) (vector? v)) (let [[op value] v] [op :entity_id value])
      (vector? v)                        [:in k v]
      :else                              [:= k v])))

(defn- scope-expr
  "The `:where` fragment keeping the rows in `synced-collection-ids` or matching `unscoped-conditions`, or nil when
  neither is given."
  [scope-key synced-collection-ids unscoped-conditions]
  (let [scoped   (when (seq synced-collection-ids)
                   [:in scope-key synced-collection-ids])
        unscoped (when (seq unscoped-conditions)
                   (into [:and] (removal-condition-exprs unscoped-conditions)))]
    (if (and scoped unscoped)
      [:or scoped unscoped]
      (or scoped unscoped))))

(defn- removal-exprs
  "The `:where` fragments (see [[removal-condition-exprs]]) selecting the `model-key` rows an import removes:
  scoped to `synced-collection-ids` or `unscoped-conditions` (when `scope-key` is given), excluding `entity-ids`,
  and matching `removal-conditions`. Returns nil for a scoped model with neither (removes nothing)."
  [{:keys [scope-key synced-collection-ids unscoped-conditions entity-ids removal-conditions]}]
  (let [scope (when scope-key
                (scope-expr scope-key synced-collection-ids unscoped-conditions))]
    (when-not (and scope-key (nil? scope))
      (cond-> []
        scope            (conj scope)
        (seq entity-ids) (conj [:not-in :entity_id entity-ids])
        :always          (into (removal-condition-exprs removal-conditions))))))

(mu/defn delete-removed-instances!
  "Deletes the `model-key` rows an import removes (see [[removal-exprs]]); a no-op for a scoped model with no
  synced collections, and a delete of every row when nothing restricts it."
  [model-key    :- :keyword
   removal-opts :- RemovalOpts]
  (when-let [exprs (removal-exprs removal-opts)]
    (if (seq exprs)
      (t2/delete! model-key {:where (if (= 1 (count exprs)) (first exprs) (into [:and] exprs))})
      (t2/delete! model-key))))

(defn- unsynced-anti-join-expr
  "A `[:not [:exists ...]]` fragment keeping only rows with no RemoteSyncObject of `model-type` in 'synced'
  status — i.e. unsynced local work (an already-synced entity's removal is a normal reconcile, not data loss).
  `id-column` is the qualified id column of the model's own table."
  [model-type id-column]
  [:not [:exists ^:allow-subquery {:select [1]
                                   :from   [:remote_sync_object]
                                   :where  [:and
                                            [:= :remote_sync_object.model_type model-type]
                                            [:= :remote_sync_object.model_id id-column]
                                            [:= :remote_sync_object.status "synced"]]}]])

(defn- unsynced-instance-expr
  [model-key model-type removal-opts]
  (let [id-column (keyword (str (name (t2/table-name model-key)) ".id"))]
    (into [:and (unsynced-anti-join-expr model-type id-column)] (removal-exprs removal-opts))))

(mu/defn unsynced-instance-count
  "The number of `model-key` rows [[delete-removed-instances!]] would remove (see [[removal-exprs]]) that also
  have no RemoteSyncObject of `model-type` in synced status — unsynced local work an import would otherwise wipe
  out."
  [model-key    :- :keyword
   model-type   :- :string
   removal-opts :- RemovalOpts]
  (t2/count model-key {:where (unsynced-instance-expr model-key model-type removal-opts)}))

(mu/defn unsynced-instance-names
  "Up to `limit` values of `name-col` from the rows [[unsynced-instance-count]] counts."
  [model-key    :- :keyword
   model-type   :- :string
   name-col     :- [:enum :name :term]
   removal-opts :- RemovalOpts
   limit        :- ms/PositiveInt]
  (t2/select-fn-vec name-col model-key {:where (unsynced-instance-expr model-key model-type removal-opts)
                                        :limit limit}))

(mu/defn instance
  "The instance of `model` with `id`, or nil."
  [model :- :keyword
   id    :- ms/PositiveInt]
  (t2/select-one model :id id))

(mu/defn instance-with-columns
  "The `columns` of the instance of `model` with `id`, or nil."
  [model   :- :keyword
   columns :- [:sequential :keyword]
   id      :- ms/PositiveInt]
  (t2/select-one (into [model] columns)
                 :id id))

(mu/defn instance-names
  "The `:id` and `:name` of the instances of `model` with `ids`."
  [model :- :keyword
   ids   :- [:sequential ms/PositiveInt]]
  (t2/select [model :id :name] :id [:in ids]))

(mu/defn archived-by-id
  "A map of ID to the `:archived` flag of the instances of `model` with `ids`."
  [model :- :keyword
   ids   :- [:sequential ms/PositiveInt]]
  (t2/select-pk->fn :archived model :id [:in ids]))

(mu/defn instances-in-collections
  "The instances of `model` in the Collections with `collection-ids`, excluding those archived under the optional
  `archived-key` column."
  [model          :- :keyword
   collection-ids :- [:sequential ::lib.schema.id/collection]
   archived-key   :- [:maybe :keyword]]
  (t2/select model {:where [:and
                            [:in :collection_id collection-ids]
                            (when archived-key [:= archived-key false])]}))

(mu/defn instances-with-columns-by-entity-ids
  "The `columns` of the instances of `model` with `entity-ids`."
  [model      :- :keyword
   columns    :- [:sequential :keyword]
   entity-ids :- [:set :string]]
  (t2/select (into [model] columns) :entity_id [:in entity-ids]))

(mu/defn delete-instances!
  "Delete the instances of `model` with `ids`."
  [model :- :keyword
   ids   :- [:sequential ms/PositiveInt]]
  (t2/delete! model :id [:in ids]))

(defn- tracking-select-parts
  "The SELECT/FROM/JOIN of a `model-key` instance (TableUserSettings, FieldUserSettings, Dimension, Segment, or
  Measure) with its Table for sync tracking, plus the `alias` the row its id refers to is under (so callers can
  address its `:id` and `:entity_id` columns). The user settings are keyed by the id of their Table or Field.
  Segment and Measure share alias \"s\" — both are looked up the same way by [[tracking-details-by-entity-ids]]."
  [model-key]
  (case model-key
    :model/TableUserSettings {:alias  "t"
                              :select [:t.name [:t.id :table_id] :t.collection_id [:t.name :table_name]]
                              :from   [[(t2/table-name :model/Table) :t]]}
    (:model/FieldUserSettings :model/Dimension)
    {:alias  "f"
     :select [:f.name :f.table_id [:t.collection_id :collection_id] [:t.name :table_name]]
     :from   [[:metabase_field :f]]
     :join   [[(t2/table-name :model/Table) :t] [:= :f.table_id :t.id]]}
    :model/Segment {:alias  "s"
                    :select [:s.name :s.table_id [:t.collection_id :collection_id] [:t.name :table_name]]
                    :from   [[:segment :s]]
                    :join   [[(t2/table-name :model/Table) :t] [:= :s.table_id :t.id]]}
    :model/Measure {:alias  "s"
                    :select [:s.name :s.table_id [:t.collection_id :collection_id] [:t.name :table_name]]
                    :from   [[:measure :s]]
                    :join   [[(t2/table-name :model/Table) :t] [:= :s.table_id :t.id]]}))

(mu/defn tracking-details-by-id
  "The name, table id, collection id, and table name of the `model-key` (TableUserSettings, FieldUserSettings,
  Dimension, Segment, or Measure) instance with `model-id`, or nil."
  [model-key :- :keyword
   model-id  :- ms/PositiveInt]
  (let [{:keys [alias select from join]} (tracking-select-parts model-key)]
    (first (t2/query (m/assoc-some {:select select :from from :where [:= (keyword alias "id") model-id]}
                                   :join join)))))

(mu/defn tracking-details-by-entity-ids
  "The `:id`, name, table id, collection id, and table name of the `model-key` (Segment or Measure) instances with
  `entity-ids`."
  [model-key  :- :keyword
   entity-ids :- [:or [:set :string] [:sequential :string]]]
  (let [{:keys [alias select from join]} (tracking-select-parts model-key)
        id-column (keyword alias "id")]
    (t2/query {:select (into [id-column] select)
               :from   from
               :join   join
               :where  [:in (keyword alias "entity_id") entity-ids]})))

(mu/defn entity-id
  "The entity ID of the instance of `model` with `id`."
  [model :- :keyword
   id    :- ms/PositiveInt]
  (t2/select-one-fn :entity_id model :id id))

(mu/defn entity-ids-by-id
  "A map of ID to entity ID for the instances of `model` with `ids`."
  [model :- :keyword
   ids   :- [:sequential ms/PositiveInt]]
  (t2/select-pk->fn :entity_id model :id [:in ids]))

(mu/defn existing-entity-ids
  "The subset of `entity-ids` that instances of `model` have."
  [model      :- :keyword
   entity-ids :- [:or [:set :string] [:sequential :string]]]
  (t2/select-fn-set :entity_id model :entity_id [:in entity-ids]))

(mu/defn ids-by-entity-ids
  "The IDs of the instances of `model` with `entity-ids`."
  [model      :- :keyword
   entity-ids :- [:set :string]]
  (t2/select-pks-vec model :entity_id [:in entity-ids]))

(mu/defn card-types
  "The `:id`, `:type`, and `:display` of the Cards with `card-ids`."
  [card-ids :- [:sequential ::lib.schema.id/card]]
  (t2/select [:model/Card :id :type :display] :id [:in card-ids]))

(mu/defn card-counts-by-type :- [:map-of :keyword :int]
  "The number of Cards with `card-ids`, by `:type`."
  [card-ids :- [:sequential ::lib.schema.id/card]]
  (t2/select-fn->fn :type :count [:model/Card :type [:%count.* :count]] :id [:in card-ids] {:group-by [:type]}))

(mu/defn published-table-ids :- [:set ::lib.schema.id/table]
  "The ids of the Tables published in the Collections with `collection-ids`."
  [collection-ids :- [:sequential ::lib.schema.id/collection]]
  (set (t2/select-pks-set :model/Table {:from  [[(t2/table-name :model/Table) :t]]
                                        :where [:and [:= :t.is_published true] [:in :t.collection_id collection-ids]]})))

(mu/defn table-ids-with-user-settings
  "The ids of the Tables among `table-ids` that have a TableUserSettings row."
  [table-ids :- [:sequential ::lib.schema.id/table]]
  (t2/select-fn-vec :table_id :model/TableUserSettings {:select [:table_id] :where [:in :table_id table-ids]}))

(mu/defn field-ids-with-user-settings
  "The ids of the Fields of the Tables with `table-ids` that have a FieldUserSettings row."
  [table-ids :- [:sequential ::lib.schema.id/table]]
  (t2/select-fn-vec :field_id :model/FieldUserSettings
                    {:select [:u.field_id]
                     :from   [[(t2/table-name :model/FieldUserSettings) :u]]
                     :join   [[(t2/table-name :model/Field) :f] [:= :f.id :u.field_id]]
                     :where  [:in :f.table_id table-ids]}))

(mu/defn field-ids-with-dimensions
  "The ids of the Fields of the Tables with `table-ids` that have a Dimension."
  [table-ids :- [:sequential ::lib.schema.id/table]]
  (t2/select-fn-vec :field_id :model/Dimension
                    {:select [:d.field_id]
                     :from   [[(t2/table-name :model/Dimension) :d]]
                     :join   [[(t2/table-name :model/Field) :f] [:= :f.id :d.field_id]]
                     :where  [:in :f.table_id table-ids]}))

(mu/defn delete-dimensions!
  "Delete the Dimensions of the Fields with `field-ids`, returning the number deleted."
  [field-ids :- [:sequential ::lib.schema.id/field]]
  (t2/delete! :model/Dimension :field_id [:in field-ids]))

(mu/defn tables-tracking-details
  "The `:id`, `:name`, and `:collection_id` of the Tables with `table-ids`."
  [table-ids :- [:sequential ::lib.schema.id/table]]
  (t2/select [:model/Table :id :name :collection_id] :id [:in table-ids]))

(mu/defn fields-tracking-details
  "The `:id`, name, table id, collection id, and table name of the Fields with `field-ids`."
  [field-ids :- [:sequential ::lib.schema.id/field]]
  (let [{:keys [select from join]} (tracking-select-parts :model/FieldUserSettings)]
    (t2/query {:select (into [:f.id] select) :from from :join join :where [:in :f.id field-ids]})))

(mu/defn snippets
  "The `:id`, `:name`, and `:collection_id` of every NativeQuerySnippet."
  []
  (t2/select [:model/NativeQuerySnippet :id :name :collection_id]))

(mu/defn glossary-entries
  "The `:id` and `:term` of every Glossary entry."
  []
  (t2/select [:model/Glossary :id :term]))

(mu/defn untracked-glossary-entries
  "The `:id` and `:term` of every Glossary entry with no RemoteSyncObject."
  []
  (t2/select [:model/Glossary :id :term]
             {:where [:not [:exists ^:allow-subquery {:select [1]
                                                      :from   [:remote_sync_object]
                                                      :where  [:and
                                                               [:= :remote_sync_object.model_type "Glossary"]
                                                               [:= :remote_sync_object.model_id :glossary.id]]}]]}))

(defn- subtree-expr
  "Matches `collections` and all of their descendants."
  [collections]
  (into [:or [:in :id (map :id collections)]]
        (for [{:keys [id location]} collections]
          ;; the location its children have, so a nested collection's descendants match too
          [:like :location (str location id "/%")])))

(mu/defn collections
  "The Collections with `collection-ids`."
  [collection-ids :- [:sequential ::lib.schema.id/collection]]
  (t2/select :model/Collection :id [:in collection-ids]))

(mu/defn collections-by-id
  "A map of ID to the ID, name, location, and personal owner of the Collections with `collection-ids`."
  [collection-ids :- [:set ::lib.schema.id/collection]]
  (t2/select-pk->fn identity [:model/Collection :id :name :type :location :personal_owner_id] :id [:in collection-ids]))

(mu/defn collection-sync-states
  "The `:id` and `:is_remote_synced` of the Collections with `collection-ids`."
  [collection-ids :- [:sequential ::lib.schema.id/collection]]
  (t2/select [:model/Collection :id :is_remote_synced] :id [:in collection-ids]))

(mu/defn collection-name-and-id
  "The `:name` and `:collection_id` (its own ID) of the Collection with `collection-id`, or nil."
  [collection-id :- ::lib.schema.id/collection]
  (t2/select-one [:model/Collection :name [:id :collection_id]] :id collection-id))

(mu/defn library-collection
  "The Library Collection of `library-type`, or nil."
  [library-type :- :string]
  (t2/select-one :model/Collection :type library-type))

(mu/defn collections-with-names-in-namespace
  "The `:id` and `:name` of the Collections of `namespace-name`."
  [namespace-name :- :string]
  (t2/select [:model/Collection :id :name] :namespace namespace-name))

(mu/defn action-model-ids
  "The model Card id, or nil, of each existing Action with `action-ids`, keyed by Action id."
  [action-ids :- [:sequential ms/PositiveInt]]
  (t2/select-pk->fn :model_id [:model/Action :id :model_id] :id [:in action-ids]))

(mu/defn actions-without-model-in
  "The `:id`, `:name`, and `:collection_id` of the Actions without a model outside of any Collection or in the
  Collections with `collection-ids`."
  [collection-ids :- [:sequential ::lib.schema.id/collection]]
  (t2/select [:model/Action :id :name :collection_id]
             {:where [:and
                      [:= :model_id nil]
                      (if (seq collection-ids)
                        [:or [:= :collection_id nil] [:in :collection_id collection-ids]]
                        [:= :collection_id nil])]}))

(mu/defn snippet-collections
  "The `:id` and `:name` of the Collections of the snippets namespace."
  []
  (t2/select [:model/Collection :id :name] :namespace "snippets"))

(mu/defn snippet-collection-ids
  "The IDs of the Collections of the snippets namespace."
  []
  (t2/select-pks-set :model/Collection :namespace "snippets"))

(mu/defn collections-in-namespace
  "The `:id` and `:entity_id` of the Collections of `namespace-name`."
  [namespace-name :- :string]
  (t2/select [:model/Collection :id :entity_id] :namespace namespace-name))

(mu/defn collection-ids-in-namespace
  "The IDs of the Collections of `namespace-name`."
  [namespace-name :- :string]
  (t2/select-pks-vec :model/Collection :namespace namespace-name))

(mu/defn collection-namespace
  "The namespace of the Collection with `collection-id`, nil for the default namespace or no such Collection."
  [collection-id :- [:maybe ms/PositiveInt]]
  (when collection-id
    (t2/select-one-fn :namespace :model/Collection :id collection-id)))

(mu/defn remote-synced-collection-ids
  "The IDs of the remote-synced Collections."
  []
  (t2/select-pks-vec :model/Collection :is_remote_synced true))

(mu/defn remote-synced-root-collections
  "The `:namespace` and `:type` of the remote-synced top-level Collections."
  []
  (t2/select [:model/Collection :id :namespace :type] :is_remote_synced true :location "/"))

(mu/defn unarchived-remote-synced-root-collection-ids
  "The IDs of the unarchived remote-synced root Collections."
  []
  (t2/select-fn-set :id :model/Collection
                    {:where [:and
                             [:= :is_remote_synced true]
                             [:= :location "/"]
                             [:not :archived]]}))

(mu/defn unarchived-root-collection-ids-in-namespace
  "The IDs of the unarchived root Collections of `namespace-name`."
  [namespace-name :- :string]
  (t2/select-fn-set :id :model/Collection
                    {:where [:and
                             [:= :namespace namespace-name]
                             [:= :location "/"]
                             [:not :archived]]}))

(mu/defn subtree-collection-ids
  "The IDs of `collections` and all of their descendants."
  [collections :- [:sequential ::collections.schema/collection]]
  (t2/select-pks-set :model/Collection {:where (subtree-expr collections)}))

(mu/defn collection-content-exists? :- :boolean
  "Whether a Card, Dashboard, Document, Timeline, or Action is in the Collections with `collection-ids`, or a Table is
  published to one."
  [collection-ids :- [:sequential ::lib.schema.id/collection]]
  (boolean
   (or (t2/exists? :model/Card :collection_id [:in collection-ids])
       (t2/exists? :model/Dashboard :collection_id [:in collection-ids])
       (t2/exists? :model/Document :collection_id [:in collection-ids])
       (t2/exists? :model/Timeline :collection_id [:in collection-ids])
       (t2/exists? :model/Action :collection_id [:in collection-ids])
       (t2/exists? :model/Table {:where [:and [:= :is_published true] [:in :collection_id collection-ids]]}))))

(mu/defn remote-synced-subtree-collection-ids
  "The IDs of the remote-synced Collections among `collections` and their descendants."
  [collections :- [:sequential ::collections.schema/collection]]
  (t2/select-pks-set :model/Collection
                     {:where [:and
                              [:= :is_remote_synced true]
                              (subtree-expr collections)]}))

(mu/defn mark-subtree-remote-synced!
  "Mark `collections` and all of their descendants as remote-synced."
  [collections :- [:sequential ::collections.schema/collection]]
  (t2/query {:update (t2/table-name :model/Collection)
             :set    {:is_remote_synced true}
             :where  [:and
                      [:= :is_remote_synced false]
                      (subtree-expr collections)]}))

(mu/defn unmark-collections-remote-synced!
  "Mark the Collections with `collection-ids` as not remote-synced."
  [collection-ids :- [:set ::lib.schema.id/collection]]
  (t2/query {:update (t2/table-name :model/Collection)
             :set    {:is_remote_synced false}
             :where  [:in :id collection-ids]}))

(defn- contents-rso-expr
  "Matches the RemoteSyncObject rows of `collection-ids` and of their contents."
  [collection-ids]
  [:or
   [:and [:= :model_type "Collection"] [:in :model_id collection-ids]]
   [:in :model_collection_id collection-ids]])

(defn- rso-keys-expr
  "Matches the RemoteSyncObject rows of the `[{:model_type :model_id}]` `rows`."
  [rows]
  (into [:or] (map (fn [{:keys [model_type model_id]}]
                     [:and [:= :model_type model_type] [:= :model_id model_id]]))
        rows))

(mu/defn rso
  "The RemoteSyncObject of the entity `model-type` `model-id`, or nil."
  [model-type :- :string
   model-id   :- ModelId]
  (t2/select-one :model/RemoteSyncObject :model_type model-type :model_id model-id))

(mu/defn lock-rso
  "The RemoteSyncObject of the entity `model-type` `model-id`, locked for update, or nil."
  [model-type :- :string
   model-id   :- ModelId]
  (t2/select-one :model/RemoteSyncObject
                 {:where [:and [:= :model_type model-type] [:= :model_id model-id]]
                  :for   :update}))

(mu/defn rso-by-file-path
  "The RemoteSyncObject at `file-path`, or nil."
  [file-path :- :string]
  (t2/select-one :model/RemoteSyncObject :file_path file-path))

(mu/defn rso-exists?
  "Whether the entity `model-type` `model-id` has a RemoteSyncObject."
  [model-type :- :string
   model-id   :- ModelId]
  (t2/exists? :model/RemoteSyncObject :model_type model-type :model_id model-id))

(mu/defn rso-of-type-exists?
  "Whether any entity of `model-type` has a RemoteSyncObject."
  [model-type :- :string]
  (t2/exists? :model/RemoteSyncObject :model_type model-type))

(mu/defn rso-count-of-type
  "The number of RemoteSyncObjects of `model-type`."
  [model-type :- :string]
  (t2/count :model/RemoteSyncObject :model_type model-type))

(mu/defn rso-keys
  "The `:id`, `:model_type`, and `:model_id` of every RemoteSyncObject."
  []
  (t2/select [:model/RemoteSyncObject :id :model_type :model_id]))

(mu/defn departed-rso-keys
  "The `:id`, `:model_type`, and `:model_id` of the RemoteSyncObjects pending removal or deletion."
  []
  (t2/select [:model/RemoteSyncObject :id :model_type :model_id] :status [:in ["removed" "delete"]]))

(mu/defn all-rso-ids
  "The IDs of every RemoteSyncObject."
  []
  (t2/select-pks-set :model/RemoteSyncObject))

(mu/defn unsynced-rsos
  "The RemoteSyncObjects whose status is not synced."
  []
  (t2/select :model/RemoteSyncObject {:where [:not= :status "synced"]}))

(mu/defn dirty-rso-exists?
  "Whether a RemoteSyncObject is not synced, counting those of `excluded-model-types` only for a pending removal."
  [excluded-model-types :- [:set :string]]
  (t2/exists? :model/RemoteSyncObject
              {:where [:and
                       [:not= :status "synced"]
                       (when (seq excluded-model-types)
                         [:or
                          [:not-in :model_type excluded-model-types]
                          [:in :status ["removed" "delete"]]])]}))

(mu/defn dirty-rsos
  "The RemoteSyncObjects that are not synced, including those of `excluded-model-types` only for a pending removal."
  [excluded-model-types :- [:set :string]]
  (t2/select :model/RemoteSyncObject
             {:where [:and
                      [:not= :status "synced"]
                      (when (seq excluded-model-types)
                        [:or
                         [:not-in :model_type excluded-model-types]
                         [:in :status ["removed" "delete"]]])]}))

(mu/defn rsos-of-types
  "The RemoteSyncObjects of `model-types`."
  [model-types :- [:sequential :string]]
  (t2/select :model/RemoteSyncObject :model_type [:in model-types]))

(mu/defn tracked-model-ids
  "The model IDs of the RemoteSyncObjects of `model-type`."
  [model-type :- :string]
  (t2/select-fn-set :model_id :model/RemoteSyncObject :model_type model-type))

(mu/defn rsos-of-models
  "The RemoteSyncObjects of the entities of `model-type` with `model-ids`."
  [model-type :- :string
   model-ids  :- [:sequential ms/PositiveInt]]
  (t2/select :model/RemoteSyncObject :model_type model-type :model_id [:in model-ids]))

(mu/defn active-child-rsos
  "The RemoteSyncObjects of `model-type` under the Table with `table-id` that are not pending removal or deletion."
  [model-type :- :string
   table-id   :- ::lib.schema.id/table]
  (t2/select :model/RemoteSyncObject
             :model_type model-type
             :model_table_id table-id
             :status [:not-in ["removed" "delete"]]))

(mu/defn content-rso-statuses
  "The `:id` and `:status` of the RemoteSyncObjects of the Collections with `collection-ids` and their contents."
  [collection-ids :- [:set ::lib.schema.id/collection]]
  (t2/select [:model/RemoteSyncObject :id :status] {:where (contents-rso-expr collection-ids)}))

(mu/defn content-rsos
  "The `:id`, `:model_type`, `:model_id`, `:model_collection_id`, and `:status` of the RemoteSyncObjects of the
  Collections with `collection-ids` and of their contents."
  [collection-ids :- [:set ::lib.schema.id/collection]]
  (t2/select [:model/RemoteSyncObject :id :model_type :model_id :model_collection_id :status]
             {:where (contents-rso-expr collection-ids)}))

(mu/defn removed-content-rso-ids
  "The IDs of the RemoteSyncObjects pending removal among those of the Collections with `collection-ids` and their
  contents."
  [collection-ids :- [:sequential ::lib.schema.id/collection]]
  (t2/select-pks-set :model/RemoteSyncObject
                     {:where [:and
                              [:= :status "removed"]
                              (contents-rso-expr collection-ids)]}))

(mu/defn insert-rso!
  "Insert the RemoteSyncObject `row`."
  [row :- ::remote-sync.schema/remote-sync-object.update]
  (t2/insert! :model/RemoteSyncObject row))

(mu/defn insert-rsos!
  "Insert the RemoteSyncObject `rows`."
  [rows :- [:sequential ::remote-sync.schema/remote-sync-object.update]]
  (t2/insert! :model/RemoteSyncObject rows))

(mu/defn update-rso!
  "Apply `changes` to the RemoteSyncObject with `rso-id`."
  [rso-id  :- ms/PositiveInt
   changes :- ::remote-sync.schema/remote-sync-object.update]
  (t2/update! :model/RemoteSyncObject :id rso-id changes))

(mu/defn set-rsos-status!
  "Set the status of the RemoteSyncObjects with `rso-ids` to `status` as of `timestamp`."
  [rso-ids   :- [:sequential ms/PositiveInt]
   status    :- :string
   timestamp :- ms/TemporalInstant]
  (t2/update! :model/RemoteSyncObject :id [:in rso-ids] {:status status :status_changed_at timestamp}))

(mu/defn mark-all-rsos-synced!
  "Mark every RemoteSyncObject as synced as of `timestamp`."
  [timestamp :- ms/TemporalInstant]
  (t2/update! :model/RemoteSyncObject {:status "synced" :status_changed_at timestamp}))

(mu/defn mark-rsos-synced!
  "Mark the RemoteSyncObjects with `rso-ids` as synced as of `timestamp`, writing the `:file_path` and
  `:content_hash` of those in `metadata-by-id` and keeping the existing values of the others."
  [rso-ids        :- [:sequential ms/PositiveInt]
   metadata-by-id :- [:map-of ms/PositiveInt
                      [:map {:closed true}
                       [:id           {:optional true} ms/PositiveInt]
                       [:file_path    {:optional true} [:maybe :string]]
                       [:content_hash {:optional true} [:maybe :string]]]]
   timestamp      :- ms/TemporalInstant]
  (t2/update! :model/RemoteSyncObject
              {:id [:in (vec rso-ids)]}
              (cond-> {:status "synced" :status_changed_at timestamp}
                (seq metadata-by-id)
                (assoc :file_path    (into [:case]
                                           (concat
                                            (mapcat (fn [[id {:keys [file_path]}]]
                                                      [[:= :id id] file_path])
                                                    metadata-by-id)
                                            [:else :file_path]))
                       :content_hash (into [:case]
                                           (concat
                                            (mapcat (fn [[id {:keys [content_hash]}]]
                                                      [[:= :id id] content_hash])
                                                    metadata-by-id)
                                            [:else :content_hash]))))))

(mu/defn delete-rso!
  "Delete the RemoteSyncObject with `rso-id`."
  [rso-id :- ms/PositiveInt]
  (t2/delete! :model/RemoteSyncObject :id rso-id))

(mu/defn delete-rsos!
  "Delete the RemoteSyncObjects with `rso-ids`."
  [rso-ids :- [:sequential ms/PositiveInt]]
  (t2/delete! :model/RemoteSyncObject :id [:in rso-ids]))

(mu/defn delete-rso-of!
  "Delete the RemoteSyncObject of the entity `model-type` `model-id`."
  [model-type :- :string
   model-id   :- ModelId]
  (t2/delete! :model/RemoteSyncObject :model_type model-type :model_id model-id))

(mu/defn delete-rsos-of-type!
  "Delete the RemoteSyncObjects of `model-type`."
  [model-type :- :string]
  (t2/delete! :model/RemoteSyncObject :model_type model-type))

(mu/defn delete-rsos-of-models!
  "Delete the RemoteSyncObjects of the entities of `model-type` with `model-ids`."
  [model-type :- :string
   model-ids  :- [:set ms/PositiveInt]]
  (t2/delete! :model/RemoteSyncObject :model_type model-type :model_id [:in model-ids]))

(mu/defn delete-rsos-of-keys!
  "Delete the RemoteSyncObjects keyed by the `:model_type`/`:model_id` of `rows`."
  [rows :- [:sequential ::remote-sync.schema/remote-sync-object.update]]
  (t2/delete! :model/RemoteSyncObject {:where (rso-keys-expr rows)}))

(mu/defn delete-all-rsos!
  "Delete every RemoteSyncObject."
  []
  (t2/delete! :model/RemoteSyncObject))

(mu/defn task
  "The RemoteSyncTask with `task-id`, or nil."
  [task-id :- ms/PositiveInt]
  (t2/select-one :model/RemoteSyncTask :id [:auto/param task-id]))

(mu/defn lock-task
  "The RemoteSyncTask with `task-id`, locked for update, or nil."
  [task-id :- ms/PositiveInt]
  (t2/select-one :model/RemoteSyncTask :id task-id {:for :update}))

(mu/defn task-cancelled?
  "The cancelled flag of the RemoteSyncTask with `task-id`."
  [task-id :- ms/PositiveInt]
  (t2/select-one-fn :cancelled :model/RemoteSyncTask :id task-id))

(def ^:private last-alive-at
  "The time the task's owning thread last proved it was alive: its heartbeat, or its last progress write for
  rows that predate the heartbeat column or whose worker died before its first beat."
  [:coalesce :last_heartbeat_at :last_progress_report_at])

(mu/defn current-task
  "The newest started, unfinished RemoteSyncTask whose owner was alive after `liveness-cutoff`, or nil."
  [liveness-cutoff :- ms/TemporalInstant]
  (t2/select-one :model/RemoteSyncTask
                 {:where    [:and
                             [:<> :started_at nil]
                             [:= :ended_at nil]
                             [:< liveness-cutoff last-alive-at]]
                  :limit    1
                  :order-by [[:started_at :desc]
                             [:id :desc]]}))

(mu/defn most-recent-task
  "The newest started RemoteSyncTask, or nil."
  []
  (t2/select-one :model/RemoteSyncTask
                 {:where    [:and
                             [:<> :started_at nil]]
                  :limit    1
                  :order-by [[:started_at :desc]
                             [:id :desc]]}))

(mu/defn last-synced-task
  "The newest RemoteSyncTask whose commit the local content matches, or nil. `version` is written inside the
  transaction that commits an import or a push, and by the conflict path (which also writes `conflicts`), so
  `version` set with `conflicts` null identifies a landed commit whatever `ended_at`, `cancelled`, or
  `error_message` say: a task cancelled or superseded after its transaction committed is still the sync base."
  []
  (t2/select-one :model/RemoteSyncTask
                 {:where    [:and
                             [:<> nil :version]
                             [:= nil :conflicts]]
                  :limit    1
                  :order-by [[:started_at :desc]
                             [:id :desc]]}))

(mu/defn insert-task!
  "Insert `task` and return the new instance."
  [task :- ::remote-sync.schema/remote-sync-task.update]
  (t2/insert-returning-instance! :model/RemoteSyncTask task))

(mu/defn update-task!
  "Apply `changes` to the RemoteSyncTask with `task-id`."
  [task-id :- ms/PositiveInt
   changes :- ::remote-sync.schema/remote-sync-task.update]
  (t2/update! :model/RemoteSyncTask :id task-id changes))

(mu/defn end-task!
  "Apply `changes` to the RemoteSyncTask with `task-id` and mark it ended now."
  [task-id :- ms/PositiveInt
   changes :- ::remote-sync.schema/remote-sync-task.update]
  (t2/update! :model/RemoteSyncTask :id task-id (assoc changes :ended_at :%now)))

(mu/defn report-task-progress!
  "Record `progress` for the RemoteSyncTask with `task-id`, stamping the progress report time."
  [task-id  :- ms/PositiveInt
   progress :- number?]
  (t2/update! :model/RemoteSyncTask :id task-id {:progress progress, :last_progress_report_at :%now}))

(mu/defn touch-task!
  "Stamp the heartbeat time of the RemoteSyncTask with `task-id` if it has not ended, returning the number of rows
  updated. Never touches an ended row, so a cancel or supersede is not undone by a late beat."
  [task-id :- ms/PositiveInt]
  (t2/update! :model/RemoteSyncTask {:id task-id, :ended_at nil} {:last_heartbeat_at :%now}))

(mu/defn supersede-stale-tasks! :- [:sequential ms/PositiveInt]
  "Cancel and end now, with `message` as the error message, the started, unfinished RemoteSyncTasks whose owner
  was last alive before `cutoff`. Returns the ids of the rows ended, empty when none were stale."
  [cutoff  :- ms/TemporalInstant
   message :- :string]
  (let [stale [:and
               [:<> :started_at nil]
               [:= :ended_at nil]
               [:< last-alive-at cutoff]]
        ids   (vec (t2/select-pks-vec :model/RemoteSyncTask {:where stale}))]
    (when (seq ids)
      (t2/query {:update (t2/table-name :model/RemoteSyncTask)
                 :set    {:cancelled     true
                          :ended_at      :%now
                          :error_message message}
                 :where  [:and stale [:in :id ids]]}))
    ids))

(mu/defn delete-tasks-started-before!
  "Delete the RemoteSyncTasks started before `cutoff`, returning the number deleted."
  [cutoff :- ms/TemporalInstant]
  (t2/delete! :model/RemoteSyncTask {:where [:< :started_at cutoff]}))

(mu/defn users-by-id
  "A map of User ID to User for `user-ids`."
  [user-ids :- [:sequential [:maybe ::lib.schema.id/user]]]
  (t2/select-pk->fn identity :model/User :id [:in user-ids]))