 
 (schema/defn ^:always-validate create-company!
   "Create a company document in RethinkDB. Add `updated-at` keys where necessary."
-  [company :- common/Company]
+  [conn company :- common/Company]
   (let [real-sections (real-sections company)
         ts (common/current-timestamp)
         rs-w-ts (med/map-vals #(add-updated-at % ts) real-sections)]
     (doseq [[_ section] real-sections]
-      (common/create-resource common/section-table-name section ts))
-    (common/create-resource table-name (merge company rs-w-ts) ts)))
+      (common/create-resource conn common/section-table-name section ts))
+    (common/create-resource conn table-name (merge company rs-w-ts) ts)))
 
 (defn update-company
   "

                            (remove-sections)
                            (assoc :org-id org-id)
                            (assoc :slug slug))]
      (common/update-resource table-name primary-key original-company updated-company))))

(defun put-company
  "Given the slug of the company and a company property map, create or update the company
  and return `true` on success.
  TODO: handle case of slug mismatch between URL and properties.
  TODO: handle case of slug change."
  ([slug :guard get-company company _user] (update-company slug company))
  ([_slug company user] (create-company! (->company company user))))

(defn delete-company
  "Given the slug of the company, delete it and all its sections and return `true` on success."
  [slug]
  {:pre [(string? slug)]}
  (try
    (common/delete-resource common/section-table-name :company-slug slug)
    (catch java.lang.RuntimeException e)) ; it's OK if there are no sections to delete
  (try
    (common/delete-resource table-name slug)
    (catch java.lang.RuntimeException e))) ; it's OK if there is no company to delete

;; ----- Company revisions -----

(defn list-revisions
  "Given the slug of the company retrieve the timestamps and author of the revisions from the database."
  [slug]
  {:pre [(string? slug)]}
  (common/updated-at-order
    (common/read-resources common/section-table-name "company-slug" slug [:updated-at :author])))

;; ----- Collection of companies -----

(defn list-companies
  "Return a sequence of company property maps with slugs and names, sorted by slug.
  Note: if additional-keys are supplied only documents containing those keys will be returned"
  ([] (list-companies []))
  ([additional-keys]
    (->> (into [primary-key "name"] additional-keys)
      (common/read-resources table-name)
      (sort-by primary-key)
      vec)))

(defn get-companies-by-index
  "Given the name of a secondary index and a value, retrieve all matching companies"
  ;; e.g.: (get-companies-by-index "org-id" "slack:T06SBMH60")
  [index-key v]
  {:pre [(string? index-key)]}
  (common/read-resources table-name index-key v))

;; ----- Armageddon -----

(defn delete-all-companies!
  "Use with caution! Failure can result in partial deletes of sections and companies. Returns `true` if successful."
  []
  ;; Delete all sections and all companies
  (common/delete-all-resources! common/section-table-name)
  (common/delete-all-resources! table-name))

(defn get-company
  "Given the slug of the company, retrieve it from the database, or return nil if it doesn't exist."
  [conn slug]
  {:pre [(string? slug)]}
  (common/read-resource conn table-name slug))

(defn- core-placeholder-sections
  "Return a map of section-name -> section containing just the core
