|
20 | 20 | :refer [*db* clear-db-for-testing! |
21 | 21 | schema-info-map diff-schema-maps with-test-db]] |
22 | 22 | [puppetlabs.puppetdb.scf.hash :as shash] |
23 | | - [puppetlabs.puppetdb.time :refer [now to-timestamp]] |
| 23 | + [puppetlabs.puppetdb.time :refer [now to-timestamp] :as t] |
24 | 24 | [puppetlabs.puppetdb.scf.partitioning :as part] |
25 | 25 | [clojure.string :as str]) |
26 | 26 | (:import (java.time ZoneId ZonedDateTime) |
|
2311 | 2311 | :deferrable? "NO"} |
2312 | 2312 | :same nil}]} |
2313 | 2313 | (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