Skip to main content

mz_catalog_protos/
lib.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! All types we durably persist in the Catalog.
11
12pub mod audit_log;
13pub mod objects;
14pub mod objects_v74;
15pub mod objects_v75;
16pub mod objects_v76;
17pub mod objects_v77;
18pub mod objects_v78;
19pub mod objects_v79;
20pub mod objects_v80;
21pub mod objects_v81;
22pub mod objects_v82;
23pub mod objects_v83;
24pub mod objects_v84;
25pub mod objects_v85;
26pub mod objects_v86;
27pub mod objects_v87;
28pub mod objects_v88;
29pub mod objects_v89;
30pub mod objects_v90;
31pub mod objects_v91;
32pub mod objects_v92;
33pub mod serialization;
34
35/// The current version of the `Catalog`.
36///
37/// We will initialize new `Catalog`s with this version, and migrate existing `Catalog`s to this
38/// version. Whenever the `Catalog` changes, e.g. the types we serialize in the `Catalog`
39/// change, we need to bump this version.
40pub const CATALOG_VERSION: u64 = 92;
41
42/// The minimum `Catalog` version number that we support migrating from.
43///
44/// After bumping this we can delete the old migrations.
45pub const MIN_CATALOG_VERSION: u64 = 74;
46
47#[cfg(test)]
48mod tests {
49    use std::collections::BTreeSet;
50    use std::fs;
51    use std::path::PathBuf;
52
53    use crate::{CATALOG_VERSION, MIN_CATALOG_VERSION};
54
55    #[mz_ore::test]
56    fn test_assert_snapshots_exist() {
57        let src_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "src"].iter().collect();
58
59        // Get all the versioned object definition files.
60        let mut filenames: BTreeSet<_> = fs::read_dir(src_dir)
61            .expect("failed to read src dir")
62            .map(|entry| entry.expect("failed to read dir entry").file_name())
63            .map(|filename| filename.to_str().expect("utf8").to_string())
64            .filter(|filename| filename.starts_with("objects_v"))
65            .collect();
66
67        // Assert snapshots exist for all of the versions we support.
68        for version in MIN_CATALOG_VERSION..=CATALOG_VERSION {
69            let filename = format!("objects_v{version}.rs");
70            assert!(
71                filenames.remove(&filename),
72                "Missing snapshot for v{version}."
73            );
74        }
75
76        // Common case. Check to make sure the user bumped the CATALOG_VERSION.
77        if !filenames.is_empty()
78            && filenames.remove(&format!("objects_v{}.proto", CATALOG_VERSION + 1))
79        {
80            panic!(
81                "Found snapshot for v{}, please also bump `CATALOG_VERSION`.",
82                CATALOG_VERSION + 1
83            )
84        }
85
86        // Assert there aren't any extra snapshots.
87        assert!(
88            filenames.is_empty(),
89            "Found snapshots for unsupported catalog versions {filenames:?}.\n\
90             If you just increased `MIN_CATALOG_VERSION`, then please delete the old snapshots. \
91             If you created a new snapshot, please bump `CATALOG_VERSION`."
92        );
93    }
94
95    #[mz_ore::test]
96    fn test_assert_current_snapshot() {
97        let src_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "src"].iter().collect();
98        let current_rs = src_dir.join("objects.rs");
99        let snapshot_rs = src_dir.join(format!("objects_v{CATALOG_VERSION}.rs"));
100
101        let current = fs::read_to_string(current_rs).expect("read current");
102        let snapshot = fs::read_to_string(snapshot_rs).expect("read snapshot");
103
104        // Note: objects.rs and objects_v<CATALOG_VERSION>.rs should be exactly the same. The
105        // reason being, when bumping the catalog to the next version, CATALOG_VERSION + 1, we need a
106        // snapshot to migrate _from_, which should be a snapshot of how the types are today.
107        // Hence why the two files should be exactly the same.
108        similar_asserts::assert_eq!(current, snapshot);
109    }
110}