<commit_before>(ns migrations.033-replace-tags-namespace-and-value-with-tag-key
  (:require [clojure.java.jdbc :as j]
            [clojure.edn :as edn]
            [clojure.string :as str]
            [config.migrate-config :as config]
            [cmr.common.util :as util]
            [config.mdb-migrate-helper :as h]))

(defn up
  "Migrates the database up to version 33."
  []
  (println "migrations.033-replace-tags-namespace-and-value-with-tag-key up...")
  (doseq [result (h/query "SELECT * from cmr_tags")]
    (println (format "Processing concept-id [%s] revision [%d]"
                     (:concept_id result)
                     (long (:revision_id result))))
    (let [{:keys [id metadata]} result
          metadata (-> metadata
                       (util/gzip-blob->string)
                       (edn/read-string))
          ;; Some rows may already have a tag-key instead of namespace and value.
          {:keys [namespace value tag-key]} metadata
          tag-key (or tag-key
                      (str/lower-case (str namespace "." value)))
          metadata (-> metadata
                       (assoc :tag-key tag-key)
                       (dissoc :namespace :value :category)
                       (prn-str)
                       (util/string->gzip-bytes))
          result (assoc result :metadata metadata :native_id tag-key)]
      (j/update! (config/db) "cmr_tags" result ["id = ?" id]))))

(defn down
  "Migrates the database down from version 33."
  []
  (println "migrations.033-replace-tags-namespace-and-value-with-tag-key down...")
  (println "NOTE: category field and original native-id cannot be recovered")
  (doseq [result (h/query "SELECT * from cmr_tags")]
    (let [{:keys [id metadata]} result
          metadata (-> metadata
                       (util/gzip-blob->string)
                       (edn/read-string))
          tag-key (:tag-key metadata)
          ;; There is no way to properly recover a namespace and value that have a 
          ;; "." in the value.
          [_ namespace value] (re-matches #"(.*)\.(.*$)" tag-key)
          metadata (-> metadata
                       (assoc :namespace namespace
                              :value value)
                       (dissoc :tag-key)
                       (prn-str)
                       (util/string->gzip-bytes))
          result (assoc result :metadata metadata)]
      (j/update! (config/db) "cmr_tags" result ["id = ?" id]))))
<commit_msg>CMR-2480: Fixed small issues with migration<commit_after>(ns migrations.033-replace-tags-namespace-and-value-with-tag-key
  (:require [clojure.java.jdbc :as j]
            [clojure.edn :as edn]
            [clojure.string :as str]
            [config.migrate-config :as config]
            [cmr.common.util :as util]
            [config.mdb-migrate-helper :as h]))

(defn- print-message
  [result]
  (println (format "Processing concept-id [%s] revision [%d]"
                   (:concept_id result)
                   (long (:revision_id result)))))

(defn up
  "Migrates the database up to version 33."
  []
  (println "migrations.033-replace-tags-namespace-and-value-with-tag-key up...")
  (doseq [result (h/query "SELECT * from cmr_tags")]
    (print-message result)
    (let [{:keys [id metadata]} result
          metadata (-> metadata
                       (util/gzip-blob->string)
                       (edn/read-string))
          ;; Some rows may already have a tag-key instead of namespace and value.
          {:keys [namespace value tag-key]} metadata
          tag-key (or tag-key
                      (str/lower-case (str namespace "." value)))
          metadata (-> metadata
                       (assoc :tag-key tag-key)
                       (dissoc :namespace :value :category)
                       (prn-str)
                       (util/string->gzip-bytes))
          result (assoc result :metadata metadata :native_id tag-key)]
      (j/update! (config/db) "cmr_tags" result ["id = ?" id]))))

(defn down
  "Migrates the database down from version 33."
  []
  (println "migrations.033-replace-tags-namespace-and-value-with-tag-key down...")
  (println "NOTE: category field and original native-id cannot be recovered")
  (doseq [result (h/query "SELECT * from cmr_tags")]
    (print-message result)
    (let [{:keys [id metadata]} result
          metadata (-> metadata
                       (util/gzip-blob->string)
                       (edn/read-string))
          tag-key (:tag-key metadata)
          ;; There is no way to properly recover a namespace and value that have a
          ;; "." in the value.
          [_ namespace value] (re-matches #"(.*)\.(.*$)" tag-key)
          native-id (str namespace (char 29) value)
          metadata (-> metadata
                       (assoc :namespace namespace
                              :value value)
                       (dissoc :tag-key)
                       (prn-str)
                       (util/string->gzip-bytes))
          result (assoc result :metadata metadata :native_id native-id)]
      (j/update! (config/db) "cmr_tags" result ["id = ?" id]))))

{'fraction_non_alphanumeric': 0.1304773082942097, 'fraction_numerical': 0.006259780907668232, 'mean_word_length': 1.7313034188034189, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2773751', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1444, 'n_words': 404}