Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions src/puppetlabs/puppetdb/scf/storage.clj
Original file line number Diff line number Diff line change
Expand Up @@ -537,17 +537,16 @@
(some-> params sutils/munge-jsonb-for-storage)])}
*note-add-params-insert* hsql/format (nxt/execute! (jdbc/connection))))

(doseq [batch (partition-all 1000 new-params)]
;; The batch might only have one item with params {}.
(when-let [rows (seq (for [[resource-hash params] batch
[k v] params]
[(sutils/munge-hash-for-storage resource-hash)
(name k)
(sutils/db-serialize v)]))]
(->> {:insert-into :resource_params
:columns [:resource :name :value]
:values rows}
*note-add-params-insert* hsql/format (nxt/execute! (jdbc/connection)))))))
;; Batch by row (3 bind params each) to stay under the 65,535 limit.
(doseq [rows (partition-all 10000 (for [[resource-hash params] new-params
[k v] params]
[(sutils/munge-hash-for-storage resource-hash)
(name k)
(sutils/db-serialize v)]))]
(->> {:insert-into :resource_params
:columns [:resource :name :value]
:values rows}
*note-add-params-insert* hsql/format (nxt/execute! (jdbc/connection))))))

(def resource-ref?
"Returns true of the map is a resource reference"
Expand Down
19 changes: 19 additions & 0 deletions test/puppetlabs/puppetdb/scf/storage_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,25 @@

(is (= 4 (:c (first (query-to-vec "SELECT count(*) AS c FROM catalog_resources WHERE certname_id = (select id from certnames where certname = ?)" certname)))))))

(deftest-db replace-catalog-with-many-resource-params
;; 1100 new resources x 30 params = 99,000 bind parameters, more than the
;; JDBC driver accepts in one statement.
(let [catalog (reduce (fn [cat i]
(let [title (str "res-" i)]
(assoc-in cat [:resources {:type "Param_test" :title title}]
{:type "Param_test" :title title :exported false
:file "/tmp/params.pp" :line i :tags #{"param_test"}
:parameters (into {} (for [p (range 30)]
[(keyword (str "param" p)) (str "value" p)]))})))
catalog
(range 1100))]
(add-certname! certname)
(replace-catalog! catalog)
(is (= (* 1100 30)
(:c (first (query-to-vec "SELECT count(*) AS c FROM catalog_resources cr
INNER JOIN resource_params rp ON rp.resource = cr.resource
WHERE cr.type = 'Param_test'")))))))

(deftest-db change-line-resource-metadata
(add-certname! certname)
(replace-catalog! catalog)
Expand Down