Skip to content

Commit 8f0406e

Browse files
committed
Merge 7.x into main
2 parents a3159b1 + 43e5acf commit 8f0406e

File tree

5 files changed

+81
-5
lines changed

5 files changed

+81
-5
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ if ENV['NO_ACCEPTANCE'] != 'true'
6565
# use the pinned version
6666
gem 'beaker', '~> 4.1'
6767
end
68-
gem 'beaker-hostgenerator', '~> 2.2.3'
68+
gem 'beaker-hostgenerator', '~> 2.4'
6969
gem 'beaker-abs', *location_for(ENV['BEAKER_ABS_VERSION'] || '~> 0.2')
7070
gem 'beaker-vmpooler', *location_for(ENV['BEAKER_VMPOOLER_VERSION'] || "~> 1.3")
7171
gem 'beaker-puppet', '~> 1.0'

ext/test/upgrade-and-exit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ psql -U puppetdb puppetdb -c 'select max(version) from schema_migrations;' \
8383
> "$tmpdir/out"
8484
cat "$tmpdir/out"
8585
# This must be updated every time we add a new migration
86-
grep -qE ' 87$' "$tmpdir/out"
86+
grep -qE ' 88$' "$tmpdir/out"
8787

8888
test ! -e "$PDBBOX"/var/mq-migrated

resources/ext/cli/delete-reports.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ chown "$pg_user:$pg_user" "$tmp_dir"
7878

7979
# Verify that the PuppetDB schema version is the expected value
8080
# so that we do not incorrectly delete the report data.
81-
expected_schema_ver=87
81+
expected_schema_ver=88
8282
su - "$pg_user" -s /bin/sh -c "$psql_cmd -p $pg_port -d $pdb_db_name -c 'COPY ( SELECT max(version) FROM schema_migrations ) TO STDOUT;' > $tmp_dir/schema_ver"
8383
actual_schema_ver="$(cat "$tmp_dir/schema_ver")"
8484
if test "$actual_schema_ver" -ne $expected_schema_ver; then

src/puppetlabs/puppetdb/scf/migrate.clj

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,20 @@
25142514
" FROM reports_latest"
25152515
" WHERE reports_latest.id = resource_events.report_id"]))
25162516

2517+
(defn prevent-duplicate-catalogs
2518+
[]
2519+
(jdbc/do-commands
2520+
;; Clear any possible duplicates
2521+
["DELETE FROM catalogs c1 USING catalogs c2"
2522+
" WHERE c1.certname = c2.certname"
2523+
" AND (c1.producer_timestamp, c1.id) < (c2.producer_timestamp, c2.id)"]
2524+
2525+
;; Remove the old index
2526+
"DROP INDEX catalogs_certname_idx"
2527+
2528+
;; Create a unique constraint on the certname, which creates the unique index
2529+
"ALTER TABLE catalogs ADD CONSTRAINT catalogs_certname_idx UNIQUE (certname)"))
2530+
25172531
(def migrations
25182532
"The available migrations, as a map from migration version to migration function."
25192533
{00 require-schema-migrations-table
@@ -2584,7 +2598,8 @@
25842598
84 remove-catalog-resources-file-trgm-index
25852599
85 split-certnames-table
25862600
86 store-latest-reports-separately
2587-
87 store-latest-events-separately})
2601+
87 store-latest-events-separately
2602+
88 prevent-duplicate-catalogs})
25882603
;; Make sure that if you change the structure of reports
25892604
;; or resource events, you also update the delete-reports
25902605
;; cli command.

test/puppetlabs/puppetdb/scf/migrate_test.clj

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
:refer [*db* clear-db-for-testing!
2121
schema-info-map diff-schema-maps with-test-db]]
2222
[puppetlabs.puppetdb.scf.hash :as shash]
23-
[puppetlabs.puppetdb.time :refer [now to-timestamp]]
23+
[puppetlabs.puppetdb.time :refer [now to-timestamp] :as t]
2424
[puppetlabs.puppetdb.scf.partitioning :as part]
2525
[clojure.string :as str])
2626
(:import (java.time ZoneId ZonedDateTime)
@@ -2311,3 +2311,64 @@
23112311
:deferrable? "NO"}
23122312
:same nil}]}
23132313
(diff-schema-maps before-migration (schema-info-map *db*)))))))
2314+
2315+
(deftest migration-88-prevent-duplicate-catalogs
2316+
(testing "reports table declarative partitioning migration"
2317+
(jdbc/with-db-connection *db*
2318+
(clear-db-for-testing!)
2319+
(fast-forward-to-migration! 82)
2320+
(let [ts1 (to-timestamp (now))
2321+
ts2 (-> (now)
2322+
(t/plus (t/hours 1))
2323+
to-timestamp)
2324+
fake-hash (sutils/munge-hash-for-storage "0001")]
2325+
2326+
(jdbc/insert-multi! :certnames
2327+
[{:certname "host-1"}
2328+
{:certname "host-2"}])
2329+
2330+
(jdbc/insert-multi! :catalogs
2331+
[{:id 1 :hash fake-hash
2332+
:certname "host-1" :producer_timestamp ts1
2333+
:api_version 1 :catalog_version "one"}
2334+
{:id 2 :hash fake-hash
2335+
:certname "host-1" :producer_timestamp ts2
2336+
:api_version 1 :catalog_version "one"}
2337+
{:id 3 :hash fake-hash
2338+
:certname "host-1" :producer_timestamp ts2
2339+
:api_version 1 :catalog_version "one"}
2340+
{:id 4 :hash fake-hash
2341+
:certname "host-2" :producer_timestamp ts1
2342+
:api_version 1 :catalog_version "one"}])
2343+
(let [before-migration (schema-info-map *db*)
2344+
_ (apply-migration-for-testing! 88)
2345+
diff (-> (diff-schema-maps before-migration (schema-info-map *db*))
2346+
(update :index-diff set)
2347+
(update :constraint-diff set))]
2348+
(is (= {:index-diff
2349+
#{{:left-only {:unique? false}
2350+
:right-only {:unique? true}
2351+
:same {:index "catalogs_certname_idx"
2352+
:user "pdb_test"
2353+
:primary? false
2354+
:is_partial false
2355+
:functional? false
2356+
:type "btree"
2357+
:index_keys ["certname"]
2358+
:table "catalogs"
2359+
:schema "public"}}}
2360+
2361+
:table-diff nil
2362+
2363+
:constraint-diff
2364+
#{{:left-only nil
2365+
:right-only {:constraint_name "catalogs_certname_idx"
2366+
:table_name "catalogs"
2367+
:constraint_type "UNIQUE"
2368+
:initially_deferred "NO"
2369+
:deferrable? "NO"}
2370+
:same nil}}}
2371+
diff))
2372+
(is (= [{:id 3 :certname "host-1" :producer_timestamp ts2}
2373+
{:id 4 :certname "host-2" :producer_timestamp ts1}]
2374+
(query-to-vec "select id, certname, producer_timestamp from catalogs"))))))))

0 commit comments

Comments
 (0)