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_v67;
15pub mod objects_v68;
16pub mod objects_v69;
17pub mod objects_v70;
18pub mod objects_v71;
19pub mod objects_v72;
20pub mod objects_v73;
21pub mod objects_v74;
22pub mod objects_v75;
23pub mod objects_v76;
24pub mod objects_v77;
25pub mod objects_v78;
26pub mod serialization;
27
28/// The current version of the `Catalog`.
29///
30/// We will initialize new `Catalog`s with this version, and migrate existing `Catalog`s to this
31/// version. Whenever the `Catalog` changes, e.g. the types we serialize in the `Catalog`
32/// change, we need to bump this version.
33pub const CATALOG_VERSION: u64 = 78;
34
35/// The minimum `Catalog` version number that we support migrating from.
36///
37/// After bumping this we can delete the old migrations.
38pub const MIN_CATALOG_VERSION: u64 = 67;
39
40#[cfg(test)]
41mod tests {
42    use std::collections::BTreeSet;
43    use std::fs;
44    use std::path::PathBuf;
45
46    use crate::{CATALOG_VERSION, MIN_CATALOG_VERSION};
47
48    #[mz_ore::test]
49    fn test_assert_snapshots_exist() {
50        let src_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "src"].iter().collect();
51
52        // Get all the versioned object definition files.
53        let mut filenames: BTreeSet<_> = fs::read_dir(src_dir)
54            .expect("failed to read src dir")
55            .map(|entry| entry.expect("failed to read dir entry").file_name())
56            .map(|filename| filename.to_str().expect("utf8").to_string())
57            .filter(|filename| filename.starts_with("objects_v"))
58            .collect();
59
60        // Assert snapshots exist for all of the versions we support.
61        for version in MIN_CATALOG_VERSION..=CATALOG_VERSION {
62            let filename = format!("objects_v{version}.rs");
63            assert!(
64                filenames.remove(&filename),
65                "Missing snapshot for v{version}."
66            );
67        }
68
69        // Common case. Check to make sure the user bumped the CATALOG_VERSION.
70        if !filenames.is_empty()
71            && filenames.remove(&format!("objects_v{}.proto", CATALOG_VERSION + 1))
72        {
73            panic!(
74                "Found snapshot for v{}, please also bump `CATALOG_VERSION`.",
75                CATALOG_VERSION + 1
76            )
77        }
78
79        // Assert there aren't any extra snapshots.
80        assert!(
81            filenames.is_empty(),
82            "Found snapshots for unsupported catalog versions {filenames:?}.\n\
83             If you just increased `MIN_CATALOG_VERSION`, then please delete the old snapshots. \
84             If you created a new snapshot, please bump `CATALOG_VERSION`."
85        );
86    }
87
88    #[mz_ore::test]
89    fn test_assert_current_snapshot() {
90        let src_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "src"].iter().collect();
91        let current_rs = src_dir.join("objects.rs");
92        let snapshot_rs = src_dir.join(format!("objects_v{CATALOG_VERSION}.rs"));
93
94        let current = fs::read_to_string(current_rs).expect("read current");
95        let snapshot = fs::read_to_string(snapshot_rs).expect("read snapshot");
96
97        // Note: objects.rs and objects_v<CATALOG_VERSION>.rs should be exactly the same. The
98        // reason being, when bumping the catalog to the next version, CATALOG_VERSION + 1, we need a
99        // snapshot to migrate _from_, which should be a snapshot of how the types are today.
100        // Hence why the two files should be exactly the same.
101        similar_asserts::assert_eq!(current, snapshot);
102    }
103}