 ;    nil))
 ;
 ;
-;(defmeta experiments
+;(defmeta dataset
 ;  :id "INT NOT NULL AUTO_INCREMENT PRIMARY KEY"
 ;  :name "varchar(255) NOT NULL UNIQUE"
 ;  :probeMap "varchar(255)"


; XXX What should max file name length be?
; XXX Unique columns? Indexes?
(def sources-table
  ["CREATE TABLE IF NOT EXISTS `sources` (
   `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   `name` VARCHAR(2000) NOT NULL,
   `time` TIMESTAMP NOT NULL,
   `hash` VARCHAR(40) NOT NULL)"])

; XXX FIX NAME
(def experiments-table
  ["CREATE TABLE IF NOT EXISTS `experiments` (
   `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   `name` varchar(1000) NOT NULL UNIQUE,
   `probeMap` varchar(255),

  (assoc md :text (json/write-str md :escape-slash false)))

(defn- load-probe-meta [feature-list]
  (doseq [[field-id feature] feature-list]
    (let [fmeta (normalize-meta feature-meta feature)
          fmeta (merge fmeta {:field_id field-id})
          feature-id (insert feature (values fmeta))
          order (:order feature)]
      (doseq [a-code (:state feature)]
        (insert code (values {:feature_id feature-id
                               :ordering (order a-code)
                               :value a-code}))))))

;
;
;

(defn- fields-in-exp [exp]
  (subselect field (fields :id) (where {:dataset_id exp})))

(defn- scores-with-fields [fs]
  (subselect field_score (fields :score_id) (where {:field_id [in fs]})))

(defn- clear-by-exp [exp]
  (let [f (fields-in-exp exp)]
    (delete sample (where {:dataset_id exp}))
    (delete scores (where {:id [in (scores-with-fields f)]}))
    (delete field_score (where {:field_id [in f]}))
    (delete field (where {:id [in f]}))))

; Merge cohort, returning id
(comment (defn- merge-cohort [cohort]
  (exec-raw ["MERGE INTO cohorts(name) KEY(name) VALUES (?)" [cohort]])
  (let [[{cid :ID}] (select cohorts (where {:name cohort}))]
    cid)))

; Update meta entity record.
(defn- merge-m-ent [m-ent ename metadata]
