 (defn- get-dataset-record-id-for-collection-sql
   "Generate SQL to retrieve the id for a given collection/dataset from the catalog-rest table."
   [provider-id collection-id]
-  ;; FIXME - you have need to create table names with user prefixes in multiple places. This should
-  ;; be extracted into a helper function.
-  (let [dataset-table (keyword (str catalog-rest-tablespace "." provider-id "-dataset-records"))]
-    (su/build (select [:id] (from dataset-table) (where `(= :echo-collection-id ~collection-id))))))
+  (su/build (select [:id] (from (catalog-rest-dataset-table provider-id))
+                    (where `(= :echo-collection-id ~collection-id)))))
 
 (defn- get-dataset-record-id-for-collection
   "Retrieve the id for a given collection/dataset from the catalog-rest table."

(defn- delete-collection-granules-sql
  "Generate SQL to delete granules for a given collection from a provider's granule table."
  [provider-id collection-id]
  (let [granule-table (keyword (str metadata-db-tablespace "." provider-id "-granules"))]
    (su/build (delete granule-table (where `(= :parent-collection-id ~collection-id))))))

(defn- delete-collection-granules

(defn- copy-collection-data-sql
  "Generate SQL to copy the dataset/collection data from the catalog rest database to the
  metadata db for the given provider for all the provider's collections or a single collection."
  ([provider-id] (copy-collection-data-sql provider-id nil))
  ([provider-id  collection-id] (let [dataset-table (keyword (str (catalog-rest-tablespace)
                                                                  "."
                                                                  provider-id
                                                                  "-dataset-records"))
                                      collection-table (keyword (str (metadata-db-tablespace)
                                                                     "."
                                                                     provider-id
                                                                     "-collections"))
                                      collection-id (first collection-id)]
                                  (su/build (insert collection-table [:concept-id
                                                                      :native-id
                                                                      :metadata
                                                                      :format
                                                                      :short-name
                                                                      :version-id
                                                                      :entry-title]
                                                    (select [:echo-collection-id
                                                             :dataset-id
                                                             :compressed-xml
                                                             :xml-mime-type
                                                             :short-name
                                                             :version-id
                                                             :long-name]
                                                            (from dataset-table)
                                                            (when collection-id
                                                              (where `(= :echo-collection-id ~collection-id)))))))))


(defn- copy-collection-data
