 
 (def schema-attributes
   [;; Job attributes
-    {:db/id (d/tempid :db.part/db)
-     :db/ident :job/command
-     :db/valueType :db.type/string
-     :db/cardinality :db.cardinality/one
-     :db.install/_attribute :db.part/db}
-    {:db/id (d/tempid :db.part/db)
-     :db/ident :job/commit-latch
-     :db/valueType :db.type/ref
-     :db/cardinality :db.cardinality/one
-     :db.install/_attribute :db.part/db}
-    {:db/id (d/tempid :db.part/db)
-     :db/ident :job/user
-     :db/valueType :db.type/string
-     :db/cardinality :db.cardinality/one
-     :db.install/_attribute :db.part/db}
-    {:db/id (d/tempid :db.part/db)
-     :db/ident :job/name
-     :db/valueType :db.type/string
-     :db/cardinality :db.cardinality/one
-     :db.install/_attribute :db.part/db}
-    {:db/id (d/tempid :db.part/db)
-     :db/ident :job/uuid
-     :db/valueType :db.type/uuid
-     :db/cardinality :db.cardinality/one
-     :db/unique :db.unique/identity
-     :db.install/_attribute :db.part/db}
-    {:db/id (d/tempid :db.part/db)
-     :db/ident :job/max-retries
-     :db/valueType :db.type/long
-     :db/cardinality :db.cardinality/one
-     :db.install/_attribute :db.part/db}
-    {:db/id (d/tempid :db.part/db)
-     :db/ident :job/max-runtime
-     :db/valueType :db.type/long
-     :db/cardinality :db.cardinality/one
-     :db.install/_attribute :db.part/db}
-    {:db/id (d/tempid :db.part/db)
-     :db/ident :job/environment
-     :db/valueType :db.type/ref
-     :db/cardinality :db.cardinality/many
-     :db.install/_attribute :db.part/db}
-    {:db/id (d/tempid :db.part/db)
-     :db/ident :job/label
-     :db/valueType :db.type/ref
-     :db/cardinality :db.cardinality/many
-     :db.install/_attribute :db.part/db}
-    {:db/id (d/tempid :db.part/db)
-     :db/ident :job/state
-     :db/valueType :db.type/ref
-     :db/cardinality :db.cardinality/one
-     :db.install/_attribute :db.part/db}
-    {:db/id (d/tempid :db.part/db)
-     :db/ident :job/instance
-     :db/valueType :db.type/ref
-     :db/isComponent true
-     :db/cardinality :db.cardinality/many
-     :db.install/_attribute :db.part/db}
-    {:db/id (d/tempid :db.part/db)
-     :db/ident :job/resource
-     :db/valueType :db.type/ref
-     :db/isComponent true
-     :db/cardinality :db.cardinality/many
-     :db.install/_attribute :db.part/db}
+   {:db/id (d/tempid :db.part/db)
+    :db/ident :job/command
+    :db/valueType :db.type/string
+    :db/cardinality :db.cardinality/one
+    :db.install/_attribute :db.part/db}
+   {:db/id (d/tempid :db.part/db)
+    :db/ident :job/commit-latch
+    :db/valueType :db.type/ref
+    :db/cardinality :db.cardinality/one
+    :db.install/_attribute :db.part/db}
+   {:db/id (d/tempid :db.part/db)
+    :db/ident :job/user
+    :db/valueType :db.type/string
+    :db/cardinality :db.cardinality/one
+    :db.install/_attribute :db.part/db}
+   {:db/id (d/tempid :db.part/db)
+    :db/ident :job/name
+    :db/valueType :db.type/string
+    :db/cardinality :db.cardinality/one
+    :db.install/_attribute :db.part/db}
+   {:db/id (d/tempid :db.part/db)
+    :db/ident :job/uuid
+    :db/valueType :db.type/uuid
+    :db/cardinality :db.cardinality/one
+    :db/unique :db.unique/identity
+    :db.install/_attribute :db.part/db}
+   {:db/id (d/tempid :db.part/db)
+    :db/ident :job/max-retries
+    :db/valueType :db.type/long
+    :db/cardinality :db.cardinality/one
+    :db.install/_attribute :db.part/db}
+   {:db/id (d/tempid :db.part/db)
+    :db/ident :job/max-runtime
+    :db/valueType :db.type/long
+    :db/cardinality :db.cardinality/one
+    :db.install/_attribute :db.part/db}
+   {:db/id (d/tempid :db.part/db)
+    :db/ident :job/environment
+    :db/valueType :db.type/ref
+    :db/cardinality :db.cardinality/many
+    :db.install/_attribute :db.part/db}
+   {:db/id (d/tempid :db.part/db)
+    :db/ident :job/label
+    :db/valueType :db.type/ref
+    :db/cardinality :db.cardinality/many
+    :db.install/_attribute :db.part/db}
+   {:db/id (d/tempid :db.part/db)
+    :db/ident :job/state
+    :db/valueType :db.type/ref
+    :db/cardinality :db.cardinality/one
+    :db.install/_attribute :db.part/db}
+   {:db/id (d/tempid :db.part/db)
+    :db/ident :job/instance
+    :db/valueType :db.type/ref
+    :db/isComponent true
+    :db/cardinality :db.cardinality/many
+    :db.install/_attribute :db.part/db}
+   {:db/id (d/tempid :db.part/db)
+    :db/ident :job/resource
+    :db/valueType :db.type/ref
+    :db/isComponent true
+    :db/cardinality :db.cardinality/many
+    :db.install/_attribute :db.part/db}
    {:db/id (d/tempid :db.part/db)
     :db/ident :job/submit-time
     :db/index true

                         mea-culpa-limit (or (when (:job/disable-mea-culpa-retries job-ent)
                                               0)
                                             (:scheduler.config/mea-culpa-failure-limit
                                              (d/entity db :scheduler/config))
                                             5)]
                     (->> job-ent
                          :job/instance

                     ;; Checking the validity of the target state transition
                     (when (get-in state-transitions [old-state new-state])
                       (into [[:db/add instance :instance/status new-state]]
                             (let [instance-ent (d/entity db instance)
                                   job-ent (:job/_instance instance-ent)
                                   job (:db/id job-ent)
                                   other-instances (->> (:job/instance job-ent)
                                                        (remove #(= (:db/id %) (:db/id instance-ent))))
                                   instance-states (->> other-instances
                                                        (map :instance/status)
                                                        (cons new-state))
                                   any-success? (some #{:instance.status/success} instance-states)
                                   any-running? (some #{:instance.status/running} instance-states)
                                   any-unknown? (some #{:instance.status/unknown} instance-states)
                                   all-failed? (every? #{:instance.status/failed} instance-states)
                                   prior-state (:job/state job-ent)
                                   reason (d/entity db reason)
                                   all-attempts-consumed?
                                   (d/invoke db :job/all-attempts-consumed? db
                                             (update-in (into {} job-ent) [:job/instance]
                                                        conj {:instance/status new-state
                                                              :instance/reason reason}))]
                               (cond
                                 (= prior-state :job.state/completed)
                                 []

                                 (or (and all-failed?
                                          all-attempts-consumed?)
                                     any-success?)
                                 [[:db/add job :job/state :job.state/completed]]

                                 (or any-running?
                                     any-unknown?)
                                 [[:db/add job :job/state :job.state/running]]

                                 :else
                                 [[:db/add job :job/state :job.state/waiting]])))))}}

   {:db/id (d/tempid :db.part/user)
    :db/ident :job/allowed-to-start?
